Skip to content

Commit 1b4282c

Browse files
authored
Merge pull request #95 from utPLSQL/bugfix/coverage_html_assets_subfolder
CoverageHTML Reporter will now reference its assets correctly when output is written to a subfolder.
2 parents d639129 + 7df3131 commit 1b4282c

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

src/main/java/org/utplsql/cli/reporters/LocalAssetsCoverageHTMLReporter.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.utplsql.api.reporter.ReporterFactory;
77
import org.utplsql.cli.ReporterOptions;
88

9+
import java.nio.file.Path;
910
import java.nio.file.Paths;
1011
import java.sql.Connection;
1112
import java.sql.SQLException;
@@ -27,15 +28,34 @@ public LocalAssetsCoverageHTMLReporter(String selfType, Object[] attributes) {
2728
public Reporter init(Connection con, CompatibilityProxy compatibilityProxy, ReporterFactory reporterFactory) throws SQLException {
2829
super.init(con, compatibilityProxy, reporterFactory);
2930

30-
if ( options != null && options.outputToFile() )
31-
writeReportAssetsTo(Paths.get(getAssetsPath()));
31+
if ( hasOutputToFile() ) {
32+
writeReportAssetsTo(getPhysicalAssetPath());
33+
}
3234

3335
return this;
3436
}
3537

38+
private String getNameOfOutputFile() {
39+
Path outputPath = Paths.get(options.getOutputFileName());
40+
return outputPath.getName(outputPath.getNameCount()-1).toString();
41+
}
42+
43+
private Path getPhysicalAssetPath() {
44+
Path outputPath = Paths.get(options.getOutputFileName());
45+
if ( outputPath.getNameCount() > 1 )
46+
return outputPath.getParent().resolve(getAssetsPath());
47+
else
48+
return Paths.get(getAssetsPath());
49+
}
50+
3651
private void setAssetsPathFromOptions() {
37-
if ( options != null && options.outputToFile() )
38-
setAssetsPath(options.getOutputFileName()+"_assets/");
52+
if ( hasOutputToFile() ) {
53+
setAssetsPath(getNameOfOutputFile() + "_assets/");
54+
}
55+
}
56+
57+
private boolean hasOutputToFile() {
58+
return (options != null && options.outputToFile());
3959
}
4060

4161
@Override

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,26 @@ public void coverageReporterWriteAssetsToOutput() throws Exception {
109109
assertTrue(content.contains("<title>Code coverage</title>"));
110110
}
111111

112+
@Test
113+
public void coverageReporterWriteAssetsToSubfolder() throws Exception {
114+
115+
Path origCoveratePath = getTempCoverageFilePath();
116+
Path coveragePath = Paths.get(origCoveratePath.toString(), origCoveratePath.toString());
117+
Path coverageAssetsPath = Paths.get(coveragePath.toString() + "_assets");
118+
119+
TestHelper.runApp("run", TestHelper.getConnectionString(),
120+
"-f=ut_coverage_html_reporter", "-o=" + coveragePath, "-s");
121+
122+
123+
// Check application file exists
124+
File applicationJs = coverageAssetsPath.resolve(Paths.get("application.js")).toFile();
125+
assertTrue(applicationJs.exists());
126+
127+
// Check correct script-part in HTML source exists
128+
String content = new String(Files.readAllBytes(coveragePath));
129+
assertTrue(content.contains("<script src='" + origCoveratePath.toString() + "_assets" + "/application.js'"));
130+
131+
// Check correct title exists
132+
assertTrue(content.contains("<title>Code coverage</title>"));
133+
}
112134
}

0 commit comments

Comments
 (0)