#1414 Command line parameter to disable "failOnViolation" behavior

This commit is contained in:
Andreas Dangel
2015-09-26 17:08:20 +02:00
parent 38741d4638
commit bbd0bf98fd
9 changed files with 80 additions and 4 deletions

View File

@ -457,7 +457,7 @@ public class PMD {
// already been initialized at this point
try {
int violations = PMD.doPMD(configuration);
if (violations > 0) {
if (violations > 0 && configuration.isFailOnViolation()) {
status = PMDCommandLineInterface.VIOLATIONS_FOUND;
} else {
status = 0;

View File

@ -101,6 +101,7 @@ public class PMDConfiguration extends AbstractConfiguration {
private boolean reportShortNames = false;
private Properties reportProperties = new Properties();
private boolean showSuppressedViolations = false;
private boolean failOnViolation = true;
private boolean stressTest;
private boolean benchmark;
@ -493,4 +494,22 @@ public class PMDConfiguration extends AbstractConfiguration {
public void setBenchmark(boolean benchmark) {
this.benchmark = benchmark;
}
/**
* Whether PMD should exit with status 4 (the default behavior, true) if violations
* are found or just with 0 (to not break the build, e.g.).
* @return failOnViolation
*/
public boolean isFailOnViolation() {
return failOnViolation;
}
/**
* Sets whether PMD should exit with status 4 (the default behavior, true) if violations
* are found or just with 0 (to not break the build, e.g.).
* @param failOnViolation failOnViolation
*/
public void setFailOnViolation(boolean failOnViolation) {
this.failOnViolation = failOnViolation;
}
}

View File

@ -78,6 +78,9 @@ public class PMDParameters {
@Parameter(names = "-auxclasspath", description = "Specifies the classpath for libraries used by the source code. This is used by the type resolution. Alternatively, a 'file://' URL to a text file containing path elements on consecutive lines can be specified.")
private String auxclasspath;
@Parameter(names = {"-failOnViolation", "--failOnViolation"}, arity = 1, description = "By default PMD exits with status 4 if violations are found. Disable this option with '-failOnViolation false' to exit with 0 instead and just write the report.")
private boolean failOnViolation = true;
// this has to be a public static class, so that JCommander can use it!
public static class PropertyConverter implements IStringConverter<Properties> {
@ -135,6 +138,7 @@ public class PMDParameters {
configuration.setStressTest(params.isStress());
configuration.setSuppressMarker(params.getSuppressmarker());
configuration.setThreads(params.getThreads());
configuration.setFailOnViolation(params.isFailOnViolation());
LanguageVersion languageVersion = LanguageRegistry.findLanguageVersionByTerseName(params.getLanguage() + " " + params.getVersion());
if(languageVersion != null) {
@ -224,6 +228,10 @@ public class PMDParameters {
return format;
}
public boolean isFailOnViolation() {
return failOnViolation;
}
/**
* @return the uri alternative to source directory.
*/

View File

@ -89,7 +89,11 @@ public class CPDCommandLineInterface {
cpd.go();
if (cpd.getMatches().hasNext()) {
System.out.println(arguments.getRenderer().render(cpd.getMatches()));
setStatusCodeOrExit(DUPLICATE_CODE_FOUND);
if (arguments.isFailOnViolation()) {
setStatusCodeOrExit(DUPLICATE_CODE_FOUND);
} else {
setStatusCodeOrExit(0);
}
} else {
setStatusCodeOrExit(0);
}

View File

@ -92,6 +92,9 @@ public class CPDConfiguration extends AbstractConfiguration {
@Parameter(names = { "--help", "-h" }, description = "Print help text", required = false, help = true)
private boolean help;
@Parameter(names = {"--failOnViolation", "-failOnViolation"}, arity = 1, description = "By default CPD exits with status 4 if code duplications are found. Disable this option with '-failOnViolation false' to exit with 0 instead and just write the report.")
private boolean failOnViolation = true;
// this has to be a public static class, so that JCommander can use it!
public static class LanguageConverter implements IStringConverter<Language> {
@ -402,4 +405,12 @@ public class CPDConfiguration extends AbstractConfiguration {
public void setSkipBlocksPattern(String skipBlocksPattern) {
this.skipBlocksPattern = skipBlocksPattern;
}
public boolean isFailOnViolation() {
return failOnViolation;
}
public void setFailOnViolation(boolean failOnViolation) {
this.failOnViolation = failOnViolation;
}
}

View File

@ -50,6 +50,21 @@ public class CPDCommandLineInterfaceTest {
Assert.assertEquals(4, Integer.parseInt(System.getProperty(CPDCommandLineInterface.STATUS_CODE_PROPERTY)));
}
/**
* Test ignore identifiers argument with failOnViolation=false
*/
@Test
public void testIgnoreIdentifiersFailOnViolationFalse() throws Exception {
runCPD("--minimum-tokens", "34", "--language", "java",
"--files", "src/test/resources/net/sourceforge/pmd/cpd/clitest/",
"--ignore-identifiers",
"--failOnViolation", "false");
String out = bufferStdout.toString("UTF-8");
Assert.assertTrue(out.contains("Found a 7 line (36 tokens) duplication"));
Assert.assertEquals(0, Integer.parseInt(System.getProperty(CPDCommandLineInterface.STATUS_CODE_PROPERTY)));
}
/**
* Test excludes option.
*/

View File

@ -11,6 +11,10 @@
* [#1344](https://sourceforge.net/p/pmd/bugs/1344/): AbstractNaming should check reverse
* [#1361](https://sourceforge.net/p/pmd/bugs/1361/): ShortVariable and ShortMethodName configuration
* [#1414](https://sourceforge.net/p/pmd/bugs/1414/): Command line parameter to disable "failOnViolation" behavior
PMD and CPD Command Line Interfaces have a new optional parameter: `failOnViolation`. Executing PMD with the option
`-failOnViolation false` will perform the PMD checks but won't fail the build and still exit with status 0.
This is useful if you only want to generate the report with violations but don't want to fail your build.
**New Rules:**

View File

@ -129,6 +129,13 @@ The options "minimum-tokens" and "files" are the two required options; there are
<td>no</td>
<td></td>
</tr>
<tr>
<td>--failOnViolation {true|false}</td>
<td>By default CPD exits with status 4 if code duplications are found.
Disable this option with '--failOnViolation false' to exit with 0 instead and just write the report.</td>
<td>no</td>
<td></td>
</tr>
<tr>
<td>--ignore-literals</td>
<td>Ignore number values and string contents when comparing text</td>
@ -231,7 +238,7 @@ This behavior has been introduced to ease CPD integration into scripts or hooks,
<table>
<tr><td>0</td><td>Everything is fine, now code duplications found</td></tr>
<tr><td>1</td><td>Couldn't understand command line parameters or CPD exited with an exception</td></tr>
<tr><td>4</td><td>At least one code duplication has been detected</td></tr>
<tr><td>4</td><td>At least one code duplication has been detected unless '--failOnViolation false' is used.</td></tr>
</table>

View File

@ -190,6 +190,14 @@ The tool comes with a rather extensive help text, simply running with `-help`!
<td>no</td>
<td></td>
</tr>
<tr>
<td>-failOnViolation {true|false}</td>
<td>By default PMD exits with status 4 if violations are found.
Disable this option with '-failOnViolation false' to exit with 0 instead and just write the report.
</td>
<td>no</td>
<td></td>
</tr>
</table>
@ -202,7 +210,7 @@ This behavior has been introduced to ease PMD integration into scripts or hooks,
<table>
<tr><td>0</td><td>Everything is fine, now violations found</td></tr>
<tr><td>1</td><td>Couldn't understand command line parameters or PMD exited with an exception</td></tr>
<tr><td>4</td><td>At least one violation has been detected</td></tr>
<tr><td>4</td><td>At least one violation has been detected unless '-failOnViolation false' is set.</td></tr>
</table>