Skip to content

Commit

Permalink
Refactor UniqueIdTrackingListenerIntegrationTests to use a temp dir
Browse files Browse the repository at this point in the history
This way, the build script doesn't have to be tracked as an input which
avoids running the test task if it is changed.
  • Loading branch information
marcphilipp committed Nov 28, 2024
1 parent ec0b681 commit 5f1159d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ public class OutputDir {
Pattern.quote(OUTPUT_DIR_UNIQUE_NUMBER_PLACEHOLDER));

public static OutputDir create(Optional<String> customDir) {
return create(customDir, () -> Paths.get("."));
}

static OutputDir create(Optional<String> customDir, Supplier<Path> currentWorkingDir) {
try {
return createSafely(customDir, () -> Paths.get(".").toAbsolutePath());
return createSafely(customDir, currentWorkingDir);
}
catch (IOException e) {
throw new UncheckedIOException("Failed to create output dir", e);
Expand All @@ -53,7 +57,7 @@ static OutputDir createSafely(Optional<String> customDir, Supplier<Path> current

private static OutputDir createSafely(Optional<String> customDir, Supplier<Path> currentWorkingDir,
SecureRandom random) throws IOException {
Path cwd = currentWorkingDir.get();
Path cwd = currentWorkingDir.get().toAbsolutePath();
Path outputDir;

if (customDir.isPresent() && StringUtils.isNotBlank(customDir.get())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> uniqueIds = new ArrayList<>();
Expand Down Expand Up @@ -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<Path> 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");
}

Expand Down
2 changes: 0 additions & 2 deletions platform-tests/platform-tests.gradle.kts
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -116,28 +126,18 @@ private Condition<Event> 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<String> 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
Expand All @@ -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));
Expand All @@ -172,59 +172,45 @@ private void verifyUniqueIdsAreTracked(String outputDir, String prefix, Map<Stri
configurationParameters = new HashMap<>(configurationParameters);
configurationParameters.put(LISTENER_ENABLED_PROPERTY_NAME, "true");

deleteFiles(outputDir, prefix);

try {
List<String> 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<String, String> 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<String> executeTests(Map<String, String> configurationParameters) {
private List<String> executeTests(Map<String, String> configurationParameters) {
return executeTests(configurationParameters, selectClasses());
}

private static List<String> executeTests(Map<String, String> configurationParameters,
ClassSelector... classSelectors) {
private List<String> executeTests(Map<String, String> configurationParameters, ClassSelector... classSelectors) {
List<String> 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() {

Expand Down Expand Up @@ -260,8 +246,7 @@ private static ClassSelector[] selectClasses() {
selectClass(DisabledTestCase.class) };
}

private static Stream<Path> findFiles(String dir, String prefix) throws IOException {
Path outputDir = Paths.get(dir);
private static Stream<Path> findFiles(Path outputDir, String prefix) throws IOException {
if (!Files.exists(outputDir)) {
return Stream.empty();
}
Expand All @@ -270,18 +255,7 @@ private static Stream<Path> 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<String> readAllFiles(String outputDir, String prefix) throws IOException {
private Stream<String> readAllFiles(Path outputDir, String prefix) throws IOException {
return findFiles(outputDir, prefix).map(outputFile -> {
try {
return Files.readAllLines(outputFile);
Expand All @@ -306,6 +280,7 @@ void passingTest() {
void skippedTest() {
}

@SuppressWarnings("DataFlowIssue")
@Test
void abortedTest() {
assumeTrue(false);
Expand Down

0 comments on commit 5f1159d

Please sign in to comment.