From 959fd854c36080caa13123de2089ebcb7396adb4 Mon Sep 17 00:00:00 2001 From: Goooler Date: Thu, 24 Aug 2023 09:42:44 +0800 Subject: [PATCH 1/3] Check if EditorConfig file exist for Ktlint --- plugin-gradle/CHANGES.md | 1 + .../gradle/spotless/KotlinExtension.java | 10 +++++--- .../gradle/spotless/KotlinExtensionTest.java | 24 +++++++++++++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index 1ce2f06a42..27d60e7270 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -6,6 +6,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ### Added * 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)) * Support GJF own import order. ([#1780](https://github.com/diffplug/spotless/pull/1780)) +* Check if EditorConfig file exist for Ktlint. ([#1788](https://github.com/diffplug/spotless/pull/1788) ### Fixed * 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)) * 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)) diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinExtension.java index f2c3a29d75..27d93717f4 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinExtension.java @@ -85,11 +85,15 @@ public class KotlinFormatExtension { addStep(createStep()); } - public KotlinFormatExtension setEditorConfigPath(Object editorConfigFile) throws IOException { - if (editorConfigFile == null) { + public KotlinFormatExtension setEditorConfigPath(Object editorConfigPath) throws IOException { + if (editorConfigPath == null) { this.editorConfigPath = null; } else { - this.editorConfigPath = FileSignature.signAsList(getProject().file(editorConfigFile)); + File editorConfigFile = getProject().file(editorConfigPath); + if (!editorConfigFile.exists()) { + throw new IllegalArgumentException("EditorConfig file does not exist: " + editorConfigFile); + } + this.editorConfigPath = FileSignature.signAsList(editorConfigFile); } replaceStep(createStep()); return this; diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinExtensionTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinExtensionTest.java index 10fbe5bd90..98e2259be4 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinExtensionTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinExtensionTest.java @@ -15,6 +15,9 @@ */ package com.diffplug.gradle.spotless; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.File; import java.io.IOException; import org.junit.jupiter.api.Test; @@ -81,6 +84,27 @@ void withExperimentalEditorConfigOverride() throws IOException { assertFile("src/main/kotlin/Main.kt").sameAsResource("kotlin/ktlint/experimentalEditorConfigOverride.clean"); } + @Test + void testWithInvalidEditorConfigFile() throws IOException { + String invalidPath = "invalid/path/to/.editorconfig".replace('/', File.separatorChar); + + setFile("build.gradle").toLines( + "plugins {", + " id 'org.jetbrains.kotlin.jvm' version '1.5.31'", + " id 'com.diffplug.spotless'", + "}", + "repositories { mavenCentral() }", + "spotless {", + " kotlin {", + " ktlint().setEditorConfigPath('" + invalidPath + "')", + " }", + "}"); + setFile("src/main/kotlin/Main.kt").toResource("kotlin/ktlint/experimentalEditorConfigOverride.dirty"); + String buildOutput = gradleRunner().withArguments("spotlessApply").buildAndFail().getOutput(); + assertTrue(buildOutput.contains("EditorConfig file does not exist: ")); + assertTrue(buildOutput.contains(invalidPath)); + } + @Test void testWithHeader() throws IOException { setFile("build.gradle").toLines( From b3c59f7d4b9e214c3f4c14d9333b816e3a90a34b Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Thu, 24 Aug 2023 12:31:36 -0700 Subject: [PATCH 2/3] Fix on windows. --- .../com/diffplug/gradle/spotless/KotlinExtensionTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinExtensionTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinExtensionTest.java index 98e2259be4..1c23728244 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinExtensionTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinExtensionTest.java @@ -15,7 +15,7 @@ */ package com.diffplug.gradle.spotless; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; import java.io.File; import java.io.IOException; @@ -96,13 +96,13 @@ void testWithInvalidEditorConfigFile() throws IOException { "repositories { mavenCentral() }", "spotless {", " kotlin {", - " ktlint().setEditorConfigPath('" + invalidPath + "')", + " ktlint().setEditorConfigPath('" + invalidPath.replace("\\", "\\\\") + "')", " }", "}"); setFile("src/main/kotlin/Main.kt").toResource("kotlin/ktlint/experimentalEditorConfigOverride.dirty"); String buildOutput = gradleRunner().withArguments("spotlessApply").buildAndFail().getOutput(); - assertTrue(buildOutput.contains("EditorConfig file does not exist: ")); - assertTrue(buildOutput.contains(invalidPath)); + assertThat(buildOutput).contains("EditorConfig file does not exist: "); + assertThat(buildOutput).contains(invalidPath); } @Test From 62a957e51b723b3572bbe4beef9255e910d9c036 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Thu, 24 Aug 2023 12:31:48 -0700 Subject: [PATCH 3/3] Categorize as fix rather than new feature. --- plugin-gradle/CHANGES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index 27d60e7270..2a5bd5db46 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -6,12 +6,12 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ### Added * 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)) * Support GJF own import order. ([#1780](https://github.com/diffplug/spotless/pull/1780)) -* Check if EditorConfig file exist for Ktlint. ([#1788](https://github.com/diffplug/spotless/pull/1788) ### Fixed * 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)) * 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)) * Fix configuration cache failure when using LineEnding.GIT_ATTRIBUTES ([#1644](https://github.com/diffplug/spotless/issues/1644)) * Fix configuration cache failure when formatting proto files with Buf. ([#1779]https://github.com/diffplug/spotless/pull/1779)) +* Check if EditorConfig file exist for Ktlint. ([#1788](https://github.com/diffplug/spotless/pull/1788) ### Changes * Bump default `eslint` version to latest `8.31.0` -> `8.45.0` ([#1761](https://github.com/diffplug/spotless/pull/1761)) * Bump default `prettier` version to latest (v2) `2.8.1` -> `2.8.8`. ([#1760](https://github.com/diffplug/spotless/pull/1760))