Skip to content

Commit

Permalink
Check if EditorConfig file exist for Ktlint
Browse files Browse the repository at this point in the history
  • Loading branch information
Goooler committed Aug 24, 2023
1 parent 060a202 commit ade5c71
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
1 change: 1 addition & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
### 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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@

import java.io.IOException;

import org.gradle.testkit.runner.BuildResult;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertTrue;

class KotlinExtensionTest extends GradleIntegrationHarness {
private static final String HEADER = "// License Header";
private static final String HEADER_WITH_YEAR = "// License Header $YEAR";
Expand Down Expand Up @@ -81,6 +84,25 @@ void withExperimentalEditorConfigOverride() throws IOException {
assertFile("src/main/kotlin/Main.kt").sameAsResource("kotlin/ktlint/experimentalEditorConfigOverride.clean");
}

@Test
void testWithInvalidEditorConfigFile() throws IOException {
setFile("build.gradle").toLines(
"plugins {",
" id 'org.jetbrains.kotlin.jvm' version '1.5.31'",
" id 'com.diffplug.spotless'",
"}",
"repositories { mavenCentral() }",
"spotless {",
" kotlin {",
" ktlint().setEditorConfigPath('invalid/path/to/.editorconfig')",
" }",
"}");
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("invalid/path/to/.editorconfig"));
}

@Test
void testWithHeader() throws IOException {
setFile("build.gradle").toLines(
Expand Down

0 comments on commit ade5c71

Please sign in to comment.