Skip to content

Commit 959fd85

Browse files
committed
Check if EditorConfig file exist for Ktlint
1 parent 060a202 commit 959fd85

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

plugin-gradle/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
66
### Added
77
* Add a `jsonPatch` step to `json` formatter configurations. This allows patching of JSON documents using [JSON Patches](https://jsonpatch.com). ([#1753](https://github.com/diffplug/spotless/pull/1753))
88
* Support GJF own import order. ([#1780](https://github.com/diffplug/spotless/pull/1780))
9+
* Check if EditorConfig file exist for Ktlint. ([#1788](https://github.com/diffplug/spotless/pull/1788)
910
### Fixed
1011
* Add support for `prettier` version `3.0.0` and newer. ([#1760]https://github.com/diffplug/spotless/pull/1760), [#1751](https://github.com/diffplug/spotless/issues/1751))
1112
* Fix npm install calls when npm cache is not up-to-date. ([#1760]https://github.com/diffplug/spotless/pull/1760), [#1750](https://github.com/diffplug/spotless/issues/1750))

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinExtension.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,15 @@ public class KotlinFormatExtension {
8585
addStep(createStep());
8686
}
8787

88-
public KotlinFormatExtension setEditorConfigPath(Object editorConfigFile) throws IOException {
89-
if (editorConfigFile == null) {
88+
public KotlinFormatExtension setEditorConfigPath(Object editorConfigPath) throws IOException {
89+
if (editorConfigPath == null) {
9090
this.editorConfigPath = null;
9191
} else {
92-
this.editorConfigPath = FileSignature.signAsList(getProject().file(editorConfigFile));
92+
File editorConfigFile = getProject().file(editorConfigPath);
93+
if (!editorConfigFile.exists()) {
94+
throw new IllegalArgumentException("EditorConfig file does not exist: " + editorConfigFile);
95+
}
96+
this.editorConfigPath = FileSignature.signAsList(editorConfigFile);
9397
}
9498
replaceStep(createStep());
9599
return this;

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinExtensionTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
package com.diffplug.gradle.spotless;
1717

18+
import static org.junit.jupiter.api.Assertions.assertTrue;
19+
20+
import java.io.File;
1821
import java.io.IOException;
1922

2023
import org.junit.jupiter.api.Test;
@@ -81,6 +84,27 @@ void withExperimentalEditorConfigOverride() throws IOException {
8184
assertFile("src/main/kotlin/Main.kt").sameAsResource("kotlin/ktlint/experimentalEditorConfigOverride.clean");
8285
}
8386

87+
@Test
88+
void testWithInvalidEditorConfigFile() throws IOException {
89+
String invalidPath = "invalid/path/to/.editorconfig".replace('/', File.separatorChar);
90+
91+
setFile("build.gradle").toLines(
92+
"plugins {",
93+
" id 'org.jetbrains.kotlin.jvm' version '1.5.31'",
94+
" id 'com.diffplug.spotless'",
95+
"}",
96+
"repositories { mavenCentral() }",
97+
"spotless {",
98+
" kotlin {",
99+
" ktlint().setEditorConfigPath('" + invalidPath + "')",
100+
" }",
101+
"}");
102+
setFile("src/main/kotlin/Main.kt").toResource("kotlin/ktlint/experimentalEditorConfigOverride.dirty");
103+
String buildOutput = gradleRunner().withArguments("spotlessApply").buildAndFail().getOutput();
104+
assertTrue(buildOutput.contains("EditorConfig file does not exist: "));
105+
assertTrue(buildOutput.contains(invalidPath));
106+
}
107+
84108
@Test
85109
void testWithHeader() throws IOException {
86110
setFile("build.gradle").toLines(

0 commit comments

Comments
 (0)