Skip to content

Commit d9a76a1

Browse files
committed
chore: test cleanup behaviour
1 parent 8223905 commit d9a76a1

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

app/src/main/java/com/diffplug/spotless/cli/core/SpotlessRunCleanup.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
import java.nio.file.Files;
2020
import java.nio.file.Path;
2121
import java.util.Comparator;
22+
import java.util.concurrent.ConcurrentLinkedQueue;
2223

24+
import org.jetbrains.annotations.VisibleForTesting;
2325
import org.slf4j.Logger;
2426
import org.slf4j.LoggerFactory;
2527

@@ -30,9 +32,21 @@ public enum SpotlessRunCleanup {
3032

3133
private static final Cleaner CLEANER = Cleaner.create();
3234

35+
private static final ConcurrentLinkedQueue<Cleaner.Cleanable> CLEANABLES = new ConcurrentLinkedQueue<>();
36+
3337
public void deleteDirOnCleanup(Object reference, Path path) {
3438
LOGGER.debug("Registering cleanup for directory: {} -- reference: {}", path, reference);
35-
CLEANER.register(reference, new PathCleanup(path));
39+
CLEANABLES.add(CLEANER.register(reference, new PathCleanup(path)));
40+
}
41+
42+
@VisibleForTesting
43+
public void clearCleanables() {
44+
while (!CLEANABLES.isEmpty()) {
45+
Cleaner.Cleanable cleanable = CLEANABLES.poll();
46+
if (cleanable != null) {
47+
cleanable.clean();
48+
}
49+
}
3650
}
3751

3852
private record PathCleanup(Path path) implements Runnable {

app/src/test/java/com/diffplug/spotless/cli/core/ExecutionLayoutTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@
1515
*/
1616
package com.diffplug.spotless.cli.core;
1717

18+
import java.io.IOException;
19+
import java.nio.file.Files;
1820
import java.nio.file.Path;
1921

22+
import org.junit.jupiter.api.AfterEach;
23+
import org.junit.jupiter.api.BeforeEach;
2024
import org.junit.jupiter.api.Test;
2125
import org.mockito.Mockito;
2226

@@ -26,6 +30,19 @@
2630

2731
class ExecutionLayoutTest extends ResourceHarness {
2832

33+
SpotlessRunCleanup cleanup;
34+
35+
@BeforeEach
36+
void setUp() throws IOException {
37+
cleanup = SpotlessRunCleanup.INSTANCE;
38+
}
39+
40+
@AfterEach
41+
void cleanup() {
42+
cleanup.clearCleanables();
43+
cleanup = null;
44+
}
45+
2946
@Test
3047
void itResolvesGradleBuildDir() {
3148
setFile("settings.gradle").toLines("rootProject.name = 'test'");
@@ -65,6 +82,19 @@ void itResolvesTmpBuildDir() {
6582
.startsWith(System.getProperty("java.io.tmpdir"));
6683
}
6784

85+
@Test
86+
void itCleansUpTmpBuildDir() throws IOException {
87+
ExecutionLayout layout = ExecutionLayout.create(fileResolver(), commandLineStream());
88+
Path buildDir = layout.buildDir();
89+
buildDir.toFile().mkdirs(); // make sure it's ready
90+
Path tmpFile = buildDir.resolve("tmpFile.txt");
91+
Files.writeString(tmpFile, "Test %s".formatted(buildDir));
92+
93+
cleanup.clearCleanables();
94+
95+
assertThat(Files.exists(buildDir)).isFalse();
96+
}
97+
6898
private FileResolver fileResolver() {
6999
return new FileResolver(rootFolder().toPath());
70100
}

0 commit comments

Comments
 (0)