Skip to content

Commit

Permalink
Create junitPlatformTest task during configuration phase
Browse files Browse the repository at this point in the history
This enables users to retrieve and add dependencies, configure inputs, and other user-supplied details at configuration time.

We need the user supplied configuration to be applied lazily after they configure it, which means we cannot do it at plugin `apply` time.
We can also use `Callable`/`Closure` in the `inputs.property` method, but that will only work for properties but not for methods.
Another way of accomplishing lazy properties is by using `conventionMapping`, but that also has pitfalls (see https://github.com/gradle/gradle/blob/master/design-docs/internal-apis-made-public.md).
This means we don't have an effective way of lazily configuring the `args` or the `systemProperty` of the task.

The easiest way forward is to simply create the task at configuration time and then apply the rest of the configuration in the `afterEvaluate` method to avoid using internal `conventionMapping`.

closes junit-team#708

See related Gradle issue for making lazy properties at gradle/gradle#726
  • Loading branch information
mkobit committed Mar 27, 2017
1 parent 8faff0d commit 63e3ee0
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,15 @@ class JUnitPlatformPlugin implements Plugin<Project> {
deps.add(project.dependencies.create("org.junit.platform:junit-platform-console:${version}"))
}

JavaExec junitTask = project.tasks.create(TASK_NAME, JavaExec) {
it.with {
group = JavaBasePlugin.VERIFICATION_GROUP
description = 'Runs tests on the JUnit Platform.'
}
}

project.afterEvaluate {
configure(project, junitExtension)
configure(project, junitTask, junitExtension)
}
}

Expand All @@ -65,48 +72,46 @@ class JUnitPlatformPlugin implements Plugin<Project> {
return properties.getProperty("version")
}

private void configure(Project project, JUnitPlatformExtension junitExtension) {
project.tasks.create(TASK_NAME, JavaExec) { junitTask ->
junitTask.with {
group = JavaBasePlugin.VERIFICATION_GROUP
description = 'Runs tests on the JUnit Platform.'
inputs.property('enableStandardTestTask', junitExtension.enableStandardTestTask)
inputs.property('selectors.uris', junitExtension.selectors.uris)
inputs.property('selectors.files', junitExtension.selectors.files)
inputs.property('selectors.directories', junitExtension.selectors.directories)
inputs.property('selectors.packages', junitExtension.selectors.packages)
inputs.property('selectors.classes', junitExtension.selectors.classes)
inputs.property('selectors.methods', junitExtension.selectors.methods)
inputs.property('selectors.resources', junitExtension.selectors.resources)
inputs.property('filters.engines.include', junitExtension.filters.engines.include)
inputs.property('filters.engines.exclude', junitExtension.filters.engines.exclude)
inputs.property('filters.tags.include', junitExtension.filters.tags.include)
inputs.property('filters.tags.exclude', junitExtension.filters.tags.exclude)
inputs.property('filters.includeClassNamePatterns', junitExtension.filters.includeClassNamePatterns)
inputs.property('filters.packages.include', junitExtension.filters.packages.include)
inputs.property('filters.packages.exclude', junitExtension.filters.packages.exclude)

def reportsDir = junitExtension.reportsDir ?: project.file("$project.buildDir/test-results/junit-platform")
outputs.dir reportsDir

if (junitExtension.logManager) {
systemProperty 'java.util.logging.manager', junitExtension.logManager
}

configureTaskDependencies(project, it, junitExtension)

// Build the classpath from the user's test runtime classpath and the JUnit
// Platform modules.
//
// Note: the user's test runtime classpath must come first; otherwise, code
// instrumented by Clover in JUnit's build will be shadowed by JARs pulled in
// via the junitPlatform configuration... leading to zero code coverage for
// the respective modules.
classpath = project.sourceSets.test.runtimeClasspath + project.configurations.junitPlatform

main = ConsoleLauncher.class.getName()
args buildArgs(project, junitExtension, reportsDir)
private void configure(Project project, JavaExec junitTask, JUnitPlatformExtension junitExtension) {
junitTask.with {
group = JavaBasePlugin.VERIFICATION_GROUP
description = 'Runs tests on the JUnit Platform.'
inputs.property('enableStandardTestTask', junitExtension.enableStandardTestTask)
inputs.property('selectors.uris', junitExtension.selectors.uris)
inputs.property('selectors.files', junitExtension.selectors.files)
inputs.property('selectors.directories', junitExtension.selectors.directories)
inputs.property('selectors.packages', junitExtension.selectors.packages)
inputs.property('selectors.classes', junitExtension.selectors.classes)
inputs.property('selectors.methods', junitExtension.selectors.methods)
inputs.property('selectors.resources', junitExtension.selectors.resources)
inputs.property('filters.engines.include', junitExtension.filters.engines.include)
inputs.property('filters.engines.exclude', junitExtension.filters.engines.exclude)
inputs.property('filters.tags.include', junitExtension.filters.tags.include)
inputs.property('filters.tags.exclude', junitExtension.filters.tags.exclude)
inputs.property('filters.includeClassNamePatterns', junitExtension.filters.includeClassNamePatterns)
inputs.property('filters.packages.include', junitExtension.filters.packages.include)
inputs.property('filters.packages.exclude', junitExtension.filters.packages.exclude)

def reportsDir = junitExtension.reportsDir ?: project.file("$project.buildDir/test-results/junit-platform")
outputs.dir reportsDir

if (junitExtension.logManager) {
systemProperty 'java.util.logging.manager', junitExtension.logManager
}

configureTaskDependencies(project, it, junitExtension)

// Build the classpath from the user's test runtime classpath and the JUnit
// Platform modules.
//
// Note: the user's test runtime classpath must come first; otherwise, code
// instrumented by Clover in JUnit's build will be shadowed by JARs pulled in
// via the junitPlatform configuration... leading to zero code coverage for
// the respective modules.
classpath = project.sourceSets.test.runtimeClasspath + project.configurations.junitPlatform

main = ConsoleLauncher.class.getName()
args buildArgs(project, junitExtension, reportsDir)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ import org.gradle.api.tasks.testing.Test
import org.gradle.testfixtures.ProjectBuilder
import org.junit.platform.console.ConsoleLauncher
import org.junit.platform.engine.discovery.ClassNameFilter
import spock.lang.Issue
import spock.lang.Specification

/**
* @since 1.0
*/
class JUnitPlatformPluginSpec extends Specification {

Project project
private Project project

def setup() {
project = ProjectBuilder.builder().build()
Expand Down Expand Up @@ -275,4 +276,18 @@ class JUnitPlatformPluginSpec extends Specification {
.findAll { version -> version.startsWith("1.") && !version.contains("+")}
.size() == 2
}

@Issue('https://github.com/junit-team/junit5/issues/708')
def "can configure the junitPlatformTest task during the configuration phase"() {
given:
String customDescription = 'My custom description'
project.apply plugin: 'org.junit.platform.gradle.plugin'

when:
final junitPlatformTest = project.tasks.getByName('junitPlatformTest')
junitPlatformTest.description = customDescription

then:
junitPlatformTest.description == customDescription
}
}

0 comments on commit 63e3ee0

Please sign in to comment.