Skip to content

Commit 5f1159d

Browse files
committed
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.
1 parent ec0b681 commit 5f1159d

File tree

4 files changed

+56
-73
lines changed

4 files changed

+56
-73
lines changed

junit-platform-launcher/src/main/java/org/junit/platform/launcher/listeners/OutputDir.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,12 @@ public class OutputDir {
3636
Pattern.quote(OUTPUT_DIR_UNIQUE_NUMBER_PLACEHOLDER));
3737

3838
public static OutputDir create(Optional<String> customDir) {
39+
return create(customDir, () -> Paths.get("."));
40+
}
41+
42+
static OutputDir create(Optional<String> customDir, Supplier<Path> currentWorkingDir) {
3943
try {
40-
return createSafely(customDir, () -> Paths.get(".").toAbsolutePath());
44+
return createSafely(customDir, currentWorkingDir);
4145
}
4246
catch (IOException e) {
4347
throw new UncheckedIOException("Failed to create output dir", e);
@@ -53,7 +57,7 @@ static OutputDir createSafely(Optional<String> customDir, Supplier<Path> current
5357

5458
private static OutputDir createSafely(Optional<String> customDir, Supplier<Path> currentWorkingDir,
5559
SecureRandom random) throws IOException {
56-
Path cwd = currentWorkingDir.get();
60+
Path cwd = currentWorkingDir.get().toAbsolutePath();
5761
Path outputDir;
5862

5963
if (customDir.isPresent() && StringUtils.isNotBlank(customDir.get())) {

junit-platform-launcher/src/main/java/org/junit/platform/launcher/listeners/UniqueIdTrackingListener.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
import java.nio.charset.StandardCharsets;
1818
import java.nio.file.Files;
1919
import java.nio.file.Path;
20+
import java.nio.file.Paths;
2021
import java.util.ArrayList;
2122
import java.util.List;
23+
import java.util.function.Supplier;
2224

2325
import org.apiguardian.api.API;
2426
import org.junit.platform.commons.logging.Logger;
@@ -125,6 +127,8 @@ public class UniqueIdTrackingListener implements TestExecutionListener {
125127
*/
126128
public static final String DEFAULT_OUTPUT_FILE_PREFIX = "junit-platform-unique-ids";
127129

130+
static final String WORKING_DIR_PROPERTY_NAME = "junit.platform.listeners.uid.tracking.working.dir";
131+
128132
private final Logger logger = LoggerFactory.getLogger(UniqueIdTrackingListener.class);
129133

130134
private final List<String> uniqueIds = new ArrayList<>();
@@ -201,7 +205,9 @@ public void testPlanExecutionFinished(TestPlan testPlan) {
201205
private Path createOutputFile(ConfigurationParameters configurationParameters) {
202206
String prefix = configurationParameters.get(OUTPUT_FILE_PREFIX_PROPERTY_NAME) //
203207
.orElse(DEFAULT_OUTPUT_FILE_PREFIX);
204-
return OutputDir.create(configurationParameters.get(OUTPUT_DIR_PROPERTY_NAME)) //
208+
Supplier<Path> workingDirSupplier = () -> configurationParameters.get(WORKING_DIR_PROPERTY_NAME).map(
209+
Paths::get).orElseGet(() -> Paths.get("."));
210+
return OutputDir.create(configurationParameters.get(OUTPUT_DIR_PROPERTY_NAME), workingDirSupplier) //
205211
.createFile(prefix, "txt");
206212
}
207213

platform-tests/platform-tests.gradle.kts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11

22
import junitbuild.extensions.capitalized
3-
import org.gradle.api.tasks.PathSensitivity.NONE
43
import org.gradle.api.tasks.PathSensitivity.RELATIVE
54
import org.gradle.internal.os.OperatingSystem
65

@@ -103,7 +102,6 @@ tasks {
103102
test {
104103
// Additional inputs for remote execution with Test Distribution
105104
inputs.dir("src/test/resources").withPathSensitivity(RELATIVE)
106-
inputs.file(buildFile).withPathSensitivity(NONE) // for UniqueIdTrackingListenerIntegrationTests
107105
}
108106
test_4_12 {
109107
useJUnitPlatform {

platform-tests/src/test/java/org/junit/platform/launcher/listeners/UniqueIdTrackingListenerIntegrationTests.java

Lines changed: 43 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import static org.junit.platform.launcher.listeners.UniqueIdTrackingListener.LISTENER_ENABLED_PROPERTY_NAME;
2424
import static org.junit.platform.launcher.listeners.UniqueIdTrackingListener.OUTPUT_DIR_PROPERTY_NAME;
2525
import static org.junit.platform.launcher.listeners.UniqueIdTrackingListener.OUTPUT_FILE_PREFIX_PROPERTY_NAME;
26+
import static org.junit.platform.launcher.listeners.UniqueIdTrackingListener.WORKING_DIR_PROPERTY_NAME;
2627
import static org.junit.platform.testkit.engine.Event.byTestDescriptor;
2728
import static org.junit.platform.testkit.engine.EventConditions.abortedWithReason;
2829
import static org.junit.platform.testkit.engine.EventConditions.event;
@@ -35,7 +36,6 @@
3536
import java.io.UncheckedIOException;
3637
import java.nio.file.Files;
3738
import java.nio.file.Path;
38-
import java.nio.file.Paths;
3939
import java.util.ArrayList;
4040
import java.util.HashMap;
4141
import java.util.List;
@@ -44,15 +44,16 @@
4444
import java.util.stream.Stream;
4545

4646
import org.assertj.core.api.Condition;
47+
import org.junit.jupiter.api.BeforeEach;
4748
import org.junit.jupiter.api.Disabled;
4849
import org.junit.jupiter.api.DynamicTest;
4950
import org.junit.jupiter.api.Nested;
5051
import org.junit.jupiter.api.Test;
5152
import org.junit.jupiter.api.TestFactory;
53+
import org.junit.jupiter.api.io.TempDir;
5254
import org.junit.platform.engine.TestDescriptor;
5355
import org.junit.platform.engine.TestExecutionResult;
5456
import org.junit.platform.engine.discovery.ClassSelector;
55-
import org.junit.platform.launcher.LauncherDiscoveryRequest;
5657
import org.junit.platform.launcher.TestExecutionListener;
5758
import org.junit.platform.launcher.TestIdentifier;
5859
import org.junit.platform.launcher.TestPlan;
@@ -88,6 +89,15 @@ class UniqueIdTrackingListenerIntegrationTests {
8889

8990
private static final String[] expectedConcurrentUniqueIds = { testA, testB, testC, testD, testE, testF };
9091

92+
@TempDir
93+
Path workingDir;
94+
95+
@BeforeEach
96+
void createFakeGradleFiles() throws Exception {
97+
Files.createFile(workingDir.resolve("build.gradle"));
98+
Files.createDirectory(workingDir.resolve("build"));
99+
}
100+
91101
@Test
92102
void confirmExpectedUniqueIdsViaEngineTestKit() {
93103
// @formatter:off
@@ -116,28 +126,18 @@ private Condition<Event> uniqueId(String uniqueId) {
116126

117127
@Test
118128
void listenerIsRegisteredButDisabledByDefault() throws Exception {
119-
long numListenersRegistered = ServiceLoader.load(TestExecutionListener.class).stream()//
129+
var numListenersRegistered = ServiceLoader.load(TestExecutionListener.class).stream()//
120130
.filter(provider -> UniqueIdTrackingListener.class.equals(provider.type()))//
121131
.count();
122132
assertThat(numListenersRegistered).isEqualTo(1);
123133

124-
String outputDir = "build";
125-
String prefix = DEFAULT_OUTPUT_FILE_PREFIX;
134+
var actualUniqueIds = executeTests(Map.of());
126135

127-
deleteFiles(outputDir, prefix);
136+
// Sanity check using the results of our local TestExecutionListener
137+
assertThat(actualUniqueIds).containsExactlyInAnyOrder(expectedUniqueIds);
128138

129-
try {
130-
List<String> actualUniqueIds = executeTests(Map.of());
131-
132-
// Sanity check using the results of our local TestExecutionListener
133-
assertThat(actualUniqueIds).containsExactlyInAnyOrder(expectedUniqueIds);
134-
135-
// Check that files were not generated by the UniqueIdTrackingListener
136-
assertThat(findFiles(outputDir, prefix)).isEmpty();
137-
}
138-
finally {
139-
deleteFiles(outputDir, prefix);
140-
}
139+
// Check that files were not generated by the UniqueIdTrackingListener
140+
assertThat(findFiles(workingDir, DEFAULT_OUTPUT_FILE_PREFIX)).isEmpty();
141141
}
142142

143143
@Test
@@ -147,20 +147,20 @@ void verifyUniqueIdsAreTrackedWithDefaults() throws Exception {
147147

148148
@Test
149149
void verifyUniqueIdsAreTrackedWithCustomOutputFile() throws Exception {
150-
String customPrefix = "test_ids";
150+
var customPrefix = "test_ids";
151151
verifyUniqueIdsAreTracked("build", customPrefix, Map.of(OUTPUT_FILE_PREFIX_PROPERTY_NAME, customPrefix));
152152
}
153153

154154
@Test
155155
void verifyUniqueIdsAreTrackedWithCustomOutputDir() throws Exception {
156-
String customDir = "build/UniqueIdTrackingListenerIntegrationTests";
156+
var customDir = "build/UniqueIdTrackingListenerIntegrationTests";
157157
verifyUniqueIdsAreTracked(customDir, DEFAULT_OUTPUT_FILE_PREFIX, Map.of(OUTPUT_DIR_PROPERTY_NAME, customDir));
158158
}
159159

160160
@Test
161161
void verifyUniqueIdsAreTrackedWithCustomOutputFileAndCustomOutputDir() throws Exception {
162-
String customPrefix = "test_ids";
163-
String customDir = "build/UniqueIdTrackingListenerIntegrationTests";
162+
var customPrefix = "test_ids";
163+
var customDir = "build/UniqueIdTrackingListenerIntegrationTests";
164164

165165
verifyUniqueIdsAreTracked(customDir, customPrefix,
166166
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<Stri
172172
configurationParameters = new HashMap<>(configurationParameters);
173173
configurationParameters.put(LISTENER_ENABLED_PROPERTY_NAME, "true");
174174

175-
deleteFiles(outputDir, prefix);
176-
177-
try {
178-
List<String> actualUniqueIds = executeTests(configurationParameters);
175+
var actualUniqueIds = executeTests(configurationParameters);
179176

180-
// Sanity check using the results of our local TestExecutionListener
181-
assertThat(actualUniqueIds).containsExactlyInAnyOrder(expectedUniqueIds);
177+
// Sanity check using the results of our local TestExecutionListener
178+
assertThat(actualUniqueIds).containsExactlyInAnyOrder(expectedUniqueIds);
182179

183-
// Check contents of the file (or files) generated by the UniqueIdTrackingListener
184-
assertThat(readAllFiles(outputDir, prefix)).containsExactlyInAnyOrder(expectedUniqueIds);
185-
}
186-
finally {
187-
deleteFiles(outputDir, prefix);
188-
}
180+
// Check contents of the file (or files) generated by the UniqueIdTrackingListener
181+
assertThat(readAllFiles(workingDir.resolve(outputDir), prefix)).containsExactlyInAnyOrder(expectedUniqueIds);
189182
}
190183

191184
@Test
192185
void verifyUniqueIdsAreTrackedWithConcurrentlyExecutingTestPlans() throws Exception {
193-
String customDir = "build/UniqueIdTrackingListenerIntegrationTests";
194-
String prefix = DEFAULT_OUTPUT_FILE_PREFIX;
186+
var customDir = workingDir.resolve("build/UniqueIdTrackingListenerIntegrationTests");
187+
var prefix = DEFAULT_OUTPUT_FILE_PREFIX;
195188

196189
Map<String, String> configurationParameters = new HashMap<>();
197190
configurationParameters.put(LISTENER_ENABLED_PROPERTY_NAME, "true");
198-
configurationParameters.put(OUTPUT_DIR_PROPERTY_NAME, customDir);
199-
200-
deleteFiles(customDir, prefix);
191+
configurationParameters.put(OUTPUT_DIR_PROPERTY_NAME, customDir.toAbsolutePath().toString());
201192

202-
try {
203-
Stream.of(TestCase2.class, TestCase3.class, TestCase4.class).parallel()//
204-
.forEach(clazz -> executeTests(configurationParameters, selectClass(clazz)));
193+
Stream.of(TestCase2.class, TestCase3.class, TestCase4.class).parallel()//
194+
.forEach(clazz -> executeTests(configurationParameters, selectClass(clazz)));
205195

206-
// 3 output files should have been generated.
207-
assertThat(findFiles(customDir, prefix)).hasSize(3);
196+
// 3 output files should have been generated.
197+
assertThat(findFiles(customDir, prefix)).hasSize(3);
208198

209-
// Check contents of the file (or files) generated by the UniqueIdTrackingListener
210-
assertThat(readAllFiles(customDir, prefix)).containsExactlyInAnyOrder(expectedConcurrentUniqueIds);
211-
}
212-
finally {
213-
deleteFiles(customDir, prefix);
214-
}
199+
// Check contents of the file (or files) generated by the UniqueIdTrackingListener
200+
assertThat(readAllFiles(customDir, prefix)).containsExactlyInAnyOrder(expectedConcurrentUniqueIds);
215201
}
216202

217-
private static List<String> executeTests(Map<String, String> configurationParameters) {
203+
private List<String> executeTests(Map<String, String> configurationParameters) {
218204
return executeTests(configurationParameters, selectClasses());
219205
}
220206

221-
private static List<String> executeTests(Map<String, String> configurationParameters,
222-
ClassSelector... classSelectors) {
207+
private List<String> executeTests(Map<String, String> configurationParameters, ClassSelector... classSelectors) {
223208
List<String> uniqueIds = new ArrayList<>();
224-
LauncherDiscoveryRequest request = request()//
209+
var request = request()//
225210
.selectors(classSelectors)//
226211
.filters(includeEngines("junit-jupiter"))//
227212
.configurationParameters(configurationParameters)//
213+
.configurationParameter(WORKING_DIR_PROPERTY_NAME, workingDir.toAbsolutePath().toString())//
228214
.build();
229215
LauncherFactory.create().execute(request, new TestExecutionListener() {
230216

@@ -260,8 +246,7 @@ private static ClassSelector[] selectClasses() {
260246
selectClass(DisabledTestCase.class) };
261247
}
262248

263-
private static Stream<Path> findFiles(String dir, String prefix) throws IOException {
264-
Path outputDir = Paths.get(dir);
249+
private static Stream<Path> findFiles(Path outputDir, String prefix) throws IOException {
265250
if (!Files.exists(outputDir)) {
266251
return Stream.empty();
267252
}
@@ -270,18 +255,7 @@ private static Stream<Path> findFiles(String dir, String prefix) throws IOExcept
270255
&& path.getFileName().toString().startsWith(prefix)));
271256
}
272257

273-
private void deleteFiles(String outputDir, String prefix) throws IOException {
274-
findFiles(outputDir, prefix).forEach(file -> {
275-
try {
276-
Files.deleteIfExists(file);
277-
}
278-
catch (IOException ex) {
279-
throw new UncheckedIOException(ex);
280-
}
281-
});
282-
}
283-
284-
private Stream<String> readAllFiles(String outputDir, String prefix) throws IOException {
258+
private Stream<String> readAllFiles(Path outputDir, String prefix) throws IOException {
285259
return findFiles(outputDir, prefix).map(outputFile -> {
286260
try {
287261
return Files.readAllLines(outputFile);
@@ -306,6 +280,7 @@ void passingTest() {
306280
void skippedTest() {
307281
}
308282

283+
@SuppressWarnings("DataFlowIssue")
309284
@Test
310285
void abortedTest() {
311286
assumeTrue(false);

0 commit comments

Comments
 (0)