Skip to content

Commit 4a2e8e0

Browse files
JLLeitschuhmarcphilipp
authored andcommitted
Gradle Plugin: Static accessors for sub-extensions on JunitPlatform
This allows staticly typed build systems (eg. written in Kotlin) to configure the JUnitPlatformExtension sub-extensions without jumping through confusing syntax hoops. Closes #902
1 parent 2994263 commit 4a2e8e0

File tree

4 files changed

+110
-5
lines changed

4 files changed

+110
-5
lines changed

junit-platform-gradle-plugin/src/main/groovy/org/junit/platform/gradle/plugin/FiltersExtension.groovy

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
package org.junit.platform.gradle.plugin
1111

12+
import org.gradle.api.Action
1213
import org.junit.platform.engine.discovery.ClassNameFilter
1314

1415
/**
@@ -82,4 +83,25 @@ class FiltersExtension {
8283
excludeClassNamePatterns.addAll(patterns)
8384
}
8485

86+
/**
87+
* Configure the {@link PackagesExtension} for this plugin.
88+
*/
89+
void packages(Action<PackagesExtension> closure) {
90+
closure.execute(getProperty(JUnitPlatformPlugin.FILTERS_PACKAGES_EXTENSION_NAME) as PackagesExtension)
91+
}
92+
93+
/**
94+
* Configure the {@link TagsExtension} for this plugin.
95+
*/
96+
void tags(Action<TagsExtension> closure) {
97+
closure.execute(getProperty(JUnitPlatformPlugin.FILTERS_TAGS_EXTENSION_NAME) as TagsExtension)
98+
}
99+
100+
/**
101+
* Configure the {@link EnginesExtension} for this plugin.
102+
*/
103+
void engines(Action<EnginesExtension> closure) {
104+
closure.execute(getProperty(JUnitPlatformPlugin.FILTERS_ENGINES_EXTENSION_NAME) as EnginesExtension)
105+
}
106+
85107
}

junit-platform-gradle-plugin/src/main/groovy/org/junit/platform/gradle/plugin/JUnitPlatformExtension.groovy

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
package org.junit.platform.gradle.plugin
1111

12+
import org.gradle.api.Action
1213
import org.gradle.api.Project
1314
import org.junit.platform.console.options.Details
1415

@@ -79,4 +80,19 @@ class JUnitPlatformExtension {
7980
*/
8081
Details details = Details.NONE
8182

83+
84+
/**
85+
* Configure the {@link SelectorsExtension} for this plugin.
86+
*/
87+
void selectors(Action<SelectorsExtension> closure) {
88+
closure.execute(getProperty(JUnitPlatformPlugin.SELECTORS_EXTENSION_NAME) as SelectorsExtension)
89+
}
90+
91+
/**
92+
* Configure the {@link FiltersExtension} for this plugin.
93+
*/
94+
void filters(Action<FiltersExtension> closure) {
95+
closure.execute(getProperty(JUnitPlatformPlugin.FILTERS_EXTENSION_NAME) as FiltersExtension)
96+
}
97+
8298
}

