From 7df3131e2871470b9b6c5669e7301cd2dabfd8ca Mon Sep 17 00:00:00 2001 From: pesse Date: Tue, 24 Jul 2018 22:40:50 +0200 Subject: [PATCH] CoverageHTML Reporter will now reference its assets correctly when output is written to a subfolder. Fixes #94 --- .../LocalAssetsCoverageHTMLReporter.java | 28 ++++++++++++++++--- .../cli/RunCommandCoverageReporterIT.java | 22 +++++++++++++++ 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/utplsql/cli/reporters/LocalAssetsCoverageHTMLReporter.java b/src/main/java/org/utplsql/cli/reporters/LocalAssetsCoverageHTMLReporter.java index a61d41a..330d2cc 100644 --- a/src/main/java/org/utplsql/cli/reporters/LocalAssetsCoverageHTMLReporter.java +++ b/src/main/java/org/utplsql/cli/reporters/LocalAssetsCoverageHTMLReporter.java @@ -6,6 +6,7 @@ import org.utplsql.api.reporter.ReporterFactory; import org.utplsql.cli.ReporterOptions; +import java.nio.file.Path; import java.nio.file.Paths; import java.sql.Connection; import java.sql.SQLException; @@ -27,15 +28,34 @@ public LocalAssetsCoverageHTMLReporter(String selfType, Object[] attributes) { public Reporter init(Connection con, CompatibilityProxy compatibilityProxy, ReporterFactory reporterFactory) throws SQLException { super.init(con, compatibilityProxy, reporterFactory); - if ( options != null && options.outputToFile() ) - writeReportAssetsTo(Paths.get(getAssetsPath())); + if ( hasOutputToFile() ) { + writeReportAssetsTo(getPhysicalAssetPath()); + } return this; } + private String getNameOfOutputFile() { + Path outputPath = Paths.get(options.getOutputFileName()); + return outputPath.getName(outputPath.getNameCount()-1).toString(); + } + + private Path getPhysicalAssetPath() { + Path outputPath = Paths.get(options.getOutputFileName()); + if ( outputPath.getNameCount() > 1 ) + return outputPath.getParent().resolve(getAssetsPath()); + else + return Paths.get(getAssetsPath()); + } + private void setAssetsPathFromOptions() { - if ( options != null && options.outputToFile() ) - setAssetsPath(options.getOutputFileName()+"_assets/"); + if ( hasOutputToFile() ) { + setAssetsPath(getNameOfOutputFile() + "_assets/"); + } + } + + private boolean hasOutputToFile() { + return (options != null && options.outputToFile()); } @Override diff --git a/src/test/java/org/utplsql/cli/RunCommandCoverageReporterIT.java b/src/test/java/org/utplsql/cli/RunCommandCoverageReporterIT.java index 1b0fa36..a8cd82d 100644 --- a/src/test/java/org/utplsql/cli/RunCommandCoverageReporterIT.java +++ b/src/test/java/org/utplsql/cli/RunCommandCoverageReporterIT.java @@ -109,4 +109,26 @@ public void coverageReporterWriteAssetsToOutput() throws Exception { assertTrue(content.contains("Codestin Search App")); } + @Test + public void coverageReporterWriteAssetsToSubfolder() throws Exception { + + Path origCoveratePath = getTempCoverageFilePath(); + Path coveragePath = Paths.get(origCoveratePath.toString(), origCoveratePath.toString()); + Path coverageAssetsPath = Paths.get(coveragePath.toString() + "_assets"); + + TestHelper.runApp("run", TestHelper.getConnectionString(), + "-f=ut_coverage_html_reporter", "-o=" + coveragePath, "-s"); + + + // Check application file exists + File applicationJs = coverageAssetsPath.resolve(Paths.get("application.js")).toFile(); + assertTrue(applicationJs.exists()); + + // Check correct script-part in HTML source exists + String content = new String(Files.readAllBytes(coveragePath)); + assertTrue(content.contains("