Skip to content

Commit 1b93beb

Browse files
authored
Merge pull request #26 from viniciusam/bugfix/return_code
Exit with error code on failure
2 parents 5361b15 + 14d39c0 commit 1b93beb

File tree

3 files changed

+76
-59
lines changed

3 files changed

+76
-59
lines changed

README.md

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,67 +6,68 @@ Provides an easy way of invoking utPLSQL from command-line. Main features:
66
* Ability to run tests with multiple reporters simultaneously.
77
* Ability to save output from every individual reporter to a separate output file.
88
* Allows execution of selected suites, subset of suite.
9-
* ~~Maps project and test files to database objects for reporting purposes.~~ (Comming Soon)
9+
* Maps project and test files to database objects for reporting purposes. (Comming Soon)
1010

1111
## Downloading
1212
You can download development versions on [Bintray](https://bintray.com/viniciusam/utPLSQL-cli/utPLSQL-cli-develop#files).
1313

1414

1515
## Requirements
1616
* [Java SE Runtime Environment 8](http://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html)
17-
* ~~When using reporters for Sonar or Coveralls client needs to be invoked from project's root directory.~~
17+
* When using reporters for Sonar or Coveralls client needs to be invoked from project's root directory.
1818

1919
## Usage
2020
utplsql run user/pass@[[host][:port]/]db [-p=(ut_path|ut_paths)] [-f=format [-o=output_file] [-s] ...]
2121

2222
```
23-
user - Username to connect as.
24-
password - Password of the user.
25-
host - Server address, defaults to 127.0.0.1.
26-
port - Server port, defaults to 1521.
27-
db - Database to connect to.
28-
-p=suite_path(s) - A suite path or a comma separated list of suite paths for unit test to be executed.
29-
The path(s) can be in one of the following formats:
30-
schema[.package[.procedure]]
31-
schema:suite[.suite[.suite][...]][.procedure]
32-
Both formats can be mixed in the list.
33-
If only schema is provided, then all suites owner by that schema are executed.
34-
If -p is omitted, the current schema is used.
35-
-f=format - A reporter to be used for reporting.
36-
If no -f option is provided, the default ut_documentation_reporter is used.
37-
Available options:
38-
-f=ut_documentation_reporter
39-
A textual pretty-print of unit test results (usually use for console output)
40-
-f=ut_teamcity_reporter
41-
For reporting live progress of test execution with Teamcity CI.
42-
-f=ut_xunit_reporter
43-
Used for reporting test results with CI servers like Jenkins/Hudson/Teamcity.
44-
-f=ut_coverage_html_reporter
45-
Generates a HTML coverage report with summary and line by line information on code coverage.
46-
Based on open-source simplecov-html coverage reporter for Ruby.
47-
Includes source code in the report.
48-
-f=ut_coveralls_reporter
49-
Generates a JSON coverage report providing information on code coverage with line numbers.
50-
Designed for [Coveralls](https://coveralls.io/).
51-
-f=ut_coverage_sonar_reporter
52-
Generates a JSON coverage report providing information on code coverage with line numbers.
53-
Designed for [SonarQube](https://about.sonarqube.com/) to report coverage.
54-
-f=ut_sonar_test_reporter
55-
Generates a JSON report providing detailed information on test execution.
56-
Designed for [SonarQube](https://about.sonarqube.com/) to report test execution.
57-
58-
-o=output - Defines file name to save the output from the specified reporter.
59-
If defined, the output is not displayed on screen by default. This can be changed with the -s parameter.
60-
If not defined, then output will be displayed on screen, even if the parameter -s is not specified.
61-
If more than one -o parameter is specified for one -f parameter, the last one is taken into consideration.
62-
-s - Forces putting output to to screen for a given -f parameter.
63-
-c - If specified, enables printing of test results in colors as defined by ANSICONSOLE standards.
64-
Works only on reporeters that support colors (ut_documentation_reporter).
23+
user - Username to connect as.
24+
password - Password of the user.
25+
host - Server address, defaults to 127.0.0.1.
26+
port - Server port, defaults to 1521.
27+
db - Database to connect to.
28+
-p=suite_path(s) - A suite path or a comma separated list of suite paths for unit test to be executed.
29+
The path(s) can be in one of the following formats:
30+
schema[.package[.procedure]]
31+
schema:suite[.suite[.suite][...]][.procedure]
32+
Both formats can be mixed in the list.
33+
If only schema is provided, then all suites owner by that schema are executed.
34+
If -p is omitted, the current schema is used.
35+
-f=format - A reporter to be used for reporting.
36+
If no -f option is provided, the default ut_documentation_reporter is used.
37+
Available options:
38+
-f=ut_documentation_reporter
39+
A textual pretty-print of unit test results (usually use for console output)
40+
-f=ut_teamcity_reporter
41+
For reporting live progress of test execution with Teamcity CI.
42+
-f=ut_xunit_reporter
43+
Used for reporting test results with CI servers like Jenkins/Hudson/Teamcity.
44+
-f=ut_coverage_html_reporter
45+
Generates a HTML coverage report with summary and line by line information on code coverage.
46+
Based on open-source simplecov-html coverage reporter for Ruby.
47+
Includes source code in the report.
48+
-f=ut_coveralls_reporter
49+
Generates a JSON coverage report providing information on code coverage with line numbers.
50+
Designed for [Coveralls](https://coveralls.io/).
51+
-f=ut_coverage_sonar_reporter
52+
Generates a JSON coverage report providing information on code coverage with line numbers.
53+
Designed for [SonarQube](https://about.sonarqube.com/) to report coverage.
54+
-f=ut_sonar_test_reporter
55+
Generates a JSON report providing detailed information on test execution.
56+
Designed for [SonarQube](https://about.sonarqube.com/) to report test execution.
57+
58+
-o=output - Defines file name to save the output from the specified reporter.
59+
If defined, the output is not displayed on screen by default. This can be changed with the -s parameter.
60+
If not defined, then output will be displayed on screen, even if the parameter -s is not specified.
61+
If more than one -o parameter is specified for one -f parameter, the last one is taken into consideration.
62+
-s - Forces putting output to to screen for a given -f parameter.
63+
-c - If specified, enables printing of test results in colors as defined by ANSICONSOLE standards.
64+
Works only on reporeters that support colors (ut_documentation_reporter).
65+
--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.
6566
```
6667

6768
Parameters -f, -o, -s are correlated. That is parameters -o and -s are controlling outputs for reporter specified by the preceding -f parameter.
6869

69-
~~Sonar and Coveralls reporter will only provide valid reports, when source_path and/or test_path are provided, and ut_run is executed from your project's root path.~~
70+
Sonar and Coveralls reporter will only provide valid reports, when source_path and/or test_path are provided, and ut_run is executed from your project's root path.
7071

7172
Examples:
7273

@@ -77,7 +78,7 @@ utplsql run hr/hr@xe -p=hr_test -f=ut_documentation_reporter -o=run.log -s -f=ut
7778
Invokes all Unit tests from schema/package "hr_test" with two reporters:
7879

7980
* ut_documentation_reporter - will output to screen and save output to file "run.log"
80-
* ~~ut_coverage_html_reporter - will report only on database objects that are mapping to file structure from "source" folder and save output to file "coverage.html"~~
81+
* ut_coverage_html_reporter - will report only on database objects that are mapping to file structure from "source" folder and save output to file "coverage.html"
8182

8283
```
8384
utplsql run hr/hr@xe

src/main/java/io/github/utplsql/cli/Cli.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
public class Cli {
88

9+
public static final int DEFAULT_ERROR_CODE = 1;
10+
911
public static final String HELP_CMD = "-h";
1012
public static final String RUN_CMD = "run";
1113

@@ -15,14 +17,15 @@ public static void main(String[] args) {
1517
RunCommand runCmd = new RunCommand();
1618
jc.addCommand(RUN_CMD, runCmd);
1719

20+
int exitCode = DEFAULT_ERROR_CODE;
21+
1822
try {
1923
jc.parse(args);
20-
boolean hasCmd = jc.getParsedCommand() != null;
2124

22-
if (hasCmd && jc.getParsedCommand().equals(RUN_CMD)) {
23-
runCmd.run();
25+
if (RUN_CMD.equals(jc.getParsedCommand())) {
26+
exitCode = runCmd.run();
2427
} else {
25-
jc.usage();
28+
throw new ParameterException("Command not specified.");
2629
}
2730
} catch (ParameterException e) {
2831
if (jc.getParsedCommand() != null) {
@@ -34,6 +37,8 @@ public static void main(String[] args) {
3437
} catch (Exception e) {
3538
e.printStackTrace();
3639
}
40+
41+
System.exit(exitCode);
3742
}
3843

3944
private static class HelpCommand {

src/main/java/io/github/utplsql/cli/RunCommand.java

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.github.utplsql.api.CustomTypes;
66
import io.github.utplsql.api.OutputBuffer;
77
import io.github.utplsql.api.TestRunner;
8+
import io.github.utplsql.api.exception.SomeTestsFailedException;
89
import io.github.utplsql.api.reporter.Reporter;
910
import io.github.utplsql.api.reporter.ReporterFactory;
1011

@@ -51,6 +52,11 @@ public class RunCommand {
5152
description = "enables printing of test results in colors as defined by ANSICONSOLE standards")
5253
private boolean colorConsole = false;
5354

55+
@Parameter(
56+
names = {"--failure-exit-code"},
57+
description = "override the exit code on failure, default = 1")
58+
private int failureExitCode = 1;
59+
5460
@Parameter(names = {"-source_path"}, description = "path to project source files")
5561
private String sourcePath;
5662

@@ -92,7 +98,7 @@ public List<ReporterOptions> getReporterOptionsList() {
9298
return reporterOptionsList;
9399
}
94100

95-
public void run() throws Exception {
101+
public int run() throws Exception {
96102
final ConnectionInfo ci = getConnectionInfo();
97103

98104
final List<ReporterOptions> reporterOptionsList = getReporterOptionsList();
@@ -111,6 +117,7 @@ public void run() throws Exception {
111117

112118
final List<String> sourceFiles = sourceFilesTmp;
113119
final List<String> testFiles = testFilesTmp;
120+
final int[] returnCode = {0};
114121

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

@@ -123,25 +130,28 @@ public void run() throws Exception {
123130
reporterList.add(reporter);
124131
}
125132
} catch (SQLException e) {
126-
// TODO
127-
e.printStackTrace();
133+
System.out.println(e.getMessage());
134+
return Cli.DEFAULT_ERROR_CODE;
128135
}
129136

130137
ExecutorService executorService = Executors.newFixedThreadPool(1 + reporterList.size());
131138

132139
// Run tests.
133140
executorService.submit(() -> {
134-
try (Connection conn = ci.getConnection()){
141+
try (Connection conn = ci.getConnection()) {
135142
new TestRunner()
136143
.addPathList(testPaths)
137144
.addReporterList(reporterList)
138145
.withSourceFiles(sourceFiles)
139146
.withTestFiles(testFiles)
140-
.colorConsole(colorConsole)
147+
.colorConsole(this.colorConsole)
148+
.failOnErrors(true)
141149
.run(conn);
150+
} catch (SomeTestsFailedException e) {
151+
returnCode[0] = this.failureExitCode;
142152
} catch (SQLException e) {
143-
// TODO
144-
e.printStackTrace();
153+
System.out.println(e.getMessage());
154+
returnCode[0] = Cli.DEFAULT_ERROR_CODE;
145155
}
146156
});
147157

@@ -163,8 +173,8 @@ public void run() throws Exception {
163173

164174
new OutputBuffer(ro.getReporterObj()).printAvailable(conn, printStreams);
165175
} catch (SQLException | FileNotFoundException e) {
166-
// TODO
167-
e.printStackTrace();
176+
System.out.println(e.getMessage());
177+
returnCode[0] = Cli.DEFAULT_ERROR_CODE;
168178
} finally {
169179
if (fileOutStream != null)
170180
fileOutStream.close();
@@ -174,6 +184,7 @@ public void run() throws Exception {
174184

175185
executorService.shutdown();
176186
executorService.awaitTermination(60, TimeUnit.MINUTES);
187+
return returnCode[0];
177188
}
178189

179190
}

0 commit comments

Comments
 (0)