Skip to content

Commit 3b7ae2d

Browse files
authored
Fix input snapshotting for test tasks (elastic#55981)
1 parent 5d617d5 commit 3b7ae2d

File tree

2 files changed

+58
-9
lines changed

2 files changed

+58
-9
lines changed

buildSrc/src/main/java/org/elasticsearch/gradle/ElasticsearchJavaPlugin.java

+16-9
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@
2424
import org.elasticsearch.gradle.info.GlobalBuildInfoPlugin;
2525
import org.elasticsearch.gradle.test.ErrorReportingTestListener;
2626
import org.elasticsearch.gradle.util.Util;
27+
import org.gradle.api.Action;
2728
import org.gradle.api.GradleException;
2829
import org.gradle.api.JavaVersion;
2930
import org.gradle.api.Plugin;
3031
import org.gradle.api.Project;
32+
import org.gradle.api.Task;
3133
import org.gradle.api.artifacts.Configuration;
3234
import org.gradle.api.artifacts.ModuleDependency;
3335
import org.gradle.api.artifacts.ProjectDependency;
@@ -241,14 +243,19 @@ public static void configureTestTasks(Project project) {
241243
*/
242244
SystemPropertyCommandLineArgumentProvider nonInputProperties = new SystemPropertyCommandLineArgumentProvider();
243245

244-
test.doFirst(t -> {
245-
project.mkdir(testOutputDir);
246-
project.mkdir(heapdumpDir);
247-
project.mkdir(test.getWorkingDir());
248-
project.mkdir(test.getWorkingDir().toPath().resolve("temp"));
249-
250-
// TODO remove once jvm.options are added to test system properties
251-
test.systemProperty("java.locale.providers", "SPI,COMPAT");
246+
// We specifically use an anonymous inner class here because lambda task actions break Gradle cacheability
247+
// See: https://docs.gradle.org/current/userguide/more_about_tasks.html#sec:how_does_it_work
248+
test.doFirst(new Action<>() {
249+
@Override
250+
public void execute(Task t) {
251+
project.mkdir(testOutputDir);
252+
project.mkdir(heapdumpDir);
253+
project.mkdir(test.getWorkingDir());
254+
project.mkdir(test.getWorkingDir().toPath().resolve("temp"));
255+
256+
// TODO remove once jvm.options are added to test system properties
257+
test.systemProperty("java.locale.providers", "SPI,COMPAT");
258+
}
252259
});
253260
if (BuildParams.isInFipsJvm()) {
254261
project.getDependencies().add("testRuntimeOnly", "org.bouncycastle:bc-fips:1.0.1");
@@ -269,7 +276,7 @@ public static void configureTestTasks(Project project) {
269276
"-XX:+HeapDumpOnOutOfMemoryError"
270277
);
271278

272-
test.getJvmArgumentProviders().add(() -> List.of("-XX:HeapDumpPath=$heapdumpDir"));
279+
test.getJvmArgumentProviders().add(new SimpleCommandLineArgumentProvider("-XX:HeapDumpPath=" + heapdumpDir));
273280

274281
String argline = System.getProperty("tests.jvm.argline");
275282
if (argline != null) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.gradle;
21+
22+
import org.gradle.process.CommandLineArgumentProvider;
23+
24+
import java.util.Arrays;
25+
import java.util.List;
26+
27+
/**
28+
* A {@link CommandLineArgumentProvider} implementation that simply returns the given list. This implementation does not track any
29+
* arguments as inputs, so this is useful for passing arguments that should not be used for the purposes of input snapshotting.
30+
*/
31+
public class SimpleCommandLineArgumentProvider implements CommandLineArgumentProvider {
32+
private final List<String> arguments;
33+
34+
public SimpleCommandLineArgumentProvider(String... arguments) {
35+
this.arguments = Arrays.asList(arguments);
36+
}
37+
38+
@Override
39+
public Iterable<String> asArguments() {
40+
return arguments;
41+
}
42+
}

0 commit comments

Comments
 (0)