From af25b2f5073dd22dc99cf7d0901ea0920dac27c3 Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Sat, 1 Feb 2025 12:47:55 -0800 Subject: [PATCH] BREAKING: reworked excludes methods and added addExcludes methods --- .../java/rife/bld/extension/PmdOperation.java | 140 +++++++++++++++++- .../rife/bld/extension/PmdOperationTest.java | 113 +++++++++++--- 2 files changed, 226 insertions(+), 27 deletions(-) diff --git a/src/main/java/rife/bld/extension/PmdOperation.java b/src/main/java/rife/bld/extension/PmdOperation.java index aa0a61d..25b25c6 100644 --- a/src/main/java/rife/bld/extension/PmdOperation.java +++ b/src/main/java/rife/bld/extension/PmdOperation.java @@ -52,7 +52,7 @@ public class PmdOperation extends AbstractOperation { /** * The list of paths to exclude. */ - private final List excludes_ = new ArrayList<>(); + private final Collection excludes_ = new ArrayList<>(); /** * The input paths (source) list. */ @@ -142,6 +142,79 @@ public class PmdOperation extends AbstractOperation { */ private int threads_ = 1; + /** + * Adds paths to exclude from the analysis. + * + * @param excludes one or more paths to exclude + * @return this operation + * @see #excludes(Path...) + * @since 1.2.0 + */ + public PmdOperation addExcludes(Path... excludes) { + return addExcludes(List.of(excludes)); + } + + /** + * Adds paths to exclude from the analysis. + * + * @param excludes paths to exclude + * @return this operation + * @see #excludes(Collection) + * @since 1.2.0 + */ + public PmdOperation addExcludes(Collection excludes) { + excludes_.addAll(excludes); + return this; + } + + /** + * Adds paths to exclude from the analysis. + * + * @param excludes one or more paths to exclude + * @return this operation + * @see #excludesFiles(Collection) + * @since 1.2.0 + */ + public PmdOperation addExcludesFiles(Collection excludes) { + return addExcludes(excludes.stream().map(File::toPath).toList()); + } + + /** + * Adds paths to exclude from the analysis. + * + * @param excludes one or more paths to exclude + * @return this operation + * @see #excludesFiles(File...) + * @since 1.2.0 + */ + public PmdOperation addExcludesFiles(File... excludes) { + return addExcludesFiles(List.of(excludes)); + } + + /** + * Adds paths to exclude from the analysis. + * + * @param excludes one or more paths to exclude + * @return this operation + * @see #excludesStrings(Collection) + * @since 1.2.0 + */ + public PmdOperation addExcludesStrings(Collection excludes) { + return addExcludes(excludes.stream().map(Paths::get).toList()); + } + + /** + * Adds paths to exclude from the analysis. + * + * @param excludes one or more paths to exclude + * @return this operation + * @see #excludesStrings(String...) + * @since 1.2.0 + */ + public PmdOperation addExcludesStrings(String... excludes) { + return addExcludesStrings(List.of(excludes)); + } + /** * Adds paths to source files, or directories containing source files to analyze.\ * @@ -322,23 +395,26 @@ public class PmdOperation extends AbstractOperation { } /** - * Adds paths to exclude from the analysis. + * Sets paths to exclude from the analysis. * * @param excludes one or more paths to exclude * @return this operation + * @see #addExcludes(Path...) */ public PmdOperation excludes(Path... excludes) { - excludes_.addAll(List.of(excludes)); + excludes(List.of(excludes)); return this; } /** - * Adds paths to exclude from the analysis. + * Sets paths to exclude from the analysis. * * @param excludes paths to exclude * @return this operation + * @see #addExcludes(Collection) */ public PmdOperation excludes(Collection excludes) { + excludes_.clear(); excludes_.addAll(excludes); return this; } @@ -348,10 +424,60 @@ public class PmdOperation extends AbstractOperation { * * @return the exclude paths */ - public List excludes() { + public Collection excludes() { return excludes_; } + /** + * Sets paths to exclude from the analysis. + * + * @param excludes one or more paths to exclude + * @return this operation + * @see #excludesFiles(Collection) + * @since 1.2.0 + */ + public PmdOperation excludesFiles(Collection excludes) { + excludes(excludes.stream().map(File::toPath).toList()); + return this; + } + + /** + * Sets paths to exclude from the analysis. + * + * @param excludes one or more paths to exclude + * @return this operation + * @see #excludesFiles(File...) + * @since 1.2.0 + */ + public PmdOperation excludesFiles(File... excludes) { + return excludesFiles(List.of(excludes)); + } + + /** + * Sets paths to exclude from the analysis. + * + * @param excludes one or more paths to exclude + * @return this operation + * @see #excludesStrings(Collection) + * @since 1.2.0 + */ + public PmdOperation excludesStrings(Collection excludes) { + excludes(excludes.stream().map(Paths::get).toList()); + return this; + } + + /** + * Sets paths to exclude from the analysis. + * + * @param excludes one or more paths to exclude + * @return this operation + * @see #excludesStrings(String...) + * @since 1.2.0 + */ + public PmdOperation excludesStrings(String... excludes) { + return excludesStrings(List.of(excludes)); + } + /** * Performs the PMD code analysis operation. */ @@ -519,7 +645,7 @@ public class PmdOperation extends AbstractOperation { // setExcludes if (!excludes_.isEmpty()) { - config.setExcludes(excludes_); + config.setExcludes(excludes_.stream().toList()); } // setFailOnError @@ -656,7 +782,7 @@ public class PmdOperation extends AbstractOperation { * * @param inputPath a collection of input paths * @return this operation - * @see #addInputPathsFiles(Collection) ) + * @see #addInputPathsFiles(Collection) */ public PmdOperation inputPathsFiles(Collection inputPath) { return inputPaths(inputPath.stream().map(File::toPath).toList()); diff --git a/src/test/java/rife/bld/extension/PmdOperationTest.java b/src/test/java/rife/bld/extension/PmdOperationTest.java index 48545e5..77321d6 100644 --- a/src/test/java/rife/bld/extension/PmdOperationTest.java +++ b/src/test/java/rife/bld/extension/PmdOperationTest.java @@ -47,6 +47,7 @@ import static org.assertj.core.api.Assertions.assertThatCode; * @since 1.0 */ class PmdOperationTest { + static final String BAR = "bar"; static final String CATEGORY_FOO = "category/foo.xml"; static final Path CODE_STYLE_SAMPLE = Path.of("src/test/resources/java/CodeStyle.java"); static final String CODE_STYLE_XML = "category/java/codestyle.xml"; @@ -55,6 +56,11 @@ class PmdOperationTest { static final String DOCUMENTATION_XML = "category/java/documentation.xml"; static final Path ERROR_PRONE_SAMPLE = Path.of("src/test/resources/java/ErrorProne.java"); static final String ERROR_PRONE_XML = "category/java/errorprone.xml"; + static final File FILE_BAR = new File(BAR); + static final String FOO = "foo"; + static final File FILE_FOO = new File(FOO); + static final Path PATH_BAR = Path.of(BAR); + static final Path PATH_FOO = Path.of(FOO); static final String PERFORMANCE_XML = "category/java/performance.xml"; static final String SECURITY_XML = "category/java/security.xml"; static final String TEST = "test"; @@ -67,6 +73,39 @@ class PmdOperationTest { .reportFile(Paths.get("build", COMMAND_NAME, "pmd-test-report.txt")); } + @Test + void testAddExcludes() { + var pmd = newPmdOperation().ruleSets(CATEGORY_FOO).addExcludes(PATH_FOO); + var config = pmd.initConfiguration(COMMAND_NAME); + assertThat(config.getExcludes()).containsExactly(PATH_FOO); + + pmd = pmd.addExcludes(PATH_BAR); + config = pmd.initConfiguration(COMMAND_NAME); + assertThat(config.getExcludes()).containsExactly(PATH_FOO, PATH_BAR); + } + + @Test + void testAddExcludesFiles() { + var pmd = newPmdOperation().ruleSets(CATEGORY_FOO).addExcludesFiles(FILE_FOO); + var config = pmd.initConfiguration(COMMAND_NAME); + assertThat(config.getExcludes()).containsExactly(FILE_FOO.toPath()); + + pmd = pmd.addExcludesFiles(FILE_BAR); + config = pmd.initConfiguration(COMMAND_NAME); + assertThat(config.getExcludes()).containsExactly(FILE_FOO.toPath(), FILE_BAR.toPath()); + } + + @Test + void testAddExcludesStrings() { + var pmd = newPmdOperation().ruleSets(CATEGORY_FOO).addExcludesStrings(FOO); + var config = pmd.initConfiguration(COMMAND_NAME); + assertThat(config.getExcludes()).containsExactly(PATH_FOO); + + pmd = pmd.addExcludesStrings(BAR); + config = pmd.initConfiguration(COMMAND_NAME); + assertThat(config.getExcludes()).containsExactly(PATH_FOO, PATH_BAR); + } + @Test void testAddInputPaths() throws ExitStatusException { var project = new BaseProject(); @@ -168,20 +207,55 @@ class PmdOperationTest { @Test void testExcludes() { - var foo = Path.of("foo/bar"); - var bar = Path.of("bar/foo"); var foz = Path.of("foz/baz"); var baz = Path.of("baz/foz"); - var excludes = List.of(foo, bar); - var pmd = newPmdOperation().ruleSets(CATEGORY_FOO).excludes(excludes); + var pmd = newPmdOperation().ruleSets(CATEGORY_FOO).excludes(PATH_FOO, PATH_BAR); var config = pmd.initConfiguration(COMMAND_NAME); - assertThat(config.getExcludes()).containsExactly(excludes.toArray(new Path[0])); + assertThat(pmd.excludes()).containsExactly(List.of(PATH_FOO, PATH_BAR).toArray(new Path[0])); + assertThat(config.getExcludes()).containsExactly(List.of(PATH_FOO, PATH_BAR).toArray(new Path[0])); - pmd = pmd.excludes(baz, foz); - assertThat(pmd.excludes()).hasSize(4); - config = pmd.initConfiguration(COMMAND_NAME); - assertThat(config.getExcludes()).hasSize(4).contains(bar, foz); + var excludes = List.of(List.of(PATH_FOO, PATH_BAR), List.of(foz, baz)); + for (var exclude : excludes) { + pmd = newPmdOperation().ruleSets(CATEGORY_FOO).excludes(exclude); + config = pmd.initConfiguration(COMMAND_NAME); + assertThat(config.getExcludes()).containsExactly(exclude.toArray(new Path[0])); + } + } + + @Test + void testExcludesFiles() { + var foz = new File("foz"); + var baz = new File("baz"); + + var pmd = newPmdOperation().ruleSets(CATEGORY_FOO).excludesFiles(FILE_FOO, FILE_BAR); + var config = pmd.initConfiguration(COMMAND_NAME); + assertThat(config.getExcludes()).containsExactly(FILE_FOO.toPath(), FILE_BAR.toPath()); + + var excludes = List.of(List.of(FILE_FOO, FILE_BAR), List.of(foz, baz)); + for (var exclude : excludes) { + pmd = newPmdOperation().ruleSets(CATEGORY_FOO).excludesFiles(exclude); + config = pmd.initConfiguration(COMMAND_NAME); + assertThat(config.getExcludes()).containsExactly(exclude.stream().map(File::toPath).toArray(Path[]::new)); + } + } + + @Test + void testExcludesStrings() { + var foz = "foz"; + var baz = "baz"; + + var pmd = newPmdOperation().ruleSets(CATEGORY_FOO).excludesStrings(FOO, BAR); + var config = pmd.initConfiguration(COMMAND_NAME); + assertThat(pmd.excludes()).containsExactly(PATH_FOO, PATH_BAR); + assertThat(config.getExcludes()).containsExactly(PATH_FOO, PATH_BAR); + + var excludes = List.of(List.of(FOO, BAR), List.of(foz, baz)); + for (var exclude : excludes) { + pmd = newPmdOperation().ruleSets(CATEGORY_FOO).excludesStrings(exclude); + config = pmd.initConfiguration(COMMAND_NAME); + assertThat(config.getExcludes()).containsExactly(exclude.stream().map(Paths::get).toArray(Path[]::new)); + } } @Test @@ -388,8 +462,8 @@ class PmdOperationTest { @Test void testPrependAuxClasspath() { - var pmd = newPmdOperation().ruleSets(CATEGORY_FOO).prependAuxClasspath("foo", "bar"); - assertThat(pmd.prependAuxClasspath()).isEqualTo("foo" + File.pathSeparator + "bar"); + var pmd = newPmdOperation().ruleSets(CATEGORY_FOO).prependAuxClasspath(FOO, BAR); + assertThat(pmd.prependAuxClasspath()).isEqualTo(FOO + File.pathSeparator + BAR); } @Test @@ -400,27 +474,26 @@ class PmdOperationTest { @Test void testRelativizeRoots() { - var foo = Path.of("foo/bar"); - var bar = Path.of("bar/foo"); var baz = Path.of("baz/foz"); - var pmd = newPmdOperation().ruleSets(List.of(CATEGORY_FOO)).relativizeRoots(foo).relativizeRoots(bar.toFile()) - .relativizeRoots(baz.toString()).relativizeRoots(List.of(foo, bar, baz)); + var pmd = newPmdOperation().ruleSets(List.of(CATEGORY_FOO)).relativizeRoots(PATH_FOO). + relativizeRoots(PATH_BAR.toFile()).relativizeRoots(baz.toString()) + .relativizeRoots(List.of(PATH_FOO, PATH_BAR, baz)); var config = pmd.initConfiguration(COMMAND_NAME); assertThat(config.getRelativizeRoots()).isEqualTo(pmd.relativizeRoots()) - .containsExactly(foo, bar, baz, foo, bar, baz); + .containsExactly(PATH_FOO, PATH_BAR, baz, PATH_FOO, PATH_BAR, baz); pmd = newPmdOperation().ruleSets(List.of(CATEGORY_FOO)) - .relativizeRootsFiles(List.of(foo.toFile(), bar.toFile(), baz.toFile())); + .relativizeRootsFiles(List.of(PATH_FOO.toFile(), PATH_BAR.toFile(), baz.toFile())); config = pmd.initConfiguration(COMMAND_NAME); assertThat(config.getRelativizeRoots()).as("List(File...)").isEqualTo(pmd.relativizeRoots()) - .containsExactly(foo, bar, baz); + .containsExactly(PATH_FOO, PATH_BAR, baz); pmd = newPmdOperation().ruleSets(List.of(CATEGORY_FOO)) - .relativizeRootsStrings(List.of(foo.toString(), bar.toString(), baz.toString())); + .relativizeRootsStrings(List.of(PATH_FOO.toString(), PATH_BAR.toString(), baz.toString())); config = pmd.initConfiguration(COMMAND_NAME); assertThat(config.getRelativizeRoots()).as("List(String....)").isEqualTo(pmd.relativizeRoots()) - .containsExactly(foo, bar, baz); + .containsExactly(PATH_FOO, PATH_BAR, baz); } @Test