diff --git a/README.md b/README.md index fd88eed..9c2a288 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,12 @@ For example CLI-3.0.4 is compatible with database framework 3.0.0-3.0.4 but not --failure-exit-code - Override the exit code on failure, defaults to 1. You can set it to 0 to always exit with a success status. -scc - If specified, skips the compatibility-check with the version of the database framework. If you skip compatibility-check, CLI will expect the most actual framework version +-include=package_list - Comma-separated object list to include in the coverage report. + Format: [schema.]package[,[schema.]package ...]. + See coverage reporting options in framework documentation. +-exclude=package_list - Comma-separated object list to exclude from the coverage report. + Format: [schema.]package[,[schema.]package ...]. + See coverage reporting options in framework documentation. ``` Parameters -f, -o, -s are correlated. That is parameters -o and -s are controlling outputs for reporter specified by the preceding -f parameter. diff --git a/pom.xml b/pom.xml index 2f5dd67..107d0aa 100644 --- a/pom.xml +++ b/pom.xml @@ -20,7 +20,7 @@ org.utplsql java-api - 3.0.4 + 3.0.4-SNAPSHOT compile diff --git a/src/main/java/org/utplsql/cli/RunCommand.java b/src/main/java/org/utplsql/cli/RunCommand.java index 690a453..4f41d02 100644 --- a/src/main/java/org/utplsql/cli/RunCommand.java +++ b/src/main/java/org/utplsql/cli/RunCommand.java @@ -18,6 +18,7 @@ import java.sql.Connection; import java.sql.SQLException; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -83,6 +84,21 @@ public class RunCommand { "most actual. Use this if you use CLI with a development version of utPLSQL-framework") private boolean skipCompatibilityCheck = false; + @Parameter( + names = {"-include"}, + description = "Comma-separated object list to include in the coverage report. " + + "Format: [schema.]package[,[schema.]package ...]. See coverage reporting options in framework documentation" + ) + private String includeObjects = null; + + @Parameter( + names = {"-exclude"}, + description = "Comma-separated object list to exclude from the coverage report. " + + "Format: [schema.]package[,[schema.]package ...]. See coverage reporting options in framework documentation" + ) + private String excludeObjects = null; + + private CompatibilityProxy compatibilityProxy; public ConnectionInfo getConnectionInfo() { @@ -112,6 +128,24 @@ public int run() throws Exception { sourceMappingOptions[0] = getFileMapperOptionsByParamListItem(this.sourcePathParams, baseDir); testMappingOptions[0] = getFileMapperOptionsByParamListItem(this.testPathParams, baseDir); + ArrayList includeObjectsList; + ArrayList excludeObjectsList; + + if (includeObjects != null && !includeObjects.isEmpty()) { + includeObjectsList = new ArrayList<>(Arrays.asList(includeObjects.split(","))); + } else { + includeObjectsList = new ArrayList<>(); + } + + if (excludeObjects != null && !excludeObjects.isEmpty()) { + excludeObjectsList = new ArrayList<>(Arrays.asList(excludeObjects.split(","))); + } else { + excludeObjectsList = new ArrayList<>(); + } + + final ArrayList finalIncludeObjectsList = includeObjectsList; + final ArrayList finalExcludeObjectsList = excludeObjectsList; + // Do the reporters initialization, so we can use the id to run and gather results. try (Connection conn = ci.getConnection()) { @@ -143,7 +177,7 @@ public int run() throws Exception { // Run tests. executorService.submit(() -> { try (Connection conn = ci.getConnection()) { - new TestRunner() + TestRunner testRunner = new TestRunner() .addPathList(testPaths) .addReporterList(reporterList) .sourceMappingOptions(sourceMappingOptions[0]) @@ -151,7 +185,10 @@ public int run() throws Exception { .colorConsole(this.colorConsole) .failOnErrors(true) .skipCompatibilityCheck(skipCompatibilityCheck) - .run(conn); + .includeObjects(finalIncludeObjectsList) + .excludeObjects(finalExcludeObjectsList); + + testRunner.run(conn); } catch (SomeTestsFailedException e) { returnCode[0] = this.failureExitCode; } catch (SQLException e) { diff --git a/src/test/java/org/utplsql/cli/RunCommandCoverageReporterSystemTest.java b/src/test/java/org/utplsql/cli/RunCommandCoverageReporterSystemTest.java new file mode 100644 index 0000000..4ad03ca --- /dev/null +++ b/src/test/java/org/utplsql/cli/RunCommandCoverageReporterSystemTest.java @@ -0,0 +1,91 @@ +package org.utplsql.cli; + +import com.sun.org.apache.xerces.internal.impl.xpath.regex.Match; +import org.junit.Assert; +import org.junit.Test; +import org.utplsql.api.compatibility.OptionalFeatures; + +import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Scanner; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * System tests for Code Coverage Reporter + * + * @author pesse + */ +public class RunCommandCoverageReporterSystemTest { + + private static final Pattern REGEX_COVERAGE_TITLE = Pattern.compile("([a-zA-Z0-9\\._]+)<\\/a>"); + + private String getTempCoverageFileName(int counter) { + + return "tmpCoverage_" + String.valueOf(System.currentTimeMillis()) + "_" + String.valueOf(counter) + ".html"; + } + + /** + * Returns a random filename which does not yet exist on the local path + * + * @return + */ + private Path getTempCoverageFilePath() { + int i = 1; + Path p = Paths.get(getTempCoverageFileName(i)); + + while (Files.exists(p) && i < 100) + p = Paths.get(getTempCoverageFileName(i++)); + + if (i >= 100) + throw new IllegalStateException("Could not get temporary file for coverage output"); + + return p; + } + + /** Checks Coverage HTML Output if a given packageName is listed + * + * @param content + * @param packageName + * @return + */ + private boolean hasCoverageListed( String content, String packageName) { + Matcher m = REGEX_COVERAGE_TITLE.matcher(content); + + while ( m.find() ) { + if ( packageName.equals(m.group(1)) ) + return true; + } + + return false; + } + + @Test + public void run_CodeCoverageWithIncludeAndExclude() { + + try { + Path coveragePath = getTempCoverageFilePath(); + + RunCommand runCmd = RunCommandTestHelper.createRunCommand(RunCommandTestHelper.getConnectionString(), + "-f=ut_coverage_html_reporter", "-o=" + coveragePath, "-s", "-exclude=app.award_bonus,app.betwnstr"); + + try { + int result = runCmd.run(); + + String content = new Scanner(coveragePath).useDelimiter("\\Z").next(); + + Assert.assertEquals(true, hasCoverageListed(content, "app.remove_rooms_by_name")); + Assert.assertEquals(false, hasCoverageListed(content, "app.award_bonus")); + Assert.assertEquals(false, hasCoverageListed(content, "app.betwnstr")); + + } finally { + Files.delete(coveragePath); + } + } catch (Exception e) { + Assert.fail(e.getMessage()); + } + + } +} diff --git a/src/test/java/org/utplsql/cli/RunCommandSystemTest.java b/src/test/java/org/utplsql/cli/RunCommandSystemTest.java new file mode 100644 index 0000000..19e5488 --- /dev/null +++ b/src/test/java/org/utplsql/cli/RunCommandSystemTest.java @@ -0,0 +1,40 @@ +package org.utplsql.cli; + +import com.beust.jcommander.JCommander; +import org.junit.Assert; +import org.junit.Test; +import org.utplsql.api.CustomTypes; +import org.utplsql.api.compatibility.OptionalFeatures; + +import java.util.List; + +/** + * System tests for run command. + */ +public class RunCommandSystemTest { + + + @Test + public void run_Default() { + RunCommand runCmd = RunCommandTestHelper.createRunCommand(RunCommandTestHelper.getConnectionString(), + "-f=ut_documentation_reporter", + "-c", + "--failure-exit-code=2"); + + try { + int result = runCmd.run(); + + // Only expect failure-exit-code to work on several framework versions + if (OptionalFeatures.FAIL_ON_ERROR.isAvailableFor(runCmd.getDatabaseVersion()) ) + Assert.assertEquals(2, result); + else + Assert.assertEquals(0, result); + } + catch ( Exception e ) { + Assert.fail(e.getMessage()); + } + } + + + +} diff --git a/src/test/java/org/utplsql/cli/RunCommandTest.java b/src/test/java/org/utplsql/cli/RunCommandTest.java index ff72b13..f8248f3 100644 --- a/src/test/java/org/utplsql/cli/RunCommandTest.java +++ b/src/test/java/org/utplsql/cli/RunCommandTest.java @@ -13,34 +13,9 @@ */ public class RunCommandTest { - private static String sUrl; - private static String sUser; - private static String sPass; - - static { - sUrl = System.getenv("DB_URL") != null ? System.getenv("DB_URL") : "192.168.99.100:1521:XE"; - sUser = System.getenv("DB_USER") != null ? System.getenv("DB_USER") : "app"; - sPass = System.getenv("DB_PASS") != null ? System.getenv("DB_PASS") : "app"; - } - - private RunCommand createRunCommand(String... args) { - RunCommand runCmd = new RunCommand(); - - JCommander.newBuilder() - .addObject(runCmd) - .args(args) - .build(); - - return runCmd; - } - - private String getConnectionString() { - return sUser + "/" + sPass + "@" + sUrl; - } - @Test public void reporterOptions_Default() { - RunCommand runCmd = createRunCommand(getConnectionString()); + RunCommand runCmd = RunCommandTestHelper.createRunCommand(RunCommandTestHelper.getConnectionString()); List reporterOptionsList = runCmd.getReporterOptionsList(); @@ -53,7 +28,7 @@ public void reporterOptions_Default() { @Test public void reporterOptions_OneReporter() { - RunCommand runCmd = createRunCommand(getConnectionString(), "-f=ut_documentation_reporter", "-o=output.txt"); + RunCommand runCmd = RunCommandTestHelper.createRunCommand(RunCommandTestHelper.getConnectionString(), "-f=ut_documentation_reporter", "-o=output.txt"); List reporterOptionsList = runCmd.getReporterOptionsList(); @@ -66,7 +41,7 @@ public void reporterOptions_OneReporter() { @Test public void reporterOptions_OneReporterForceScreen() { - RunCommand runCmd = createRunCommand(getConnectionString(), "-f=ut_documentation_reporter", "-o=output.txt", "-s"); + RunCommand runCmd = RunCommandTestHelper.createRunCommand(RunCommandTestHelper.getConnectionString(), "-f=ut_documentation_reporter", "-o=output.txt", "-s"); List reporterOptionsList = runCmd.getReporterOptionsList(); @@ -79,7 +54,7 @@ public void reporterOptions_OneReporterForceScreen() { @Test public void reporterOptions_OneReporterForceScreenInverse() { - RunCommand runCmd = createRunCommand(getConnectionString(), "-f=ut_documentation_reporter", "-s", "-o=output.txt"); + RunCommand runCmd = RunCommandTestHelper.createRunCommand(RunCommandTestHelper.getConnectionString(), "-f=ut_documentation_reporter", "-s", "-o=output.txt"); List reporterOptionsList = runCmd.getReporterOptionsList(); @@ -92,7 +67,7 @@ public void reporterOptions_OneReporterForceScreenInverse() { @Test public void reporterOptions_TwoReporters() { - RunCommand runCmd = createRunCommand(getConnectionString(), + RunCommand runCmd = RunCommandTestHelper.createRunCommand(RunCommandTestHelper.getConnectionString(), "-f=ut_documentation_reporter", "-f=ut_coverage_html_reporter", "-o=coverage.html", "-s"); @@ -111,25 +86,4 @@ public void reporterOptions_TwoReporters() { Assert.assertTrue(reporterOptions2.outputToScreen()); } - @Test - public void run_Default() { - RunCommand runCmd = createRunCommand(getConnectionString(), - "-f=ut_documentation_reporter", - "-c", - "--failure-exit-code=2"); - - try { - int result = runCmd.run(); - - // Only expect failure-exit-code to work on several framework versions - if (OptionalFeatures.FAIL_ON_ERROR.isAvailableFor(runCmd.getDatabaseVersion()) ) - Assert.assertEquals(2, result); - else - Assert.assertEquals(0, result); - } - catch ( Exception e ) { - Assert.fail(e.getMessage()); - } - } - } diff --git a/src/test/java/org/utplsql/cli/RunCommandTestHelper.java b/src/test/java/org/utplsql/cli/RunCommandTestHelper.java new file mode 100644 index 0000000..9f9cf4f --- /dev/null +++ b/src/test/java/org/utplsql/cli/RunCommandTestHelper.java @@ -0,0 +1,30 @@ +package org.utplsql.cli; + +import com.beust.jcommander.JCommander; + +class RunCommandTestHelper { + private static String sUrl; + private static String sUser; + private static String sPass; + + static { + sUrl = System.getenv("DB_URL") != null ? System.getenv("DB_URL") : "192.168.99.100:1521:XE"; + sUser = System.getenv("DB_USER") != null ? System.getenv("DB_USER") : "app"; + sPass = System.getenv("DB_PASS") != null ? System.getenv("DB_PASS") : "app"; + } + + static RunCommand createRunCommand(String... args) { + RunCommand runCmd = new RunCommand(); + + JCommander.newBuilder() + .addObject(runCmd) + .args(args) + .build(); + + return runCmd; + } + + static String getConnectionString() { + return sUser + "/" + sPass + "@" + sUrl; + } +}