Skip to content

Commit 10a95e0

Browse files
authored
Fix Spotless targetExclude patterns being overridden (palantir#1690)
Fix Spotless targetExclude patterns being overridden
1 parent dd4a4f3 commit 10a95e0

2 files changed

Lines changed: 126 additions & 10 deletions

File tree

‎gradle-palantir-java-format/src/main/java/com/palantir/javaformat/gradle/SpotlessInterop.java‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,17 @@ public SpotlessInterop(Supplier<FormatterService> formatterService) {
5252
@Override
5353
public void execute(JavaExtension java) {
5454
// Exclude generated source directories
55-
// Note: We cannot simply exclude **/build/**/* because some repos might contain a build directory
56-
// within the src sourceset that should be formatted.
57-
java.targetExclude("**/build/generated*/**");
58-
java.targetExclude("**/src/generated*/**");
59-
java.targetExclude("**/generated_*src/**");
60-
java.targetExclude("**/generated_*Src/**");
61-
// build/groovy-dsl-plugins contains Java wrapper classes for
62-
// https://docs.gradle.org/9.3.1/userguide/implementing_gradle_plugins_convention.html
63-
// and don't need to be formatted.
64-
java.targetExclude("**/groovy-dsl-plugins/**");
55+
java.targetExclude(
56+
// Note: We cannot simply exclude **/build/**/* because some repos might contain a build directory
57+
// within the src sourceset that should be formatted.
58+
"**/build/generated*/**",
59+
"**/src/generated*/**",
60+
"**/generated_*src/**",
61+
"**/generated_*Src/**",
62+
// build/groovy-dsl-plugins contains Java wrapper classes for
63+
// https://docs.gradle.org/9.3.1/userguide/implementing_gradle_plugins_convention.html
64+
// and don't need to be formatted.
65+
"**/groovy-dsl-plugins/**");
6566
// This is configuration cache safe as happening afterEvaluate
6667
java.addStep(spotlessJavaFormatStep());
6768
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* (c) Copyright 2019 Palantir Technologies Inc. All rights reserved.
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.palantir.javaformat.gradle;
17+
18+
import static com.palantir.gradle.testing.assertion.GradlePluginTestAssertions.assertThat;
19+
20+
import com.palantir.gradle.testing.execution.GradleInvoker;
21+
import com.palantir.gradle.testing.execution.InvocationResult;
22+
import com.palantir.gradle.testing.junit.DisabledConfigurationCache;
23+
import com.palantir.gradle.testing.junit.GradlePluginTests;
24+
import com.palantir.gradle.testing.project.RootProject;
25+
import java.io.File;
26+
import org.junit.jupiter.api.BeforeEach;
27+
import org.junit.jupiter.params.ParameterizedTest;
28+
import org.junit.jupiter.params.provider.ValueSource;
29+
30+
@GradlePluginTests
31+
@DisabledConfigurationCache
32+
class SpotlessExcludesTest {
33+
34+
private static final String CLASSPATH_FILE = new File("build/impl.classpath").getAbsolutePath();
35+
36+
@BeforeEach
37+
void setup(RootProject project) {
38+
project.buildGradle()
39+
.plugins()
40+
.add("java")
41+
.add("com.palantir.java-format")
42+
.add("com.diffplug.spotless");
43+
44+
project.buildGradle().append("""
45+
dependencies {
46+
palantirJavaFormat files(file("%s").text.split(':'))
47+
}
48+
""", CLASSPATH_FILE);
49+
// Add jvm args to allow spotless and formatter gradle plugins to run with Java 16+
50+
project.gradlePropertiesFile()
51+
.appendProperty(
52+
"org.gradle.jvmargs",
53+
"--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED "
54+
+ "--add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED "
55+
+ "--add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED "
56+
+ "--add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED "
57+
+ "--add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED");
58+
}
59+
60+
@ParameterizedTest
61+
@ValueSource(
62+
strings = {
63+
"build/generated/java",
64+
"src/generated/java",
65+
"src/main/generated_testsrc",
66+
"src/main/generated_foo_testsrc",
67+
"generated_testSrc",
68+
"build/groovy-dsl-plugins/output"
69+
})
70+
void format_ignores_excluded_directories(String srcDir, GradleInvoker gradle, RootProject project) {
71+
project.buildGradle().append("""
72+
sourceSets {
73+
main {
74+
java { srcDir '%s' }
75+
}
76+
}
77+
""", srcDir);
78+
79+
project.file(srcDir + "/test/Test.java").overwrite("""
80+
package test;
81+
import java.lang.Void;
82+
public class Test { Void test() { return null; } }
83+
""");
84+
85+
InvocationResult result = gradle.withArgs("spotlessJavaCheck").buildsSuccessfully();
86+
87+
assertThat(result).task(":spotlessJava").succeeded();
88+
}
89+
90+
@ParameterizedTest
91+
@ValueSource(
92+
strings = {
93+
"foo/generated_foo/src/bar",
94+
"src/main/java/test/generatedNamespace",
95+
})
96+
void format_checks_non_generated_files(String srcDir, GradleInvoker gradle, RootProject project) {
97+
project.buildGradle().append("""
98+
sourceSets {
99+
main {
100+
java { srcDir '%s' }
101+
}
102+
}
103+
""", srcDir);
104+
105+
project.file(srcDir + "/Test.java").overwrite("""
106+
package test;
107+
import java.lang.Void;
108+
public class Test { Void test() { return null; } }
109+
""");
110+
111+
InvocationResult result = gradle.withArgs("spotlessJavaCheck").buildsWithFailure();
112+
113+
assertThat(result).task(":spotlessJava").succeeded();
114+
}
115+
}

0 commit comments

Comments
 (0)