Skip to content

Commit e2a5b92

Browse files
committed
Move isolated project tests to separate file and fix ktlint incompatibility
1 parent c336483 commit e2a5b92

File tree

3 files changed

+90
-36
lines changed

3 files changed

+90
-36
lines changed

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/BaseKotlinExtension.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ private KtlintConfig(
160160
Map<String, Object> editorConfigOverride,
161161
List<String> customRuleSets) throws IOException {
162162
Objects.requireNonNull(version);
163-
File defaultEditorConfig = getProject().getRootProject().getLayout().getProjectDirectory().file(".editorconfig").getAsFile();
163+
File defaultEditorConfig = new File(getProject().getRootProject().getProjectDir(), ".editorconfig");
164164
FileSignature editorConfigPath = defaultEditorConfig.exists() ? FileSignature.signAsList(defaultEditorConfig) : null;
165165
this.version = version;
166166
this.editorConfigPath = editorConfigPath;
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.diffplug.gradle.spotless;
2+
3+
import com.diffplug.common.base.StringPrinter;
4+
5+
import org.assertj.core.api.Assertions;
6+
import org.gradle.testkit.runner.GradleRunner;
7+
import org.junit.jupiter.api.Test;
8+
9+
import java.io.IOException;
10+
11+
class IsolatedProjectTest extends GradleIntegrationHarness {
12+
private static final int N = 10;
13+
14+
@Override
15+
public GradleRunner gradleRunner() throws IOException {
16+
setFile("gradle.properties").toContent("org.gradle.unsafe.isolated-projects=true");
17+
return super.gradleRunner();
18+
}
19+
20+
private void createNSubprojects() throws IOException {
21+
for (int i = 0; i < N; i++) {
22+
createSubproject(Integer.toString(i));
23+
}
24+
String settings = StringPrinter.buildString(printer -> {
25+
for (int i = 0; i < N; i++) {
26+
printer.println("include '" + i + "'");
27+
}
28+
});
29+
setFile("settings.gradle").toContent(settings);
30+
}
31+
32+
void createSubproject(String name) throws IOException {
33+
setFile(name + "/build.gradle").toLines(
34+
"plugins {",
35+
" id 'com.diffplug.spotless'",
36+
"}",
37+
"repositories { mavenCentral() }",
38+
"spotless {",
39+
" kotlin {",
40+
" target file('Test.kt')",
41+
" ktlint()",
42+
" }",
43+
"}");
44+
setFile(name + "/Test.kt").toResource("kotlin/ktlint/basic.dirty");
45+
}
46+
47+
@Test
48+
void rootIsSupported() throws IOException {
49+
setFile("build.gradle").toLines(
50+
"plugins {",
51+
" id 'com.diffplug.spotless'",
52+
"}",
53+
"repositories { mavenCentral() }",
54+
"spotless {",
55+
" kotlin {",
56+
" target file('Test.kt')",
57+
" ktlint()",
58+
" }",
59+
"}");
60+
setFile("Test.kt").toResource("kotlin/ktlint/basic.dirty");
61+
createNSubprojects();
62+
gradleRunner().withArguments("spotlessApply").build();
63+
}
64+
65+
@Test
66+
void noRootIsSupported() throws IOException {
67+
setFile("build.gradle").toLines();
68+
createNSubprojects();
69+
gradleRunner().withArguments("spotlessApply").build();
70+
}
71+
72+
@Test
73+
void predeclaredIsUnsupported() throws IOException {
74+
setFile("build.gradle").toLines(
75+
"plugins {",
76+
" id 'com.diffplug.spotless'",
77+
"}",
78+
"repositories { mavenCentral() }",
79+
"spotless { predeclareDeps() }",
80+
"spotlessPredeclare {",
81+
" kotlin { ktlint() }",
82+
"}");
83+
createNSubprojects();
84+
Assertions.assertThat(gradleRunner().withArguments("spotlessApply").buildAndFail().getOutput())
85+
.containsAnyOf("Cannot access project", "cannot access 'Project.tasks'");
86+
}
87+
}
88+

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/MultiProjectTest.java

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2026 DiffPlug
2+
* Copyright 2016-2025 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -151,38 +151,4 @@ public void predeclaredUndeclared() throws IOException {
151151
Assertions.assertThat(gradleRunner().withArguments("spotlessApply").buildAndFail().getOutput())
152152
.contains("Could not find method spotlessPredeclare() for arguments");
153153
}
154-
155-
@Test
156-
void nonPredeclaredSupportsIsolatedProjects() throws IOException {
157-
setFile("build.gradle").toLines(
158-
"plugins {",
159-
" id 'com.diffplug.spotless'",
160-
"}",
161-
"repositories { mavenCentral() }",
162-
"",
163-
"spotless {",
164-
" java {",
165-
" target file('test.java')",
166-
" googleJavaFormat('1.17.0')",
167-
" }",
168-
"}");
169-
createNSubprojects();
170-
gradleRunner().withArguments("spotlessApply", "-Dorg.gradle.unsafe.isolated-projects=true").build();
171-
}
172-
173-
@Test
174-
void predeclaredRequiresNonIsolatedProjects() throws IOException {
175-
setFile("build.gradle").toLines(
176-
"plugins {",
177-
" id 'com.diffplug.spotless'",
178-
"}",
179-
"repositories { mavenCentral() }",
180-
"spotless { predeclareDeps() }",
181-
"spotlessPredeclare {",
182-
" java { googleJavaFormat('1.17.0') }",
183-
"}");
184-
createNSubprojects();
185-
Assertions.assertThat(gradleRunner().withArguments("spotlessApply", "-Dorg.gradle.unsafe.isolated-projects=true").buildAndFail().getOutput())
186-
.containsAnyOf("Cannot access project", "cannot access 'Project.tasks'");
187-
}
188154
}

0 commit comments

Comments
 (0)