Skip to content

Commit d1cf2c6

Browse files
committed
Refactoring of dir deletion
1 parent 592dc8a commit d1cf2c6

File tree

1 file changed

+39
-50
lines changed

1 file changed

+39
-50
lines changed

cli/src/test/java/de/otto/jlineup/cli/acceptance/JLineupCLIAcceptanceTest.java

+39-50
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
import java.io.IOException;
1616
import java.io.PrintStream;
1717
import java.net.URL;
18-
import java.nio.file.*;
19-
import java.nio.file.attribute.BasicFileAttributes;
18+
import java.nio.file.Files;
19+
import java.nio.file.Path;
20+
import java.nio.file.Paths;
2021
import java.util.ArrayList;
22+
import java.util.Comparator;
2123
import java.util.List;
2224
import java.util.Map;
2325
import java.util.stream.Collectors;
@@ -63,7 +65,7 @@ public void cleanUpStreams() {
6365
}
6466

6567
@After
66-
public void deleteTempDir() throws IOException {
68+
public void deleteTempDir() throws Exception {
6769
deleteDir(tempDirectory);
6870
}
6971

@@ -72,52 +74,51 @@ public void shouldExitWithExitStatus1IfConfigHasNoUrls() throws Exception {
7274
exit.expectSystemExitWithStatus(1);
7375
exit.checkAssertionAfterwards(() -> assertThat(systemErrCaptor.toString(), containsString("No URLs configured.")));
7476

75-
Main.main(new String[]{"--working-dir",tempDirectory.toString(),"--config","src/test/resources/acceptance/acceptance_no_urls.lineup.json"});
77+
Main.main(new String[]{"--working-dir", tempDirectory.toString(), "--config", "src/test/resources/acceptance/acceptance_no_urls.lineup.json"});
7678
}
7779

7880
@Test
7981
public void shouldExitWithExitStatus1IfThereIsAJSException() throws Exception {
8082
exit.expectSystemExitWithStatus(1);
8183
exit.checkAssertionAfterwards(() -> assertThat(combinedOutput(), containsString("doesnotexist")));
8284

83-
Main.main(new String[]{"--working-dir",tempDirectory.toString(),"--config","src/test/resources/acceptance/acceptance_wrong_js.lineup.json","--replace-in-url###CWD###="+CWD});
85+
Main.main(new String[]{"--working-dir", tempDirectory.toString(), "--config", "src/test/resources/acceptance/acceptance_wrong_js.lineup.json", "--replace-in-url###CWD###=" + CWD});
8486
}
8587

8688
@Test
8789
public void shouldExitWithExitStatus1IfThereIsAMalformedUrlInFirefox() throws Exception {
8890
exit.expectSystemExitWithStatus(1);
8991
exit.checkAssertionAfterwards(() -> assertThat(combinedOutput(), containsString("Reached error page")));
9092

91-
Main.main(new String[]{"--working-dir",tempDirectory.toString(),"--config","src/test/resources/acceptance/acceptance_wrong_url_firefox.lineup.json"});
93+
Main.main(new String[]{"--working-dir", tempDirectory.toString(), "--config", "src/test/resources/acceptance/acceptance_wrong_url_firefox.lineup.json"});
9294
}
9395

9496
@Test
9597
public void shouldExitWithExitStatus1IfThereIsAMalformedUrlInChrome() throws Exception {
9698
exit.expectSystemExitWithStatus(1);
9799
exit.checkAssertionAfterwards(() -> assertThat(combinedOutput(), CoreMatchers.anyOf(containsString("ERR_NAME_RESOLUTION_FAILED"), containsString("ERR_NAME_NOT_RESOLVED"))));
98100

99-
Main.main(new String[]{"--working-dir",tempDirectory.toString(),"--config","src/test/resources/acceptance/acceptance_wrong_url_chrome.lineup.json"});
101+
Main.main(new String[]{"--working-dir", tempDirectory.toString(), "--config", "src/test/resources/acceptance/acceptance_wrong_url_chrome.lineup.json"});
100102
}
101103

102104
@Test
103105
public void shouldExitWithExitStatus1IfThereIsAGlobalTimeout() throws Exception {
104106
exit.expectSystemExitWithStatus(1);
105107
exit.checkAssertionAfterwards(() -> assertThat(combinedOutput(), containsString("Timeout")));
106108

107-
Main.main(new String[]{"--working-dir",tempDirectory.toString(),"--config","src/test/resources/acceptance/acceptance_timeout.lineup.json"});
109+
Main.main(new String[]{"--working-dir", tempDirectory.toString(), "--config", "src/test/resources/acceptance/acceptance_timeout.lineup.json"});
108110
}
109111

110112
@Test
111113
public void shouldOpenTestPage() throws Exception {
112-
Main.main(new String[]{"--working-dir",tempDirectory.toString(),"--url","file://"+ CWD +"/src/test/resources/acceptance/webpage/test.html"});
114+
Main.main(new String[]{"--working-dir", tempDirectory.toString(), "--url", "file://" + CWD + "/src/test/resources/acceptance/webpage/test.html"});
113115
}
114116

115117

116-
117118
@Test
118119
public void shouldRunJLineupWithTestPageThatDoesntChange_WithChrome() throws Exception {
119-
Main.main(new String[]{"--working-dir",tempDirectory.toString(),"--config","src/test/resources/acceptance/acceptance_chrome.lineup.json","--replace-in-url###CWD###="+CWD, "--step","before"});
120-
Main.main(new String[]{"--working-dir",tempDirectory.toString(),"--config","src/test/resources/acceptance/acceptance_chrome.lineup.json","--replace-in-url###CWD###="+CWD ,"--step","after"});
120+
Main.main(new String[]{"--working-dir", tempDirectory.toString(), "--config", "src/test/resources/acceptance/acceptance_chrome.lineup.json", "--replace-in-url###CWD###=" + CWD, "--step", "before"});
121+
Main.main(new String[]{"--working-dir", tempDirectory.toString(), "--config", "src/test/resources/acceptance/acceptance_chrome.lineup.json", "--replace-in-url###CWD###=" + CWD, "--step", "after"});
121122

122123
final Path reportJson = Paths.get(tempDirectory.toString(), "report", "report.json");
123124
assertThat("Report JSON exists", Files.exists(reportJson));
@@ -134,14 +135,14 @@ public void shouldRunJLineupWithTestPageThatDoesntChange_WithChrome() throws Exc
134135

135136
@Test
136137
public void shouldPassCommandLineParametersToChrome() throws Exception {
137-
Main.main(new String[]{"--working-dir",tempDirectory.toString(),"--config","src/test/resources/acceptance/acceptance_chrome.lineup.json","--replace-in-url###CWD###="+CWD,"--step","before","--chrome-parameter","--user-agent=\"fakeuseragent\""});
138+
Main.main(new String[]{"--working-dir", tempDirectory.toString(), "--config", "src/test/resources/acceptance/acceptance_chrome.lineup.json", "--replace-in-url###CWD###=" + CWD, "--step", "before", "--chrome-parameter", "--user-agent=\"fakeuseragent\""});
138139
assertThat(systemOutCaptor.toString(), containsString("User agent: \"fakeuseragent\""));
139140
}
140141

141142
@Test
142143
public void shouldRenderLogoTheSame_WithChrome() throws Exception {
143-
Main.main(new String[]{"--working-dir",tempDirectory.toString(),"--config","src/test/resources/acceptance/acceptance_chrome_svg.lineup.json","--replace-in-url###CWD###="+CWD, "--step","before"});
144-
Main.main(new String[]{"--working-dir",tempDirectory.toString(),"--config","src/test/resources/acceptance/acceptance_chrome_svg.lineup.json","--replace-in-url###CWD###="+CWD, "--step","after"});
144+
Main.main(new String[]{"--working-dir", tempDirectory.toString(), "--config", "src/test/resources/acceptance/acceptance_chrome_svg.lineup.json", "--replace-in-url###CWD###=" + CWD, "--step", "before"});
145+
Main.main(new String[]{"--working-dir", tempDirectory.toString(), "--config", "src/test/resources/acceptance/acceptance_chrome_svg.lineup.json", "--replace-in-url###CWD###=" + CWD, "--step", "after"});
145146

146147
final Path reportJson = Paths.get(tempDirectory.toString(), "report", "report.json");
147148
assertThat("Report JSON exists", Files.exists(reportJson));
@@ -158,8 +159,8 @@ public void shouldRenderLogoTheSame_WithChrome() throws Exception {
158159

159160
@Test
160161
public void shouldRunJLineupWithTestPageThatDoesntChange_WithChromeHeadless() throws Exception {
161-
Main.main(new String[]{"--working-dir",tempDirectory.toString(),"--config","src/test/resources/acceptance/acceptance_chrome-headless.lineup.json","--replace-in-url###CWD###="+CWD," --step","before"});
162-
Main.main(new String[]{"--working-dir",tempDirectory.toString(),"--config","src/test/resources/acceptance/acceptance_chrome-headless.lineup.json","--replace-in-url###CWD###="+CWD ,"--step","after"});
162+
Main.main(new String[]{"--working-dir", tempDirectory.toString(), "--config", "src/test/resources/acceptance/acceptance_chrome-headless.lineup.json", "--replace-in-url###CWD###=" + CWD, " --step", "before"});
163+
Main.main(new String[]{"--working-dir", tempDirectory.toString(), "--config", "src/test/resources/acceptance/acceptance_chrome-headless.lineup.json", "--replace-in-url###CWD###=" + CWD, "--step", "after"});
163164

164165
final Path reportJson = Paths.get(tempDirectory.toString(), "report", "report.json");
165166
assertThat("Report JSON exists", Files.exists(reportJson));
@@ -176,8 +177,8 @@ public void shouldRunJLineupWithTestPageThatDoesntChange_WithChromeHeadless() th
176177

177178
@Test
178179
public void shouldRunJLineupWithTestPageThatDoesntChange_WithFirefox() throws Exception {
179-
Main.main(new String[]{"--working-dir",tempDirectory.toString(),"--config","src/test/resources/acceptance/acceptance_firefox.lineup.json","--replace-in-url###CWD###="+CWD," --step","before"});
180-
Main.main(new String[]{"--working-dir",tempDirectory.toString(),"--config","src/test/resources/acceptance/acceptance_firefox.lineup.json","--replace-in-url###CWD###="+CWD ,"--step","after"});
180+
Main.main(new String[]{"--working-dir", tempDirectory.toString(), "--config", "src/test/resources/acceptance/acceptance_firefox.lineup.json", "--replace-in-url###CWD###=" + CWD, " --step", "before"});
181+
Main.main(new String[]{"--working-dir", tempDirectory.toString(), "--config", "src/test/resources/acceptance/acceptance_firefox.lineup.json", "--replace-in-url###CWD###=" + CWD, "--step", "after"});
181182

182183
final Path reportJson = Paths.get(tempDirectory.toString(), "report", "report.json");
183184
assertThat("Report JSON exists", Files.exists(reportJson));
@@ -194,8 +195,8 @@ public void shouldRunJLineupWithTestPageThatDoesntChange_WithFirefox() throws Ex
194195

195196
@Test
196197
public void shouldRunJLineupWithTestPageThatDoesntChange_ReportFormat2() throws Exception {
197-
Main.main(new String[]{"--working-dir",tempDirectory.toString(),"--config","src/test/resources/acceptance/acceptance_reportv2.lineup.json","--replace-in-url###CWD###="+CWD," --step","before"});
198-
Main.main(new String[]{"--working-dir",tempDirectory.toString(),"--config","src/test/resources/acceptance/acceptance_reportv2.lineup.json","--replace-in-url###CWD###="+CWD ,"--step","after"});
198+
Main.main(new String[]{"--working-dir", tempDirectory.toString(), "--config", "src/test/resources/acceptance/acceptance_reportv2.lineup.json", "--replace-in-url###CWD###=" + CWD, " --step", "before"});
199+
Main.main(new String[]{"--working-dir", tempDirectory.toString(), "--config", "src/test/resources/acceptance/acceptance_reportv2.lineup.json", "--replace-in-url###CWD###=" + CWD, "--step", "after"});
199200

200201
final Path reportJson = Paths.get(tempDirectory.toString(), "report", "report.json");
201202
assertThat("Report JSON exists", Files.exists(reportJson));
@@ -212,8 +213,8 @@ public void shouldRunJLineupWithTestPageThatDoesntChange_ReportFormat2() throws
212213

213214
@Test
214215
public void shouldRunJLineupWithTestPageThatDoesntChange_LegacyReportFormat() throws Exception {
215-
Main.main(new String[]{"--working-dir",tempDirectory.toString(),"--config","src/test/resources/acceptance/acceptance_legacy.lineup.json","--replace-in-url###CWD###="+CWD, "--step","before"});
216-
Main.main(new String[]{"--working-dir",tempDirectory.toString(),"--config","src/test/resources/acceptance/acceptance_legacy.lineup.json","--replace-in-url###CWD###="+CWD ,"--step","after"});
216+
Main.main(new String[]{"--working-dir", tempDirectory.toString(), "--config", "src/test/resources/acceptance/acceptance_legacy.lineup.json", "--replace-in-url###CWD###=" + CWD, "--step", "before"});
217+
Main.main(new String[]{"--working-dir", tempDirectory.toString(), "--config", "src/test/resources/acceptance/acceptance_legacy.lineup.json", "--replace-in-url###CWD###=" + CWD, "--step", "after"});
217218

218219
final Path reportJson = Paths.get(tempDirectory.toString(), "report", "report.json");
219220
assertThat("Report JSON exists", Files.exists(reportJson));
@@ -230,8 +231,8 @@ public void shouldRunJLineupWithTestPageThatDoesntChange_LegacyReportFormat() th
230231

231232
@Test
232233
public void shouldRunJLineupWithTestPageThatDoesntChange_DefaultReportFormat() throws Exception {
233-
Main.main(new String[]{"--working-dir",tempDirectory.toString(),"--config","src/test/resources/acceptance/acceptance.lineup.json","--replace-in-url###CWD###="+CWD, "--step","before"});
234-
Main.main(new String[]{"--working-dir",tempDirectory.toString(),"--config","src/test/resources/acceptance/acceptance.lineup.json","--replace-in-url###CWD###="+CWD ,"--step","after"});
234+
Main.main(new String[]{"--working-dir", tempDirectory.toString(), "--config", "src/test/resources/acceptance/acceptance.lineup.json", "--replace-in-url###CWD###=" + CWD, "--step", "before"});
235+
Main.main(new String[]{"--working-dir", tempDirectory.toString(), "--config", "src/test/resources/acceptance/acceptance.lineup.json", "--replace-in-url###CWD###=" + CWD, "--step", "after"});
235236

236237
final Path reportJson = Paths.get(tempDirectory.toString(), "report", "report.json");
237238
assertThat("Report JSON exists", Files.exists(reportJson));
@@ -258,45 +259,33 @@ public void shouldRunJLineupWithTestPageThatHasAVeryLongPath() throws Exception
258259

259260
@Test
260261
public void shouldNotCrashPhantomjsFontsNotLoaded() throws Exception {
261-
Main.main(new String[]{"--working-dir",tempDirectory.toString(),"--config","src/test/resources/acceptance/acceptance_phantom_fonts.lineup.json","--replace-in-url###CWD###="+CWD, "--step","before"});
262-
Main.main(new String[]{"--working-dir",tempDirectory.toString(),"--config","src/test/resources/acceptance/acceptance_phantom_fonts.lineup.json","--replace-in-url###CWD###="+CWD ,"--step","after"});
262+
Main.main(new String[]{"--working-dir", tempDirectory.toString(), "--config", "src/test/resources/acceptance/acceptance_phantom_fonts.lineup.json", "--replace-in-url###CWD###=" + CWD, "--step", "before"});
263+
Main.main(new String[]{"--working-dir", tempDirectory.toString(), "--config", "src/test/resources/acceptance/acceptance_phantom_fonts.lineup.json", "--replace-in-url###CWD###=" + CWD, "--step", "after"});
263264
assertThat(systemOutCaptor.toString(), containsString("WARNING: 'wait-for-fonts-time' is ignored because PhantomJS doesn't support this feature."));
264265
}
265266

266267
@Test
267268
public void shouldPrintConfig() throws Exception {
268269
exit.checkAssertionAfterwards(() -> assertThat(systemOutCaptor.toString(), containsString("http://www.example.com")));
269270
exit.expectSystemExitWithStatus(0);
270-
Main.main(new String[]{"--working-dir",tempDirectory.toString(),"--print-config"});
271+
Main.main(new String[]{"--working-dir", tempDirectory.toString(), "--print-config"});
271272
}
272273

273274
private String getTextFileContentAsString(Path reportJson) throws IOException {
274275
final List<String> reportJsonLines = Files.readAllLines(reportJson);
275276
return reportJsonLines.stream().collect(Collectors.joining());
276277
}
277278

278-
private void deleteDir(Path path) throws IOException {
279-
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
280-
@Override
281-
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
282-
throws IOException
283-
{
284-
Files.delete(file);
285-
return FileVisitResult.CONTINUE;
286-
}
287-
@Override
288-
public FileVisitResult postVisitDirectory(Path dir, IOException e)
289-
throws IOException
290-
{
291-
if (e == null) {
292-
Files.delete(dir);
293-
return FileVisitResult.CONTINUE;
294-
} else {
295-
// directory iteration failed
296-
throw e;
297-
}
298-
}
299-
});
279+
private void deleteDir(Path path) throws Exception {
280+
Files
281+
.walk(path)
282+
.sorted(Comparator.reverseOrder())
283+
.map(Path::toFile)
284+
.forEach(file -> {
285+
if (!file.delete()) {
286+
throw new RuntimeException("Couldn't delete file " + file.getAbsolutePath());
287+
}
288+
});
300289
}
301290

302291
private String combinedOutput() {

0 commit comments

Comments
 (0)