Skip to content

Commit

Permalink
Merge pull request #58 from utPLSQL/feature/include-exclude-params
Browse files Browse the repository at this point in the history
Added include and exclude params for coverage reports (dup for issue #6)
  • Loading branch information
pesse authored Jan 2, 2018
2 parents 0ed3a31 + b7f99e2 commit 2cb8ec9
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 54 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<dependency>
<groupId>org.utplsql</groupId>
<artifactId>java-api</artifactId>
<version>3.0.4</version>
<version>3.0.4-SNAPSHOT</version>
<scope>compile</scope>
<exclusions>
<exclusion>
Expand Down
41 changes: 39 additions & 2 deletions src/main/java/org/utplsql/cli/RunCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -112,6 +128,24 @@ public int run() throws Exception {
sourceMappingOptions[0] = getFileMapperOptionsByParamListItem(this.sourcePathParams, baseDir);
testMappingOptions[0] = getFileMapperOptionsByParamListItem(this.testPathParams, baseDir);

ArrayList<String> includeObjectsList;
ArrayList<String> 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<String> finalIncludeObjectsList = includeObjectsList;
final ArrayList<String> finalExcludeObjectsList = excludeObjectsList;

// Do the reporters initialization, so we can use the id to run and gather results.
try (Connection conn = ci.getConnection()) {

Expand Down Expand Up @@ -143,15 +177,18 @@ 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])
.testMappingOptions(testMappingOptions[0])
.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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 href=\"[a-zA-Z0-9#]+\" class=\"src_link\" title=\"[a-zA-Z\\._]+\">([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());
}

}
}
40 changes: 40 additions & 0 deletions src/test/java/org/utplsql/cli/RunCommandSystemTest.java
Original file line number Diff line number Diff line change
@@ -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());
}
}



}
56 changes: 5 additions & 51 deletions src/test/java/org/utplsql/cli/RunCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<ReporterOptions> reporterOptionsList = runCmd.getReporterOptionsList();

Expand All @@ -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<ReporterOptions> reporterOptionsList = runCmd.getReporterOptionsList();

Expand All @@ -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<ReporterOptions> reporterOptionsList = runCmd.getReporterOptionsList();

Expand All @@ -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<ReporterOptions> reporterOptionsList = runCmd.getReporterOptionsList();

Expand All @@ -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");

Expand All @@ -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());
}
}

}
30 changes: 30 additions & 0 deletions src/test/java/org/utplsql/cli/RunCommandTestHelper.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 2cb8ec9

Please sign in to comment.