Skip to content

Commit 2519190

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

3 files changed

Lines changed: 104 additions & 36 deletions

File tree

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: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright 2026 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.gradle.spotless;
17+
18+
import java.io.IOException;
19+
20+
import org.assertj.core.api.Assertions;
21+
import org.gradle.testkit.runner.GradleRunner;
22+
import org.junit.jupiter.api.Test;
23+
24+
import com.diffplug.common.base.StringPrinter;
25+
26+
class IsolatedProjectTest extends GradleIntegrationHarness {
27+
private static final int N = 10;
28+
29+
@Override
30+
public GradleRunner gradleRunner() throws IOException {
31+
setFile("gradle.properties").toContent("org.gradle.unsafe.isolated-projects=true");
32+
return super.gradleRunner();
33+
}
34+
35+
private void createNSubprojects() throws IOException {
36+
for (int i = 0; i < N; i++) {
37+
createSubproject(Integer.toString(i));
38+
}
39+
String settings = StringPrinter.buildString(printer -> {
40+
for (int i = 0; i < N; i++) {
41+
printer.println("include '" + i + "'");
42+
}
43+
});
44+
setFile("settings.gradle").toContent(settings);
45+
}
46+
47+
void createSubproject(String name) throws IOException {
48+
setFile(name + "/build.gradle").toLines(
49+
"plugins {",
50+
" id 'com.diffplug.spotless'",
51+
"}",
52+
"repositories { mavenCentral() }",
53+
"spotless {",
54+
" kotlin {",
55+
" target file('Test.kt')",
56+
" ktlint()",
57+
" }",
58+
"}");
59+
setFile(name + "/Test.kt").toResource("kotlin/ktlint/basic.dirty");
60+
}
61+
62+
@Test
63+
void rootIsSupported() throws IOException {
64+
setFile("build.gradle").toLines(
65+
"plugins {",
66+
" id 'com.diffplug.spotless'",
67+
"}",
68+
"repositories { mavenCentral() }",
69+
"spotless {",
70+
" kotlin {",
71+
" target file('Test.kt')",
72+
" ktlint()",
73+
" }",
74+
"}");
75+
setFile("Test.kt").toResource("kotlin/ktlint/basic.dirty");
76+
createNSubprojects();
77+
gradleRunner().withArguments("spotlessApply").build();
78+
}
79+
80+
@Test
81+
void noRootIsSupported() throws IOException {
82+
setFile("build.gradle").toLines();
83+
createNSubprojects();
84+
gradleRunner().withArguments("spotlessApply").build();
85+
}
86+
87+
@Test
88+
void predeclaredIsUnsupported() throws IOException {
89+
setFile("build.gradle").toLines(
90+
"plugins {",
91+
" id 'com.diffplug.spotless'",
92+
"}",
93+
"repositories { mavenCentral() }",
94+
"spotless { predeclareDeps() }",
95+
"spotlessPredeclare {",
96+
" kotlin { ktlint() }",
97+
"}");
98+
createNSubprojects();
99+
Assertions.assertThat(gradleRunner().withArguments("spotlessApply").buildAndFail().getOutput())
100+
.containsAnyOf("Cannot access project", "cannot access 'Project.tasks'");
101+
}
102+
}

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)