[cpd] Deprecate skipLexicalErrors

This commit is contained in:
Andreas Dangel 2024-05-17 16:16:20 +02:00
parent b624b41673
commit 10c78f26de
No known key found for this signature in database
GPG Key ID: 93450DF2DF9A3FA3
8 changed files with 55 additions and 13 deletions

View File

@ -132,8 +132,8 @@ exactly identical.
description="Don't scan subdirectories. By default, subdirectories are considered."
%}
{% include custom/cli_option_row.html options="--skip-lexical-errors"
description="Skip files which can't be tokenized due to invalid characters instead of aborting CPD.
By default, CPD analysis is stopped on the first error."
description="<span class='label label-primary'>Deprecated</span> Skip files which can't be tokenized due to invalid characters instead of aborting CPD.
By default, CPD analysis is stopped on the first error. This is deprecated. Use `--fail-on-error` instead."
%}
{% include custom/cli_option_row.html options="--format,-f"
option_arg="format"
@ -439,8 +439,10 @@ Andy Glover wrote an Ant task for CPD; here's how to use it:
default="false"
%}
{% include custom/cli_option_row.html options="skipLexicalErrors"
description="Skip files which can't be tokenized due to invalid characters instead of aborting CPD."
default="false"
description="<span class='label label-primary'>Deprecated</span> Skip files which can't be tokenized
due to invalid characters instead of aborting CPD. This parameter is deprecated and
ignored since PMD 7.2.0. It is now by default true. Use `failOnError` instead to fail the build."
default="true"
%}
{% include custom/cli_option_row.html options="skipBlocks"
description="Enables or disabled skipping of blocks like a pre-processor. See also option skipBlocksPattern."

View File

@ -65,11 +65,14 @@ Since this release, PMD will also expose any getter returning a collection of an
a build with errors will now fail and with that parameter, the previous behavior can be restored.
This parameter is available for both PMD and CPD.
* The CLI parameter `--skip-lexical-errors` is deprecated. Use the new parameter `--[no-]--fail-on-error` instead.
##### Ant
* CPDTask has a new parameter `failOnError`. In controls, whether to fail the build if any recoverable errors occurred.
By default, the build will fail. CPD will still create a report with all detected duplications, but the report might
be incomplete.
* The parameter `skipLexicalError` in CPDTask is deprecated. Use the new parameter `failOnError` instead.
#### Deprecated API

View File

@ -75,6 +75,7 @@ public class CPDTask extends Task {
private boolean ignoreIdentifiers;
private boolean ignoreAnnotations;
private boolean ignoreUsings;
@Deprecated
private boolean skipLexicalErrors;
private boolean skipDuplicateFiles;
private boolean skipBlocks = true;
@ -100,7 +101,14 @@ public class CPDTask extends Task {
config.setOnlyRecognizeLanguage(config.getLanguageRegistry().getLanguageById(language));
config.setSourceEncoding(Charset.forName(encoding));
config.setSkipDuplicates(skipDuplicateFiles);
config.setSkipLexicalErrors(skipLexicalErrors);
if (skipLexicalErrors) {
log("skipLexicalErrors is deprecated and ignored. Lexical errors are now by default skipped. Use failOnError=\"false\" to not fail the build.", Project.MSG_WARN);
failOnError = false;
}
// implicitly enable skipLexicalErrors, so that we can fail the build at the end. A report is created in any case.
config.setSkipLexicalErrors(true);
config.setIgnoreAnnotations(ignoreAnnotations);
config.setIgnoreLiterals(ignoreLiterals);
@ -132,7 +140,6 @@ public class CPDTask extends Task {
log(ioe.toString(), Project.MSG_ERR);
throw new BuildException("IOException during task execution", ioe);
} catch (ReportException re) {
re.printStackTrace();
log(re.toString(), Project.MSG_ERR);
throw new BuildException("ReportException during task execution", re);
} finally {
@ -229,6 +236,10 @@ public class CPDTask extends Task {
this.ignoreUsings = value;
}
/**
* @deprecated Use {@link #setFailOnError(boolean)} instead.
*/
@Deprecated
public void setSkipLexicalErrors(boolean skipLexicalErrors) {
this.skipLexicalErrors = skipLexicalErrors;
}

View File

@ -17,7 +17,7 @@
</target>
<target name="failOnErrorDefault">
<cpd minimumTokenCount="20" outputFile="${pmd.home}/target/cpd.ant.tests" language="dummy" skipLexicalErrors="true">
<cpd minimumTokenCount="20" outputFile="${pmd.home}/target/cpd.ant.tests" language="dummy">
<fileset dir="${pmd.home}/${src}">
<include name="**/*.dummy"/>
</fileset>
@ -25,7 +25,7 @@
</target>
<target name="failOnErrorIgnore">
<cpd minimumTokenCount="20" outputFile="${pmd.home}/target/cpd.ant.tests" language="dummy" failOnError="false" skipLexicalErrors="true">
<cpd minimumTokenCount="20" outputFile="${pmd.home}/target/cpd.ant.tests" language="dummy" failOnError="false">
<fileset dir="${pmd.home}/${src}">
<include name="**/*.dummy"/>
</fileset>

View File

@ -68,8 +68,12 @@ public class CpdCommand extends AbstractAnalysisPmdSubcommand<CPDConfiguration>
@Option(names = "--ignore-sequences", description = "Ignore sequences of identifiers and literals")
private boolean ignoreIdentifierAndLiteralSequences;
/**
* @deprecated Use {@link #failOnError} instead.
*/
@Option(names = "--skip-lexical-errors",
description = "Skip files which can't be tokenized due to invalid characters, instead of aborting with an error.")
description = "Skip files which can't be tokenized due to invalid characters, instead of aborting with an error. Deprecated - use --[no-]fail-on-error instead.")
@Deprecated
private boolean skipLexicalErrors;
@Option(names = "--no-skip-blocks",
@ -124,6 +128,14 @@ public class CpdCommand extends AbstractAnalysisPmdSubcommand<CPDConfiguration>
configuration.setSourceEncoding(encoding.getEncoding());
configuration.setInputUri(uri);
if (skipLexicalErrors) {
configuration.getReporter().warn("--skip-lexical-errors is deprecated. Use --no-fail-on-error instead.");
configuration.setFailOnError(false);
}
// implicitly enable skipLexicalErrors, so that we can fail the build at the end. A report is created in any case.
configuration.setSkipLexicalErrors(true);
return configuration;
}

View File

@ -221,7 +221,7 @@ class CpdCliTest extends BaseCliTest {
*/
@Test
void testSkipLexicalErrors() throws Exception {
runCli(RECOVERED_ERRORS_OR_VIOLATIONS,
runCli(VIOLATIONS_FOUND,
"--minimum-tokens", "10",
"-d", BASE_RES_PATH + "badandgood/",
"--format", "text",
@ -239,7 +239,7 @@ class CpdCliTest extends BaseCliTest {
"-d", Paths.get(BASE_RES_PATH, "badandgood", "BadFile.java").toString(),
"--format", "text")
.verify(r -> {
r.checkStdErr(containsPattern("Error while tokenizing: Lexical error in file '.*?BadFile\\.java'"));
r.checkStdErr(containsPattern("Skipping file: Lexical error in file '.*?BadFile\\.java'"));
r.checkStdOut(emptyString());
});
}
@ -252,14 +252,14 @@ class CpdCliTest extends BaseCliTest {
"--format", "text",
"--no-fail-on-error")
.verify(r -> {
r.checkStdErr(containsPattern("Error while tokenizing: Lexical error in file '.*?BadFile\\.java'"));
r.checkStdErr(containsPattern("Skipping file: Lexical error in file '.*?BadFile\\.java'"));
r.checkStdOut(emptyString());
});
}
@Test
void testExitCodeWithLexicalErrorsAndSkipLexical() throws Exception {
runCli(RECOVERED_ERRORS_OR_VIOLATIONS,
runCli(OK,
"--minimum-tokens", "10",
"-d", Paths.get(BASE_RES_PATH, "badandgood", "BadFile.java").toString(),
"--format", "text",

View File

@ -65,6 +65,7 @@ public class CPDConfiguration extends AbstractConfiguration {
private boolean ignoreIdentifierAndLiteralSequences = false;
@Deprecated
private boolean skipLexicalErrors = false;
private boolean noSkipBlocks = false;
@ -226,10 +227,20 @@ public class CPDConfiguration extends AbstractConfiguration {
this.ignoreIdentifierAndLiteralSequences = ignoreIdentifierAndLiteralSequences;
}
/**
* @deprecated This option will be removed. With {@link #isFailOnError()}, you can
* control whether lexical errors should fail the build or not.
*/
@Deprecated
public boolean isSkipLexicalErrors() {
return skipLexicalErrors;
}
/**
* @deprecated This option will be removed. With {@link #setFailOnError(boolean)}, you can
* control whether lexical errors should fail the build or not.
*/
@Deprecated
public void setSkipLexicalErrors(boolean skipLexicalErrors) {
this.skipLexicalErrors = skipLexicalErrors;
}

View File

@ -151,6 +151,9 @@ public final class CpdAnalysis implements AutoCloseable {
@SuppressWarnings("PMD.CloseResource")
public void performAnalysis(Consumer<CPDReport> consumer) {
if (configuration.isSkipLexicalErrors()) {
LOGGER.warn("The option skipLexicalErrors is deprecated. Use failOnError instead.");
}
try (SourceManager sourceManager = new SourceManager(files.getCollectedFiles())) {
Map<Language, CpdLexer> tokenizers =