From 2b6b4a5e829a0a38d6a889eca60e8a064b337ae7 Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Tue, 27 Aug 2024 12:55:48 -0700 Subject: [PATCH] Cleaned up API to match bld operations and options APIs --- .../rife/bld/extension/PmdOperationBuild.java | 9 +- .../java/rife/bld/extension/PmdOperation.java | 143 +++++++++++++----- .../rife/bld/extension/PmdOperationTest.java | 80 ++++++++-- 3 files changed, 180 insertions(+), 52 deletions(-) diff --git a/src/bld/java/rife/bld/extension/PmdOperationBuild.java b/src/bld/java/rife/bld/extension/PmdOperationBuild.java index c6b4ca9..ed1ec9a 100644 --- a/src/bld/java/rife/bld/extension/PmdOperationBuild.java +++ b/src/bld/java/rife/bld/extension/PmdOperationBuild.java @@ -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 PmdOperationBuild() { .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() diff --git a/src/main/java/rife/bld/extension/PmdOperation.java b/src/main/java/rife/bld/extension/PmdOperation.java index 8aaea0d..ff67cae 100644 --- a/src/main/java/rife/bld/extension/PmdOperation.java +++ b/src/main/java/rife/bld/extension/PmdOperation.java @@ -142,8 +142,7 @@ public class PmdOperation extends AbstractOperation { * @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 PmdOperation addInputPaths(Path... inputPath) { * @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 PmdOperation addInputPaths(File... inputPath) { * * @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 PmdOperation addInputPaths(Collection inputPath) { 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 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 inputPath) { + return addInputPaths(inputPath.stream().map(Paths::get).toList()); + } + /** * Adds new rule set paths. *

@@ -203,8 +222,7 @@ public PmdOperation addInputPaths(Collection inputPath) { * @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 PmdOperation addRuleSet(String... ruleSet) { * * @param ruleSet a collection of rule set paths * @return this operation - * @see #ruleSets(Collection + * @see #ruleSets(Collection) */ public PmdOperation addRuleSet(Collection ruleSet) { ruleSets_.addAll(ruleSet); @@ -240,6 +258,21 @@ public PmdOperation cache(Path cache) { 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 PmdOperation cache(Path cache) { * @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 PmdOperation defaultLanguageVersions(Collection language *

The valid values are the standard character sets of {@link java.nio.charset.Charset Charset}.

*/ public PmdOperation encoding(String encoding) { - encoding_ = Charset.forName(encoding); - return this; + return encoding(Charset.forName(encoding)); } /** @@ -385,8 +416,7 @@ public PmdOperation ignoreFile(Path ignoreFile) { * @return this operation */ public PmdOperation ignoreFile(File ignoreFile) { - ignoreFile_ = ignoreFile.toPath(); - return this; + return ignoreFile(ignoreFile.toPath()); } /** @@ -396,8 +426,7 @@ public PmdOperation ignoreFile(File ignoreFile) { * @return this operation */ public PmdOperation ignoreFile(String ignoreFile) { - ignoreFile_ = Paths.get(ignoreFile); - return this; + return ignoreFile(Path.of(ignoreFile)); } /** @@ -495,9 +524,7 @@ public PMDConfiguration initConfiguration(String commandName) { * @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 PmdOperation inputPaths(Path... inputPath) { * @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 PmdOperation inputPaths(File... inputPath) { * @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 PmdOperation inputPaths(String... inputPath) { * * @param inputPath a collection of input paths * @return this operation - * @see #addInputPaths(Collection) + * @see #addInputPaths(Path...) */ public PmdOperation inputPaths(Collection inputPath) { inputPaths_.clear(); @@ -554,6 +577,32 @@ public Collection inputPaths() { return inputPaths_; } + /** + * Sets paths to source files, or directories containing source files to analyze. + *

+ * Previous entries are disregarded. + * + * @param inputPath a collection of input paths + * @return this operation + * @see #addInputPaths(File...) + */ + public PmdOperation inputPathsFiles(Collection inputPath) { + return inputPaths(inputPath.stream().map(File::toPath).toList()); + } + + /** + * Sets paths to source files, or directories containing source files to analyze. + *

+ * Previous entries are disregarded. + * + * @param inputPath a collection of input paths + * @return this operation + * @see #addInputPaths(String...) + */ + public PmdOperation inputPathsStrings(Collection inputPath) { + return inputPaths(inputPath.stream().map(Paths::get).toList()); + } + /** * Sets the default language versions. * @@ -561,8 +610,7 @@ public Collection inputPaths() { * @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 int performPmdAnalysis(String commandName, PMDConfiguration config) throw * * @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 PmdOperation relativizeRoots(Path... relativeRoot) { * * @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 PmdOperation relativizeRoots(File... relativeRoot) { * * @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 PmdOperation relativizeRoots(String... relativeRoot) { * * @param relativeRoot a collection of relative root paths * @return this operations + * @see #relativizeRoots(Path...) */ public PmdOperation relativizeRoots(Collection relativeRoot) { relativizeRoots_.addAll(relativeRoot); @@ -715,6 +764,28 @@ public Collection relativizeRoots() { 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 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 relativeRoot) { + return relativizeRoots(relativeRoot.stream().map(Paths::get).toList()); + } + /** * Sets the path to the report page. * diff --git a/src/test/java/rife/bld/extension/PmdOperationTest.java b/src/test/java/rife/bld/extension/PmdOperationTest.java index 6d042a7..d9e08fb 100644 --- a/src/test/java/rife/bld/extension/PmdOperationTest.java +++ b/src/test/java/rife/bld/extension/PmdOperationTest.java @@ -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 @@ void testAddInputPath() throws ExitStatusException { 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 @@ void testAddRuleSets() throws ExitStatusException { @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 @@ void testInputPaths() throws ExitStatusException { 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 @@ void testJavaCodeStyleAndErrorProne() throws ExitStatusException { 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 @@ void testJavaDesign() throws ExitStatusException { 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 @@ void testRelativizeRoots() { 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