Skip to content

Commit debd8d2

Browse files
committed
Do not act on 'whitebox(...)' config if test folder does not exist
1 parent 1bf4d37 commit debd8d2

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

src/main/java/org/gradlex/javamodule/testing/JavaModuleTestingExtension.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,11 @@ public JavaModuleTestingExtension(Project project) {
6363
jvmTestSuite.useJUnitJupiter(); // override old Gradle convention to default to JUnit5 for all suites
6464
}
6565

66-
boolean testFolderExists = jvmTestSuite.getSources().getJava().getSrcDirs().stream().anyMatch(File::exists);
6766
if (isTestModule) {
6867
blackbox(jvmTestSuite);
69-
} else if (testFolderExists) {
68+
} else {
7069
whitebox(jvmTestSuite, conf -> conf.getOpensTo().add("org.junit.platform.commons"));
7170
}
72-
73-
// Remove the dependencies added by Gradle in case the test directory is missing. This allows the use of 'useJUnitJupiter("")' without hassle.
74-
if (!testFolderExists) {
75-
project.getConfigurations().getByName(jvmTestSuite.getSources().getImplementationConfigurationName(), implementation ->
76-
implementation.withDependencies(dependencySet -> dependencySet.removeIf(d -> "org.junit.jupiter".equals(d.getGroup()) && "junit-jupiter".equals(d.getName()))));
77-
project.getConfigurations().getByName(jvmTestSuite.getSources().getRuntimeOnlyConfigurationName(), runtimeOnly ->
78-
runtimeOnly.withDependencies(dependencySet -> dependencySet.removeIf(d -> "org.junit.platform".equals(d.getGroup()) && "junit-platform-launcher".equals(d.getName()))));
79-
}
8071
});
8172
}
8273

@@ -124,6 +115,17 @@ public void whitebox(TestSuite jvmTestSuite) {
124115
*/
125116
public void whitebox(TestSuite jvmTestSuite, Action<WhiteboxJvmTestSuite> conf) {
126117
if (jvmTestSuite instanceof JvmTestSuite) {
118+
SourceSet suiteSourceSet = ((JvmTestSuite) jvmTestSuite).getSources();
119+
boolean testFolderExists = suiteSourceSet.getJava().getSrcDirs().stream().anyMatch(File::exists);
120+
if (!testFolderExists) {
121+
// Remove the dependencies added by Gradle in case the test directory is missing. Then stop. This allows the use of 'useJUnitJupiter("")' without hassle.
122+
project.getConfigurations().getByName(suiteSourceSet.getImplementationConfigurationName(), implementation ->
123+
implementation.withDependencies(dependencySet -> dependencySet.removeIf(d -> "org.junit.jupiter".equals(d.getGroup()) && "junit-jupiter".equals(d.getName()))));
124+
project.getConfigurations().getByName(suiteSourceSet.getRuntimeOnlyConfigurationName(), runtimeOnly ->
125+
runtimeOnly.withDependencies(dependencySet -> dependencySet.removeIf(d -> "org.junit.platform".equals(d.getGroup()) && "junit-platform-launcher".equals(d.getName()))));
126+
return;
127+
}
128+
127129
WhiteboxJvmTestSuite whiteboxJvmTestSuite = project.getObjects().newInstance(WhiteboxJvmTestSuite.class);
128130
whiteboxJvmTestSuite.getSourcesUnderTest().convention(project.getExtensions().getByType(SourceSetContainer.class).getByName(SourceSet.MAIN_SOURCE_SET_NAME));
129131
whiteboxJvmTestSuite.getRequires().addAll(requiresFromModuleInfo((JvmTestSuite) jvmTestSuite, whiteboxJvmTestSuite.getSourcesUnderTest(), false));

src/test/groovy/org/gradlex/javamodule/testing/test/CustomizationTest.groovy

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,23 @@ class CustomizationTest extends Specification {
117117
then:
118118
result.task(":app:test").outcome == TaskOutcome.NO_SOURCE
119119
}
120+
121+
def "build does not fail when JUnit has no version, the test folder is empty and whitebox was manually configured"() {
122+
given:
123+
appTestModuleInfoFile.parentFile.deleteDir()
124+
appBuildFile << '''
125+
testing.suites.withType<JvmTestSuite>().all {
126+
useJUnitJupiter("") // <- no version, we want to manage that ourselves
127+
}
128+
javaModuleTesting.whitebox(testing.suites["test"]) {
129+
requires.add("org.junit.jupiter.api")
130+
}
131+
'''
132+
133+
when:
134+
def result = runTests()
135+
136+
then:
137+
result.task(":app:test").outcome == TaskOutcome.NO_SOURCE
138+
}
120139
}

0 commit comments

Comments
 (0)