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 959fd85
Show file tree
Hide file tree
Showing 3 changed files with 32 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. ([#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))
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 @@ -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;
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 959fd85

Please sign in to comment.