From 5f1159d4d00b527026e20cdf9008650339a217e6 Mon Sep 17 00:00:00 2001 From: Marc Philipp Date: Thu, 28 Nov 2024 12:46:26 +0100 Subject: [PATCH] Refactor UniqueIdTrackingListenerIntegrationTests to use a temp dir This way, the build script doesn't have to be tracked as an input which avoids running the test task if it is changed. --- .../launcher/listeners/OutputDir.java | 8 +- .../listeners/UniqueIdTrackingListener.java | 8 +- platform-tests/platform-tests.gradle.kts | 2 - ...queIdTrackingListenerIntegrationTests.java | 111 +++++++----------- 4 files changed, 56 insertions(+), 73 deletions(-) diff --git a/junit-platform-launcher/src/main/java/org/junit/platform/launcher/listeners/OutputDir.java b/junit-platform-launcher/src/main/java/org/junit/platform/launcher/listeners/OutputDir.java index 27856d5154b8..389a83083a7c 100644 --- a/junit-platform-launcher/src/main/java/org/junit/platform/launcher/listeners/OutputDir.java +++ b/junit-platform-launcher/src/main/java/org/junit/platform/launcher/listeners/OutputDir.java @@ -36,8 +36,12 @@ public class OutputDir { Pattern.quote(OUTPUT_DIR_UNIQUE_NUMBER_PLACEHOLDER)); public static OutputDir create(Optional customDir) { + return create(customDir, () -> Paths.get(".")); + } + + static OutputDir create(Optional customDir, Supplier currentWorkingDir) { try { - return createSafely(customDir, () -> Paths.get(".").toAbsolutePath()); + return createSafely(customDir, currentWorkingDir); } catch (IOException e) { throw new UncheckedIOException("Failed to create output dir", e); @@ -53,7 +57,7 @@ static OutputDir createSafely(Optional customDir, Supplier current private static OutputDir createSafely(Optional customDir, Supplier currentWorkingDir, SecureRandom random) throws IOException { - Path cwd = currentWorkingDir.get(); + Path cwd = currentWorkingDir.get().toAbsolutePath(); Path outputDir; if (customDir.isPresent() && StringUtils.isNotBlank(customDir.get())) { diff --git a/junit-platform-launcher/src/main/java/org/junit/platform/launcher/listeners/UniqueIdTrackingListener.java b/junit-platform-launcher/src/main/java/org/junit/platform/launcher/listeners/UniqueIdTrackingListener.java index a7a02b662731..79e49e28c5ee 100644 --- a/junit-platform-launcher/src/main/java/org/junit/platform/launcher/listeners/UniqueIdTrackingListener.java +++ b/junit-platform-launcher/src/main/java/org/junit/platform/launcher/listeners/UniqueIdTrackingListener.java @@ -17,8 +17,10 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; +import java.util.function.Supplier; import org.apiguardian.api.API; import org.junit.platform.commons.logging.Logger; @@ -125,6 +127,8 @@ public class UniqueIdTrackingListener implements TestExecutionListener { */ public static final String DEFAULT_OUTPUT_FILE_PREFIX = "junit-platform-unique-ids"; + static final String WORKING_DIR_PROPERTY_NAME = "junit.platform.listeners.uid.tracking.working.dir"; + private final Logger logger = LoggerFactory.getLogger(UniqueIdTrackingListener.class); private final List uniqueIds = new ArrayList<>(); @@ -201,7 +205,9 @@ public void testPlanExecutionFinished(TestPlan testPlan) { private Path createOutputFile(ConfigurationParameters configurationParameters) { String prefix = configurationParameters.get(OUTPUT_FILE_PREFIX_PROPERTY_NAME) // .orElse(DEFAULT_OUTPUT_FILE_PREFIX); - return OutputDir.create(configurationParameters.get(OUTPUT_DIR_PROPERTY_NAME)) // + Supplier workingDirSupplier = () -> configurationParameters.get(WORKING_DIR_PROPERTY_NAME).map( + Paths::get).orElseGet(() -> Paths.get(".")); + return OutputDir.create(configurationParameters.get(OUTPUT_DIR_PROPERTY_NAME), workingDirSupplier) // .createFile(prefix, "txt"); } diff --git a/platform-tests/platform-tests.gradle.kts b/platform-tests/platform-tests.gradle.kts index c5b9fc768fa9..4c1c6960898f 100644 --- a/platform-tests/platform-tests.gradle.kts +++ b/platform-tests/platform-tests.gradle.kts @@ -1,6 +1,5 @@ import junitbuild.extensions.capitalized -import org.gradle.api.tasks.PathSensitivity.NONE import org.gradle.api.tasks.PathSensitivity.RELATIVE import org.gradle.internal.os.OperatingSystem @@ -103,7 +102,6 @@ tasks { test { // Additional inputs for remote execution with Test Distribution inputs.dir("src/test/resources").withPathSensitivity(RELATIVE) - inputs.file(buildFile).withPathSensitivity(NONE) // for UniqueIdTrackingListenerIntegrationTests } test_4_12 { useJUnitPlatform { diff --git a/platform-tests/src/test/java/org/junit/platform/launcher/listeners/UniqueIdTrackingListenerIntegrationTests.java b/platform-tests/src/test/java/org/junit/platform/launcher/listeners/UniqueIdTrackingListenerIntegrationTests.java index ada4c823a6ad..feb645cefaa8 100644 --- a/platform-tests/src/test/java/org/junit/platform/launcher/listeners/UniqueIdTrackingListenerIntegrationTests.java +++ b/platform-tests/src/test/java/org/junit/platform/launcher/listeners/UniqueIdTrackingListenerIntegrationTests.java @@ -23,6 +23,7 @@ import static org.junit.platform.launcher.listeners.UniqueIdTrackingListener.LISTENER_ENABLED_PROPERTY_NAME; import static org.junit.platform.launcher.listeners.UniqueIdTrackingListener.OUTPUT_DIR_PROPERTY_NAME; import static org.junit.platform.launcher.listeners.UniqueIdTrackingListener.OUTPUT_FILE_PREFIX_PROPERTY_NAME; +import static org.junit.platform.launcher.listeners.UniqueIdTrackingListener.WORKING_DIR_PROPERTY_NAME; import static org.junit.platform.testkit.engine.Event.byTestDescriptor; import static org.junit.platform.testkit.engine.EventConditions.abortedWithReason; import static org.junit.platform.testkit.engine.EventConditions.event; @@ -35,7 +36,6 @@ import java.io.UncheckedIOException; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -44,15 +44,16 @@ import java.util.stream.Stream; import org.assertj.core.api.Condition; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.DynamicTest; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestFactory; +import org.junit.jupiter.api.io.TempDir; import org.junit.platform.engine.TestDescriptor; import org.junit.platform.engine.TestExecutionResult; import org.junit.platform.engine.discovery.ClassSelector; -import org.junit.platform.launcher.LauncherDiscoveryRequest; import org.junit.platform.launcher.TestExecutionListener; import org.junit.platform.launcher.TestIdentifier; import org.junit.platform.launcher.TestPlan; @@ -88,6 +89,15 @@ class UniqueIdTrackingListenerIntegrationTests { private static final String[] expectedConcurrentUniqueIds = { testA, testB, testC, testD, testE, testF }; + @TempDir + Path workingDir; + + @BeforeEach + void createFakeGradleFiles() throws Exception { + Files.createFile(workingDir.resolve("build.gradle")); + Files.createDirectory(workingDir.resolve("build")); + } + @Test void confirmExpectedUniqueIdsViaEngineTestKit() { // @formatter:off @@ -116,28 +126,18 @@ private Condition uniqueId(String uniqueId) { @Test void listenerIsRegisteredButDisabledByDefault() throws Exception { - long numListenersRegistered = ServiceLoader.load(TestExecutionListener.class).stream()// + var numListenersRegistered = ServiceLoader.load(TestExecutionListener.class).stream()// .filter(provider -> UniqueIdTrackingListener.class.equals(provider.type()))// .count(); assertThat(numListenersRegistered).isEqualTo(1); - String outputDir = "build"; - String prefix = DEFAULT_OUTPUT_FILE_PREFIX; + var actualUniqueIds = executeTests(Map.of()); - deleteFiles(outputDir, prefix); + // Sanity check using the results of our local TestExecutionListener + assertThat(actualUniqueIds).containsExactlyInAnyOrder(expectedUniqueIds); - try { - List actualUniqueIds = executeTests(Map.of()); - - // Sanity check using the results of our local TestExecutionListener - assertThat(actualUniqueIds).containsExactlyInAnyOrder(expectedUniqueIds); - - // Check that files were not generated by the UniqueIdTrackingListener - assertThat(findFiles(outputDir, prefix)).isEmpty(); - } - finally { - deleteFiles(outputDir, prefix); - } + // Check that files were not generated by the UniqueIdTrackingListener + assertThat(findFiles(workingDir, DEFAULT_OUTPUT_FILE_PREFIX)).isEmpty(); } @Test @@ -147,20 +147,20 @@ void verifyUniqueIdsAreTrackedWithDefaults() throws Exception { @Test void verifyUniqueIdsAreTrackedWithCustomOutputFile() throws Exception { - String customPrefix = "test_ids"; + var customPrefix = "test_ids"; verifyUniqueIdsAreTracked("build", customPrefix, Map.of(OUTPUT_FILE_PREFIX_PROPERTY_NAME, customPrefix)); } @Test void verifyUniqueIdsAreTrackedWithCustomOutputDir() throws Exception { - String customDir = "build/UniqueIdTrackingListenerIntegrationTests"; + var customDir = "build/UniqueIdTrackingListenerIntegrationTests"; verifyUniqueIdsAreTracked(customDir, DEFAULT_OUTPUT_FILE_PREFIX, Map.of(OUTPUT_DIR_PROPERTY_NAME, customDir)); } @Test void verifyUniqueIdsAreTrackedWithCustomOutputFileAndCustomOutputDir() throws Exception { - String customPrefix = "test_ids"; - String customDir = "build/UniqueIdTrackingListenerIntegrationTests"; + var customPrefix = "test_ids"; + var customDir = "build/UniqueIdTrackingListenerIntegrationTests"; verifyUniqueIdsAreTracked(customDir, customPrefix, Map.of(OUTPUT_DIR_PROPERTY_NAME, customDir, OUTPUT_FILE_PREFIX_PROPERTY_NAME, customPrefix)); @@ -172,59 +172,45 @@ private void verifyUniqueIdsAreTracked(String outputDir, String prefix, Map(configurationParameters); configurationParameters.put(LISTENER_ENABLED_PROPERTY_NAME, "true"); - deleteFiles(outputDir, prefix); - - try { - List actualUniqueIds = executeTests(configurationParameters); + var actualUniqueIds = executeTests(configurationParameters); - // Sanity check using the results of our local TestExecutionListener - assertThat(actualUniqueIds).containsExactlyInAnyOrder(expectedUniqueIds); + // Sanity check using the results of our local TestExecutionListener + assertThat(actualUniqueIds).containsExactlyInAnyOrder(expectedUniqueIds); - // Check contents of the file (or files) generated by the UniqueIdTrackingListener - assertThat(readAllFiles(outputDir, prefix)).containsExactlyInAnyOrder(expectedUniqueIds); - } - finally { - deleteFiles(outputDir, prefix); - } + // Check contents of the file (or files) generated by the UniqueIdTrackingListener + assertThat(readAllFiles(workingDir.resolve(outputDir), prefix)).containsExactlyInAnyOrder(expectedUniqueIds); } @Test void verifyUniqueIdsAreTrackedWithConcurrentlyExecutingTestPlans() throws Exception { - String customDir = "build/UniqueIdTrackingListenerIntegrationTests"; - String prefix = DEFAULT_OUTPUT_FILE_PREFIX; + var customDir = workingDir.resolve("build/UniqueIdTrackingListenerIntegrationTests"); + var prefix = DEFAULT_OUTPUT_FILE_PREFIX; Map configurationParameters = new HashMap<>(); configurationParameters.put(LISTENER_ENABLED_PROPERTY_NAME, "true"); - configurationParameters.put(OUTPUT_DIR_PROPERTY_NAME, customDir); - - deleteFiles(customDir, prefix); + configurationParameters.put(OUTPUT_DIR_PROPERTY_NAME, customDir.toAbsolutePath().toString()); - try { - Stream.of(TestCase2.class, TestCase3.class, TestCase4.class).parallel()// - .forEach(clazz -> executeTests(configurationParameters, selectClass(clazz))); + Stream.of(TestCase2.class, TestCase3.class, TestCase4.class).parallel()// + .forEach(clazz -> executeTests(configurationParameters, selectClass(clazz))); - // 3 output files should have been generated. - assertThat(findFiles(customDir, prefix)).hasSize(3); + // 3 output files should have been generated. + assertThat(findFiles(customDir, prefix)).hasSize(3); - // Check contents of the file (or files) generated by the UniqueIdTrackingListener - assertThat(readAllFiles(customDir, prefix)).containsExactlyInAnyOrder(expectedConcurrentUniqueIds); - } - finally { - deleteFiles(customDir, prefix); - } + // Check contents of the file (or files) generated by the UniqueIdTrackingListener + assertThat(readAllFiles(customDir, prefix)).containsExactlyInAnyOrder(expectedConcurrentUniqueIds); } - private static List executeTests(Map configurationParameters) { + private List executeTests(Map configurationParameters) { return executeTests(configurationParameters, selectClasses()); } - private static List executeTests(Map configurationParameters, - ClassSelector... classSelectors) { + private List executeTests(Map configurationParameters, ClassSelector... classSelectors) { List uniqueIds = new ArrayList<>(); - LauncherDiscoveryRequest request = request()// + var request = request()// .selectors(classSelectors)// .filters(includeEngines("junit-jupiter"))// .configurationParameters(configurationParameters)// + .configurationParameter(WORKING_DIR_PROPERTY_NAME, workingDir.toAbsolutePath().toString())// .build(); LauncherFactory.create().execute(request, new TestExecutionListener() { @@ -260,8 +246,7 @@ private static ClassSelector[] selectClasses() { selectClass(DisabledTestCase.class) }; } - private static Stream findFiles(String dir, String prefix) throws IOException { - Path outputDir = Paths.get(dir); + private static Stream findFiles(Path outputDir, String prefix) throws IOException { if (!Files.exists(outputDir)) { return Stream.empty(); } @@ -270,18 +255,7 @@ private static Stream findFiles(String dir, String prefix) throws IOExcept && path.getFileName().toString().startsWith(prefix))); } - private void deleteFiles(String outputDir, String prefix) throws IOException { - findFiles(outputDir, prefix).forEach(file -> { - try { - Files.deleteIfExists(file); - } - catch (IOException ex) { - throw new UncheckedIOException(ex); - } - }); - } - - private Stream readAllFiles(String outputDir, String prefix) throws IOException { + private Stream readAllFiles(Path outputDir, String prefix) throws IOException { return findFiles(outputDir, prefix).map(outputFile -> { try { return Files.readAllLines(outputFile); @@ -306,6 +280,7 @@ void passingTest() { void skippedTest() { } + @SuppressWarnings("DataFlowIssue") @Test void abortedTest() { assumeTrue(false);