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 1c23728244..7ce1819d60 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 @@ -105,6 +105,42 @@ void testWithInvalidEditorConfigFile() throws IOException { assertThat(buildOutput).contains(invalidPath); } + @Test + void testReadCodeStyleFromEditorConfigFile() throws IOException { + setFile(".editorconfig").toResource("kotlin/ktlint/ktlint_official/.editorconfig"); + setFile("build.gradle").toLines( + "plugins {", + " id 'org.jetbrains.kotlin.jvm' version '1.5.31'", + " id 'com.diffplug.spotless'", + "}", + "repositories { mavenCentral() }", + "spotless {", + " kotlin {", + " ktlint()", + " }", + "}"); + checkKtlintOfficialStyle(); + } + + @Test + void testSetEditorConfigCanOverrideEditorConfigFile() throws IOException { + setFile(".editorconfig").toResource("kotlin/ktlint/intellij_idea/.editorconfig"); + setFile("build.gradle").toLines( + "plugins {", + " id 'org.jetbrains.kotlin.jvm' version '1.5.31'", + " id 'com.diffplug.spotless'", + "}", + "repositories { mavenCentral() }", + "spotless {", + " kotlin {", + " ktlint().editorConfigOverride([", + " ktlint_code_style: \"ktlint_official\",", + " ])", + " }", + "}"); + checkKtlintOfficialStyle(); + } + @Test void testWithHeader() throws IOException { setFile("build.gradle").toLines( @@ -144,4 +180,11 @@ void testWithCustomMaxWidthDefaultStyleKtfmt() throws IOException { gradleRunner().withArguments("spotlessApply").build(); assertFile("src/main/kotlin/max-width.kt").sameAsResource("kotlin/ktfmt/max-width.clean"); } + + private void checkKtlintOfficialStyle() throws IOException { + String path = "src/main/kotlin/Main.kt"; + setFile(path).toResource("kotlin/ktlint/experimentalEditorConfigOverride.dirty"); + gradleRunner().withArguments("spotlessApply").build(); + assertFile(path).sameAsResource("kotlin/ktlint/experimentalEditorConfigOverride.ktlintOfficial.clean"); + } } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/kotlin/KtlintTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/kotlin/KtlintTest.java index 060e0de84f..2916e118a5 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/kotlin/KtlintTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/kotlin/KtlintTest.java @@ -44,4 +44,29 @@ void testKtlintEditorConfigOverride() throws Exception { mavenRunner().withArguments("spotless:apply").runNoError(); assertFile(path).sameAsResource("kotlin/ktlint/experimentalEditorConfigOverride.clean"); } + + @Test + void testReadCodeStyleFromEditorConfigFile() throws Exception { + setFile(".editorconfig").toResource("kotlin/ktlint/ktlint_official/.editorconfig"); + writePomWithKotlinSteps(""); + checkKtlintOfficialStyle(); + } + + @Test + void testSetEditorConfigCanOverrideEditorConfigFile() throws Exception { + setFile(".editorconfig").toResource("kotlin/ktlint/intellij_idea/.editorconfig"); + writePomWithKotlinSteps("\n" + + " \n" + + " ktlint_official\n" + + " \n" + + ""); + checkKtlintOfficialStyle(); + } + + private void checkKtlintOfficialStyle() throws Exception { + String path = "src/main/kotlin/Main.kt"; + setFile(path).toResource("kotlin/ktlint/experimentalEditorConfigOverride.dirty"); + mavenRunner().withArguments("spotless:apply").runNoError(); + assertFile(path).sameAsResource("kotlin/ktlint/experimentalEditorConfigOverride.ktlintOfficial.clean"); + } } diff --git a/testlib/src/main/resources/kotlin/ktlint/experimentalEditorConfigOverride.ktlintOfficial.clean b/testlib/src/main/resources/kotlin/ktlint/experimentalEditorConfigOverride.ktlintOfficial.clean new file mode 100644 index 0000000000..2466bb93fc --- /dev/null +++ b/testlib/src/main/resources/kotlin/ktlint/experimentalEditorConfigOverride.ktlintOfficial.clean @@ -0,0 +1,6 @@ +fun main() { + val list = + listOf( + "hello", + ) +} diff --git a/testlib/src/main/resources/kotlin/ktlint/intellij_idea/.editorconfig b/testlib/src/main/resources/kotlin/ktlint/intellij_idea/.editorconfig new file mode 100644 index 0000000000..9efc62a854 --- /dev/null +++ b/testlib/src/main/resources/kotlin/ktlint/intellij_idea/.editorconfig @@ -0,0 +1,6 @@ +root = true + +[*.{kt,kts}] +ij_kotlin_allow_trailing_comma = true +ij_kotlin_allow_trailing_comma_on_call_site = true +ktlint_code_style = intellij_idea diff --git a/testlib/src/main/resources/kotlin/ktlint/ktlint_official/.editorconfig b/testlib/src/main/resources/kotlin/ktlint/ktlint_official/.editorconfig new file mode 100644 index 0000000000..93a01f2e59 --- /dev/null +++ b/testlib/src/main/resources/kotlin/ktlint/ktlint_official/.editorconfig @@ -0,0 +1,6 @@ +root = true + +[*.{kt,kts}] +ij_kotlin_allow_trailing_comma = true +ij_kotlin_allow_trailing_comma_on_call_site = true +ktlint_code_style = ktlint_official