Skip to content

Commit 5a0443c

Browse files
Excavator: Migrate Groovy nebula test PalantirJavaFormatSpotlessPluginTest to the new Java Junit framework
1 parent 3f82777 commit 5a0443c

File tree

6 files changed

+4910
-178
lines changed

6 files changed

+4910
-178
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ buildscript {
1717
classpath 'com.palantir.gradle.idea-language-injector:gradle-idea-language-injector:0.2.0'
1818
classpath 'com.palantir.gradle.jdks:gradle-jdks:0.69.0'
1919
classpath 'com.palantir.gradle.jdkslatest:gradle-jdks-latest:0.24.0'
20-
classpath 'com.palantir.gradle.plugintesting:gradle-plugin-testing:0.6.0'
20+
classpath 'com.palantir.gradle.plugintesting:gradle-plugin-testing:0.39.0'
2121
classpath 'com.palantir.javaformat:gradle-palantir-java-format:2.82.0'
2222
classpath 'com.palantir.suppressible-error-prone:gradle-suppressible-error-prone:2.26.0'
2323
classpath 'me.champeau.jmh:jmh-gradle-plugin:0.7.3'

gradle-palantir-java-format/src/test/groovy/com/palantir/javaformat/gradle/PalantirJavaFormatSpotlessPluginTest.groovy

Lines changed: 0 additions & 154 deletions
This file was deleted.
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
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+
import static org.assertj.core.api.Assertions.assertThat;
20+
21+
import com.palantir.gradle.testing.execution.GradleInvoker;
22+
import com.palantir.gradle.testing.execution.InvocationResult;
23+
import com.palantir.gradle.testing.junit.GradlePluginTests;
24+
import com.palantir.gradle.testing.project.RootProject;
25+
import java.io.File;
26+
import java.io.IOException;
27+
import java.io.UncheckedIOException;
28+
import java.nio.file.Files;
29+
import org.junit.jupiter.params.ParameterizedTest;
30+
import org.junit.jupiter.params.provider.CsvSource;
31+
32+
@GradlePluginTests
33+
@SuppressWarnings({
34+
"GradleTestPluginsBlock", // These plugins are loaded via buildscript classpath, not plugin portal
35+
"GradleTestStringFormatting" // Suppress due to nullable parameters from parameterized test
36+
})
37+
class PalantirJavaFormatSpotlessPluginTest {
38+
/** ./gradlew writeImplClasspath generates this file. */
39+
private static final String CLASSPATH_FILE = new File("build/impl.classpath").getAbsolutePath();
40+
41+
private static final File NATIVE_IMAGE_FILE = new File("build/nativeImage.path");
42+
43+
private static String getNativeConfig() {
44+
try {
45+
return String.format(
46+
"palantirJavaFormatNative files(\"%s\")", Files.readString(NATIVE_IMAGE_FILE.toPath()));
47+
} catch (IOException e) {
48+
throw new UncheckedIOException("Failed to read native image path", e);
49+
}
50+
}
51+
52+
@ParameterizedTest(name = "[{index}] extraGradleProperties={0}, javaVersion={1}, expectedOutput={2}")
53+
@CsvSource(delimiter = '|', textBlock = """
54+
#extraGradleProperties | javaVersion | expectedOutput
55+
| 21 | Using the Java-based formatter
56+
palantir.native.formatter=true | 21 | Using the Java-based formatter
57+
palantir.native.formatter=true | 17 | Using the native-image formatter
58+
""")
59+
void formats_with_spotless_when_spotless_is_applied(
60+
String extraGradleProperties,
61+
String javaVersion,
62+
String expectedOutput,
63+
GradleInvoker gradle,
64+
RootProject rootProject)
65+
throws IOException {
66+
String extraDependencies =
67+
(extraGradleProperties == null || extraGradleProperties.isEmpty()) ? "" : getNativeConfig();
68+
69+
rootProject.settingsGradle().append("""
70+
buildscript {
71+
repositories {
72+
mavenCentral() { metadataSources { mavenPom(); ignoreGradleMetadataRedirection() } }
73+
gradlePluginPortal() { metadataSources { mavenPom(); ignoreGradleMetadataRedirection() } }
74+
}
75+
dependencies {
76+
classpath 'com.palantir.gradle.jdks:gradle-jdks-settings:0.62.0'
77+
}
78+
}
79+
""");
80+
81+
// Apply plugin from buildscript classpath (cannot use .plugins().add() for buildscript-loaded plugins)
82+
String jdksSettingsPlugin = "apply plugin: 'com.palantir.jdks.settings'";
83+
rootProject.settingsGradle().append(jdksSettingsPlugin);
84+
85+
rootProject.buildGradle().append("""
86+
buildscript {
87+
repositories {
88+
mavenCentral() { metadataSources { mavenPom(); ignoreGradleMetadataRedirection() } }
89+
gradlePluginPortal() { metadataSources { mavenPom(); ignoreGradleMetadataRedirection() } }
90+
}
91+
dependencies {
92+
classpath 'com.palantir.baseline:gradle-baseline-java:6.21.0'
93+
classpath 'com.palantir.gradle.jdks:gradle-jdks:0.62.0'
94+
classpath 'com.palantir.gradle.jdkslatest:gradle-jdks-latest:0.17.0'
95+
96+
constraints {
97+
classpath 'com.diffplug.spotless:6.22.0'
98+
}
99+
}
100+
}
101+
""");
102+
103+
rootProject.buildGradle().plugins().add("java");
104+
105+
// Apply plugins from buildscript classpath (cannot use .plugins().add() for buildscript-loaded plugins)
106+
String javaFormatPlugin = "apply plugin: 'com.palantir.java-format'";
107+
rootProject.buildGradle().append(javaFormatPlugin);
108+
109+
String baselineJavaVersionsPlugin = "apply plugin: 'com.palantir.baseline-java-versions'";
110+
rootProject.buildGradle().append(baselineJavaVersionsPlugin);
111+
112+
String jdksPlugin = "apply plugin: 'com.palantir.jdks'";
113+
rootProject.buildGradle().append(jdksPlugin);
114+
115+
String jdksLatestPlugin = "apply plugin: 'com.palantir.jdks.latest'";
116+
rootProject.buildGradle().append(jdksLatestPlugin);
117+
118+
rootProject.buildGradle().append("""
119+
120+
javaVersions {
121+
libraryTarget = %s
122+
}
123+
124+
jdks {
125+
daemonTarget = %s
126+
}
127+
""", javaVersion, javaVersion);
128+
129+
// Add jvm args to allow spotless and formatter gradle plugins to run with Java 16+
130+
rootProject.gradlePropertiesFile().append("""
131+
org.gradle.jvmargs=--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED \\
132+
--add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED \\
133+
--add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED \\
134+
--add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED \\
135+
--add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
136+
palantir.jdk.setup.enabled=true
137+
""");
138+
139+
if (extraGradleProperties != null && !extraGradleProperties.isEmpty()) {
140+
rootProject.gradlePropertiesFile().append(extraGradleProperties);
141+
}
142+
143+
// Run wrapper task to set up the Gradle wrapper
144+
gradle.withArgs("wrapper").buildsSuccessfully();
145+
146+
String spotlessPlugin = "apply plugin: 'com.diffplug.spotless'";
147+
rootProject.buildGradle().append(spotlessPlugin);
148+
149+
rootProject.buildGradle().append("""
150+
dependencies {
151+
palantirJavaFormat files(file("%s").text.split(':'))
152+
%s
153+
}
154+
""", CLASSPATH_FILE, extraDependencies);
155+
156+
rootProject.file("src/main/java/Main.java").overwrite(invalidJavaFile());
157+
158+
InvocationResult result = gradle.withArgs("spotlessApply", "--info").buildsSuccessfully();
159+
String formattedFile = rootProject.file("src/main/java/Main.java").text();
160+
161+
assertThat(result).output().contains(expectedOutput);
162+
assertThat(formattedFile).isEqualTo(validJavaFile());
163+
}
164+
165+
private String validJavaFile() {
166+
return """
167+
package test;
168+
169+
public class Test {
170+
void test() {
171+
int x = 1;
172+
System.out.println("Hello");
173+
Optional.of("hello").orElseGet(() -> {
174+
return "Hello World";
175+
});
176+
}
177+
}
178+
""";
179+
}
180+
181+
private String invalidJavaFile() {
182+
return """
183+
package test;
184+
import com.java.unused;
185+
public class Test { void test() {int x = 1;
186+
System.out.println(
187+
"Hello"
188+
);
189+
Optional.of("hello").orElseGet(() -> {
190+
return "Hello World";
191+
});
192+
} }
193+
""";
194+
}
195+
}

0 commit comments

Comments
 (0)