Skip to content

Commit 52df63e

Browse files
committed
Compute es.entitlement.testOnlyPath only if it's possible
1 parent 9a08123 commit 52df63e

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/ElasticsearchTestBasePlugin.java

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
import javax.inject.Inject;
4040

41+
import static java.util.Objects.requireNonNull;
4142
import static org.elasticsearch.gradle.internal.util.ParamsUtils.loadBuildParams;
4243
import static org.elasticsearch.gradle.util.FileUtils.mkdirs;
4344
import static org.elasticsearch.gradle.util.GradleUtils.maybeConfigure;
@@ -174,25 +175,29 @@ public void execute(Task t) {
174175
// we use 'temp' relative to CWD since this is per JVM and tests are forbidden from writing to CWD
175176
nonInputProperties.systemProperty("java.io.tmpdir", test.getWorkingDir().toPath().resolve("temp"));
176177

177-
String projectName = project.getName();
178178
SourceSetContainer sourceSets = project.getExtensions().getByType(SourceSetContainer.class);
179-
FileCollection mainRuntime = sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME).getRuntimeClasspath();
180-
FileCollection testRuntime = sourceSets.getByName(SourceSet.TEST_SOURCE_SET_NAME).getRuntimeClasspath();
181-
FileCollection testOnlyFiles = testRuntime.minus(mainRuntime);
182-
// Configuration compileOnly = project.getConfigurations()
183-
// .findByName(RESOLVEABLE_COMPILE_ONLY_CONFIGURATION_NAME);
184-
// if (compileOnly != null) {
185-
// testOnlyFiles = testOnlyFiles.minus(compileOnly);
186-
// }
187-
nonInputProperties.systemProperty("es.entitlement.testOnlyPath", () -> {
188-
String asPath = testOnlyFiles.getAsPath();
189-
String[] pathEntries = asPath.split(File.pathSeparator);
190-
Arrays.sort(pathEntries);
191-
// System.err.println(
192-
// "PATDOYLE - for " + project.getName() + " " + test.getName() + " using testOnlyPath:\n" + String.join("\n", pathEntries)
193-
// );
194-
return asPath;
195-
});
179+
SourceSet mainSourceSet = sourceSets.findByName(SourceSet.MAIN_SOURCE_SET_NAME);
180+
SourceSet testSourceSet = sourceSets.findByName(SourceSet.TEST_SOURCE_SET_NAME);
181+
if (mainSourceSet != null && testSourceSet != null) {
182+
FileCollection mainRuntime = mainSourceSet.getRuntimeClasspath();
183+
FileCollection testRuntime = testSourceSet.getRuntimeClasspath();
184+
FileCollection testOnlyFiles = testRuntime.minus(mainRuntime);
185+
// Configuration compileOnly = project.getConfigurations()
186+
// .findByName(RESOLVEABLE_COMPILE_ONLY_CONFIGURATION_NAME);
187+
// if (compileOnly != null) {
188+
// testOnlyFiles = testOnlyFiles.minus(compileOnly);
189+
// }
190+
nonInputProperties.systemProperty("es.entitlement.testOnlyPath", () -> {
191+
String asPath = testOnlyFiles.getAsPath();
192+
String[] pathEntries = asPath.split(File.pathSeparator);
193+
Arrays.sort(pathEntries); // For readability during troubleshooting. Consumers don't care about order.
194+
// System.err.println(
195+
// "PATDOYLE - for " + project.getName() + " " + test.getName() + " using testOnlyPath:\n" + String.join("\n",
196+
// pathEntries)
197+
// );
198+
return asPath;
199+
});
200+
}
196201

197202
test.systemProperties(getProviderFactory().systemPropertiesPrefixedBy("tests.").get());
198203
test.systemProperties(getProviderFactory().systemPropertiesPrefixedBy("es.").get());
@@ -236,6 +241,8 @@ public void execute(Task t) {
236241
Configuration shadowConfig = project.getConfigurations().getByName(ShadowBasePlugin.CONFIGURATION_NAME);
237242
// Add the shadow JAR artifact itself
238243
FileCollection shadowJar = project.files(project.getTasks().named("shadowJar"));
244+
FileCollection mainRuntime = requireNonNull(mainSourceSet).getRuntimeClasspath();
245+
FileCollection testRuntime = requireNonNull(testSourceSet).getRuntimeClasspath();
239246
test.setClasspath(testRuntime.minus(mainRuntime).plus(shadowConfig).plus(shadowJar));
240247
}
241248
});

0 commit comments

Comments
 (0)