Skip to content

Commit

Permalink
Use system property to calculate test-ids location
Browse files Browse the repository at this point in the history
  • Loading branch information
dnestoro committed Feb 20, 2025
1 parent d2a3660 commit 3a2166c
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

def agentOutput = layout.buildDirectory.dir("agent")

ext {
Expand Down Expand Up @@ -143,4 +144,5 @@ tasks.register("nativeTest", Exec) {
dependsOn nativeTestCompile
workingDir = "${buildDir}"
executable = "${buildDir}/native-image-tests"
args = ["-Djunit.platform.listeners.uid.tracking.output.dir=${testIdsDir.get().asFile.absolutePath}"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public void beforeAnalysis(BeforeAnalysisAccess access) {

private List<? extends DiscoverySelector> getSelectors() {
try {
Path uniqueIdDirectory = Paths.get(System.getProperty(UniqueIdTrackingListener.OUTPUT_DIR_PROPERTY_NAME));
Path uniqueIdDirectory = Path.of(System.getProperty(UniqueIdTrackingListener.OUTPUT_DIR_PROPERTY_NAME));
String uniqueIdFilePrefix = System.getProperty(UniqueIdTrackingListener.OUTPUT_FILE_PREFIX_PROPERTY_NAME,
UniqueIdTrackingListener.DEFAULT_OUTPUT_FILE_PREFIX);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ public static void main(String... args) {

/* scan runtime arguments */
String xmlOutput = DEFAULT_OUTPUT_FOLDER;
String testIds = null;
boolean silent = false;

LinkedList<String> arguments = new LinkedList<>(Arrays.asList(args));
Expand All @@ -94,17 +93,13 @@ public static void main(String... args) {
System.out.println("----------------------------------------\n");
System.out.println("Flags:");
System.out.println(stringPad("--xml-output-dir") + "Selects report xml output directory (default: `" + DEFAULT_OUTPUT_FOLDER + "`)");
System.out.println(stringPad("--test-ids") + "Provides path to generated testIDs");
System.out.println(stringPad("--silent") + "Only output xml without stdout summary");
System.out.println(stringPad("--help") + "Displays this help screen");
System.exit(0);
break;
case "--xml-output-dir":
xmlOutput = arguments.poll();
break;
case "--test-ids":
testIds = arguments.poll();
break;
case "--silent":
silent = true;
break;
Expand All @@ -119,14 +114,8 @@ public static void main(String... args) {
throw new RuntimeException("xml-output-dir argument passed incorrectly to the launcher class.");
}

if (testIds == null) {
System.out.println("[junit-platform-native] WARNING: test-ids not provided to the NativeImageJUnitLauncher. " +
"This should only happen if you are running tests binary manually (instead of using 'gradle nativeTest' command)");
testIds = getTestIDsFromDefaultLocations();
}

Launcher launcher = LauncherFactory.create();
TestPlan testPlan = getTestPlan(launcher, testIds);
TestPlan testPlan = getTestPlan(launcher);

PrintWriter out = new PrintWriter(System.out);
if (!silent) {
Expand All @@ -151,28 +140,30 @@ public static void main(String... args) {
System.exit(failedCount > 0 ? 1 : 0);
}

private static TestPlan getTestPlan(Launcher launcher, String testIDs) {
List<? extends DiscoverySelector> selectors = getSelectors(testIDs);
private static TestPlan getTestPlan(Launcher launcher) {
List<? extends DiscoverySelector> selectors = getSelectors();
LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
.selectors(selectors)
.build();

return launcher.discover(request);
}

private static List<? extends DiscoverySelector> getSelectors(String testIDs) {
private static List<? extends DiscoverySelector> getSelectors() {
try {
Path outputDir = Paths.get(testIDs);
String prefix = System.getProperty(UniqueIdTrackingListener.OUTPUT_FILE_PREFIX_PROPERTY_NAME,
String systemPropertyBasedLocation = System.getProperty(UniqueIdTrackingListener.OUTPUT_DIR_PROPERTY_NAME);
Path uniqueIdDirectory = systemPropertyBasedLocation != null ? Path.of(systemPropertyBasedLocation) : getTestIDsFromDefaultLocations();
String uniqueIdFilePrefix = System.getProperty(UniqueIdTrackingListener.OUTPUT_FILE_PREFIX_PROPERTY_NAME,
UniqueIdTrackingListener.DEFAULT_OUTPUT_FILE_PREFIX);
List<UniqueIdSelector> selectors = readAllFiles(outputDir, prefix)

List<UniqueIdSelector> selectors = readAllFiles(uniqueIdDirectory, uniqueIdFilePrefix)
.map(DiscoverySelectors::selectUniqueId)
.collect(Collectors.toList());
if (!selectors.isEmpty()) {
System.out.printf(
"[junit-platform-native] Running in 'test listener' mode using files matching pattern [%s*] "
+ "found in folder [%s] and its subfolders.%n",
prefix, outputDir.toAbsolutePath());
uniqueIdFilePrefix, uniqueIdDirectory.toAbsolutePath());
return selectors;
}
} catch (Exception ex) {
Expand Down Expand Up @@ -201,28 +192,28 @@ private static Stream<Path> findFiles(Path dir, String prefix) throws IOExceptio
&& path.getFileName().toString().startsWith(prefix)));
}

private static String getTestIDsFromDefaultLocations() {
private static Path getTestIDsFromDefaultLocations() {
System.out.println("[junit-platform-native] WARNING: Trying to find test-ids on default locations.");
Path defaultGradleTestIDsLocation = getGradleTestIdsDefaultLocation();
Path defaultMavenTestIDsLocation = getMavenTestIDsDefaultLocation();

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

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

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

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

private static Path getGradleTestIdsDefaultLocation() {
Expand All @@ -241,7 +232,12 @@ private static Path getMavenTestIDsDefaultLocation() {

private static String getBuildDirectory(String buildDir) {
String executableLocation = Path.of(".").toAbsolutePath().toString();
return executableLocation.substring(0, executableLocation.indexOf(buildDir) + buildDir.length());
int index = executableLocation.indexOf(buildDir);
if (index < 0) {
return buildDir.substring(1);
}

return executableLocation.substring(0, + buildDir.length());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
import org.gradle.api.tasks.bundling.AbstractArchiveTask;
import org.gradle.api.tasks.bundling.Jar;
import org.gradle.api.tasks.testing.Test;
import org.gradle.internal.impldep.org.junit.platform.launcher.listeners.UniqueIdTrackingListener;
import org.gradle.jvm.toolchain.JavaToolchainService;
import org.gradle.language.base.plugins.LifecycleBasePlugin;
import org.gradle.process.CommandLineArgumentProvider;
Expand All @@ -122,6 +123,7 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -768,8 +770,16 @@ private static NativeImageOptions createTestOptions(GraalVMExtension graalExtens
ListProperty<String> runtimeArgs = testExtension.getRuntimeArgs();
runtimeArgs.add("--xml-output-dir");
runtimeArgs.add(project.getLayout().getBuildDirectory().dir("test-results/" + binaryName + "-native").map(d -> d.getAsFile().getAbsolutePath()));
runtimeArgs.add("--test-ids");
runtimeArgs.add(project.getLayout().getBuildDirectory().dir("test-results/" + binaryName + "/testlist").map(d -> d.getAsFile().getAbsolutePath()));

String testIds;
String propertyBasedLocation = System.getProperty(UniqueIdTrackingListener.OUTPUT_DIR_PROPERTY_NAME);
if (propertyBasedLocation != null) {
testIds = Path.of(propertyBasedLocation).toAbsolutePath().toString();
} else {
testIds = project.getLayout().getBuildDirectory().dir("test-results/" + binaryName + "/testlist").map(d -> d.getAsFile().getAbsolutePath()).get();
}

runtimeArgs.add("-Djunit.platform.listeners.uid.tracking.output.dir=" + testIds);

testExtension.buildArgs("--features=org.graalvm.junit.platform.JUnitPlatformFeature");
ConfigurableFileCollection classpath = testExtension.getClasspath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,6 @@ private void runNativeTests(Path executable) throws MojoExecutionException {
throw new MojoExecutionException("Failed creating xml output directory");
}

Path testIdsLocation = Path.of(NativeExtension.testIdsDirectory(outputDirectory.getAbsolutePath()));
if (!testIdsLocation.toFile().exists()) {
throw new MojoExecutionException("Test-ids not available under target/test-ids");
}

try {
ProcessBuilder processBuilder = new ProcessBuilder(executable.toAbsolutePath().toString());
processBuilder.inheritIO();
Expand All @@ -232,8 +227,6 @@ private void runNativeTests(Path executable) throws MojoExecutionException {
List<String> command = new ArrayList<>();
command.add("--xml-output-dir");
command.add(xmlLocation.toString());
command.add("--test-ids");
command.add(testIdsLocation.toString());
systemProperties.forEach((key, value) -> command.add("-D" + key + "=" + value));

processBuilder.command().addAll(command);
Expand Down

0 comments on commit 3a2166c

Please sign in to comment.