[core] CLI - make sure positional inputPaths are not lost
This commit is contained in:
@ -7,7 +7,6 @@ package net.sourceforge.pmd.cli.commands.internal;
|
||||
import java.net.URI;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import net.sourceforge.pmd.AbstractConfiguration;
|
||||
@ -34,6 +33,14 @@ public abstract class AbstractAnalysisPmdSubcommand<C extends AbstractConfigurat
|
||||
arity = "1..*", split = ",")
|
||||
protected List<Path> inputPaths;
|
||||
|
||||
@Parameters(arity = "*", description = "Path to a source file, or directory containing source files to analyze. "
|
||||
+ "Equivalent to using --dir.")
|
||||
// Note: we can't directly use inputPaths here, because "--dir" might be specified later in the
|
||||
// command line. And PicoCli will then initialize the field inputPaths with a fresh empty List, replacing
|
||||
// anything what was added before.So we need to combine the two lists in #validate()
|
||||
private List<Path> positionalInputPaths;
|
||||
|
||||
|
||||
@Option(names = "--file-list",
|
||||
description =
|
||||
"Path to a file containing a list of files to analyze, one path per line. "
|
||||
@ -50,16 +57,6 @@ public abstract class AbstractAnalysisPmdSubcommand<C extends AbstractConfigurat
|
||||
+ "Disable this option with '--no-fail-on-violation' to exit with 0 instead and just write the report.",
|
||||
defaultValue = "true", negatable = true)
|
||||
protected boolean failOnViolation;
|
||||
|
||||
@Parameters(arity = "*", description = "Path to a source file, or directory containing source files to analyze. "
|
||||
+ "Equivalent to using --dir.")
|
||||
public void setInputPaths(final List<Path> inputPaths) {
|
||||
if (this.inputPaths == null) {
|
||||
this.inputPaths = new ArrayList<>();
|
||||
}
|
||||
|
||||
this.inputPaths.addAll(inputPaths);
|
||||
}
|
||||
|
||||
protected List<Path> relativizeRootPaths;
|
||||
|
||||
@ -84,6 +81,14 @@ public abstract class AbstractAnalysisPmdSubcommand<C extends AbstractConfigurat
|
||||
protected final void validate() throws ParameterException {
|
||||
super.validate();
|
||||
|
||||
if (positionalInputPaths != null) {
|
||||
if (inputPaths == null) {
|
||||
inputPaths = positionalInputPaths;
|
||||
} else {
|
||||
inputPaths.addAll(positionalInputPaths);
|
||||
}
|
||||
}
|
||||
|
||||
if ((inputPaths == null || inputPaths.isEmpty()) && uri == null && fileListPath == null) {
|
||||
throw new ParameterException(spec.commandLine(),
|
||||
"Please provide a parameter for source root directory (--dir or -d), "
|
||||
|
@ -119,12 +119,19 @@ class CpdCliTest extends BaseCliTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void testWrongCliOptionResultsInErrorLogging() throws Exception {
|
||||
void testWrongCliOptionResultsInErrorLoggingAfterDir() throws Exception {
|
||||
// --ignore-identifiers doesn't take an argument anymore - it is interpreted as a file for inputPaths
|
||||
final CliExecutionResult result = runCli(VIOLATIONS_FOUND, "--minimum-tokens", "34", "--dir", SRC_DIR, "--ignore-identifiers", "false");
|
||||
result.checkStdErr(containsString("No such file false"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testWrongCliOptionResultsInErrorLoggingBeforeDir() throws Exception {
|
||||
// --ignore-identifiers doesn't take an argument anymore - it is interpreted as a file for inputPaths
|
||||
final CliExecutionResult result = runCli(VIOLATIONS_FOUND, "--minimum-tokens", "34", "--ignore-identifiers", "false", "--dir", SRC_DIR);
|
||||
result.checkStdErr(containsString("No such file false"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFindJavaDuplication() throws Exception {
|
||||
runCli(VIOLATIONS_FOUND, "--minimum-tokens", "7", "--dir", SRC_DIR)
|
||||
|
@ -15,7 +15,7 @@ import org.hamcrest.Matchers;
|
||||
import picocli.CommandLine;
|
||||
import picocli.CommandLine.ParseResult;
|
||||
|
||||
abstract class BaseCommandTest<T> {
|
||||
abstract class BaseCommandTest<T extends AbstractAnalysisPmdSubcommand<?>> {
|
||||
|
||||
protected abstract T createCommand();
|
||||
|
||||
@ -30,6 +30,7 @@ abstract class BaseCommandTest<T> {
|
||||
protected T setupAndParse(final String... params) {
|
||||
final T cmd = createCommand();
|
||||
final ParseResult parseResult = parseCommand(cmd, params);
|
||||
cmd.validate();
|
||||
|
||||
assertThat(parseResult.errors(), Matchers.empty());
|
||||
|
||||
|
Reference in New Issue
Block a user