Upgrade to Deteket 1.23.4

This commit is contained in:
Erik C. Thauvin 2023-11-26 16:41:33 -08:00
parent 133d996029
commit 4382b83b8c
5 changed files with 129 additions and 59 deletions

1
.idea/misc.xml generated
View file

@ -1,4 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="EntryPointsManager"> <component name="EntryPointsManager">
<pattern value="rife.bld.extension.DetektOperationBuild" method="pmd" /> <pattern value="rife.bld.extension.DetektOperationBuild" method="pmd" />

View file

@ -29,8 +29,12 @@ public class ExampleBuild extends Project {
autoDownloadPurge = true; autoDownloadPurge = true;
repositories = List.of(MAVEN_LOCAL, MAVEN_CENTRAL, RIFE2_RELEASES); repositories = List.of(MAVEN_LOCAL, MAVEN_CENTRAL, RIFE2_RELEASES);
final var kotlin = version(1, 9, 21);
scope(compile) scope(compile)
.include(dependency("org.jetbrains.kotlin", "kotlin-stdlib", version(1, 9, 21))); .include(dependency("org.jetbrains.kotlin", "kotlin-stdlib", kotlin))
.include(dependency("org.jetbrains.kotlin", "kotlin-stdlib-jdk7", kotlin))
.include(dependency("org.jetbrains.kotlin", "kotlin-stdlib-jdk8", kotlin));
scope(test) scope(test)
.include(dependency("org.jetbrains.kotlin:kotlin-test-junit5:1.9.21")) .include(dependency("org.jetbrains.kotlin:kotlin-test-junit5:1.9.21"))
.include(dependency("org.junit.jupiter", "junit-jupiter", version(5, 10, 1))) .include(dependency("org.junit.jupiter", "junit-jupiter", version(5, 10, 1)))
@ -64,6 +68,7 @@ public class ExampleBuild extends Project {
@BuildCommand(summary = "Checks source with Detekt") @BuildCommand(summary = "Checks source with Detekt")
public void detekt() throws ExitStatusException, IOException, InterruptedException { public void detekt() throws ExitStatusException, IOException, InterruptedException {
// The source code located in the project's root will be checked
new DetektOperation() new DetektOperation()
.fromProject(this) .fromProject(this)
.execute(); .execute();
@ -71,6 +76,7 @@ public class ExampleBuild extends Project {
@BuildCommand(value = "detekt-baseline", summary = "Creates the Detekt baseline") @BuildCommand(value = "detekt-baseline", summary = "Creates the Detekt baseline")
public void detektBaseline() throws ExitStatusException, IOException, InterruptedException { public void detektBaseline() throws ExitStatusException, IOException, InterruptedException {
// The detekt-baseline.xml file will be created in the project's root
new DetektOperation() new DetektOperation()
.fromProject(this) .fromProject(this)
.baseline("detekt-baseline.xml") .baseline("detekt-baseline.xml")
@ -80,15 +86,19 @@ public class ExampleBuild extends Project {
@BuildCommand(value = "detekt-main", summary = "Checks main source with Detekt") @BuildCommand(value = "detekt-main", summary = "Checks main source with Detekt")
public void detektMain() throws ExitStatusException, IOException, InterruptedException { public void detektMain() throws ExitStatusException, IOException, InterruptedException {
var op = new DetektOperation().fromProject(this); // The source code located in src/main/kotlin will be checked
op.input().clear(); new DetektOperation()
op.input("src/main/kotlin").execute(); .fromProject(this)
.input("src/main/kotlin")
.execute();
} }
@BuildCommand(value = "detekt-test", summary = "Checks test source with Detekt") @BuildCommand(value = "detekt-test", summary = "Checks test source with Detekt")
public void detektTest() throws ExitStatusException, IOException, InterruptedException { public void detektTest() throws ExitStatusException, IOException, InterruptedException {
var op = new DetektOperation().fromProject(this); // The source code located in src/test/kotlin will be checked
op.input().clear(); new DetektOperation()
op.input("src/test/kotlin").execute(); .fromProject(this)
.input("src/test/kotlin")
.execute();
} }
} }

View file

@ -41,15 +41,10 @@ public class DetektOperationBuild extends Project {
autoDownloadPurge = true; autoDownloadPurge = true;
repositories = List.of(MAVEN_CENTRAL, RIFE2_RELEASES); repositories = List.of(MAVEN_CENTRAL, RIFE2_RELEASES);
var detekt = version(1, 23, 3);
scope(compile) scope(compile)
.include(dependency("com.uwyn.rife2", "bld", version(1, 7, 5))) .include(dependency("com.uwyn.rife2", "bld", version(1, 7, 5)))
.include(dependency("io.gitlab.arturbosch.detekt", "detekt-cli", detekt)) .include(dependency("io.gitlab.arturbosch.detekt", "detekt-cli", version(1, 23, 4)));
.include(dependency("io.gitlab.arturbosch.detekt", "detekt-tooling", detekt))
.include(dependency("com.beust", "jcommander", "1.82"))
.include(dependency("com.fasterxml:aalto-xml:1.3.2"));
scope(test) scope(test)
.include(dependency("com.puppycrawl.tools", "checkstyle", version(10, 12, 5)))
.include(dependency("org.junit.jupiter", "junit-jupiter", version(5, 10, 1))) .include(dependency("org.junit.jupiter", "junit-jupiter", version(5, 10, 1)))
.include(dependency("org.junit.platform", "junit-platform-console-standalone", version(1, 10, 1))) .include(dependency("org.junit.platform", "junit-platform-console-standalone", version(1, 10, 1)))
.include(dependency("org.assertj", "assertj-core", version(3, 24, 2))); .include(dependency("org.assertj", "assertj-core", version(3, 24, 2)));

View file

@ -20,7 +20,6 @@ import rife.bld.BaseProject;
import rife.bld.operations.AbstractProcessOperation; import rife.bld.operations.AbstractProcessOperation;
import java.io.File; import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
@ -34,6 +33,46 @@ import java.util.logging.Logger;
* @since 1.0 * @since 1.0
*/ */
public class DetektOperation extends AbstractProcessOperation<DetektOperation> { public class DetektOperation extends AbstractProcessOperation<DetektOperation> {
private static final List<String> DETEKT_JARS = List.of(
"detekt-cli-",
"jcommander-",
"detekt-core-",
"detekt-rules-",
"detekt-rules-errorprone-",
"detekt-tooling-",
"detekt-parser-",
"detekt-report-md-",
"detekt-metrics-",
"detekt-api-",
"detekt-psi-utils-",
"kotlin-compiler-embeddable-",
"kotlin-reflect-",
"detekt-report-html-",
"detekt-report-txt-",
"detekt-report-xml-",
"detekt-report-sarif-",
"detekt-utils-",
"detekt-rules-complexity-",
"detekt-rules-coroutines-",
"detekt-rules-documentation-",
"detekt-rules-empty-",
"detekt-rules-exceptions-",
"detekt-rules-naming-",
"detekt-rules-performance-",
"detekt-rules-style-",
"sarif4k-jvm-",
"kotlinx-serialization-json-jvm-",
"kotlinx-serialization-core-jvm-",
"kotlin-stdlib-jdk8-",
"kotlin-stdlib-jdk7-",
"kotlin-stdlib-",
"contester-breakpoint-",
"kotlin-script-runtime-",
"kotlin-daemon-embeddable-",
"trove4j-",
"annotations-",
"snakeyaml-engine-",
"kotlinx-html-jvm-");
private static final Logger LOGGER = Logger.getLogger(DetektReport.class.getName()); private static final Logger LOGGER = Logger.getLogger(DetektReport.class.getName());
private final Collection<String> classpath_ = new ArrayList<>(); private final Collection<String> classpath_ = new ArrayList<>();
private final Collection<String> config_ = new ArrayList<>(); private final Collection<String> config_ = new ArrayList<>();
@ -73,8 +112,8 @@ public class DetektOperation extends AbstractProcessOperation<DetektOperation> {
/** /**
* Allow rules to autocorrect code if they support it. The default rule * Allow rules to autocorrect code if they support it. The default rule
* sets do NOT support autocorrecting and won't change any line in the * sets do NOT support autocorrecting and won't change any line in the
* users code base. However custom rules can be written to support auto * users code base. However, custom rules can be written to support
* correcting. The additional 'formatting' rule set, added with * autocorrecting. The additional 'formatting' rule set, added with
* {@link #plugins(String...) Plugins}, does support it and needs this flag. * {@link #plugins(String...) Plugins}, does support it and needs this flag.
* *
* @param autoCorrect {@code true} or {@code false} * @param autoCorrect {@code true} or {@code false}
@ -86,7 +125,7 @@ public class DetektOperation extends AbstractProcessOperation<DetektOperation> {
} }
/** /**
* Specifies a directory as the base path. Currently it impacts all file * Specifies a directory as the base path. Currently, it impacts all file
* paths in the formatted reports. File paths in console output and txt * paths in the formatted reports. File paths in console output and txt
* report are not affected and remain as absolute paths. * report are not affected and remain as absolute paths.
* *
@ -124,8 +163,8 @@ public class DetektOperation extends AbstractProcessOperation<DetektOperation> {
} }
/** /**
* EXPERIMENTAL: Paths where to find user class files and depending jar * EXPERIMENTAL: Paths where to find user class files and depending jar files.
* files. Used for type resolution. * Used for type resolution.
* *
* @param paths one or more files * @param paths one or more files
* @return this operation instance * @return this operation instance
@ -136,8 +175,8 @@ public class DetektOperation extends AbstractProcessOperation<DetektOperation> {
} }
/** /**
* EXPERIMENTAL: Paths where to find user class files and depending jar * EXPERIMENTAL: Paths where to find user class files and depending jar files.
* files. Used for type resolution. * Used for type resolution.
* *
* @param paths the list of files * @param paths the list of files
* @return this operation instance * @return this operation instance
@ -238,7 +277,7 @@ public class DetektOperation extends AbstractProcessOperation<DetektOperation> {
final List<String> args = new ArrayList<>(); final List<String> args = new ArrayList<>();
args.add(javaTool()); args.add(javaTool());
args.add("-cp"); args.add("-cp");
args.add(Path.of(project_.libBldDirectory().getAbsolutePath(), "*").toString()); args.add(getDetektJarList(project_.libBldDirectory()));
args.add("io.gitlab.arturbosch.detekt.cli.Main"); args.add("io.gitlab.arturbosch.detekt.cli.Main");
// all-rules // all-rules
@ -252,13 +291,13 @@ public class DetektOperation extends AbstractProcessOperation<DetektOperation> {
} }
// base-path // base-path
if (basePath_ != null) { if (isNotBlank(basePath_)) {
args.add("--base-path"); args.add("--base-path");
args.add(basePath_); args.add(basePath_);
} }
// baseline // baseline
if (baseline_ != null) { if (isNotBlank(baseline_)) {
args.add("--baseline"); args.add("--baseline");
args.add(baseline_); args.add(baseline_);
} }
@ -281,7 +320,7 @@ public class DetektOperation extends AbstractProcessOperation<DetektOperation> {
} }
// config-resource // config-resource
if (configResource_ != null) { if (isNotBlank(configResource_)) {
args.add("--config-resource"); args.add("--config-resource");
args.add(configResource_); args.add(configResource_);
} }
@ -302,9 +341,9 @@ public class DetektOperation extends AbstractProcessOperation<DetektOperation> {
} }
// excludes // excludes
if (excludes_ != null) { if (isNotBlank(excludes_)) {
args.add("--excludes"); args.add("--excludes");
args.add(excludes_);
} }
// generate-config // generate-config
@ -313,7 +352,7 @@ public class DetektOperation extends AbstractProcessOperation<DetektOperation> {
} }
// includes // includes
if (includes_ != null) { if (isNotBlank(includes_)) {
args.add("--includes"); args.add("--includes");
args.add(includes_); args.add(includes_);
} }
@ -325,19 +364,19 @@ public class DetektOperation extends AbstractProcessOperation<DetektOperation> {
} }
// jdk-home // jdk-home
if (jdkHome_ != null) { if (isNotBlank(jdkHome_)) {
args.add("--jdk-home"); args.add("--jdk-home");
args.add(jdkHome_); args.add(jdkHome_);
} }
// jvm-target // jvm-target
if (jvmTarget_ != null) { if (isNotBlank(jvmTarget_)) {
args.add("--jvm-target"); args.add("--jvm-target");
args.add(jvmTarget_); args.add(jvmTarget_);
} }
// language-version // language-version
if (languageVersion_ != null) { if (isNotBlank(languageVersion_)) {
args.add("--language-version"); args.add("--language-version");
args.add(languageVersion_); args.add(languageVersion_);
} }
@ -377,8 +416,11 @@ public class DetektOperation extends AbstractProcessOperation<DetektOperation> {
/** /**
* Configures the operation from a {@link BaseProject}. * Configures the operation from a {@link BaseProject}.
* <p> * <p>
* Sets the {@link #input input} to {@code src/main/kotlin}, {@code src/test/kotlin} and {@code detekt-baseline.xml} * Sets the following:
* if they exist. * <ul>
* <li>{@link #baseline baseline} to {@code detekt-baseline.xml}, if it exists</li>
* <li>{@link #excludes excludes} to exclude {@code build} and {@code resources} directories</li>
* </ul>
* *
* @param project the project to configure the operation from * @param project the project to configure the operation from
* @return this operation instance * @return this operation instance
@ -386,18 +428,11 @@ public class DetektOperation extends AbstractProcessOperation<DetektOperation> {
@Override @Override
public DetektOperation fromProject(BaseProject project) { public DetektOperation fromProject(BaseProject project) {
project_ = project; project_ = project;
var main = new File(project.srcMainDirectory(), "kotlin");
if (main.exists()) {
input_.add(main.getAbsolutePath());
}
var test = new File(project.srcTestDirectory(), "kotlin");
if (test.exists()) {
input_.add(test.getAbsolutePath());
}
var baseline = new File(project.workDirectory(), "detekt-baseline.xml"); var baseline = new File(project.workDirectory(), "detekt-baseline.xml");
if (baseline.exists()) { if (baseline.exists()) {
baseline_ = baseline.getAbsolutePath(); baseline_ = baseline.getAbsolutePath();
} }
excludes(".*/build/.*,.*/resources/.*");
return this; return this;
} }
@ -414,6 +449,30 @@ public class DetektOperation extends AbstractProcessOperation<DetektOperation> {
return this; return this;
} }
/*
* Retrieves the matching JARs files from the given directory.
*/
private String getDetektJarList(File directory) {
var jars = new ArrayList<String>();
if (directory.isDirectory()) {
var files = directory.listFiles();
if (files != null) {
for (var f : files) {
if (!f.getName().endsWith("-sources.jar") && !f.getName().endsWith("-javadoc.jar")) {
for (var m : DETEKT_JARS) {
if (f.getName().startsWith(m)) {
jars.add(f.getAbsolutePath());
break;
}
}
}
}
}
}
return String.join(":", jars);
}
/** /**
* Globbing patterns describing paths to include in the analysis. Useful in * Globbing patterns describing paths to include in the analysis. Useful in
* combination with {@link #excludes(String) excludes} patterns. * combination with {@link #excludes(String) excludes} patterns.
@ -457,6 +516,13 @@ public class DetektOperation extends AbstractProcessOperation<DetektOperation> {
return input_; return input_;
} }
/*
* Determines if a string is not blank.
*/
private boolean isNotBlank(String s) {
return s != null && !s.isBlank();
}
/** /**
* EXPERIMENTAL: Use a custom JDK home directory to include into the * EXPERIMENTAL: Use a custom JDK home directory to include into the
* classpath. * classpath.
@ -545,7 +611,7 @@ public class DetektOperation extends AbstractProcessOperation<DetektOperation> {
} }
/** /**
* Generates a report for given 'report-id' and stores it on given 'path'. * Generates a report for given {@link DetektReportId report-id} and stores it on given 'path'.
* *
* @param reports one or more reports * @param reports one or more reports
* @return this operation instance * @return this operation instance

View file

@ -29,7 +29,7 @@ import java.util.logging.ConsoleHandler;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import static org.assertj.core.api.Assertions.*; // NOPMD import static org.assertj.core.api.Assertions.*;
@SuppressWarnings("PMD.AvoidDuplicateLiterals") @SuppressWarnings("PMD.AvoidDuplicateLiterals")
@ -46,12 +46,19 @@ class DetektOperationTest {
} }
@Test @Test
void testExamplesExecute() { void testExampleBaseline() throws IOException, ExitStatusException, InterruptedException {
var tmpDir = Files.createTempDirectory("bld-detekt-").toFile();
tmpDir.deleteOnExit();
var baseline = new File(tmpDir, "detekt-baseline.xml");
var op = new DetektOperation() var op = new DetektOperation()
.fromProject(new BaseProjectBlueprint(new File("examples"), "com.example", .fromProject(new BaseProjectBlueprint(new File("examples"), "com.example",
"Example")) "Example"))
.debug(true); .baseline(baseline.getAbsolutePath())
assertThatThrownBy(op::execute).isInstanceOf(ExitStatusException.class); .createBaseline(true);
op.execute();
assertThat(baseline).exists();
} }
@Test @Test
@ -89,18 +96,11 @@ class DetektOperationTest {
} }
@Test @Test
void testExampleBaseline() throws IOException, ExitStatusException, InterruptedException { void testExamplesExecute() {
var tmpDir = Files.createTempDirectory("bld-detekt-").toFile();
tmpDir.deleteOnExit();
var baseline = new File(tmpDir, "detekt-baseline.xml");
var op = new DetektOperation() var op = new DetektOperation()
.fromProject(new BaseProjectBlueprint(new File("examples"), "com.example", .fromProject(new BaseProjectBlueprint(new File("examples"), "com.example",
"Example")) "Example"))
.baseline(baseline.getAbsolutePath()) .debug(true);
.createBaseline(true); assertThatThrownBy(op::execute).isInstanceOf(ExitStatusException.class);
op.execute();
assertThat(baseline).exists();
} }
} }