Skip to content

Commit

Permalink
Add more Ktlint EditorConfig test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Goooler committed Oct 30, 2023
1 parent 0270ad0 commit 4b747b3
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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("<ktlint/>");
checkKtlintOfficialStyle();
}

@Test
void testSetEditorConfigCanOverrideEditorConfigFile() throws Exception {
setFile(".editorconfig").toResource("kotlin/ktlint/intellij_idea/.editorconfig");
writePomWithKotlinSteps("<ktlint>\n" +
" <editorConfigOverride>\n" +
" <ktlint_code_style>ktlint_official</ktlint_code_style>\n" +
" </editorConfigOverride>\n" +
"</ktlint>");
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");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fun main() {
val list =
listOf(
"hello",
)
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4b747b3

Please sign in to comment.