|
17 | 17 | package org.gradlex.javamodule.testing; |
18 | 18 |
|
19 | 19 | import org.gradle.api.Action; |
| 20 | +import org.gradle.api.Describable; |
20 | 21 | import org.gradle.api.Project; |
21 | 22 | import org.gradle.api.artifacts.Configuration; |
22 | 23 | import org.gradle.api.artifacts.ConfigurationContainer; |
@@ -98,6 +99,24 @@ public void whitebox(TestSuite jvmTestSuite) { |
98 | 99 | whitebox(jvmTestSuite, NO_OP_ACTION); |
99 | 100 | } |
100 | 101 |
|
| 102 | + /** |
| 103 | + * Turn the given JVM Test Suite into a Classpath Test Suite. |
| 104 | + * For example: |
| 105 | + * <p> |
| 106 | + * javaModuleTesting.classpath(testing.suites["test"]) |
| 107 | + * <p> |
| 108 | + * This restores the default behavior of Gradle to run tests on the Classpath if |
| 109 | + * no 'module-info.java' is present in the source folder of the given test suite. |
| 110 | + * |
| 111 | + * @param jvmTestSuite the JVM Test Suite to configure |
| 112 | + */ |
| 113 | + @SuppressWarnings("unused") |
| 114 | + public void classpath(TestSuite jvmTestSuite) { |
| 115 | + if (jvmTestSuite instanceof JvmTestSuite) { |
| 116 | + revertJvmTestSuiteForWhitebox((JvmTestSuite) jvmTestSuite); |
| 117 | + } |
| 118 | + } |
| 119 | + |
101 | 120 | /** |
102 | 121 | * Turn the given JVM Test Suite into a Whitebox Test Suite. |
103 | 122 | * If needed, configure additional 'requires' and open the |
@@ -249,4 +268,25 @@ private void configureJvmTestSuiteForWhitebox(JvmTestSuite jvmTestSuite, Whitebo |
249 | 268 | } |
250 | 269 | }); |
251 | 270 | } |
| 271 | + |
| 272 | + /** |
| 273 | + * Resets changes performed in 'configureJvmTestSuiteForWhitebox' to Gradle defaults. |
| 274 | + */ |
| 275 | + private void revertJvmTestSuiteForWhitebox(JvmTestSuite jvmTestSuite) { |
| 276 | + TaskContainer tasks = project.getTasks(); |
| 277 | + SourceSet testSources = jvmTestSuite.getSources(); |
| 278 | + |
| 279 | + tasks.named(testSources.getCompileJavaTaskName(), JavaCompile.class, compileJava -> { |
| 280 | + compileJava.setClasspath(testSources.getCompileClasspath()); |
| 281 | + compileJava.getOptions().getCompilerArgumentProviders().removeIf(p -> p instanceof WhiteboxTestCompileArgumentProvider); |
| 282 | + compileJava.getActions().removeIf(a -> a instanceof Describable |
| 283 | + && JavaCompileSetModulePathAction.class.getName().equals(((Describable) a).getDisplayName())); |
| 284 | + }); |
| 285 | + |
| 286 | + tasks.named(testSources.getName(), Test.class, test -> { |
| 287 | + test.setClasspath(testSources.getRuntimeClasspath()); |
| 288 | + test.setTestClassesDirs(testSources.getOutput().getClassesDirs()); |
| 289 | + test.getJvmArgumentProviders().removeIf(p -> p instanceof WhiteboxTestRuntimeArgumentProvider); |
| 290 | + }); |
| 291 | + } |
252 | 292 | } |
0 commit comments