[cli] Add new param "--no-fail-on-processing-error"

This commit is contained in:
Andreas Dangel 2024-05-03 09:33:46 +02:00
parent 0fc23fc9f4
commit e667bf6773
No known key found for this signature in database
GPG Key ID: 93450DF2DF9A3FA3
6 changed files with 43 additions and 2 deletions

View File

@ -48,6 +48,12 @@ public abstract class AbstractAnalysisPmdSubcommand<C extends AbstractConfigurat
defaultValue = "true", negatable = true)
protected boolean failOnViolation;
@Option(names = "--no-fail-on-processing-error",
description = "By default PMD exits with status 5 if processing errors or violations are found. "
+ "Disable this option with '--no-fail-on-processing-error' to exit with 0 instead and just write the report.",
defaultValue = "true", negatable = true)
protected boolean failOnProcessingError;
protected List<Path> relativizeRootPaths;
@Option(names = { "--relativize-paths-with", "-z"}, description = "Path relative to which directories are rendered in the report. "

View File

@ -103,6 +103,7 @@ public class CpdCommand extends AbstractAnalysisPmdSubcommand<CPDConfiguration>
configuration.addRelativizeRoots(relativizeRootPaths);
}
configuration.setFailOnViolation(failOnViolation);
configuration.setFailOnProcessingError(failOnProcessingError);
configuration.setInputFilePath(fileListPath);
if (inputPaths != null) {
configuration.setInputPathList(new ArrayList<>(inputPaths));
@ -134,7 +135,7 @@ public class CpdCommand extends AbstractAnalysisPmdSubcommand<CPDConfiguration>
cpd.performAnalysis(report -> hasViolations.setValue(!report.getMatches().isEmpty()));
boolean hasProcessingErrors = configuration.getReporter().numErrors() > 0;
if (hasProcessingErrors) {
if (hasProcessingErrors && configuration.isFailOnProcessingError()) {
return CliExitCode.VIOLATIONS_OR_PROCESSING_ERRORS;
}

View File

@ -270,6 +270,7 @@ public class PmdCommand extends AbstractAnalysisPmdSubcommand<PMDConfiguration>
configuration.setSuppressMarker(suppressMarker);
configuration.setThreads(threads);
configuration.setFailOnViolation(failOnViolation);
configuration.setFailOnProcessingError(failOnProcessingError);
configuration.setAnalysisCacheLocation(cacheLocation != null ? cacheLocation.toString() : null);
configuration.setIgnoreIncrementalAnalysis(noCache);
@ -330,7 +331,7 @@ public class PmdCommand extends AbstractAnalysisPmdSubcommand<PMDConfiguration>
if (pmdReporter.numErrors() > 0) {
// processing errors are ignored
return CliExitCode.ERROR;
} else if (stats.getNumErrors() > 0) {
} else if (stats.getNumErrors() > 0 && configuration.isFailOnProcessingError()) {
return CliExitCode.VIOLATIONS_OR_PROCESSING_ERRORS;
} else if (stats.getNumViolations() > 0 && configuration.isFailOnViolation()) {
return CliExitCode.VIOLATIONS_FOUND;

View File

@ -244,6 +244,19 @@ class CpdCliTest extends BaseCliTest {
});
}
@Test
void testExitCodeWithLexicalErrorsNoFail() throws Exception {
runCli(OK,
"--minimum-tokens", "10",
"-d", Paths.get(BASE_RES_PATH, "badandgood", "BadFile.java").toString(),
"--format", "text",
"--no-fail-on-processing-error")
.verify(r -> {
r.checkStdErr(containsPattern("Error while tokenizing: Lexical error in file '.*?BadFile\\.java'"));
r.checkStdOut(emptyString());
});
}
@Test
void testExitCodeWithLexicalErrorsAndSkipLexical() throws Exception {
runCli(VIOLATIONS_OR_PROCESSING_ERRORS,

View File

@ -329,6 +329,17 @@ class PmdCliTest extends BaseCliTest {
});
}
@Test
void exitStatusWithProcessingErrorsNoFail() throws Exception {
runCli(OK, "--use-version", "dummy-parserThrows",
"-d", srcDir.toString(), "-f", "text", "-R", RULESET_WITH_VIOLATION,
"--no-fail-on-processing-error")
.verify(r -> {
r.checkStdOut(containsString("someSource.dummy\t-\tParseException: Parse exception: ohio"));
r.checkStdErr(containsString("An error occurred while executing PMD."));
});
}
@Test
void testZipFileAsSource() throws Exception {
Path zipArchive = createTemporaryZipArchive("sources.zip");

View File

@ -46,6 +46,7 @@ public abstract class AbstractConfiguration {
private Path ignoreFilePath;
private List<Path> excludes = new ArrayList<>();
private boolean collectRecursive = true;
private boolean failOnProcessingError = true;
protected AbstractConfiguration(LanguageRegistry languageRegistry, PmdReporter messageReporter) {
@ -377,4 +378,12 @@ public abstract class AbstractConfiguration {
public void collectFilesRecursively(boolean collectRecursive) {
this.collectRecursive = collectRecursive;
}
public boolean isFailOnProcessingError() {
return failOnProcessingError;
}
public void setFailOnProcessingError(boolean failOnProcessingError) {
this.failOnProcessingError = failOnProcessingError;
}
}