From 3873b007c8286e944018350de8ab8d0c5a4040c8 Mon Sep 17 00:00:00 2001 From: forozco Date: Thu, 18 Jul 2019 15:33:16 -0400 Subject: [PATCH 1/4] correctly handle multiple test sets --- .../baseline/plugins/BaselineTesting.java | 74 +++++++++++++------ .../BaselineTestingIntegrationTest.groovy | 27 ++++++- 2 files changed, 76 insertions(+), 25 deletions(-) diff --git a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineTesting.java b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineTesting.java index ea1117c5d..3ce9b2568 100644 --- a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineTesting.java +++ b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineTesting.java @@ -19,7 +19,11 @@ import java.util.Objects; import org.gradle.api.Plugin; import org.gradle.api.Project; +import org.gradle.api.artifacts.Dependency; import org.gradle.api.plugins.JavaPlugin; +import org.gradle.api.plugins.JavaPluginConvention; +import org.gradle.api.specs.Spec; +import org.gradle.api.tasks.SourceSet; import org.gradle.api.tasks.testing.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -39,38 +43,60 @@ public void apply(Project project) { } }); - project.getPlugins().withType(JavaPlugin.class, p -> { - + project.getPlugins().withType(JavaPlugin.class, unused -> { // afterEvaluate necessary because the junit-jupiter dep might be added further down the build.gradle - project.afterEvaluate(unused -> { - project.getConfigurations() - .getByName(JavaPlugin.TEST_RUNTIME_CLASSPATH_CONFIGURATION_NAME) - .getAllDependencies() - .matching(dep -> Objects.equals(dep.getGroup(), "org.junit.jupiter") - && dep.getName().equals("junit-jupiter")) - .stream() - .findAny() - .ifPresent(ignored -> enableJUnit5ForAllTestTasks(project)); + project.afterEvaluate(proj -> { + proj.getConvention() + .getPlugin(JavaPluginConvention.class) + .getSourceSets() + .matching(ss -> hasCompileDependenciesMatching(proj, ss, this::isJunitJupiter)) + .forEach(ss -> { + log.info("Detected 'org:junit.jupiter:junit-jupiter', enabling useJUnitPlatform()"); + String testTaskName = ss.getTaskName(null, "test"); + Test testTask = (Test) proj.getTasks().findByName(testTaskName); + if (testTask == null) { + // Fall back to the source set name, since that is what gradle-testsets-plugin does + testTask = (Test) proj.getTasks().findByName(ss.getName()); + if (testTask == null) { + log.warn( + "Detected 'org:junit.jupiter:junit-jupiter', but unable to find test task"); + return; + } + } + enableJunit5ForTestTask(testTask); + }); }); }); } - private void enableJUnit5ForAllTestTasks(Project project) { - log.info("Detected 'org:junit.jupiter:junit-jupiter', enabling useJUnitPlatform()"); - project.getTasks().withType(Test.class).configureEach(task -> { - task.useJUnitPlatform(); + private boolean hasCompileDependenciesMatching(Project project, SourceSet sourceSet, Spec spec) { + return project.getConfigurations() + .getByName(sourceSet.getCompileClasspathConfigurationName()) + .getAllDependencies() + .matching(spec) + .stream() + .findAny() + .isPresent(); + } - task.systemProperty("junit.platform.output.capture.stdout", "true"); - task.systemProperty("junit.platform.output.capture.stderr", "true"); + private boolean isJunitJupiter(Dependency dep) { + return Objects.equals(dep.getGroup(), "org.junit.jupiter") + && dep.getName().equals("junit-jupiter"); + } - // https://junit.org/junit5/docs/snapshot/user-guide/#writing-tests-parallel-execution - task.systemProperty("junit.jupiter.execution.parallel.enabled", "true"); + private void enableJunit5ForTestTask(Test task) { + task.useJUnitPlatform(); - // Computes the desired parallelism based on the number of available processors/cores - task.systemProperty("junit.jupiter.execution.parallel.config.strategy", "dynamic"); + task.systemProperty("junit.platform.output.capture.stdout", "true"); + task.systemProperty("junit.platform.output.capture.stderr", "true"); - // provide some stdout feedback when tests fail - task.testLogging(testLogging -> testLogging.events("failed")); - }); + // https://junit.org/junit5/docs/snapshot/user-guide/#writing-tests-parallel-execution + task.systemProperty("junit.jupiter.execution.parallel.enabled", "true"); + + // Computes the desired parallelism based on the number of available processors/cores + task.systemProperty("junit.jupiter.execution.parallel.config.strategy", "dynamic"); + + // provide some stdout feedback when tests fail + task.testLogging(testLogging -> testLogging.events("failed")); } } diff --git a/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineTestingIntegrationTest.groovy b/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineTestingIntegrationTest.groovy index eeccbbcb2..f139fbddd 100644 --- a/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineTestingIntegrationTest.groovy +++ b/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineTestingIntegrationTest.groovy @@ -16,7 +16,6 @@ package com.palantir.baseline - import org.gradle.testkit.runner.BuildResult import org.gradle.testkit.runner.TaskOutcome @@ -79,4 +78,30 @@ class BaselineTestingIntegrationTest extends AbstractPluginTest { new File(projectDir, "build/reports/tests/test/classes/test.TestClass4.html").exists() new File(projectDir, "build/reports/tests/test/classes/test.TestClass5.html").exists() } + + def 'runs integration tests with junit5'() { + when: + buildFile << ''' + plugins { + id 'org.unbroken-dome.test-sets' version '2.1.1' + } + '''.stripIndent() + buildFile << standardBuildFile + buildFile << ''' + + testSets { + integrationTest + } + + dependencies { + integrationTestImplementation "org.junit.jupiter:junit-jupiter:5.4.2" + } + '''.stripIndent() + file('src/integrationTest/java/test/TestClass5.java') << junit5Test + + then: + BuildResult result = with('integrationTest', '--write-locks').build() + result.task(':integrationTest').outcome == TaskOutcome.SUCCESS + new File(projectDir, "build/reports/tests/test/classes/test.TestClass5.html").exists() + } } From 5d6c06b889480b4d43dac0bf537b50574b90f6bb Mon Sep 17 00:00:00 2001 From: forozco Date: Thu, 18 Jul 2019 15:43:53 -0400 Subject: [PATCH 2/4] fix --- .../com/palantir/baseline/BaselineTestingIntegrationTest.groovy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineTestingIntegrationTest.groovy b/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineTestingIntegrationTest.groovy index f139fbddd..d41ee6205 100644 --- a/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineTestingIntegrationTest.groovy +++ b/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineTestingIntegrationTest.groovy @@ -102,6 +102,6 @@ class BaselineTestingIntegrationTest extends AbstractPluginTest { then: BuildResult result = with('integrationTest', '--write-locks').build() result.task(':integrationTest').outcome == TaskOutcome.SUCCESS - new File(projectDir, "build/reports/tests/test/classes/test.TestClass5.html").exists() + new File(projectDir, "build/reports/tests/integrationTest/classes/test.TestClass5.html").exists() } } From f29b10916d74b79db1c988fc500eea7751807d42 Mon Sep 17 00:00:00 2001 From: forozco Date: Thu, 18 Jul 2019 19:43:53 +0000 Subject: [PATCH 3/4] Add generated changelog entries --- changelog/@unreleased/pr-701.v2.yml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 changelog/@unreleased/pr-701.v2.yml diff --git a/changelog/@unreleased/pr-701.v2.yml b/changelog/@unreleased/pr-701.v2.yml new file mode 100644 index 000000000..1327e46e3 --- /dev/null +++ b/changelog/@unreleased/pr-701.v2.yml @@ -0,0 +1,5 @@ +type: improvement +improvement: + description: Determine whether to use junitPlatform on a per source set basis + links: + - https://github.com/palantir/gradle-baseline/pull/701 From 038bb6b657c9ee4c3947ec105d5982d390f5b230 Mon Sep 17 00:00:00 2001 From: forozco Date: Mon, 22 Jul 2019 09:54:31 -0400 Subject: [PATCH 4/4] address comments --- .../groovy/com/palantir/baseline/plugins/BaselineTesting.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineTesting.java b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineTesting.java index 3ce9b2568..f51f581bb 100644 --- a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineTesting.java +++ b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineTesting.java @@ -51,7 +51,6 @@ public void apply(Project project) { .getSourceSets() .matching(ss -> hasCompileDependenciesMatching(proj, ss, this::isJunitJupiter)) .forEach(ss -> { - log.info("Detected 'org:junit.jupiter:junit-jupiter', enabling useJUnitPlatform()"); String testTaskName = ss.getTaskName(null, "test"); Test testTask = (Test) proj.getTasks().findByName(testTaskName); if (testTask == null) { @@ -63,6 +62,8 @@ public void apply(Project project) { return; } } + log.info("Detected 'org:junit.jupiter:junit-jupiter', enabling useJUnitPlatform() on {}", + testTask.getName()); enableJunit5ForTestTask(testTask); }); });