Skip to content

Commit 88c0688

Browse files
author
Samuel Nitsche
committed
Fix and refactor CoverageHTMLTests
1 parent 8f4a7e1 commit 88c0688

File tree

1 file changed

+47
-20
lines changed

1 file changed

+47
-20
lines changed

src/test/java/org/utplsql/cli/RunCommandCoverageReporterIT.java

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package org.utplsql.cli;
22

3+
import org.junit.jupiter.api.AfterEach;
4+
import org.junit.jupiter.api.BeforeEach;
35
import org.junit.jupiter.api.Test;
46

57
import java.io.File;
68
import java.nio.file.Files;
79
import java.nio.file.Path;
810
import java.nio.file.Paths;
9-
import java.util.List;
11+
import java.util.HashSet;
1012
import java.util.Scanner;
13+
import java.util.Set;
1114
import java.util.regex.Matcher;
1215
import java.util.regex.Pattern;
1316

@@ -23,6 +26,12 @@ public class RunCommandCoverageReporterIT {
2326

2427
private static final Pattern REGEX_COVERAGE_TITLE = Pattern.compile("<a href=\"[a-zA-Z0-9#]+\" class=\"src_link\" title=\"[a-zA-Z\\._]+\">([a-zA-Z0-9\\._]+)<\\/a>");
2528

29+
private Set<Path> tempPaths;
30+
31+
private void addTempPath(Path path) {
32+
tempPaths.add(path);
33+
}
34+
2635
private String getTempCoverageFileName(int counter) {
2736

2837
return "tmpCoverage_" + String.valueOf(System.currentTimeMillis()) + "_" + String.valueOf(counter) + ".html";
@@ -34,15 +43,19 @@ private String getTempCoverageFileName(int counter) {
3443
* @return
3544
*/
3645
private Path getTempCoverageFilePath() {
46+
3747
int i = 1;
3848
Path p = Paths.get(getTempCoverageFileName(i));
3949

40-
while (Files.exists(p) && i < 100)
50+
while ((Files.exists(p) || tempPaths.contains(p)) && i < 100)
4151
p = Paths.get(getTempCoverageFileName(i++));
4252

4353
if (i >= 100)
4454
throw new IllegalStateException("Could not get temporary file for coverage output");
4555

56+
addTempPath(p);
57+
addTempPath(Paths.get(p.toString()+"_assets"));
58+
4659
return p;
4760
}
4861

@@ -64,6 +77,11 @@ private boolean hasCoverageListed(String content, String packageName) {
6477
return false;
6578
}
6679

80+
@BeforeEach
81+
public void setupTest() {
82+
tempPaths = new HashSet<>();
83+
}
84+
6785
@Test
6886
public void run_CodeCoverageWithIncludeAndExclude() throws Exception {
6987

@@ -72,39 +90,48 @@ public void run_CodeCoverageWithIncludeAndExclude() throws Exception {
7290
RunCommand runCmd = RunCommandTestHelper.createRunCommand(RunCommandTestHelper.getConnectionString(),
7391
"-f=ut_coverage_html_reporter", "-o=" + coveragePath, "-s", "-exclude=app.award_bonus,app.betwnstr");
7492

75-
try {
76-
int result = runCmd.run();
7793

78-
String content = new Scanner(coveragePath).useDelimiter("\\Z").next();
94+
int result = runCmd.run();
7995

80-
assertEquals(true, hasCoverageListed(content, "app.remove_rooms_by_name"));
81-
assertEquals(false, hasCoverageListed(content, "app.award_bonus"));
82-
assertEquals(false, hasCoverageListed(content, "app.betwnstr"));
96+
String content = new Scanner(coveragePath).useDelimiter("\\Z").next();
8397

84-
} finally {
85-
Files.delete(coveragePath);
86-
}
98+
assertEquals(true, hasCoverageListed(content, "app.remove_rooms_by_name"));
99+
assertEquals(false, hasCoverageListed(content, "app.award_bonus"));
100+
assertEquals(false, hasCoverageListed(content, "app.betwnstr"));
87101

88102
}
89103

90104
@Test
91105
public void coverageReporterWriteAssetsToOutput() throws Exception {
92106
Path coveragePath = getTempCoverageFilePath();
107+
Path coverageAssetsPath = Paths.get(coveragePath.toString() + "_assets");
93108

94109
RunCommand runCmd = RunCommandTestHelper.createRunCommand(RunCommandTestHelper.getConnectionString(),
95110
"-f=ut_coverage_html_reporter", "-o=" + coveragePath, "-s");
96-
try {
97-
int result = runCmd.run();
98111

99-
List<ReporterOptions> reporterOptions = runCmd.getReporterOptionsList();
100-
File applicationJs = coveragePath.resolve(Paths.get("assets", "application.js")).toFile();
112+
runCmd.run();
101113

102-
assertTrue(applicationJs.exists());
114+
File applicationJs = coverageAssetsPath.resolve(Paths.get("application.js")).toFile();
103115

104-
} finally {
105-
if ( Files.exists(coveragePath))
106-
Files.delete(coveragePath);
107-
}
116+
assertTrue(applicationJs.exists());
117+
118+
119+
}
108120

121+
@AfterEach
122+
public void deleteTempFiles() {
123+
tempPaths.forEach(p -> deleteDir(p.toFile()));
124+
}
125+
126+
void deleteDir(File file) {
127+
if (file.exists()) {
128+
File[] contents = file.listFiles();
129+
if (contents != null) {
130+
for (File f : contents) {
131+
deleteDir(f);
132+
}
133+
}
134+
file.delete();
135+
}
109136
}
110137
}

0 commit comments

Comments
 (0)