Skip to content

Commit 59bb7f4

Browse files
committed
Add late extendsFrom implementation
1 parent 0223a40 commit 59bb7f4

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.gradle.api.Action;
1212
import org.gradle.api.Describable;
1313
import org.gradle.api.Project;
14+
import org.gradle.api.artifacts.Configuration;
1415
import org.gradle.api.artifacts.ConfigurationContainer;
1516
import org.gradle.api.artifacts.dsl.DependencyHandler;
1617
import org.gradle.api.file.RegularFile;
@@ -61,6 +62,7 @@ public JavaModuleTestingExtension(Project project) {
6162
whitebox(jvmTestSuite, conf -> conf.getOpensTo().add("org.junit.platform.commons"));
6263
}
6364
});
65+
project.afterEvaluate(this::lateConfigureExtendsFrom);
6466
}
6567

6668
/**
@@ -281,12 +283,10 @@ private void configureJvmTestSuiteForWhitebox(
281283

282284
addDependencyForRequires(
283285
whiteboxJvmTestSuite,
284-
project,
285286
testSources.getImplementationConfigurationName(),
286287
whiteboxJvmTestSuite.getRequires());
287288
addDependencyForRequires(
288289
whiteboxJvmTestSuite,
289-
project,
290290
testSources.getRuntimeOnlyConfigurationName(),
291291
whiteboxJvmTestSuite.getRequiresRuntime());
292292
}
@@ -360,4 +360,30 @@ private void revertJvmTestSuiteForWhitebox(JvmTestSuite jvmTestSuite) {
360360
test.getJvmArgumentProviders().removeIf(p -> p instanceof WhiteboxTestRuntimeArgumentProvider);
361361
});
362362
}
363+
364+
private void lateConfigureExtendsFrom(Project project) {
365+
ConfigurationContainer configurations = project.getConfigurations();
366+
whiteboxOrClasspathTestSuites.forEach((testSources, sourcesUnderTest) -> {
367+
extendsFrom(
368+
configurations,
369+
testSources.getImplementationConfigurationName(),
370+
sourcesUnderTest.getImplementationConfigurationName());
371+
extendsFrom(
372+
configurations,
373+
testSources.getRuntimeOnlyConfigurationName(),
374+
sourcesUnderTest.getRuntimeOnlyConfigurationName());
375+
extendsFrom(
376+
configurations,
377+
testSources.getCompileOnlyConfigurationName(),
378+
sourcesUnderTest.getCompileOnlyConfigurationName());
379+
});
380+
}
381+
382+
private void extendsFrom(ConfigurationContainer configurations, String testScope, String underTestScope) {
383+
Configuration testScopeConf = configurations.getByName(testScope);
384+
Configuration underTestScopeConf = configurations.getByName(underTestScope);
385+
if (!testScopeConf.getExtendsFrom().contains(underTestScopeConf)) {
386+
testScopeConf.extendsFrom(underTestScopeConf);
387+
}
388+
}
363389
}

src/test/java/org/gradlex/javamodule/testing/test/CoreFunctionalityTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,42 @@ class CoreFunctionalityTest {
1313

1414
@Test
1515
void testCompileOnly_extends_compileOnly_for_whitebox_test_suites() {
16+
build.appBuildFile.appendText(
17+
"""
18+
javaModuleTesting.whitebox(testing.suites["test"]) {
19+
requires.add("org.junit.jupiter.api")
20+
}
21+
dependencies {
22+
compileOnly("jakarta.servlet:jakarta.servlet-api:6.1.0")
23+
}""");
24+
build.file("app/src/main/java/org/example/app/ServletImpl.java")
25+
.writeText(
26+
"""
27+
package org.example.app;
28+
public abstract class ServletImpl implements jakarta.servlet.Servlet { }
29+
""");
30+
build.file("app/src/test/java/org/example/app/test/ServletMock.java")
31+
.writeText(
32+
"""
33+
package org.example.app.test;
34+
public abstract class ServletMock extends org.example.app.ServletImpl { }
35+
""");
36+
build.appModuleInfoFile.writeText(
37+
"""
38+
module org.example.app {
39+
requires static jakarta.servlet;
40+
}
41+
""");
42+
43+
var result = build.runner("compileTestJava").build();
44+
var compileTestResult = result.task(":app:compileTestJava");
45+
46+
assertThat(compileTestResult).isNotNull();
47+
assertThat(compileTestResult.getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
48+
}
49+
50+
@Test
51+
void testCompileOnly_extends_compileOnly_for_classpath_test_suites() {
1652
build.appBuildFile.appendText(
1753
"""
1854
javaModuleTesting.classpath(testing.suites["test"])

0 commit comments

Comments
 (0)