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">
<component name="EntryPointsManager">
<pattern value="rife.bld.extension.DetektOperationBuild" method="pmd" />

View file

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

View file

@ -41,15 +41,10 @@ public class DetektOperationBuild extends Project {
autoDownloadPurge = true;
repositories = List.of(MAVEN_CENTRAL, RIFE2_RELEASES);
var detekt = version(1, 23, 3);
scope(compile)
.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-tooling", detekt))
.include(dependency("com.beust", "jcommander", "1.82"))
.include(dependency("com.fasterxml:aalto-xml:1.3.2"));
.include(dependency("io.gitlab.arturbosch.detekt", "detekt-cli", version(1, 23, 4)));
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.platform", "junit-platform-console-standalone", version(1, 10, 1)))
.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 java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@ -34,6 +33,46 @@ import java.util.logging.Logger;
* @since 1.0
*/
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 final Collection<String> classpath_ = 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
* 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
* correcting. The additional 'formatting' rule set, added with
* users code base. However, custom rules can be written to support
* autocorrecting. The additional 'formatting' rule set, added with
* {@link #plugins(String...) Plugins}, does support it and needs this flag.
*
* @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
* 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
* files. Used for type resolution.
* EXPERIMENTAL: Paths where to find user class files and depending jar files.
* Used for type resolution.
*
* @param paths one or more files
* @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
* files. Used for type resolution.
* EXPERIMENTAL: Paths where to find user class files and depending jar files.
* Used for type resolution.
*
* @param paths the list of files
* @return this operation instance
@ -238,7 +277,7 @@ public class DetektOperation extends AbstractProcessOperation<DetektOperation> {
final List<String> args = new ArrayList<>();
args.add(javaTool());
args.add("-cp");
args.add(Path.of(project_.libBldDirectory().getAbsolutePath(), "*").toString());
args.add(getDetektJarList(project_.libBldDirectory()));
args.add("io.gitlab.arturbosch.detekt.cli.Main");
// all-rules
@ -252,13 +291,13 @@ public class DetektOperation extends AbstractProcessOperation<DetektOperation> {
}
// base-path
if (basePath_ != null) {
if (isNotBlank(basePath_)) {
args.add("--base-path");
args.add(basePath_);
}
// baseline
if (baseline_ != null) {
if (isNotBlank(baseline_)) {
args.add("--baseline");
args.add(baseline_);
}
@ -281,7 +320,7 @@ public class DetektOperation extends AbstractProcessOperation<DetektOperation> {
}
// config-resource
if (configResource_ != null) {
if (isNotBlank(configResource_)) {
args.add("--config-resource");
args.add(configResource_);
}
@ -302,9 +341,9 @@ public class DetektOperation extends AbstractProcessOperation<DetektOperation> {
}
// excludes
if (excludes_ != null) {
if (isNotBlank(excludes_)) {
args.add("--excludes");
args.add(excludes_);
}
// generate-config
@ -313,7 +352,7 @@ public class DetektOperation extends AbstractProcessOperation<DetektOperation> {
}
// includes
if (includes_ != null) {
if (isNotBlank(includes_)) {
args.add("--includes");
args.add(includes_);
}
@ -325,19 +364,19 @@ public class DetektOperation extends AbstractProcessOperation<DetektOperation> {
}
// jdk-home
if (jdkHome_ != null) {
if (isNotBlank(jdkHome_)) {
args.add("--jdk-home");
args.add(jdkHome_);
}
// jvm-target
if (jvmTarget_ != null) {
if (isNotBlank(jvmTarget_)) {
args.add("--jvm-target");
args.add(jvmTarget_);
}
// language-version
if (languageVersion_ != null) {
if (isNotBlank(languageVersion_)) {
args.add("--language-version");
args.add(languageVersion_);
}
@ -377,8 +416,11 @@ public class DetektOperation extends AbstractProcessOperation<DetektOperation> {
/**
* Configures the operation from a {@link BaseProject}.
* <p>
* Sets the {@link #input input} to {@code src/main/kotlin}, {@code src/test/kotlin} and {@code detekt-baseline.xml}
* if they exist.
* Sets the following:
* <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
* @return this operation instance
@ -386,18 +428,11 @@ public class DetektOperation extends AbstractProcessOperation<DetektOperation> {
@Override
public DetektOperation fromProject(BaseProject 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");
if (baseline.exists()) {
baseline_ = baseline.getAbsolutePath();
}
excludes(".*/build/.*,.*/resources/.*");
return this;
}
@ -414,6 +449,30 @@ public class DetektOperation extends AbstractProcessOperation<DetektOperation> {
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
* combination with {@link #excludes(String) excludes} patterns.
@ -457,6 +516,13 @@ public class DetektOperation extends AbstractProcessOperation<DetektOperation> {
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
* 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
* @return this operation instance

View file

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