Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade diktat to 1.2.0 #1246

Merged
merged 10 commits into from Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.md
Expand Up @@ -12,6 +12,9 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
## [Unreleased]
### Added
* Support for `MAC_CLASSIC` (`\r`) line ending ([#1243](https://github.com/diffplug/spotless/pull/1243) fixes [#1196](https://github.com/diffplug/spotless/issues/1196))
### Changed
* Minimum required `diktat` version bumped to `1.2.0` ([#1246](https://github.com/diffplug/spotless/pull/1246))
* Default bumped from `1.1.0` -> `1.2.0`

## [2.26.2] - 2022-06-11
### Fixed
Expand Down
2 changes: 1 addition & 1 deletion lib/build.gradle
Expand Up @@ -51,7 +51,7 @@ dependencies {
ktlintCompileOnly "com.pinterest.ktlint:ktlint-ruleset-experimental:$VER_KTLINT"
ktlintCompileOnly "com.pinterest.ktlint:ktlint-ruleset-standard:$VER_KTLINT"

String VER_DIKTAT = "1.1.0"
String VER_DIKTAT = "1.2.0"
diktatCompileOnly "org.cqfn.diktat:diktat-rules:$VER_DIKTAT"

// used for markdown formatting
Expand Down
Expand Up @@ -21,9 +21,9 @@
import org.cqfn.diktat.ruleset.rules.DiktatRuleSetProvider;

import com.pinterest.ktlint.core.KtLint;
import com.pinterest.ktlint.core.KtLint.Params;
import com.pinterest.ktlint.core.LintError;
import com.pinterest.ktlint.core.RuleSet;
import com.pinterest.ktlint.core.api.EditorConfigOverride;

import com.diffplug.spotless.FormatterFunc;

Expand All @@ -33,15 +33,13 @@
public class DiktatFormatterFunc implements FormatterFunc.NeedsFile {

private final List<RuleSet> rulesets;
private final Map<String, String> userData;
nedtwigg marked this conversation as resolved.
Show resolved Hide resolved
private final Function2<? super LintError, ? super Boolean, Unit> formatterCallback;
private final boolean isScript;

private final ArrayList<LintError> errors = new ArrayList<>();

public DiktatFormatterFunc(boolean isScript, Map<String, String> userData) {
public DiktatFormatterFunc(boolean isScript) {
rulesets = Collections.singletonList(new DiktatRuleSetProvider().get());
this.userData = userData;
this.formatterCallback = new FormatterCallback(errors);
this.isScript = isScript;
}
Expand All @@ -65,17 +63,18 @@ public Unit invoke(LintError lintError, Boolean corrected) {
@Override
public String applyWithFile(String unix, File file) throws Exception {
errors.clear();
userData.put("file_path", file.getAbsolutePath());
String result = KtLint.INSTANCE.format(new Params(
String result = KtLint.INSTANCE.format(new KtLint.ExperimentalParams(
// Unlike Ktlint, Diktat requires full path to the file.
// See https://github.com/diffplug/spotless/issues/1189, https://github.com/analysis-dev/diktat/issues/1202
file.getAbsolutePath(),
unix,
rulesets,
userData,
Collections.emptyMap(),
formatterCallback,
isScript,
null,
false,
new EditorConfigOverride(),
false));

if (!errors.isEmpty()) {
Expand Down
33 changes: 14 additions & 19 deletions lib/src/main/java/com/diffplug/spotless/kotlin/DiktatStep.java
Expand Up @@ -30,7 +30,9 @@ public class DiktatStep {
// prevent direct instantiation
private DiktatStep() {}

private static final String DEFAULT_VERSION = "1.1.0";
private static final String MIN_SUPPORTED_VERSION = "1.2.0";

private static final String DEFAULT_VERSION = "1.2.0";
static final String NAME = "diktat";
static final String PACKAGE_DIKTAT = "org.cqfn.diktat";
static final String MAVEN_COORDINATE = PACKAGE_DIKTAT + ":diktat-rules:";
Expand All @@ -44,30 +46,25 @@ public static FormatterStep create(Provisioner provisioner) {
}

public static FormatterStep create(String versionDiktat, Provisioner provisioner) {
return create(versionDiktat, provisioner, Collections.emptyMap(), null);
return create(versionDiktat, provisioner, null);
}

public static FormatterStep create(String versionDiktat, Provisioner provisioner, @Nullable FileSignature config) {
return create(versionDiktat, provisioner, Collections.emptyMap(), config);
}

public static FormatterStep create(String versionDiktat, Provisioner provisioner, Map<String, String> userData, @Nullable FileSignature config) {
return create(versionDiktat, provisioner, false, userData, config);
return create(versionDiktat, provisioner, false, config);
}

public static FormatterStep createForScript(String versionDiktat, Provisioner provisioner, @Nullable FileSignature config) {
return createForScript(versionDiktat, provisioner, Collections.emptyMap(), config);
return create(versionDiktat, provisioner, true, config);
}

public static FormatterStep createForScript(String versionDiktat, Provisioner provisioner, Map<String, String> userData, @Nullable FileSignature config) {
return create(versionDiktat, provisioner, true, userData, config);
}

public static FormatterStep create(String versionDiktat, Provisioner provisioner, boolean isScript, Map<String, String> userData, @Nullable FileSignature config) {
public static FormatterStep create(String versionDiktat, Provisioner provisioner, boolean isScript, @Nullable FileSignature config) {
if (BadSemver.version(versionDiktat) < BadSemver.version(MIN_SUPPORTED_VERSION)) {
throw new IllegalStateException("Minimum required Diktat version is " + MIN_SUPPORTED_VERSION + ", you tried " + versionDiktat + " which is too old");
}
Objects.requireNonNull(versionDiktat, "versionDiktat");
Objects.requireNonNull(provisioner, "provisioner");
return FormatterStep.createLazy(NAME,
() -> new DiktatStep.State(versionDiktat, provisioner, isScript, userData, config),
() -> new DiktatStep.State(versionDiktat, provisioner, isScript, config),
DiktatStep.State::createFormat);
}

Expand All @@ -79,14 +76,12 @@ static final class State implements Serializable {
private final boolean isScript;
private final @Nullable FileSignature config;
final JarState jar;
private final TreeMap<String, String> userData;

State(String versionDiktat, Provisioner provisioner, boolean isScript, Map<String, String> userData, @Nullable FileSignature config) throws IOException {
State(String versionDiktat, Provisioner provisioner, boolean isScript, @Nullable FileSignature config) throws IOException {

HashSet<String> pkgSet = new HashSet<>();
pkgSet.add(MAVEN_COORDINATE + versionDiktat);

this.userData = new TreeMap<>(userData);
this.jar = JarState.from(pkgSet, provisioner);
this.isScript = isScript;
this.config = config;
Expand All @@ -98,8 +93,8 @@ FormatterFunc createFormat() throws Exception {
}

Class<?> formatterFunc = jar.getClassLoader().loadClass("com.diffplug.spotless.glue.diktat.DiktatFormatterFunc");
Constructor<?> constructor = formatterFunc.getConstructor(boolean.class, Map.class);
return (FormatterFunc.NeedsFile) constructor.newInstance(isScript, userData);
Constructor<?> constructor = formatterFunc.getConstructor(boolean.class);
return (FormatterFunc.NeedsFile) constructor.newInstance(isScript);
}
}
}
3 changes: 3 additions & 0 deletions plugin-gradle/CHANGES.md
Expand Up @@ -5,6 +5,9 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
## [Unreleased]
### Added
* Support for `MAC_CLASSIC` (`\r`) line ending ([#1243](https://github.com/diffplug/spotless/pull/1243) fixes [#1196](https://github.com/diffplug/spotless/issues/1196))
### Changed
* Minimum required `diktat` version bumped to `1.2.0` ([#1246](https://github.com/diffplug/spotless/pull/1246))
* Default bumped from `1.1.0` -> `1.2.0`

## [6.7.2] - 2022-06-11
### Fixed
Expand Down
3 changes: 3 additions & 0 deletions plugin-maven/CHANGES.md
Expand Up @@ -5,6 +5,9 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
## [Unreleased]
### Added
* Support for `MAC_CLASSIC` (`\r`) line ending ([#1243](https://github.com/diffplug/spotless/pull/1243) fixes [#1196](https://github.com/diffplug/spotless/issues/1196))
### Changed
* Minimum required `diktat` version bumped to `1.2.0` ([#1246](https://github.com/diffplug/spotless/pull/1246))
* Default bumped from `1.1.0` -> `1.2.0`

## [2.22.8] - 2022-06-11
### Fixed
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2021 DiffPlug
* Copyright 2021-2022 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,8 +15,6 @@
*/
package com.diffplug.spotless.maven.kotlin;

import java.util.Collections;

import org.apache.maven.plugins.annotations.Parameter;

import com.diffplug.spotless.FileSignature;
Expand All @@ -41,6 +39,6 @@ public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) {
config = ThrowingEx.get(() -> FileSignature.signAsList(stepConfig.getFileLocator().locateFile(configFile)));
}
String diktatVersion = version != null ? version : DiktatStep.defaultVersionDiktat();
return DiktatStep.create(diktatVersion, stepConfig.getProvisioner(), Collections.emptyMap(), config);
return DiktatStep.create(diktatVersion, stepConfig.getProvisioner(), config);
}
}
Expand Up @@ -40,7 +40,7 @@ void testDiktatWithVersion() throws Exception {

writePomWithKotlinSteps(
"<diktat>",
" <version>1.0.1</version>",
" <version>1.2.0</version>",
"</diktat>");

String path = "src/main/kotlin/Main.kt";
Expand All @@ -56,7 +56,7 @@ void testDiktatConfig() throws Exception {
File conf = setFile(configPath).toResource("kotlin/diktat/diktat-analysis.yml");
writePomWithKotlinSteps(
"<diktat>",
" <version>1.0.1</version>",
" <version>1.2.0</version>",
" <configFile>" + conf.getAbsolutePath() + "</configFile>",
"</diktat>");

Expand Down
6 changes: 3 additions & 3 deletions testlib/src/main/resources/kotlin/diktat/main.clean
@@ -1,15 +1,15 @@
package org.cqfn.diktat.example.gradle.multiproject

/**
* @return print.
* @return something
*/
class Main {
/**
* foo a [member] to this group.
*
*/
fun foo() {
println(";")
println()
bar(";")
bar()
}
}
6 changes: 3 additions & 3 deletions testlib/src/main/resources/kotlin/diktat/main.dirty
@@ -1,14 +1,14 @@
package org.cqfn.diktat.example.gradle.multiproject
/**
* @return print.
* @return something
*/
class Main {
/**
* foo a [member] to this group.
*
*/
fun foo() {
println(";")
println();
bar(";")
bar();
}
}
Expand Up @@ -18,8 +18,8 @@
import static com.diffplug.spotless.FileSignature.signAsList;

import java.io.File;
import java.util.Collections;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import com.diffplug.spotless.FileSignature;
Expand All @@ -36,31 +36,48 @@ void behavior() throws Exception {
StepHarness.forStep(step)
.testResourceException("kotlin/diktat/Unsolvable.kt", assertion -> {
assertion.isInstanceOf(AssertionError.class);
assertion.hasMessage("There are 2 unfixed errors:" +
assertion.hasMessage("There are 4 unfixed errors:" +
System.lineSeparator() + "Error on line: 1, column: 1 cannot be fixed automatically" +
System.lineSeparator() + "[FILE_NAME_INCORRECT] file name is incorrect - it should end with .kt extension and be in PascalCase: testlib" +
System.lineSeparator() + "Error on line: 1, column: 1 cannot be fixed automatically" +
System.lineSeparator() + "[FILE_NAME_MATCH_CLASS] file name is incorrect - it should match with the class described in it if there is the only one class declared: testlib vs Unsolvable");
System.lineSeparator() + "[FILE_NAME_MATCH_CLASS] file name is incorrect - it should match with the class described in it if there is the only one class declared: testlib vs Unsolvable" +
System.lineSeparator() + "Error on line: 1, column: 1 cannot be fixed automatically" +
System.lineSeparator() + "[DEBUG_PRINT] use a dedicated logging library: found println()" +
System.lineSeparator() + "Error on line: 13, column: 9 cannot be fixed automatically" +
System.lineSeparator() + "[DEBUG_PRINT] use a dedicated logging library: found println()");
});
}

@Test
void behaviorConf() throws Exception {

String configPath = "src/main/kotlin/diktat-analysis.yml";
File conf = setFile(configPath).toResource("kotlin/diktat/diktat-analysis.yml");
FileSignature config = signAsList(conf);

FormatterStep step = DiktatStep.create("1.0.1", TestProvisioner.mavenCentral(), Collections.emptyMap(), config);
FormatterStep step = DiktatStep.create("1.2.0", TestProvisioner.mavenCentral(), config);
StepHarness.forStep(step)
.testResourceException("kotlin/diktat/Unsolvable.kt", assertion -> {
assertion.isInstanceOf(AssertionError.class);
assertion.hasMessage("There are 2 unfixed errors:" +
assertion.hasMessage("There are 4 unfixed errors:" +
System.lineSeparator() + "Error on line: 1, column: 1 cannot be fixed automatically" +
System.lineSeparator() + "[FILE_NAME_INCORRECT] file name is incorrect - it should end with .kt extension and be in PascalCase: testlib" +
System.lineSeparator() + "Error on line: 1, column: 1 cannot be fixed automatically" +
System.lineSeparator() + "[FILE_NAME_MATCH_CLASS] file name is incorrect - it should match with the class described in it if there is the only one class declared: testlib vs Unsolvable");
System.lineSeparator() + "[FILE_NAME_MATCH_CLASS] file name is incorrect - it should match with the class described in it if there is the only one class declared: testlib vs Unsolvable" +
System.lineSeparator() + "Error on line: 1, column: 1 cannot be fixed automatically" +
System.lineSeparator() + "[DEBUG_PRINT] use a dedicated logging library: found println()" +
System.lineSeparator() + "Error on line: 13, column: 9 cannot be fixed automatically" +
System.lineSeparator() + "[DEBUG_PRINT] use a dedicated logging library: found println()");
});
}

@Test
void notSupportedVersion() {
final IllegalStateException notSupportedException = Assertions.assertThrows(IllegalStateException.class,
() -> DiktatStep.create("1.1.0", TestProvisioner.mavenCentral()));
Assertions.assertTrue(
notSupportedException.getMessage().contains("Minimum required Diktat version is 1.2.0, you tried 1.1.0 which is too old"));

Assertions.assertDoesNotThrow(() -> DiktatStep.create("1.2.1", TestProvisioner.mavenCentral()));
Assertions.assertDoesNotThrow(() -> DiktatStep.create("2.0.0", TestProvisioner.mavenCentral()));
}
}