move more things into AbstractConfiguration

This commit is contained in:
Clément Fournier committed 2023-02-19 15:39:47 +01:00
1 parent 0134f5e060
commit a3831e95dc
14 files changed
+337 -355

No files matched your search

@@ -5,6 +5,7 @@
package net.sourceforge.pmd.cli.commands.internal;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Iterator;
import java.util.List;
@@ -83,6 +84,25 @@ public class CpdCommand extends AbstractAnalysisPmdSubcommand {
@Option(names = "--non-recursive", description = "Don't scan subdirectiories.")
private boolean nonRecursive;
private List<Path> relativizeRootPaths;
@Option(names = { "--relativize-paths-with", "-z"}, description = "Path relative to which directories are rendered in the report. "
+ "This option allows shortening directories in the report; "
+ "without it, paths are rendered as mentioned in the source directory (option \"--dir\"). "
+ "The option can be repeated, in which case the shortest relative path will be used. "
+ "If the root path is mentioned (e.g. \"/\" or \"C:\\\"), then the paths will be rendered as absolute.",
arity = "1..*", split = ",")
public void setRelativizePathsWith(List<Path> rootPaths) {
this.relativizeRootPaths = rootPaths;
for (Path path : this.relativizeRootPaths) {
if (Files.isRegularFile(path)) {
throw new ParameterException(spec.commandLine(),
"Expected a directory path for option '--relativize-paths-with', found a file: " + path);
}
}
}
/**
* Converts these parameters into a configuration.
*
@@ -94,9 +114,12 @@ public class CpdCommand extends AbstractAnalysisPmdSubcommand {
final CPDConfiguration configuration = new CPDConfiguration();
configuration.setDebug(debug);
configuration.setExcludes(excludes);
if (relativizeRootPaths != null) {
configuration.addRelativizeRoots(relativizeRootPaths);
}
configuration.setFailOnViolation(failOnViolation);
configuration.setFileListPath(fileListPath);
configuration.setFiles(inputPaths);
configuration.setInputFilePath(fileListPath);
configuration.setInputPathList(inputPaths);
configuration.setIgnoreAnnotations(ignoreAnnotations);
configuration.setIgnoreIdentifiers(ignoreIdentifiers);
configuration.setIgnoreLiterals(ignoreLiterals);
@@ -111,7 +134,7 @@ public class CpdCommand extends AbstractAnalysisPmdSubcommand {
configuration.setSkipDuplicates(skipDuplicates);
configuration.setSkipLexicalErrors(skipLexicalErrors);
configuration.setSourceEncoding(encoding.getEncoding());
configuration.setURI(uri);
configuration.setInputUri(uri);
return configuration;
}
@@ -12,7 +12,6 @@ import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.emptyString;
import static org.hamcrest.Matchers.equalTo;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
@@ -35,10 +34,10 @@ class CpdCliTest extends BaseCliTest {
private static final String SRC_DIR = BASE_RES_PATH + "files/";
private static final Map<String, Integer> NUMBER_OF_TOKENS = ImmutableMap.of(
new File(SRC_DIR, "dup1.java").getAbsolutePath(), 89,
new File(SRC_DIR, "dup2.java").getAbsolutePath(), 89,
new File(SRC_DIR, "file_with_ISO-8859-1_encoding.java").getAbsolutePath(), 8,
new File(SRC_DIR, "file_with_utf8_bom.java").getAbsolutePath(), 9
Paths.get(SRC_DIR, "dup1.java").toString(), 89,
Paths.get(SRC_DIR, "dup2.java").toString(), 89,
Paths.get(SRC_DIR, "file_with_ISO-8859-1_encoding.java").toString(), 8,
Paths.get(SRC_DIR, "file_with_utf8_bom.java").toString(), 9
);
@TempDir
private Path tempDir;
@@ -67,7 +66,7 @@ class CpdCliTest extends BaseCliTest {
private String getExpectedFileEntryXml(final String filename) {
final int numberOfTokens = NUMBER_OF_TOKENS.get(filename);
return String.format(" <file path=\"%s\"\n totalNumberOfTokens=\"%d\"/>\n",
new File(filename).getAbsolutePath(),
filename,
numberOfTokens);
}
@@ -174,13 +174,26 @@ class PmdCliTest extends BaseCliTest {
assertFalse(Files.exists(absoluteReportFile), "Report file must not exist yet!");
try {
runCliSuccessfully("--dir", srcDir.toString(), "--rulesets", RULESET_NO_VIOLATIONS, "--report-file", reportFile.toString());
runCliSuccessfully("--dir", srcDir.toString(), "--rulesets", RULESET_NO_VIOLATIONS, "--report-file", reportFile);
assertTrue(Files.exists(absoluteReportFile), "Report file should have been created");
} finally {
Files.deleteIfExists(absoluteReportFile);
}
}
@Test
void testRelativeFileInputs() throws Exception {
SystemLambda.restoreSystemProperties(() -> {
// change working directory
System.setProperty("user.dir", srcDir.toString());
runCliSuccessfully("--dir", ".", "--rulesets", DUMMY_RULESET_WITH_VIOLATIONS)
.verify(res -> res.checkStdOut(containsString("./src/test/resources/net/sourceforge/pmd/cli/src/anotherfile.dummy")));
});
}
@Test
void debugLogging() throws Exception {
CliExecutionResult result = runCliSuccessfully("--debug", "--dir", srcDir.toString(), "--rulesets", RULESET_NO_VIOLATIONS);
@@ -56,7 +56,7 @@ class CpdCommandTest extends BaseCommandTest<CpdCommand> {
private void assertMultipleDirs(final CpdCommand result) {
final CPDConfiguration config = result.toConfiguration();
assertEquals(listOf("a", "b"), CollectionUtil.map(config.getFiles(), Path::toString));
assertEquals(listOf("a", "b"), CollectionUtil.map(config.getInputPathList(), Path::toString));
}
@Override