From a6be88bf54f7554597d576a10bfbd4e43161fada Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Sat, 22 Jun 2024 17:28:55 -0700 Subject: [PATCH] Added includes and excludes collection Changed Collection to Collection whenever applicable --- .../rife/bld/extension/DetektOperation.java | 234 +++++++++++++++--- .../bld/extension/DetektOperationTest.java | 48 ++-- 2 files changed, 228 insertions(+), 54 deletions(-) diff --git a/src/main/java/rife/bld/extension/DetektOperation.java b/src/main/java/rife/bld/extension/DetektOperation.java index a1377ba..b1d1bf1 100644 --- a/src/main/java/rife/bld/extension/DetektOperation.java +++ b/src/main/java/rife/bld/extension/DetektOperation.java @@ -17,6 +17,8 @@ package rife.bld.extension; import rife.bld.BaseProject; +import rife.bld.extension.detekt.Report; +import rife.bld.extension.detekt.ReportId; import rife.bld.operations.AbstractProcessOperation; import rife.bld.operations.exceptions.ExitStatusException; import rife.tools.exceptions.FileUtilsErrorException; @@ -24,6 +26,7 @@ import rife.tools.exceptions.FileUtilsErrorException; import java.io.File; import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.logging.Level; @@ -53,12 +56,13 @@ public class DetektOperation extends AbstractProcessOperation { "sarif4k-jvm-", "snakeyaml-engine-", "trove4j-"); - private static final Logger LOGGER = Logger.getLogger(DetektReport.class.getName()); - private final Collection classpath_ = new ArrayList<>(); - private final Collection config_ = new ArrayList<>(); - private final Collection input_ = new ArrayList<>(); - private final Collection plugins_ = new ArrayList<>(); - private final Collection report_ = new ArrayList<>(); + private static final Logger LOGGER = Logger.getLogger(Report.class.getName()); + private final Collection classpath_ = new ArrayList<>(); + private final Collection config_ = new ArrayList<>(); + private final Collection excludes_ = new ArrayList<>(); + private final Collection input_ = new ArrayList<>(); + private final Collection plugins_ = new ArrayList<>(); + private final Collection report_ = new ArrayList<>(); private boolean allRules_; private boolean autoCorrect_; private String basePath_; @@ -68,9 +72,8 @@ public class DetektOperation extends AbstractProcessOperation { private boolean createBaseline_; private boolean debug_; private boolean disableDefaultRuleSets_; - private String excludes_; private boolean generateConfig_; - private String includes_; + private final Collection includes_ = new ArrayList<>(); private String jdkHome_; private String jvmTarget_; private String languageVersion_; @@ -117,6 +120,19 @@ public class DetektOperation extends AbstractProcessOperation { return this; } + /** + * Specifies a directory as the base path. Currently, it impacts all file + * paths in the formatted reports. File paths in console output and txt + * report are not affected and remain as absolute paths. + * + * @param path the directory path + * @return this operation instance + */ + public DetektOperation basePath(File path) { + basePath_ = path.getAbsolutePath(); + return this; + } + /** * If a baseline xml file is passed in, only new code smells not in the * baseline are printed in the console. @@ -129,6 +145,18 @@ public class DetektOperation extends AbstractProcessOperation { return this; } + /** + * If a baseline xml file is passed in, only new code smells not in the + * baseline are printed in the console. + * + * @param baseline the baseline xml file + * @return this operation instance + */ + public DetektOperation baseline(File baseline) { + baseline_ = baseline.getAbsolutePath(); + return this; + } + /** * Preconfigures detekt with a bunch of rules and some opinionated defaults * for you. Allows additional provided configurations to override the @@ -143,51 +171,93 @@ public class DetektOperation extends AbstractProcessOperation { } /** - * EXPERIMENTAL: Paths where to find user class files and depending jar files. + * EXPERIMENTAL: Paths where to find user class files and jar dependencies. + * Used for type resolution. + * + * @param paths one or more files + * @return this operation instance + */ + public DetektOperation classPath(File... paths) { + classpath_.addAll(List.of(paths)); + return this; + } + + /** + * EXPERIMENTAL: Paths where to find user class files and jar dependencies. * Used for type resolution. * * @param paths one or more files * @return this operation instance */ public DetektOperation classPath(String... paths) { - classpath_.addAll(List.of(paths)); + classpath_.addAll(Arrays.stream(paths).map(File::new).toList()); return this; } + /** - * EXPERIMENTAL: Paths where to find user class files and depending jar files. + * EXPERIMENTAL: Paths where to find user class files and jar dependencies. * Used for type resolution. * - * @param paths the list of files + * @param paths the paths * @return this operation instance */ - public DetektOperation classPath(Collection paths) { + public DetektOperation classPath(Collection paths) { classpath_.addAll(paths); return this; } /** - * Path to the config file ({@code path/to/config.yml}). + * Paths where to find user class files and jar dependencies. + * + * @return the paths + */ + public Collection classPath() { + return classpath_; + } + + /** + * Paths to the config files ({@code path/to/config.yml}). * * @param configs one or more config files * @return this operation instance */ - public DetektOperation config(String... configs) { + public DetektOperation config(File... configs) { config_.addAll(List.of(configs)); return this; } /** - * Path to the config file ({@code path/to/config.yml}). + * Paths to the config files ({@code path/to/config.yml}). * - * @param configs the list pf config files + * @param configs one or more config files * @return this operation instance */ - public DetektOperation config(Collection configs) { + public DetektOperation config(String... configs) { + config_.addAll(Arrays.stream(configs).map(File::new).toList()); + return this; + } + + /** + * Paths to the config files ({@code path/to/config.yml}). + * + * @param configs the config files + * @return this operation instance + */ + public DetektOperation config(Collection configs) { config_.addAll(configs); return this; } + /** + * Paths to config files. + * + * @return the config files paths. + */ + public Collection config() { + return config_; + } + /** * Path to the config resource on detekt's classpath ({@code path/to/config.yml}). * @@ -199,6 +269,17 @@ public class DetektOperation extends AbstractProcessOperation { return this; } + /** + * Path to the config resource on detekt's classpath ({@code path/to/config.yml}). + * + * @param resource the config resource path + * @return this operation instance + */ + public DetektOperation configResource(File resource) { + configResource_ = resource.getAbsolutePath(); + return this; + } + /** * Treats current analysis findings as a smell baseline for future detekt * runs. @@ -236,14 +317,34 @@ public class DetektOperation extends AbstractProcessOperation { /** * Globbing patterns describing paths to exclude from the analysis. * - * @param patterns the patterns + * @param patterns one or more pattern * @return this operation instance */ - public DetektOperation excludes(String patterns) { - excludes_ = patterns; + public DetektOperation excludes(String... patterns) { + excludes_.addAll(List.of(patterns)); return this; } + /** + * Globbing patterns describing paths to exclude from the analysis. + * + * @param patterns a collection of patterns + * @return this operation instance + */ + public DetektOperation excludes(Collection patterns) { + excludes_.addAll(patterns); + return this; + } + + /** + * Returns the globbing patterns describing paths to exclude from the analysis. + * + * @return the globbing patterns + */ + public Collection excludes() { + return excludes_; + } + /** * Performs the operation. * @@ -311,13 +412,13 @@ public class DetektOperation extends AbstractProcessOperation { // classpath if (!classpath_.isEmpty()) { args.add("--classpath"); - args.add(String.join(File.pathSeparator, classpath_.stream().filter(this::isNotBlank).toList())); + args.add(String.join(File.pathSeparator, classpath_.stream().map(File::getAbsolutePath).toList())); } // config if (!config_.isEmpty()) { args.add("-config"); - args.add(String.join(";", config_.stream().filter(this::isNotBlank).toList())); + args.add(String.join(";", config_.stream().map(File::getAbsolutePath).toList())); } // config-resource @@ -342,9 +443,9 @@ public class DetektOperation extends AbstractProcessOperation { } // excludes - if (isNotBlank(excludes_)) { + if (!excludes_.isEmpty()) { args.add("--excludes"); - args.add(excludes_); + args.add(String.join(",", excludes_)); } // generate-config @@ -353,15 +454,15 @@ public class DetektOperation extends AbstractProcessOperation { } // includes - if (isNotBlank(includes_)) { + if (!includes_.isEmpty()) { args.add("--includes"); - args.add(includes_); + args.add(String.join(",", includes_)); } // input if (!input_.isEmpty()) { args.add("--input"); - args.add(String.join(",", input_.stream().filter(this::isNotBlank).toList())); + args.add(String.join(",", input_.stream().map(File::getAbsolutePath).toList())); } // jdk-home @@ -396,7 +497,7 @@ public class DetektOperation extends AbstractProcessOperation { // plugins if (!plugins_.isEmpty()) { args.add("--plugins"); - args.add(String.join(",", plugins_.stream().filter(this::isNotBlank).toList())); + args.add(String.join(",", plugins_.stream().map(File::getAbsolutePath).toList())); } // report @@ -433,7 +534,7 @@ public class DetektOperation extends AbstractProcessOperation { if (baseline.exists()) { baseline_ = baseline.getAbsolutePath(); } - excludes(".*/build/.*,.*/resources/.*"); + excludes(".*/build/.*", ".*/resources/.*"); return this; } @@ -476,23 +577,44 @@ public class DetektOperation extends AbstractProcessOperation { /** * Globbing patterns describing paths to include in the analysis. Useful in - * combination with {@link #excludes(String) excludes} patterns. + * combination with {@link #excludes() excludes} patterns. * - * @param patterns the patterns + * @param patterns one or more patterns * @return this operation instance */ - public DetektOperation includes(String patterns) { - includes_ = patterns; + public DetektOperation includes(String... patterns) { + includes_.addAll(List.of(patterns)); return this; } + /** + * Globbing patterns describing paths to include in the analysis. Useful in + * combination with {@link #excludes() excludes} patterns. + * + * @param patterns a collection of patterns + * @return this operation instance + */ + public DetektOperation includes(Collection patterns) { + includes_.addAll(patterns); + return this; + } + + /** + * Returns the globbing patterns describing paths to include in the analysis. + * + * @return the globbing patterns + */ + public Collection includes() { + return includes_; + } + /** * Input paths to analyze. If not specified the current working directory is used. * - * @param paths the list of paths + * @param paths the paths * @return this operation instance */ - public DetektOperation input(Collection paths) { + public DetektOperation input(Collection paths) { input_.addAll(paths); return this; } @@ -504,16 +626,28 @@ public class DetektOperation extends AbstractProcessOperation { * @return this operation instance */ public DetektOperation input(String... paths) { + input_.addAll(Arrays.stream(paths).map(File::new).toList()); + return this; + } + + /** + * Input paths to analyze. If not specified the current working directory is used. + * + * @param paths one or more paths + * @return this operation instance + */ + public DetektOperation input(File... paths) { input_.addAll(List.of(paths)); return this; } + /** * Returns the input paths to analyze. * * @return the input paths */ - public Collection input() { + public Collection input() { return input_; } @@ -596,6 +730,17 @@ public class DetektOperation extends AbstractProcessOperation { * @return this operation instance */ public DetektOperation plugins(String... jars) { + plugins_.addAll(Arrays.stream(jars).map(File::new).toList()); + return this; + } + + /** + * Extra paths to plugin jars. + * + * @param jars one or more jars + * @return this operation instance + */ + public DetektOperation plugins(File... jars) { plugins_.addAll(List.of(jars)); return this; } @@ -603,16 +748,25 @@ public class DetektOperation extends AbstractProcessOperation { /** * Extra paths to plugin jars. * - * @param jars the list of jars + * @param jars the jars paths * @return this operation instance */ - public DetektOperation plugins(Collection jars) { + public DetektOperation plugins(Collection jars) { plugins_.addAll(jars); return this; } /** - * Generates a report for given {@link DetektReportId report-id} and stores it on given 'path'. + * Extra path to plugins jars. + * + * @return the jars paths + */ + public Collection plugins() { + return plugins_; + } + + /** + * Generates a report for given {@link ReportId report-id} and stores it on given 'path'. * * @param reports one or more reports * @return this operation instance diff --git a/src/test/java/rife/bld/extension/DetektOperationTest.java b/src/test/java/rife/bld/extension/DetektOperationTest.java index c480af9..760d824 100644 --- a/src/test/java/rife/bld/extension/DetektOperationTest.java +++ b/src/test/java/rife/bld/extension/DetektOperationTest.java @@ -36,7 +36,6 @@ import java.util.logging.Logger; import static org.assertj.core.api.Assertions.*; - @SuppressWarnings("PMD.AvoidDuplicateLiterals") class DetektOperationTest { @BeforeAll @@ -67,34 +66,55 @@ class DetektOperationTest { assertThat(args).isNotEmpty(); - var params = new DetektOperation() + var op = new DetektOperation() .fromProject(new BaseProject()) .allRules(true) .autoCorrect(true) .basePath("basePath") + .basePath(new File("basePath")) .baseline("baseline") .buildUponDefaultConfig(true) - .classPath("classpath") - .classPath(List.of("path2", "path3")) - .config("config") - .config(List.of("config2", "config4")) + .classPath(new File("path1")) + .classPath("path2", "path3") + .classPath(List.of(new File("path4"), new File("path5"))) + .config(new File("config1")) + .config("config2", "config3") + .config(List.of(new File("config4"), new File("config5"))) .configResource("configResource") + .configResource(new File("configResource")) .createBaseline(true) .debug(true) .disableDefaultRuleSets(true) - .excludes("excludes") + .excludes(List.of("excludes1", "excludes2")) + .excludes("excludes3", "excludes4") .generateConfig(true) - .includes("patterns") - .input("input") + .includes(List.of("includes1", "includes2")) + .includes("includes3", "includes4", "includes5") + .input(new File("input1")) + .input("input2", "input3") + .input(List.of(new File("input4"), new File("input5"))) .jdkHome("jdkHome") .jvmTarget("jvmTarget") .languageVersion("languageVersion") .maxIssues(10) .parallel(true) - .plugins("jars") - .plugins(List.of("jars2", "jar3")) - .report(new DetektReport(DetektReportId.HTML, "reports")) - .executeConstructProcessCommandList(); + .plugins(new File("jar1")) + .plugins("jar2", "jar3") + .plugins(List.of(new File("jar4"), new File("jar5"))) + .report(new Report(ReportId.HTML, "reports")); + + assertThat(op.excludes()).as("excludes[]").containsExactly(".*/build/.*", ".*/resources/.*", + "excludes1", "excludes2", "excludes3", "excludes4"); + + for (var i = 1; i < 6; i++) { + assertThat(op.classPath()).as("classPath[" + i + ']').hasSize(5).contains(new File("path" + i)); + assertThat(op.config()).as("config[" + i + ']').hasSize(5).contains(new File("config" + i)); + assertThat(op.includes()).as("includes[" + i + ']').hasSize(5).contains("includes" + i); + assertThat(op.input()).as("input[" + i + ']').hasSize(5).contains(new File("input" + i)); + assertThat(op.plugins()).as("plugins[" + i + ']').hasSize(5).contains(new File("jar" + i)); + } + + var params = op.executeConstructProcessCommandList(); for (var p : args) { var found = false; @@ -117,7 +137,7 @@ class DetektOperationTest { var op = new DetektOperation() .fromProject(new BaseProjectBlueprint(new File("examples"), "com.example", "Example")) - .baseline(baseline.getAbsolutePath()) + .baseline(baseline) .createBaseline(true); op.execute();