junit-platform-gradle-plugin/src/main/groovy/org/junit/platform/gradle/plugin/JUnitPlatformPlugin.groovy

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,25 @@ class JUnitPlatformPlugin implements Plugin<Project> {
2525
private static final String EXTENSION_NAME = 'junitPlatform'
2626
private static final String TASK_NAME = 'junitPlatformTest'
2727

28+
protected static final String SELECTORS_EXTENSION_NAME = 'selectors'
29+
protected static final String FILTERS_EXTENSION_NAME = 'filters'
30+
protected static final String FILTERS_PACKAGES_EXTENSION_NAME = 'packages'
31+
protected static final String FILTERS_TAGS_EXTENSION_NAME = 'tags'
32+
protected static final String FILTERS_ENGINES_EXTENSION_NAME = 'engines'
33+
2834
void apply(Project project) {
2935
project.pluginManager.apply('java')
3036
def junitExtension = project.extensions.create(EXTENSION_NAME, JUnitPlatformExtension, project)
31-
junitExtension.extensions.create('selectors', SelectorsExtension)
32-
junitExtension.extensions.create('filters', FiltersExtension)
33-
junitExtension.filters.extensions.create('packages', PackagesExtension)
34-
junitExtension.filters.extensions.create('tags', TagsExtension)
35-
junitExtension.filters.extensions.create('engines', EnginesExtension)
37+
junitExtension.extensions.create(SELECTORS_EXTENSION_NAME, SelectorsExtension)
38+
junitExtension.extensions.create(FILTERS_EXTENSION_NAME, FiltersExtension)
39+
junitExtension.filters.extensions.create(FILTERS_PACKAGES_EXTENSION_NAME, PackagesExtension)
40+
junitExtension.filters.extensions.create(FILTERS_TAGS_EXTENSION_NAME, TagsExtension)
41+
junitExtension.filters.extensions.create(FILTERS_ENGINES_EXTENSION_NAME, EnginesExtension)
42+
/* NOTE TO FUTURE DEVELOPERS!
43+
* If you are adding another extension make sure you also provide a statically typed configuration method
44+
* on the extension class you are dynamically adding to here.
45+
* https://github.com/junit-team/junit5/issues/902
46+
*/
3647

3748
// configuration.defaultDependencies used below was introduced in Gradle 2.5
3849
if (GradleVersion.current().compareTo(GradleVersion.version('2.5')) < 0) {

junit-platform-gradle-plugin/src/test/groovy/org/junit/platform/gradle/plugin/JUnitPlatformPluginSpec.groovy

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@
99
*/
1010
package org.junit.platform.gradle.plugin
1111

12+
import groovy.transform.CompileStatic
13+
1214
import org.gradle.api.Project
1315
import org.gradle.api.Task
1416
import org.gradle.api.artifacts.Configuration
1517
import org.gradle.api.plugins.JavaPlugin
18+
import org.gradle.api.plugins.ObjectConfigurationAction
1619
import org.gradle.api.tasks.JavaExec
1720
import org.gradle.api.tasks.testing.Test
1821
import org.gradle.testfixtures.ProjectBuilder
@@ -83,6 +86,59 @@ class JUnitPlatformPluginSpec extends Specification {
8386
noExceptionThrown()
8487
}
8588

89+
@CompileStatic
90+
def "setting junitPlatform properties statically"() {
91+
/*
92+
* This test ensures that the extensions can be accessed as expected in statically typed languages like Kotlin.
93+
* The `Action` syntax is much nice in kotlin and doesn't require `it` everywhere.
94+
* https://github.com/junit-team/junit5/issues/902
95+
*/
96+
given:
97+
// Goofy syntax required because of closure overload
98+
project.apply ({ObjectConfigurationAction it ->
99+
it.plugin('org.junit.platform.gradle.plugin')
100+
})
101+
when:
102+
JUnitPlatformExtension junitPlatform = project.extensions.getByType(JUnitPlatformExtension)
103+
junitPlatform.platformVersion = '5.0.0-M1'
104+
junitPlatform.enableStandardTestTask = true
105+
junitPlatform.logManager = 'org.apache.logging.log4j.jul.LogManager'
106+
junitPlatform.selectors {
107+
it.uris'u:foo', 'u:bar'
108+
it.uri 'u:qux'
109+
it.files 'foo.txt', 'bar.csv'
110+
it.file 'qux.json'
111+
it.directories 'foo/bar', 'bar/qux'
112+
it.directory 'qux/bar'
113+
it.packages 'com.acme.foo', 'com.acme.bar'
114+
it.aPackage 'com.example.app'
115+
it.classes 'com.acme.Foo', 'com.acme.Bar'
116+
it.aClass 'com.example.app.Application'
117+
it.methods 'com.acme.Foo#a', 'com.acme.Foo#b'
118+
it.method 'com.example.app.Application#run(java.lang.String[])'
119+
it.resources '/bar.csv', '/foo/input.json'
120+
it.resource '/com/acme/my.properties'
121+
}
122+
junitPlatform.filters {
123+
it.includeClassNamePattern '.*Tests?'
124+
it.excludeClassNamePattern '.*TestCase'
125+
it.packages {
126+
it.include 'testpackage.included.p1', 'testpackage.included.p2'
127+
it.exclude 'testpackage.excluded.p1', 'testpackage.excluded.p2'
128+
}
129+
it.engines {
130+
it.include 'foo'
131+
it.exclude 'bar'
132+
}
133+
it.tags {
134+
it.include 'fast'
135+
it.exclude 'slow'
136+
}
137+
}
138+
then:
139+
noExceptionThrown()
140+
}
141+
86142
def "creating junitPlatformTest task"() {
87143
given:
88144
project.apply plugin: 'org.junit.platform.gradle.plugin'

0 commit comments

Comments
 (0)