Skip to content

Commit b848b3b

Browse files
committed
Use system property to calculate test-ids location
1 parent dbbf8ff commit b848b3b

File tree

10 files changed

+36
-45
lines changed

10 files changed

+36
-45
lines changed

.github/workflows/test-junit-platform-native.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
strategy:
2727
fail-fast: false
2828
matrix:
29-
java-version: [ 17, 23 ]
29+
java-version: [ 17 ]
3030
os: [ ubuntu-20.04 ]
3131
steps:
3232
- name: "☁️ Checkout repository"
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists

common/junit-platform-native/gradle/native-image-testing.gradle

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3939
* SOFTWARE.
4040
*/
41+
4142
def agentOutput = layout.buildDirectory.dir("agent")
4243

4344
ext {
@@ -143,4 +144,5 @@ tasks.register("nativeTest", Exec) {
143144
dependsOn nativeTestCompile
144145
workingDir = "${buildDir}"
145146
executable = "${buildDir}/native-image-tests"
147+
args = ["-Djunit.platform.listeners.uid.tracking.output.dir=${testIdsDir.get().asFile.absolutePath}"]
146148
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists

common/junit-platform-native/src/main/java/org/graalvm/junit/platform/JUnitPlatformFeature.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
import java.io.UncheckedIOException;
6464
import java.nio.file.Files;
6565
import java.nio.file.Path;
66-
import java.nio.file.Paths;
6766
import java.util.List;
6867
import java.util.Optional;
6968
import java.util.ServiceLoader;
@@ -112,7 +111,7 @@ public void beforeAnalysis(BeforeAnalysisAccess access) {
112111

113112
private List<? extends DiscoverySelector> getSelectors() {
114113
try {
115-
Path uniqueIdDirectory = Paths.get(System.getProperty(UniqueIdTrackingListener.OUTPUT_DIR_PROPERTY_NAME));
114+
Path uniqueIdDirectory = Path.of(System.getProperty(UniqueIdTrackingListener.OUTPUT_DIR_PROPERTY_NAME));
116115
String uniqueIdFilePrefix = System.getProperty(UniqueIdTrackingListener.OUTPUT_FILE_PREFIX_PROPERTY_NAME,
117116
UniqueIdTrackingListener.DEFAULT_OUTPUT_FILE_PREFIX);
118117

common/junit-platform-native/src/main/java/org/graalvm/junit/platform/NativeImageJUnitLauncher.java

+26-28
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import org.junit.platform.launcher.listeners.UniqueIdTrackingListener;
5656
import org.junit.platform.reporting.legacy.xml.LegacyXmlReportGeneratingListener;
5757

58+
import java.io.File;
5859
import java.io.IOException;
5960
import java.io.PrintWriter;
6061
import java.io.UncheckedIOException;
@@ -82,7 +83,6 @@ public static void main(String... args) {
8283

8384
/* scan runtime arguments */
8485
String xmlOutput = DEFAULT_OUTPUT_FOLDER;
85-
String testIds = null;
8686
boolean silent = false;
8787

8888
LinkedList<String> arguments = new LinkedList<>(Arrays.asList(args));
@@ -94,17 +94,13 @@ public static void main(String... args) {
9494
System.out.println("----------------------------------------\n");
9595
System.out.println("Flags:");
9696
System.out.println(stringPad("--xml-output-dir") + "Selects report xml output directory (default: `" + DEFAULT_OUTPUT_FOLDER + "`)");
97-
System.out.println(stringPad("--test-ids") + "Provides path to generated testIDs");
9897
System.out.println(stringPad("--silent") + "Only output xml without stdout summary");
9998
System.out.println(stringPad("--help") + "Displays this help screen");
10099
System.exit(0);
101100
break;
102101
case "--xml-output-dir":
103102
xmlOutput = arguments.poll();
104103
break;
105-
case "--test-ids":
106-
testIds = arguments.poll();
107-
break;
108104
case "--silent":
109105
silent = true;
110106
break;
@@ -119,14 +115,8 @@ public static void main(String... args) {
119115
throw new RuntimeException("xml-output-dir argument passed incorrectly to the launcher class.");
120116
}
121117

122-
if (testIds == null) {
123-
System.out.println("[junit-platform-native] WARNING: test-ids not provided to the NativeImageJUnitLauncher. " +
124-
"This should only happen if you are running tests binary manually (instead of using 'gradle nativeTest' command)");
125-
testIds = getTestIDsFromDefaultLocations();
126-
}
127-
128118
Launcher launcher = LauncherFactory.create();
129-
TestPlan testPlan = getTestPlan(launcher, testIds);
119+
TestPlan testPlan = getTestPlan(launcher);
130120

131121
PrintWriter out = new PrintWriter(System.out);
132122
if (!silent) {
@@ -151,28 +141,30 @@ public static void main(String... args) {
151141
System.exit(failedCount > 0 ? 1 : 0);
152142
}
153143

154-
private static TestPlan getTestPlan(Launcher launcher, String testIDs) {
155-
List<? extends DiscoverySelector> selectors = getSelectors(testIDs);
144+
private static TestPlan getTestPlan(Launcher launcher) {
145+
List<? extends DiscoverySelector> selectors = getSelectors();
156146
LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
157147
.selectors(selectors)
158148
.build();
159149

160150
return launcher.discover(request);
161151
}
162152

163-
private static List<? extends DiscoverySelector> getSelectors(String testIDs) {
153+
private static List<? extends DiscoverySelector> getSelectors() {
164154
try {
165-
Path outputDir = Paths.get(testIDs);
166-
String prefix = System.getProperty(UniqueIdTrackingListener.OUTPUT_FILE_PREFIX_PROPERTY_NAME,
155+
String systemPropertyBasedLocation = System.getProperty(UniqueIdTrackingListener.OUTPUT_DIR_PROPERTY_NAME);
156+
Path uniqueIdDirectory = systemPropertyBasedLocation != null ? Path.of(systemPropertyBasedLocation) : getTestIDsFromDefaultLocations();
157+
String uniqueIdFilePrefix = System.getProperty(UniqueIdTrackingListener.OUTPUT_FILE_PREFIX_PROPERTY_NAME,
167158
UniqueIdTrackingListener.DEFAULT_OUTPUT_FILE_PREFIX);
168-
List<UniqueIdSelector> selectors = readAllFiles(outputDir, prefix)
159+
160+
List<UniqueIdSelector> selectors = readAllFiles(uniqueIdDirectory, uniqueIdFilePrefix)
169161
.map(DiscoverySelectors::selectUniqueId)
170162
.collect(Collectors.toList());
171163
if (!selectors.isEmpty()) {
172164
System.out.printf(
173165
"[junit-platform-native] Running in 'test listener' mode using files matching pattern [%s*] "
174166
+ "found in folder [%s] and its subfolders.%n",
175-
prefix, outputDir.toAbsolutePath());
167+
uniqueIdFilePrefix, uniqueIdDirectory.toAbsolutePath());
176168
return selectors;
177169
}
178170
} catch (Exception ex) {
@@ -201,47 +193,53 @@ private static Stream<Path> findFiles(Path dir, String prefix) throws IOExceptio
201193
&& path.getFileName().toString().startsWith(prefix)));
202194
}
203195

204-
private static String getTestIDsFromDefaultLocations() {
205-
System.out.println("[junit-platform-native] WARNING: Trying to find test-ids on default locations.");
196+
private static Path getTestIDsFromDefaultLocations() {
197+
System.out.println("[junit-platform-native] WARNING: Trying to find test-ids on default locations. " +
198+
"This should only happen if you are running tests executable manually.");
206199
Path defaultGradleTestIDsLocation = getGradleTestIdsDefaultLocation();
207200
Path defaultMavenTestIDsLocation = getMavenTestIDsDefaultLocation();
208201

209202
if (Files.exists(defaultGradleTestIDsLocation) && Files.exists(defaultMavenTestIDsLocation)) {
210203
throw new RuntimeException("[junit-platform-native] test-ids found in both " + defaultGradleTestIDsLocation + " and " + defaultMavenTestIDsLocation +
211-
". Please specify the test-ids location by passing the '--test-ids <path-to-test-ids>' argument to your tests executable.");
204+
". Please specify the test-ids location by passing the '--test-ids <path-to-test-ids>' argument to your tests executable.");
212205
}
213206

214207
if (Files.exists(defaultGradleTestIDsLocation)) {
215208
System.out.println("[junit-platform-native] WARNING: Using test-ids from default Gradle project location:" + defaultGradleTestIDsLocation);
216-
return defaultGradleTestIDsLocation.toString();
209+
return defaultGradleTestIDsLocation;
217210
}
218211

219212
if (Files.exists(defaultMavenTestIDsLocation)) {
220213
System.out.println("[junit-platform-native] WARNING: Using test-ids from default Maven project location:" + defaultMavenTestIDsLocation);
221-
return defaultMavenTestIDsLocation.toString();
214+
return defaultMavenTestIDsLocation;
222215
}
223216

224217
throw new RuntimeException("[junit-platform-native] test-ids not provided to the NativeImageJUnitLauncher and cannot be found on default locations. " +
225-
"Searched in: " + defaultGradleTestIDsLocation + " and " + defaultMavenTestIDsLocation);
218+
"Searched in: " + defaultGradleTestIDsLocation + " and " + defaultMavenTestIDsLocation);
226219
}
227220

228221
private static Path getGradleTestIdsDefaultLocation() {
229-
return Path.of(getBuildDirectory("/build/"))
222+
return Path.of(getBuildDirectory(File.separator + "build" + File.separator))
230223
.resolve("test-results")
231224
.resolve("test")
232225
.resolve("testlist")
233226
.toAbsolutePath();
234227
}
235228

236229
private static Path getMavenTestIDsDefaultLocation() {
237-
return Path.of(getBuildDirectory("/target/"))
230+
return Path.of(getBuildDirectory(File.separator + "target" + File.separator))
238231
.resolve("test-ids")
239232
.toAbsolutePath();
240233
}
241234

242235
private static String getBuildDirectory(String buildDir) {
243236
String executableLocation = Path.of(".").toAbsolutePath().toString();
244-
return executableLocation.substring(0, executableLocation.indexOf(buildDir) + buildDir.length());
237+
int index = executableLocation.indexOf(buildDir);
238+
if (index < 0) {
239+
return buildDir.substring(1);
240+
}
241+
242+
return executableLocation.substring(0, index + buildDir.length());
245243
}
246244

247245
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists

native-gradle-plugin/src/main/java/org/graalvm/buildtools/gradle/NativeImagePlugin.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
import org.gradle.api.tasks.bundling.AbstractArchiveTask;
111111
import org.gradle.api.tasks.bundling.Jar;
112112
import org.gradle.api.tasks.testing.Test;
113+
import org.gradle.internal.impldep.org.junit.platform.launcher.listeners.UniqueIdTrackingListener;
113114
import org.gradle.jvm.toolchain.JavaToolchainService;
114115
import org.gradle.language.base.plugins.LifecycleBasePlugin;
115116
import org.gradle.process.CommandLineArgumentProvider;
@@ -650,7 +651,6 @@ public void registerTestBinary(Project project,
650651

651652
// Following ensures that required feature jar is on classpath for every project
652653
injectTestPluginDependencies(project, graalExtension.getTestSupport());
653-
654654
TaskProvider<BuildNativeImageTask> testImageBuilder = tasks.named(deriveTaskName(name, "native", "Compile"), BuildNativeImageTask.class, task -> {
655655
task.setOnlyIf(t -> graalExtension.getTestSupport().get() && testListDirectory.getAsFile().get().exists());
656656
task.getTestListDirectory().set(testListDirectory);
@@ -662,6 +662,7 @@ public void registerTestBinary(Project project,
662662
// Later this will be replaced by a dedicated task not requiring execution of tests
663663
testList.from(testListDirectory).builtBy(testTask);
664664
testOptions.getClasspath().from(testList);
665+
testOptions.getRuntimeArgs().add("-D" + JUNIT_PLATFORM_LISTENERS_UID_TRACKING_OUTPUT_DIR + "=" + testResultsDir.dir(testTask.getName() + "/testlist").get().getAsFile().getAbsolutePath());
665666
});
666667
if (isPrimaryTest) {
667668
tasks.register(DEPRECATED_NATIVE_TEST_BUILD_TASK, t -> {
@@ -768,8 +769,6 @@ private static NativeImageOptions createTestOptions(GraalVMExtension graalExtens
768769
ListProperty<String> runtimeArgs = testExtension.getRuntimeArgs();
769770
runtimeArgs.add("--xml-output-dir");
770771
runtimeArgs.add(project.getLayout().getBuildDirectory().dir("test-results/" + binaryName + "-native").map(d -> d.getAsFile().getAbsolutePath()));
771-
runtimeArgs.add("--test-ids");
772-
runtimeArgs.add(project.getLayout().getBuildDirectory().dir("test-results/" + binaryName + "/testlist").map(d -> d.getAsFile().getAbsolutePath()));
773772

774773
testExtension.buildArgs("--features=org.graalvm.junit.platform.JUnitPlatformFeature");
775774
ConfigurableFileCollection classpath = testExtension.getClasspath();
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists

native-maven-plugin/src/main/java/org/graalvm/buildtools/maven/NativeTestMojo.java

-7
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,6 @@ private void runNativeTests(Path executable) throws MojoExecutionException {
219219
throw new MojoExecutionException("Failed creating xml output directory");
220220
}
221221

222-
Path testIdsLocation = Path.of(NativeExtension.testIdsDirectory(outputDirectory.getAbsolutePath()));
223-
if (!testIdsLocation.toFile().exists()) {
224-
throw new MojoExecutionException("Test-ids not available under target/test-ids");
225-
}
226-
227222
try {
228223
ProcessBuilder processBuilder = new ProcessBuilder(executable.toAbsolutePath().toString());
229224
processBuilder.inheritIO();
@@ -232,8 +227,6 @@ private void runNativeTests(Path executable) throws MojoExecutionException {
232227
List<String> command = new ArrayList<>();
233228
command.add("--xml-output-dir");
234229
command.add(xmlLocation.toString());
235-
command.add("--test-ids");
236-
command.add(testIdsLocation.toString());
237230
systemProperties.forEach((key, value) -> command.add("-D" + key + "=" + value));
238231

239232
processBuilder.command().addAll(command);

0 commit comments

Comments
 (0)