Cleaned up API to match bld operations and options APIs

This commit is contained in:
Erik C. Thauvin 2024-08-27 12:55:48 -07:00
parent e63c1a636e
commit 2b6b4a5e82
Signed by: erik
GPG key ID: 776702A6A2DA330E
3 changed files with 180 additions and 52 deletions

View file

@ -31,7 +31,7 @@ public class PmdOperationBuild extends Project {
public PmdOperationBuild() {
pkg = "rife.bld.extension";
name = "bld-pmd";
version = version(1, 1, 4);
version = version(1, 1, 5, "SNAPSHOT");
javaRelease = 17;
@ -44,11 +44,10 @@ public class PmdOperationBuild extends Project {
.include(dependency("com.uwyn.rife2", "bld", version(2, 0, 1)))
.include(dependency("net.sourceforge.pmd", "pmd-java", pmd));
scope(runtime)
.include(dependency("net.sourceforge.pmd", "pmd-java", pmd))
.include(dependency("org.slf4j", "slf4j-simple", version(2, 0, 13)));
.include(dependency("org.slf4j", "slf4j-simple", version(2, 0, 16)));
scope(test)
.include(dependency("org.junit.jupiter", "junit-jupiter", version(5, 10, 3)))
.include(dependency("org.junit.platform", "junit-platform-console-standalone", version(1, 10, 3)))
.include(dependency("org.junit.jupiter", "junit-jupiter", version(5, 11, 0)))
.include(dependency("org.junit.platform", "junit-platform-console-standalone", version(1, 11, 0)))
.include(dependency("org.assertj", "assertj-core", version(3, 26, 3)));
javadocOperation()

View file

@ -142,8 +142,7 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
* @see #inputPaths(Path...)
*/
public PmdOperation addInputPaths(Path... inputPath) {
inputPaths_.addAll(List.of(inputPath));
return this;
return inputPaths(List.of(inputPath));
}
/**
@ -154,8 +153,7 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
* @see #inputPaths(File...)
*/
public PmdOperation addInputPaths(File... inputPath) {
inputPaths_.addAll(Arrays.stream(inputPath).map(File::toPath).toList());
return this;
return addInputPathsFiles(List.of(inputPath));
}
/**
@ -163,11 +161,10 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
*
* @param inputPath one or more paths
* @return this operation
* @see #addInputPaths(String...)
* @see #inputPaths(String...)
*/
public PmdOperation addInputPaths(String... inputPath) {
inputPaths_.addAll(Arrays.stream(inputPath).map(Paths::get).toList());
return this;
return addInputPathsStrings(List.of(inputPath));
}
/**
@ -182,6 +179,28 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
return this;
}
/**
* Adds paths to source files, or directories containing source files to analyze.
*
* @param inputPath a collection of input paths
* @return this operation
* @see #inputPathsFiles(Collection)
*/
public PmdOperation addInputPathsFiles(Collection<File> inputPath) {
return addInputPaths(inputPath.stream().map(File::toPath).toList());
}
/**
* Adds paths to source files, or directories containing source files to analyze.
*
* @param inputPath a collection of input paths
* @return this operation
* @see #inputPathsStrings(Collection)
*/
public PmdOperation addInputPathsStrings(Collection<String> inputPath) {
return addInputPaths(inputPath.stream().map(Paths::get).toList());
}
/**
* Adds new rule set paths.
* <p>
@ -203,8 +222,7 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
* @see #ruleSets(String...)
*/
public PmdOperation addRuleSet(String... ruleSet) {
ruleSets_.addAll(List.of(ruleSet));
return this;
return addRuleSet(List.of(ruleSet));
}
/**
@ -225,7 +243,7 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
*
* @param ruleSet a collection of rule set paths
* @return this operation
* @see #ruleSets(Collection
* @see #ruleSets(Collection)
*/
public PmdOperation addRuleSet(Collection<String> ruleSet) {
ruleSets_.addAll(ruleSet);
@ -240,6 +258,21 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
return this;
}
/**
* Sets the location of the cache file for incremental analysis.
*/
public PmdOperation cache(File cache) {
return cache(cache.toPath());
}
/**
* Sets the location of the cache file for incremental analysis.
*/
public PmdOperation cache(String cache) {
return cache(Path.of(cache));
}
/**
* Sets the default language version to be used for all input files.
*
@ -247,8 +280,7 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
* @return this operation
*/
public PmdOperation defaultLanguageVersions(LanguageVersion... languageVersion) {
languageVersions_.addAll(List.of(languageVersion));
return this;
return languageVersions(List.of(languageVersion));
}
/**
@ -268,8 +300,7 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
* <p>The valid values are the standard character sets of {@link java.nio.charset.Charset Charset}.</p>
*/
public PmdOperation encoding(String encoding) {
encoding_ = Charset.forName(encoding);
return this;
return encoding(Charset.forName(encoding));
}
/**
@ -385,8 +416,7 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
* @return this operation
*/
public PmdOperation ignoreFile(File ignoreFile) {
ignoreFile_ = ignoreFile.toPath();
return this;
return ignoreFile(ignoreFile.toPath());
}
/**
@ -396,8 +426,7 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
* @return this operation
*/
public PmdOperation ignoreFile(String ignoreFile) {
ignoreFile_ = Paths.get(ignoreFile);
return this;
return ignoreFile(Path.of(ignoreFile));
}
/**
@ -495,9 +524,7 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
* @see #addInputPaths(Path...)
*/
public PmdOperation inputPaths(Path... inputPath) {
inputPaths_.clear();
inputPaths_.addAll(List.of(inputPath));
return this;
return inputPaths(List.of(inputPath));
}
/**
@ -510,9 +537,7 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
* @see #addInputPaths(File...)
*/
public PmdOperation inputPaths(File... inputPath) {
inputPaths_.clear();
inputPaths_.addAll(Arrays.stream(inputPath).map(File::toPath).toList());
return this;
return inputPathsFiles(List.of(inputPath));
}
/**
@ -525,9 +550,7 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
* @see #addInputPaths(String...)
*/
public PmdOperation inputPaths(String... inputPath) {
inputPaths_.clear();
inputPaths_.addAll(Arrays.stream(inputPath).map(Paths::get).toList());
return this;
return inputPathsStrings(List.of(inputPath));
}
/**
@ -537,7 +560,7 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
*
* @param inputPath a collection of input paths
* @return this operation
* @see #addInputPaths(Collection)
* @see #addInputPaths(Path...)
*/
public PmdOperation inputPaths(Collection<Path> inputPath) {
inputPaths_.clear();
@ -554,6 +577,32 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
return inputPaths_;
}
/**
* Sets paths to source files, or directories containing source files to analyze.
* <p>
* Previous entries are disregarded.
*
* @param inputPath a collection of input paths
* @return this operation
* @see #addInputPaths(File...)
*/
public PmdOperation inputPathsFiles(Collection<File> inputPath) {
return inputPaths(inputPath.stream().map(File::toPath).toList());
}
/**
* Sets paths to source files, or directories containing source files to analyze.
* <p>
* Previous entries are disregarded.
*
* @param inputPath a collection of input paths
* @return this operation
* @see #addInputPaths(String...)
*/
public PmdOperation inputPathsStrings(Collection<String> inputPath) {
return inputPaths(inputPath.stream().map(Paths::get).toList());
}
/**
* Sets the default language versions.
*
@ -561,8 +610,7 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
* @return this operation
*/
public PmdOperation languageVersions(LanguageVersion... languageVersion) {
languageVersions_.addAll(List.of(languageVersion));
return this;
return languageVersions(List.of(languageVersion));
}
/**
@ -667,10 +715,10 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
*
* @param relativeRoot one or more relative root paths
* @return this operations
* @see #relativizeRoots(Collection)
*/
public PmdOperation relativizeRoots(Path... relativeRoot) {
relativizeRoots_.addAll(List.of(relativeRoot));
return this;
return relativizeRoots(List.of(relativeRoot));
}
/**
@ -678,10 +726,10 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
*
* @param relativeRoot one or more relative root paths
* @return this operations
* @see #relativizeRootsFiles(Collection)
*/
public PmdOperation relativizeRoots(File... relativeRoot) {
relativizeRoots_.addAll(Arrays.stream(relativeRoot).map(File::toPath).toList());
return this;
return relativizeRootsFiles(List.of(relativeRoot));
}
/**
@ -689,10 +737,10 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
*
* @param relativeRoot one or more relative root paths
* @return this operations
* @see #relativizeRootsStrings(Collection)
*/
public PmdOperation relativizeRoots(String... relativeRoot) {
relativizeRoots_.addAll(Arrays.stream(relativeRoot).map(Paths::get).toList());
return this;
return relativizeRootsStrings(List.of(relativeRoot));
}
/**
@ -700,6 +748,7 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
*
* @param relativeRoot a collection of relative root paths
* @return this operations
* @see #relativizeRoots(Path...)
*/
public PmdOperation relativizeRoots(Collection<Path> relativeRoot) {
relativizeRoots_.addAll(relativeRoot);
@ -715,6 +764,28 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
return relativizeRoots_;
}
/**
* Adds several paths to shorten paths that are output in the report.
*
* @param relativeRoot a collection of relative root paths
* @return this operations
* @see #relativizeRoots(File...)
*/
public PmdOperation relativizeRootsFiles(Collection<File> relativeRoot) {
return relativizeRoots(relativeRoot.stream().map(File::toPath).toList());
}
/**
* Adds several paths to shorten paths that are output in the report.
*
* @param relativeRoot a collection of relative root paths
* @return this operations
* @see #relativizeRoots(String...)
*/
public PmdOperation relativizeRootsStrings(Collection<String> relativeRoot) {
return relativizeRoots(relativeRoot.stream().map(Paths::get).toList());
}
/**
* Sets the path to the report page.
*

View file

@ -61,13 +61,13 @@ class PmdOperationTest {
PmdOperation newPmdOperation() {
return new PmdOperation()
.inputPaths(Path.of("src/main"), Path.of("src/test"))
.cache(Paths.get("build", COMMAND_NAME, "pmd-cache"))
.cache("build/" + COMMAND_NAME + "/pmd-cache")
.failOnViolation(false)
.reportFile(Paths.get("build", COMMAND_NAME, "pmd-test-report.txt"));
}
@Test
void testAddInputPath() throws ExitStatusException {
void testAddInputPaths() throws ExitStatusException {
var project = new BaseProject();
var pmd = new PmdOperation().fromProject(project);
@ -82,17 +82,35 @@ class PmdOperationTest {
assertThat(pmd.inputPaths()).as("main").containsExactly(project.srcMainDirectory().toPath());
pmd.inputPaths().clear();
pmd.addInputPaths(project.srcMainDirectory().toPath(), project.srcTestDirectory().toPath());
pmd = pmd.addInputPaths(project.srcMainDirectory(), project.srcTestDirectory());
assertThat(pmd.inputPaths()).as("toPath(main, test)").containsExactly(project.srcMainDirectory().toPath(),
assertThat(pmd.inputPaths()).as("main, test").containsExactly(project.srcMainDirectory().toPath(),
project.srcTestDirectory().toPath());
pmd.inputPaths().clear();
pmd.addInputPaths(List.of(project.srcMainDirectory().toPath(), project.srcTestDirectory().toPath()));
pmd = pmd.addInputPathsFiles(List.of(project.srcMainDirectory(), project.srcTestDirectory()));
assertThat(pmd.inputPaths()).as("List(main, test)").containsExactly(
project.srcMainDirectory().toPath(),
project.srcTestDirectory().toPath());
assertThat(pmd.inputPaths()).as("PathsFiles(main, test)")
.containsExactly(project.srcMainDirectory().toPath(), project.srcTestDirectory().toPath());
pmd.inputPaths().clear();
pmd = pmd.addInputPathsStrings(
List.of(project.srcMainDirectory().getAbsolutePath(), project.srcTestDirectory().getAbsolutePath()));
assertThat(pmd.inputPaths()).as("PathsStrings(main, test)")
.containsExactly(project.srcMainDirectory().toPath(), project.srcTestDirectory().toPath());
pmd.inputPaths().clear();
pmd = pmd.addInputPaths(project.srcMainDirectory().toPath(), project.srcTestDirectory().toPath());
assertThat(pmd.inputPaths()).as("toPath(main, test)")
.containsExactly(project.srcMainDirectory().toPath(), project.srcTestDirectory().toPath());
pmd.inputPaths().clear();
pmd = pmd.addInputPaths(List.of(project.srcMainDirectory().toPath(), project.srcTestDirectory().toPath()));
assertThat(pmd.inputPaths()).as("List(main, test)")
.containsExactly(project.srcMainDirectory().toPath(), project.srcTestDirectory().toPath());
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)))
.isGreaterThan(0).isEqualTo(err);
@ -124,7 +142,10 @@ class PmdOperationTest {
@Test
void testCache() throws ExitStatusException {
var cache = Path.of("build/pmd/temp-cache");
var pmd = newPmdOperation().ruleSets(CODE_STYLE_XML).inputPaths(List.of(CODE_STYLE_SAMPLE)).cache(cache);
var pmd = newPmdOperation()
.ruleSets(CODE_STYLE_XML)
.inputPaths(List.of(CODE_STYLE_SAMPLE))
.cache(cache);
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME))).isGreaterThan(0);
var f = cache.toFile();
assertThat(f.exists()).as("file exits").isTrue();
@ -213,7 +234,31 @@ class PmdOperationTest {
var pmd = newPmdOperation()
.ruleSets(PmdOperation.RULE_SET_DEFAULT, CODE_STYLE_XML)
.inputPaths(ERROR_PRONE_SAMPLE, CODE_STYLE_SAMPLE);
assertThat(pmd.inputPaths()).contains(ERROR_PRONE_SAMPLE, CODE_STYLE_SAMPLE);
assertThat(pmd.inputPaths()).as("paths").containsExactly(ERROR_PRONE_SAMPLE, CODE_STYLE_SAMPLE);
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME))).isGreaterThan(0);
pmd = newPmdOperation()
.ruleSets(PmdOperation.RULE_SET_DEFAULT, CODE_STYLE_XML)
.inputPaths(ERROR_PRONE_SAMPLE.toFile(), CODE_STYLE_SAMPLE.toFile());
assertThat(pmd.inputPaths()).as("toFile()").containsExactly(ERROR_PRONE_SAMPLE, CODE_STYLE_SAMPLE);
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME))).isGreaterThan(0);
pmd = newPmdOperation()
.ruleSets(PmdOperation.RULE_SET_DEFAULT, CODE_STYLE_XML)
.inputPaths(ERROR_PRONE_SAMPLE.toString(), CODE_STYLE_SAMPLE.toString());
assertThat(pmd.inputPaths()).as("toString").containsExactly(ERROR_PRONE_SAMPLE, CODE_STYLE_SAMPLE);
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME))).isGreaterThan(0);
pmd = newPmdOperation()
.ruleSets(PmdOperation.RULE_SET_DEFAULT, CODE_STYLE_XML)
.inputPathsFiles(List.of(ERROR_PRONE_SAMPLE.toFile(), CODE_STYLE_SAMPLE.toFile()));
assertThat(pmd.inputPaths()).as("PathsFiles").containsExactly(ERROR_PRONE_SAMPLE, CODE_STYLE_SAMPLE);
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME))).isGreaterThan(0);
pmd = newPmdOperation()
.ruleSets(PmdOperation.RULE_SET_DEFAULT, CODE_STYLE_XML)
.inputPathsStrings(List.of(ERROR_PRONE_SAMPLE.toString(), CODE_STYLE_SAMPLE.toString()));
assertThat(pmd.inputPaths()).as("PathsStrings").containsExactly(ERROR_PRONE_SAMPLE, CODE_STYLE_SAMPLE);
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME))).isGreaterThan(0);
}
@ -235,6 +280,7 @@ class PmdOperationTest {
var pmd = newPmdOperation().ruleSets(CODE_STYLE_XML).inputPaths(CODE_STYLE_SAMPLE);
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)))
.as("code style").isGreaterThan(0);
pmd = pmd.ruleSets(ERROR_PRONE_XML).inputPaths(ERROR_PRONE_SAMPLE);
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME)))
.as("code style + error prone").isGreaterThan(0);
@ -245,7 +291,7 @@ class PmdOperationTest {
var pmd = newPmdOperation()
.ruleSets(DESIGN_XML)
.inputPaths("src/test/resources/java/Design.java")
.cache(Path.of("build/pmd/design-cache"));
.cache(new File("build/pmd/design-cache"));
assertThat(pmd.performPmdAnalysis(TEST, pmd.initConfiguration(COMMAND_NAME))).isGreaterThan(0);
}
@ -336,6 +382,18 @@ class PmdOperationTest {
var config = pmd.initConfiguration(COMMAND_NAME);
assertThat(config.getRelativizeRoots()).isEqualTo(pmd.relativizeRoots());
assertThat(config.getRelativizeRoots()).containsExactly(foo, bar, baz, foo, bar, baz);
pmd = newPmdOperation().ruleSets(List.of(CATEGORY_FOO))
.relativizeRootsFiles(List.of(foo.toFile(), bar.toFile(), baz.toFile()));
config = pmd.initConfiguration(COMMAND_NAME);
assertThat(config.getRelativizeRoots()).as("toFile").isEqualTo(pmd.relativizeRoots());
assertThat(config.getRelativizeRoots()).containsExactly(foo, bar, baz);
pmd = newPmdOperation().ruleSets(List.of(CATEGORY_FOO))
.relativizeRootsStrings(List.of(foo.toString(), bar.toString(), baz.toString()));
config = pmd.initConfiguration(COMMAND_NAME);
assertThat(config.getRelativizeRoots()).as("toString").isEqualTo(pmd.relativizeRoots());
assertThat(config.getRelativizeRoots()).containsExactly(foo, bar, baz);
}
@Test