Skip to content

Commit 1e1cf3b

Browse files
Excavator: Migrate Groovy nebula test PalantirJavaFormatSpotlessPluginTest to the new Java Junit framework (#1482)
1 parent 3732f6b commit 1e1cf3b

7 files changed

Lines changed: 4344 additions & 176 deletions

File tree

build.gradle

Lines changed: 2 additions & 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.70.0'
1919
classpath 'com.palantir.gradle.jdkslatest:gradle-jdks-latest:0.24.0'
20-
classpath 'com.palantir.gradle.plugintesting:gradle-plugin-testing:0.43.0'
20+
classpath 'com.palantir.gradle.plugintesting:gradle-plugin-testing:0.44.0'
2121
classpath 'com.palantir.javaformat:gradle-palantir-java-format:2.83.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'
@@ -48,6 +48,7 @@ allprojects {
4848

4949
repositories {
5050
mavenCentral() { metadataSources { mavenPom(); ignoreGradleMetadataRedirection() } }
51+
gradlePluginPortal()
5152
}
5253
}
5354

gradle-palantir-java-format/build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ dependencies {
3030
testRuntimeOnly 'org.junit.vintage:junit-vintage-engine'
3131

3232
pluginClasspath 'com.diffplug.spotless:spotless-plugin-gradle'
33+
34+
gradlePluginForTesting 'com.palantir.gradle.jdks:gradle-jdks-settings'
35+
gradlePluginForTesting 'com.palantir.baseline:gradle-baseline-java'
36+
gradlePluginForTesting 'com.palantir.gradle.jdkslatest:gradle-jdks-latest'
37+
gradlePluginForTesting 'com.palantir.gradle.jdks:gradle-jdks'
3338
}
3439

3540
gradlePlugin {

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: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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 com.palantir.gradle.testing.execution.GradleInvoker;
19+
import com.palantir.gradle.testing.execution.InvocationResult;
20+
import com.palantir.gradle.testing.junit.DisabledConfigurationCache;
21+
import com.palantir.gradle.testing.junit.GradlePluginTests;
22+
import com.palantir.gradle.testing.project.RootProject;
23+
import java.io.File;
24+
import java.util.Optional;
25+
import org.junit.jupiter.params.ParameterizedTest;
26+
import org.junit.jupiter.params.provider.CsvSource;
27+
28+
@GradlePluginTests
29+
@DisabledConfigurationCache
30+
class PalantirJavaFormatSpotlessPluginTest {
31+
32+
/** ./gradlew writeImplClasspath generates this file. */
33+
private static final String CLASSPATH_FILE = new File("build/impl.classpath").getAbsolutePath();
34+
35+
private static final String NATIVE_IMAGE_FILE = new File("build/nativeImage.path").getAbsolutePath();
36+
37+
private static final String NATIVE_CONFIG =
38+
"palantirJavaFormatNative files(file(\"" + NATIVE_IMAGE_FILE + "\").text)";
39+
40+
@ParameterizedTest
41+
@CsvSource(
42+
delimiter = '|',
43+
value = {
44+
" | 21 | Using the Java-based formatter",
45+
"palantir.native.formatter=true | 21 | Using the Java-based formatter",
46+
"palantir.native.formatter=true | 17 | Using the native-image formatter"
47+
})
48+
void formats_with_spotless_when_spotless_is_applied(
49+
String extraGradleProperties,
50+
String javaVersion,
51+
String expectedOutput,
52+
GradleInvoker gradle,
53+
RootProject project) {
54+
55+
String extraDependencies = Optional.ofNullable(extraGradleProperties)
56+
.map(props -> NATIVE_CONFIG)
57+
.orElse("");
58+
59+
project.settingsGradle().plugins().add("com.palantir.jdks.settings");
60+
61+
// The 'com.diffplug.spotless:spotless-plugin-gradle' dependency is already added by palantir-java-format
62+
project.buildGradle()
63+
.plugins()
64+
.add("java")
65+
.add("com.palantir.java-format")
66+
.add("com.palantir.baseline-java-versions")
67+
.add("com.palantir.jdks")
68+
.add("com.palantir.jdks.latest");
69+
70+
project.buildGradle().append("""
71+
javaVersions {
72+
libraryTarget = %s
73+
}
74+
75+
jdks {
76+
daemonTarget = %s
77+
}
78+
""", javaVersion, javaVersion);
79+
80+
// Add jvm args to allow spotless and formatter gradle plugins to run with Java 16+
81+
project.gradlePropertiesFile()
82+
.appendProperty(
83+
"org.gradle.jvmargs",
84+
"--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED "
85+
+ "--add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED "
86+
+ "--add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED "
87+
+ "--add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED "
88+
+ "--add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED")
89+
.appendProperty("palantir.jdk.setup.enabled", "true");
90+
91+
project.gradlePropertiesFile()
92+
.appendLine(Optional.ofNullable(extraGradleProperties).orElse(""));
93+
94+
gradle.withArgs("wrapper").buildsSuccessfully();
95+
96+
project.buildGradle().plugins().add("com.diffplug.spotless");
97+
98+
project.buildGradle().append("""
99+
dependencies {
100+
palantirJavaFormat files(file("%s").text.split(':'))
101+
%s
102+
}
103+
""", CLASSPATH_FILE, extraDependencies);
104+
105+
project.file("src/main/java/Main.java").overwrite(invalidJavaFile());
106+
107+
InvocationResult result = gradle.withArgs("spotlessApply", "--info").buildsSuccessfully();
108+
109+
project.file("src/main/java/Main.java").assertThat().hasContent(validJavaFile());
110+
result.assertThat().output().contains(expectedOutput);
111+
}
112+
113+
private String validJavaFile() {
114+
return """
115+
package test;
116+
117+
public class Test {
118+
void test() {
119+
int x = 1;
120+
System.out.println("Hello");
121+
Optional.of("hello").orElseGet(() -> {
122+
return "Hello World";
123+
});
124+
}
125+
}
126+
""";
127+
}
128+
129+
private String invalidJavaFile() {
130+
return """
131+
package test;
132+
import com.java.unused;
133+
public class Test { void test() {int x = 1;
134+
System.out.println(
135+
"Hello"
136+
);
137+
Optional.of("hello").orElseGet(() -> {
138+
return "Hello World";
139+
});
140+
} }
141+
""";
142+
}
143+
}

test-migration-notes/PalantirJavaFormatSpotlessPluginTest.html

Lines changed: 4062 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)