Skip to content

Custom file mapping support #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,25 @@ db - Database to connect to.
Generates a JSON report providing detailed information on test execution.
Designed for [SonarQube](https://about.sonarqube.com/) to report test execution.

-o=output - Defines file name to save the output from the specified reporter.
-o=output - Defines file name to save the output from the specified reporter.
If defined, the output is not displayed on screen by default. This can be changed with the -s parameter.
If not defined, then output will be displayed on screen, even if the parameter -s is not specified.
If more than one -o parameter is specified for one -f parameter, the last one is taken into consideration.
-s - Forces putting output to to screen for a given -f parameter.
-s - Forces putting output to to screen for a given -f parameter.
-source_path=source - path to project source files, use the following options to enable custom type mappings:
-owner="app"
-regex_expression="pattern"
-type_mapping="matched_string=TYPE[/matched_string=TYPE]*"
-owner_subexpression=subexpression_number
-type_subexpression=subexpression_number
-name_subexpression=subexpression_number
-test_path=test - path to project test files, use the following options to enable custom type mappings:
-owner="app"
-regex_expression="pattern"
-type_mapping="matched_string=TYPE[/matched_string=TYPE]*"
-owner_subexpression=subexpression_number
-type_subexpression=subexpression_number
-name_subexpression=subexpression_number
-c - If specified, enables printing of test results in colors as defined by ANSICONSOLE standards.
Works only on reporeters that support colors (ut_documentation_reporter).
--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.
Expand Down
151 changes: 102 additions & 49 deletions src/main/java/io/github/utplsql/cli/RunCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import io.github.utplsql.api.CustomTypes;
import io.github.utplsql.api.OutputBuffer;
import io.github.utplsql.api.TestRunner;
import io.github.utplsql.api.*;
import io.github.utplsql.api.exception.SomeTestsFailedException;
import io.github.utplsql.api.reporter.Reporter;
import io.github.utplsql.api.reporter.ReporterFactory;
Expand Down Expand Up @@ -35,16 +33,15 @@ public class RunCommand {

@Parameter(
names = {"-p", "--path"},
description = "run suites/tests by path, format: \n" +
description = "run suites/tests by path, format: " +
"-p=[schema|schema:[suite ...][.test]|schema[.suite ...][.test]")
private List<String> testPaths = new ArrayList<>();

@Parameter(
names = {"-f", "--format"},
variableArity = true,
description = "output reporter format: \n" +
"enables specified format reporting to specified output file (-o) and to screen (-s)\n" +
"-f=reporter_name [-o=output_file [-s]]")
description = "-f=reporter_name [-o=output_file [-s]] - enables specified format reporting to specified " +
"output file (-o) and to screen (-s)")
private List<String> reporterParams = new ArrayList<>();

@Parameter(
Expand All @@ -57,11 +54,20 @@ public class RunCommand {
description = "override the exit code on failure, default = 1")
private int failureExitCode = 1;

@Parameter(names = {"-source_path"}, description = "path to project source files")
private String sourcePath;
@Parameter(
names = {"-source_path"},
variableArity = true,
description = "-source_path [-owner=\"owner\" -regex_expression=\"pattern\" " +
"-type_mapping=\"matched_string=TYPE/matched_string=TYPE\" " +
"-owner_subexpression=0 -type_subexpression=0 -name_subexpression=0] - path to project source files")
private List<String> sourcePathParams = new ArrayList<>();

@Parameter(names = {"-test_path"}, description = "path to project test files")
private String testPath;
@Parameter(
names = {"-test_path"},
variableArity = true,
description = "-test_path [-regex_expression=\"pattern\" -owner_subexpression=0 -type_subexpression=0 " +
"-name_subexpression=0] - path to project test files")
private List<String> testPathParams = new ArrayList<>();

public ConnectionInfo getConnectionInfo() {
return connectionInfoList.get(0);
Expand All @@ -71,33 +77,6 @@ public List<String> getTestPaths() {
return testPaths;
}

public List<ReporterOptions> getReporterOptionsList() {
List<ReporterOptions> reporterOptionsList = new ArrayList<>();
ReporterOptions reporterOptions = null;

for (String p : reporterParams) {
if (reporterOptions == null || !p.startsWith("-")) {
reporterOptions = new ReporterOptions(p);
reporterOptionsList.add(reporterOptions);
}
else
if (p.startsWith("-o=")) {
reporterOptions.setOutputFileName(p.substring(3));
}
else
if (p.equals("-s")) {
reporterOptions.forceOutputToScreen(true);
}
}

// If no reporter parameters were passed, use default reporter.
if (reporterOptionsList.isEmpty()) {
reporterOptionsList.add(new ReporterOptions(CustomTypes.UT_DOCUMENTATION_REPORTER));
}

return reporterOptionsList;
}

public int run() throws Exception {
final ConnectionInfo ci = getConnectionInfo();

Expand All @@ -106,18 +85,22 @@ public int run() throws Exception {
final List<Reporter> reporterList = new ArrayList<>();

final File baseDir = new File("").getAbsoluteFile();
List<String> sourceFilesTmp = null;
List<String> testFilesTmp = null;
final FileMapperOptions[] sourceMappingOptions = {null};
final FileMapperOptions[] testMappingOptions = {null};

if (this.sourcePath != null)
sourceFilesTmp = new FileWalker().getFileList(baseDir, this.sourcePath);
final int[] returnCode = {0};

if (this.testPath != null)
testFilesTmp = new FileWalker().getFileList(baseDir, this.testPath);
if (!this.sourcePathParams.isEmpty()) {
String sourcePath = this.sourcePathParams.get(0);
List<String> sourceFiles = new FileWalker().getFileList(baseDir, sourcePath);
sourceMappingOptions[0] = getMapperOptions(this.sourcePathParams, sourceFiles);
}

final List<String> sourceFiles = sourceFilesTmp;
final List<String> testFiles = testFilesTmp;
final int[] returnCode = {0};
if (!this.testPathParams.isEmpty()) {
String testPath = this.testPathParams.get(0);
List<String> testFiles = new FileWalker().getFileList(baseDir, testPath);
testMappingOptions[0] = getMapperOptions(this.testPathParams, testFiles);
}

if (testPaths.isEmpty()) testPaths.add(ci.getUser());

Expand All @@ -142,8 +125,8 @@ public int run() throws Exception {
new TestRunner()
.addPathList(testPaths)
.addReporterList(reporterList)
.withSourceFiles(sourceFiles)
.withTestFiles(testFiles)
.sourceMappingOptions(sourceMappingOptions[0])
.testMappingOptions(testMappingOptions[0])
.colorConsole(this.colorConsole)
.failOnErrors(true)
.run(conn);
Expand Down Expand Up @@ -187,4 +170,74 @@ public int run() throws Exception {
return returnCode[0];
}

public List<ReporterOptions> getReporterOptionsList() {
List<ReporterOptions> reporterOptionsList = new ArrayList<>();
ReporterOptions reporterOptions = null;

for (String p : reporterParams) {
if (reporterOptions == null || !p.startsWith("-")) {
reporterOptions = new ReporterOptions(p);
reporterOptionsList.add(reporterOptions);
}
else
if (p.startsWith("-o=")) {
reporterOptions.setOutputFileName(p.substring(3));
}
else
if (p.equals("-s")) {
reporterOptions.forceOutputToScreen(true);
}
}

// If no reporter parameters were passed, use default reporter.
if (reporterOptionsList.isEmpty()) {
reporterOptionsList.add(new ReporterOptions(CustomTypes.UT_DOCUMENTATION_REPORTER));
}

return reporterOptionsList;
}

public FileMapperOptions getMapperOptions(List<String> mappingParams, List<String> filePaths) {
FileMapperOptions mapperOptions = new FileMapperOptions(filePaths);

for (String p : mappingParams) {
if (p.startsWith("-object_owner=")) {
mapperOptions.setObjectOwner(p.substring("-object_owner=".length()));
}
else
if (p.startsWith("-regex_pattern=")) {
mapperOptions.setRegexPattern(p.substring("-regex_pattern=".length()));
}
else
if (p.startsWith("-type_mapping=")) {
String typeMappingsParam = p.substring("-type_mapping=".length());

List<KeyValuePair> typeMappings = new ArrayList<>();
for (String mapping : typeMappingsParam.split("/")) {
String[] values = mapping.split("=");
typeMappings.add(new KeyValuePair(values[0], values[1]));
}

mapperOptions.setTypeMappings(typeMappings);
}
else
if (p.startsWith("-owner_subexpression=")) {
mapperOptions.setOwnerSubExpression(Integer.parseInt(p.substring("-owner_subexpression=".length())));
}
else
if (p.startsWith("-name_subexpression=")) {
mapperOptions.setNameSubExpression(Integer.parseInt(p.substring("-name_subexpression=".length())));
}
else
if (p.startsWith("-type_subexpression=")) {
mapperOptions.setTypeSubExpression(Integer.parseInt(p.substring("-type_subexpression=".length())));
}
}

if (mapperOptions.getRegexPattern() == null || mapperOptions.getRegexPattern().isEmpty())
return null;
else
return mapperOptions;
}

}