Skip to content

Commit d680622

Browse files
authoredSep 20, 2023
Support Ktlint 1.0.0 (#1808 fixes #1803)
2 parents eb5dc61 + 4047ae0 commit d680622

File tree

16 files changed

+315
-28
lines changed

16 files changed

+315
-28
lines changed
 

‎CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ This document is intended for Spotless developers.
1010
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
1111

1212
## [Unreleased]
13+
### Added
14+
* Support Ktlint 1.0.0 ([#1808](https://github.com/diffplug/spotless/pull/1808)).
1315

1416
### Fixed
1517
* Added support for plugins when using Prettier version `3.0.0` and newer. ([#1802](https://github.com/diffplug/spotless/pull/1802))

‎lib/build.gradle

+6-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ versionCompatibility {
4848
'0.48.0',
4949
'0.49.0',
5050
'0.50.0',
51+
'1.0.0',
5152
]
5253
targetSourceSetName = 'ktlint'
5354
}
@@ -104,10 +105,14 @@ dependencies {
104105
compatKtLint0Dot49Dot0CompileAndTestOnly 'com.pinterest.ktlint:ktlint-rule-engine:0.49.0'
105106
compatKtLint0Dot49Dot0CompileAndTestOnly 'com.pinterest.ktlint:ktlint-ruleset-standard:0.49.0'
106107
compatKtLint0Dot49Dot0CompileAndTestOnly 'org.slf4j:slf4j-api:2.0.0'
107-
// ktlint latest supported version
108+
// ktlint previous supported version
108109
compatKtLint0Dot50Dot0CompileAndTestOnly 'com.pinterest.ktlint:ktlint-rule-engine:0.50.0'
109110
compatKtLint0Dot50Dot0CompileAndTestOnly 'com.pinterest.ktlint:ktlint-ruleset-standard:0.50.0'
110111
compatKtLint0Dot50Dot0CompileAndTestOnly 'org.slf4j:slf4j-api:2.0.0'
112+
// ktlint latest supported version
113+
compatKtLint1Dot0Dot0CompileAndTestOnly 'com.pinterest.ktlint:ktlint-rule-engine:1.0.0'
114+
compatKtLint1Dot0Dot0CompileAndTestOnly 'com.pinterest.ktlint:ktlint-ruleset-standard:1.0.0'
115+
compatKtLint1Dot0Dot0CompileAndTestOnly 'org.slf4j:slf4j-api:2.0.0'
111116
// palantirJavaFormat
112117
palantirJavaFormatCompileOnly 'com.palantir.javaformat:palantir-java-format:1.1.0' // this version needs to stay compilable against Java 8 for CI Job testNpm
113118
// scalafmt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
/*
2+
* Copyright 2023 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.glue.ktlint.compat;
17+
18+
import java.nio.file.Files;
19+
import java.nio.file.Path;
20+
import java.util.ArrayList;
21+
import java.util.Collections;
22+
import java.util.List;
23+
import java.util.Map;
24+
import java.util.Objects;
25+
import java.util.ServiceLoader;
26+
import java.util.Set;
27+
import java.util.stream.Collectors;
28+
import java.util.stream.Stream;
29+
30+
import org.slf4j.Logger;
31+
import org.slf4j.LoggerFactory;
32+
33+
import com.pinterest.ktlint.cli.ruleset.core.api.RuleSetProviderV3;
34+
import com.pinterest.ktlint.rule.engine.api.Code;
35+
import com.pinterest.ktlint.rule.engine.api.EditorConfigDefaults;
36+
import com.pinterest.ktlint.rule.engine.api.EditorConfigOverride;
37+
import com.pinterest.ktlint.rule.engine.api.KtLintRuleEngine;
38+
import com.pinterest.ktlint.rule.engine.api.LintError;
39+
import com.pinterest.ktlint.rule.engine.core.api.Rule;
40+
import com.pinterest.ktlint.rule.engine.core.api.RuleId;
41+
import com.pinterest.ktlint.rule.engine.core.api.RuleProvider;
42+
import com.pinterest.ktlint.rule.engine.core.api.RuleSetId;
43+
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.CodeStyleEditorConfigPropertyKt;
44+
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.EditorConfigProperty;
45+
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.EndOfLinePropertyKt;
46+
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.IndentSizeEditorConfigPropertyKt;
47+
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.IndentStyleEditorConfigPropertyKt;
48+
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.InsertFinalNewLineEditorConfigPropertyKt;
49+
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.MaxLineLengthEditorConfigPropertyKt;
50+
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.RuleExecution;
51+
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.RuleExecutionEditorConfigPropertyKt;
52+
53+
import kotlin.Pair;
54+
import kotlin.Unit;
55+
import kotlin.jvm.functions.Function2;
56+
57+
public class KtLintCompat1Dot0Dot0Adapter implements KtLintCompatAdapter {
58+
59+
private static final Logger logger = LoggerFactory.getLogger(KtLintCompat1Dot0Dot0Adapter.class);
60+
61+
private static final List<EditorConfigProperty<?>> DEFAULT_EDITOR_CONFIG_PROPERTIES;
62+
63+
static {
64+
List<EditorConfigProperty<?>> list = new ArrayList<>();
65+
list.add(CodeStyleEditorConfigPropertyKt.getCODE_STYLE_PROPERTY());
66+
list.add(EndOfLinePropertyKt.getEND_OF_LINE_PROPERTY());
67+
list.add(IndentSizeEditorConfigPropertyKt.getINDENT_SIZE_PROPERTY());
68+
list.add(IndentStyleEditorConfigPropertyKt.getINDENT_STYLE_PROPERTY());
69+
list.add(InsertFinalNewLineEditorConfigPropertyKt.getINSERT_FINAL_NEWLINE_PROPERTY());
70+
list.add(MaxLineLengthEditorConfigPropertyKt.getMAX_LINE_LENGTH_PROPERTY());
71+
list.add(RuleExecutionEditorConfigPropertyKt.getEXPERIMENTAL_RULES_EXECUTION_PROPERTY());
72+
DEFAULT_EDITOR_CONFIG_PROPERTIES = Collections.unmodifiableList(list);
73+
}
74+
75+
static class FormatterCallback implements Function2<LintError, Boolean, Unit> {
76+
77+
@Override
78+
public Unit invoke(LintError lint, Boolean corrected) {
79+
if (!corrected) {
80+
KtLintCompatReporting.report(lint.getLine(), lint.getCol(), lint.getRuleId().getValue(), lint.getDetail());
81+
}
82+
return Unit.INSTANCE;
83+
}
84+
}
85+
86+
@Override
87+
public String format(final String text, Path path, final boolean isScript,
88+
Path editorConfigPath, final Map<String, String> userData,
89+
final Map<String, Object> editorConfigOverrideMap) {
90+
final FormatterCallback formatterCallback = new FormatterCallback();
91+
92+
Set<RuleProvider> allRuleProviders = ServiceLoader.load(RuleSetProviderV3.class, RuleSetProviderV3.class.getClassLoader())
93+
.stream()
94+
.flatMap(loader -> loader.get().getRuleProviders().stream())
95+
.collect(Collectors.toUnmodifiableSet());
96+
97+
EditorConfigOverride editorConfigOverride;
98+
if (editorConfigOverrideMap.isEmpty()) {
99+
editorConfigOverride = EditorConfigOverride.Companion.getEMPTY_EDITOR_CONFIG_OVERRIDE();
100+
} else {
101+
editorConfigOverride = createEditorConfigOverride(allRuleProviders.stream().map(
102+
RuleProvider::createNewRuleInstance).collect(Collectors.toList()),
103+
editorConfigOverrideMap);
104+
}
105+
EditorConfigDefaults editorConfig;
106+
if (editorConfigPath == null || !Files.exists(editorConfigPath)) {
107+
editorConfig = EditorConfigDefaults.Companion.getEMPTY_EDITOR_CONFIG_DEFAULTS();
108+
} else {
109+
editorConfig = EditorConfigDefaults.Companion.load(editorConfigPath, Collections.emptySet());
110+
}
111+
112+
return new KtLintRuleEngine(
113+
allRuleProviders,
114+
editorConfig,
115+
editorConfigOverride,
116+
false,
117+
path.getFileSystem())
118+
.format(Code.Companion.fromPath(path), formatterCallback);
119+
}
120+
121+
/**
122+
* Create EditorConfigOverride from user provided parameters.
123+
*/
124+
private static EditorConfigOverride createEditorConfigOverride(final List<Rule> rules, Map<String, Object> editorConfigOverrideMap) {
125+
// Get properties from rules in the rule sets
126+
Stream<EditorConfigProperty<?>> ruleProperties = rules.stream()
127+
.flatMap(rule -> rule.getUsesEditorConfigProperties().stream());
128+
129+
// Create a mapping of properties to their names based on rule properties and default properties
130+
Map<String, EditorConfigProperty<?>> supportedProperties = Stream
131+
.concat(ruleProperties, DEFAULT_EDITOR_CONFIG_PROPERTIES.stream())
132+
.distinct()
133+
.collect(Collectors.toMap(EditorConfigProperty::getName, property -> property));
134+
135+
// The default style had been changed from intellij_idea to ktlint_official in version 1.0.0
136+
if (!editorConfigOverrideMap.containsKey("ktlint_code_style")) {
137+
editorConfigOverrideMap.put("ktlint_code_style", "intellij_idea");
138+
}
139+
140+
// Create config properties based on provided property names and values
141+
@SuppressWarnings("unchecked")
142+
Pair<EditorConfigProperty<?>, ?>[] properties = editorConfigOverrideMap.entrySet().stream()
143+
.map(entry -> {
144+
EditorConfigProperty<?> property = supportedProperties.get(entry.getKey());
145+
146+
if (property == null && entry.getKey().startsWith("ktlint_")) {
147+
String[] parts = entry.getKey().substring(7).split("_", 2);
148+
if (parts.length == 1) {
149+
// convert ktlint_{ruleset} to RuleSetId
150+
RuleSetId id = new RuleSetId(parts[0]);
151+
property = RuleExecutionEditorConfigPropertyKt.createRuleSetExecutionEditorConfigProperty(id, RuleExecution.enabled);
152+
} else {
153+
// convert ktlint_{ruleset}_{rulename} to RuleId
154+
RuleId id = new RuleId(parts[0] + ":" + parts[1]);
155+
property = RuleExecutionEditorConfigPropertyKt.createRuleExecutionEditorConfigProperty(id, RuleExecution.enabled);
156+
}
157+
}
158+
159+
if (property == null) {
160+
return null;
161+
} else {
162+
return new Pair<>(property, entry.getValue());
163+
}
164+
})
165+
.filter(Objects::nonNull)
166+
.toArray(Pair[]::new);
167+
168+
return EditorConfigOverride.Companion.from(properties);
169+
}
170+
}

‎lib/src/ktlint/java/com/diffplug/spotless/glue/ktlint/KtlintFormatterFunc.java

+21-15
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,28 @@ public class KtlintFormatterFunc implements FormatterFunc.NeedsFile {
3333

3434
public KtlintFormatterFunc(String version, boolean isScript, FileSignature editorConfigPath, Map<String, String> userData,
3535
Map<String, Object> editorConfigOverrideMap) {
36-
int minorVersion = Integer.parseInt(version.split("\\.")[1]);
37-
if (minorVersion >= 50) {
38-
// Fixed `RuleId` and `RuleSetId` issues
39-
// New argument to `EditorConfigDefaults.Companion.load(...)` for custom property type parsing
40-
// New argument to `new KtLintRuleEngine(...)` to fail on usage of `treeCopyHandler` extension point
41-
this.adapter = new KtLintCompat0Dot50Dot0Adapter();
42-
} else if (minorVersion == 49) {
43-
// Packages and modules moved around (`ktlint-core` -> `ktlint-rule-engine`)
44-
// Experimental ruleset was replaced by implementing `Rule.Experimental` and checking the `ktlint_experimental` `.editorconfig` property
45-
// `RuleId` and `RuleSetId` became inline classes (mangled to be unrepresentable in Java source code, so reflection is needed), tracked here: https://github.com/pinterest/ktlint/issues/2041
46-
this.adapter = new KtLintCompat0Dot49Dot0Adapter();
47-
} else if (minorVersion == 48) {
48-
// ExperimentalParams lost two constructor arguments, EditorConfigProperty moved to its own class
49-
this.adapter = new KtLintCompat0Dot48Dot0Adapter();
36+
String[] versions = version.split("\\.");
37+
int majorVersion = Integer.parseInt(versions[0]);
38+
int minorVersion = Integer.parseInt(versions[1]);
39+
if (majorVersion == 1) {
40+
this.adapter = new KtLintCompat1Dot0Dot0Adapter();
5041
} else {
51-
throw new IllegalStateException("Ktlint versions < 0.48.0 not supported!");
42+
if (minorVersion >= 50) {
43+
// Fixed `RuleId` and `RuleSetId` issues
44+
// New argument to `EditorConfigDefaults.Companion.load(...)` for custom property type parsing
45+
// New argument to `new KtLintRuleEngine(...)` to fail on usage of `treeCopyHandler` extension point
46+
this.adapter = new KtLintCompat0Dot50Dot0Adapter();
47+
} else if (minorVersion == 49) {
48+
// Packages and modules moved around (`ktlint-core` -> `ktlint-rule-engine`)
49+
// Experimental ruleset was replaced by implementing `Rule.Experimental` and checking the `ktlint_experimental` `.editorconfig` property
50+
// `RuleId` and `RuleSetId` became inline classes (mangled to be unrepresentable in Java source code, so reflection is needed), tracked here: https://github.com/pinterest/ktlint/issues/2041
51+
this.adapter = new KtLintCompat0Dot49Dot0Adapter();
52+
} else if (minorVersion == 48) {
53+
// ExperimentalParams lost two constructor arguments, EditorConfigProperty moved to its own class
54+
this.adapter = new KtLintCompat0Dot48Dot0Adapter();
55+
} else {
56+
throw new IllegalStateException("Ktlint versions < 0.48.0 not supported!");
57+
}
5258
}
5359
this.editorConfigPath = editorConfigPath;
5460
this.editorConfigOverrideMap = editorConfigOverrideMap;

‎lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ public class KtLintStep {
3636
// prevent direct instantiation
3737
private KtLintStep() {}
3838

39-
private static final String DEFAULT_VERSION = "0.50.0";
39+
private static final String DEFAULT_VERSION = "1.0.0";
4040
static final String NAME = "ktlint";
41-
static final String PACKAGE = "com.pinterest";
42-
static final String MAVEN_COORDINATE = PACKAGE + ":ktlint:";
41+
static final String MAVEN_COORDINATE_0_DOT = "com.pinterest:ktlint:";
42+
static final String MAVEN_COORDINATE_1_DOT = "com.pinterest.ktlint:ktlint-cli:";
4343

4444
public static FormatterStep create(Provisioner provisioner) {
4545
return create(defaultVersion(), provisioner);
@@ -110,7 +110,8 @@ static final class State implements Serializable {
110110
this.version = version;
111111
this.userData = new TreeMap<>(userData);
112112
this.editorConfigOverride = new TreeMap<>(editorConfigOverride);
113-
this.jarState = JarState.from(MAVEN_COORDINATE + version, provisioner);
113+
this.jarState = JarState.from((version.startsWith("0.") ? MAVEN_COORDINATE_0_DOT : MAVEN_COORDINATE_1_DOT) + version,
114+
provisioner);
114115
this.editorConfigPath = editorConfigPath;
115116
this.isScript = isScript;
116117
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2023 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.glue.ktlint.compat;
17+
18+
import static org.junit.jupiter.api.Assertions.assertEquals;
19+
20+
import java.io.IOException;
21+
import java.io.InputStream;
22+
import java.nio.charset.StandardCharsets;
23+
import java.nio.file.Files;
24+
import java.nio.file.Path;
25+
import java.nio.file.Paths;
26+
import java.util.HashMap;
27+
import java.util.Map;
28+
29+
import org.junit.jupiter.api.Test;
30+
import org.junit.jupiter.api.io.TempDir;
31+
32+
public class KtLintCompat1Dot0Dot0AdapterTest {
33+
@Test
34+
public void testDefaults(@TempDir Path path) throws IOException {
35+
KtLintCompat1Dot0Dot0Adapter KtLintCompat1Dot0Dot0Adapter = new KtLintCompat1Dot0Dot0Adapter();
36+
String text = loadAndWriteText(path, "EmptyClassBody.kt");
37+
final Path filePath = Paths.get(path.toString(), "EmptyClassBody.kt");
38+
39+
Map<String, String> userData = new HashMap<>();
40+
41+
Map<String, Object> editorConfigOverrideMap = new HashMap<>();
42+
43+
String formatted = KtLintCompat1Dot0Dot0Adapter.format(text, filePath, false, null, userData, editorConfigOverrideMap);
44+
assertEquals("class EmptyClassBody\n", formatted);
45+
}
46+
47+
@Test
48+
public void testEditorConfigCanDisable(@TempDir Path path) throws IOException {
49+
KtLintCompat1Dot0Dot0Adapter KtLintCompat1Dot0Dot0Adapter = new KtLintCompat1Dot0Dot0Adapter();
50+
String text = loadAndWriteText(path, "FailsNoSemicolons.kt");
51+
final Path filePath = Paths.get(path.toString(), "FailsNoSemicolons.kt");
52+
53+
Map<String, String> userData = new HashMap<>();
54+
55+
Map<String, Object> editorConfigOverrideMap = new HashMap<>();
56+
editorConfigOverrideMap.put("indent_style", "tab");
57+
editorConfigOverrideMap.put("ktlint_standard_no-semi", "disabled");
58+
59+
String formatted = KtLintCompat1Dot0Dot0Adapter.format(text, filePath, false, null, userData, editorConfigOverrideMap);
60+
assertEquals("class FailsNoSemicolons {\n\tval i = 0;\n}\n", formatted);
61+
}
62+
63+
private static String loadAndWriteText(Path path, String name) throws IOException {
64+
try (InputStream is = KtLintCompat1Dot0Dot0AdapterTest.class.getResourceAsStream("/" + name)) {
65+
Files.copy(is, path.resolve(name));
66+
}
67+
return new String(Files.readAllBytes(path.resolve(name)), StandardCharsets.UTF_8);
68+
}
69+
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class EmptyClassBody {
2+
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class FailsNoSemicolons {
2+
val i = 0;
3+
}

‎plugin-gradle/CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`).
44

55
## [Unreleased]
6+
### Added
7+
* Support Ktlint 1.0.0 ([#1808](https://github.com/diffplug/spotless/pull/1808)).
68

79
### Fixed
810
* Added support for plugins when using Prettier version `3.0.0` and newer. ([#1802](https://github.com/diffplug/spotless/pull/1802))

‎plugin-gradle/README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -399,10 +399,14 @@ Additionally, `editorConfigOverride` options will override what's supplied in `.
399399
spotless {
400400
kotlin {
401401
// version, userData and editorConfigOverride are all optional
402-
ktlint("0.50.0")
402+
ktlint("1.0.0")
403403
.userData(mapOf("android" to "true"))
404404
.setEditorConfigPath("$projectDir/config/.editorconfig") // sample unusual placement
405-
.editorConfigOverride(mapOf("indent_size" to 2))
405+
.editorConfigOverride(
406+
mapOf(
407+
"indent_size" to 2,
408+
)
409+
)
406410
}
407411
}
408412
```

‎plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GradleProvisioner.java

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.gradle.api.artifacts.ConfigurationContainer;
2828
import org.gradle.api.artifacts.dsl.DependencyHandler;
2929
import org.gradle.api.attributes.Bundling;
30+
import org.gradle.api.attributes.Category;
3031
import org.gradle.api.initialization.dsl.ScriptHandler;
3132
import org.slf4j.Logger;
3233
import org.slf4j.LoggerFactory;
@@ -121,6 +122,7 @@ private static Provisioner forConfigurationContainer(Project project, Configurat
121122
config.setCanBeConsumed(false);
122123
config.setVisible(false);
123124
config.attributes(attr -> {
125+
attr.attribute(Category.CATEGORY_ATTRIBUTE, project.getObjects().named(Category.class, Category.LIBRARY));
124126
attr.attribute(Bundling.BUNDLING_ATTRIBUTE, project.getObjects().named(Bundling.class, Bundling.EXTERNAL));
125127
});
126128
return config.resolve();

‎plugin-maven/CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
44

55
## [Unreleased]
6+
### Added
7+
* Support Ktlint 1.0.0 ([#1808](https://github.com/diffplug/spotless/pull/1808)).
68

79
### Fixed
810
* Added support for plugins when using Prettier version `3.0.0` and newer. ([#1802](https://github.com/diffplug/spotless/pull/1802))

‎plugin-maven/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ Additionally, `editorConfigOverride` options will override what's supplied in `.
409409

410410
```xml
411411
<ktlint>
412-
<version>0.43.2</version> <!-- optional -->
412+
<version>1.0.0</version> <!-- optional -->
413413
<editorConfigOverride> <!-- optional -->
414414
<ij_kotlin_allow_trailing_comma>true</ij_kotlin_allow_trailing_comma>
415415
<ij_kotlin_allow_trailing_comma_on_call_site>true</ij_kotlin_allow_trailing_comma_on_call_site>

‎plugin-maven/src/test/java/com/diffplug/spotless/maven/kotlin/KtlintTest.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2022 DiffPlug
2+
* Copyright 2016-2023 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,7 +32,12 @@ void testKtlint() throws Exception {
3232

3333
@Test
3434
void testKtlintEditorConfigOverride() throws Exception {
35-
writePomWithKotlinSteps("<ktlint><editorConfigOverride><ij_kotlin_allow_trailing_comma>true</ij_kotlin_allow_trailing_comma><ij_kotlin_allow_trailing_comma_on_call_site>true</ij_kotlin_allow_trailing_comma_on_call_site></editorConfigOverride></ktlint>");
35+
writePomWithKotlinSteps("<ktlint>\n" +
36+
" <editorConfigOverride>\n" +
37+
" <ij_kotlin_allow_trailing_comma>true</ij_kotlin_allow_trailing_comma>\n" +
38+
" <ij_kotlin_allow_trailing_comma_on_call_site>true</ij_kotlin_allow_trailing_comma_on_call_site>\n" +
39+
" </editorConfigOverride>\n" +
40+
"</ktlint>");
3641

3742
String path = "src/main/kotlin/Main.kt";
3843
setFile(path).toResource("kotlin/ktlint/experimentalEditorConfigOverride.dirty");

‎testlib/src/main/java/com/diffplug/spotless/TestProvisioner.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 DiffPlug
2+
* Copyright 2016-2023 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,6 +32,7 @@
3232
import org.gradle.api.artifacts.ResolveException;
3333
import org.gradle.api.artifacts.dsl.RepositoryHandler;
3434
import org.gradle.api.attributes.Bundling;
35+
import org.gradle.api.attributes.Category;
3536
import org.gradle.testfixtures.ProjectBuilder;
3637

3738
import com.diffplug.common.base.Errors;
@@ -70,6 +71,7 @@ private static Provisioner createWithRepositories(Consumer<RepositoryHandler> re
7071
config.setTransitive(withTransitives);
7172
config.setDescription(mavenCoords.toString());
7273
config.attributes(attr -> {
74+
attr.attribute(Category.CATEGORY_ATTRIBUTE, project.getObjects().named(Category.class, Category.LIBRARY));
7375
attr.attribute(Bundling.BUNDLING_ATTRIBUTE, project.getObjects().named(Bundling.class, Bundling.EXTERNAL));
7476
});
7577
try {

‎testlib/src/test/java/com/diffplug/spotless/kotlin/KtLintStepTest.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,24 @@ void works0_50_0() {
7474
"Wildcard import");
7575
}
7676

77+
@Test
78+
void works1_0_0() {
79+
FormatterStep step = KtLintStep.create("1.0.0", TestProvisioner.mavenCentral());
80+
StepHarnessWithFile.forStep(this, step)
81+
.testResource("kotlin/ktlint/basic.dirty", "kotlin/ktlint/basic.clean")
82+
.testResourceExceptionMsg("kotlin/ktlint/unsolvable.dirty").isEqualTo("Error on line: 1, column: 1\n" +
83+
"rule: standard:no-empty-file\n" +
84+
"File 'unsolvable.dirty' should not be empty");
85+
}
86+
7787
@Test
7888
void behavior() {
7989
FormatterStep step = KtLintStep.create(TestProvisioner.mavenCentral());
8090
StepHarnessWithFile.forStep(this, step)
8191
.testResource("kotlin/ktlint/basic.dirty", "kotlin/ktlint/basic.clean")
8292
.testResourceExceptionMsg("kotlin/ktlint/unsolvable.dirty").isEqualTo("Error on line: 1, column: 1\n" +
83-
"rule: standard:no-wildcard-imports\n" +
84-
"Wildcard import");
93+
"rule: standard:no-empty-file\n" +
94+
"File 'unsolvable.dirty' should not be empty");
8595
}
8696

8797
@Test

0 commit comments

Comments
 (0)
Please sign in to comment.