@@ -26,199 +26,60 @@ import java.util.stream.Collectors
2626import java.util.stream.Stream
2727
2828class ConfigurationCacheTest extends IntegrationTestKitSpec {
29- /* * ./gradlew writeImplClasspath generates this file. */
30- private static final CLASSPATH_FILE = new File (" build/impl.classpath" ). absolutePath
31- private static final NATIVE_IMAGE_FILE = new File (" build/nativeImage.path" )
32- private static final NATIVE_CONFIG = String . format(" palantirJavaFormatNative files(\" %s\" )" , NATIVE_IMAGE_FILE . text)
33-
3429 def setup () {
3530 definePluginOutsideOfPluginBlock = true
3631 keepFiles = true
3732 }
3833
39- @Unroll
40- def " plugin does not resolve configuration at configuration time" (String extraGradleProperties, String javaVersion, String expectedOutput) {
41- def extraDependencies = extraGradleProperties. isEmpty() ? " " : NATIVE_CONFIG
42- settingsFile << """
43- buildscript {
44- repositories {
45- mavenCentral() { metadataSources { mavenPom(); ignoreGradleMetadataRedirection() } }
46- gradlePluginPortal() { metadataSources { mavenPom(); ignoreGradleMetadataRedirection() } }
47- }
48- dependencies {
49- classpath 'com.palantir.gradle.jdks:gradle-jdks-settings:0.62.0'
50- }
51- }
52- apply plugin: 'com.palantir.jdks.settings'
53- """ . stripIndent(true )
54-
55- // language=gradle
34+ def " can run classes" () {
35+ // language=Gradle
5636 buildFile << """
57- buildscript {
58- repositories {
59- mavenCentral() { metadataSources { mavenPom(); ignoreGradleMetadataRedirection() } }
60- gradlePluginPortal() { metadataSources { mavenPom(); ignoreGradleMetadataRedirection() } }
61- mavenLocal()
62- }
63- dependencies {
64- classpath 'com.palantir.baseline:gradle-baseline-java:6.21.0'
65- classpath 'com.palantir.gradle.jdks:gradle-jdks:0.62.0'
66- classpath 'com.palantir.gradle.jdkslatest:gradle-jdks-latest:0.17.0'
67- classpath 'com.palantir.gradle.consistentversions:gradle-consistent-versions:999'
68-
69- constraints {
70- classpath 'com.diffplug.spotless:spotless-plugin-gradle:6.22.0'
71- }
72- }
37+ buildscript {
38+ repositories {
39+ mavenCentral() { metadataSources { mavenPom(); ignoreGradleMetadataRedirection() } }
40+ gradlePluginPortal() { metadataSources { mavenPom(); ignoreGradleMetadataRedirection() } }
41+ }
42+ dependencies {
43+ classpath 'com.palantir.baseline:gradle-baseline-java:6.32.0'
44+ classpath 'com.palantir.gradle.consistentversions:gradle-consistent-versions:2.34.0'
45+ classpath 'com.diffplug.spotless:spotless-plugin-gradle:6.22.0'
7346 }
74-
75- // The 'com.diffplug.spotless:spotless-plugin-gradle' dependency is already added by palantir-java-format
76- plugins {
77- id 'java'
47+ }
48+
49+ apply plugin: 'com.palantir.baseline'
50+ apply plugin: 'com.palantir.consistent-versions'
51+
52+ allprojects {
53+ apply plugin: 'com.palantir.java-format'
54+ apply plugin: 'java'
55+
56+ repositories {
57+ mavenCentral() { metadataSources { mavenPom(); ignoreGradleMetadataRedirection() } }
7858 }
59+ }
60+ version = '0.1.0'
61+ """ . stripIndent(true )
7962
80- apply plugin: 'com.palantir.consistent-versions'
81- apply plugin: 'com.palantir.java-format'
82- apply plugin: 'com.palantir.baseline-java-versions'
83- apply plugin: 'com.palantir.jdks'
84- apply plugin: 'com.palantir.jdks.latest'
85-
86- import com.diffplug.gradle.spotless.SpotlessPlugin
87-
88- // find which jar this class comes from
89- println("here you go sir")
90- println(SpotlessPlugin.class.protectionDomain.codeSource.location)
9163
92- javaVersions {
93- libraryTarget = ${ javaVersion}
94- }
64+ file(" versions.props" )
65+ file(" versions.lock" )
9566
96- jdks {
97- daemonTarget = ${ javaVersion}
98- }
99-
100- """ . stripIndent()
10167
102- // Add jvm args to allow spotless and formatter gradle plugins to run with Java 16+
10368 file(' gradle.properties' ) << """
10469 org.gradle.jvmargs=--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED \
10570 --add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED \
10671 --add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED \
10772 --add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED \
10873 --add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
10974 palantir.jdk.setup.enabled=true
110- """ . stripIndent()
111- file(' gradle.properties' ) << extraGradleProperties
112- file(' versions.lock' ). text = ' '
113- file(' versions.props' ). text = ' '
114-
75+ """ . stripIndent(true )
11576 runTasks(' wrapper' )
11677
117- buildFile << """
118- apply plugin: 'com.diffplug.spotless'
119-
120- dependencies {
121- palantirJavaFormat files(file("${ CLASSPATH_FILE} ").text.split(':'))
122- ${ extraDependencies}
123- }
124- """ . stripIndent()
125-
126- file(' src/main/java/Main.java' ). text = invalidJavaFile
127-
12878 when :
129- def result = runGradlewTasks( ' spotlessApply ' , ' --info ' , ' --configuration-cache ' )
79+ def result = runTasks( ' build ' )
13080
131- then :
132- result. standardOutput. contains(expectedOutput)
133- file(' src/main/java/Main.java' ). text == validJavaFile
13481
135- where :
136- extraGradleProperties | javaVersion | expectedOutput
137- // "" | 21 | "Using the Java-based formatter"
138- // "palantir.native.formatter=true" | 21 | "Using the Java-based formatter"
139- " palantir.native.formatter=true" | 17 | " brr brr patapim" // make it fail to read stdout
140- }
141-
142- private static Iterable<File > getBuildPluginClasspathInjector () {
143- return getPluginClasspathInjector(Path . of(" ../gradle-palantir-java-format/build/pluginUnderTestMetadata/plugin-under-test-metadata.properties" ))
144- }
145-
146- private static Iterable<File > getPluginClasspathInjector (Path path ) {
147- File propertiesFile = path. toFile()
148- Properties properties = new Properties ()
149- propertiesFile. withInputStream { inputStream ->
150- properties. load(inputStream)
151- }
152- String classpath = properties. getProperty(' implementation-classpath' )
153- return classpath. split(File . pathSeparator). collect { new File (it) }
154- }
155-
156- private GradlewExecutionResult runGradlewTasks (String ... tasks ) {
157- ProcessBuilder processBuilder = getProcessBuilder(tasks)
158- Process process = processBuilder. start()
159- String output = readAllInput(process. getInputStream())
160- process. waitFor(1 , TimeUnit . MINUTES )
161- GradlewExecutionResult result = new GradlewExecutionResult (process. exitValue(), output)
162- assert result. success
163- return result
164- }
165-
166- final class GradlewExecutionResult {
167-
168- private final Boolean success
169- private final String standardOutput
170- private final Throwable failure
171-
172- GradlewExecutionResult (int exitValue , String output ) {
173- this . success = exitValue == 0
174- this . standardOutput = output
175- this . failure = exitValue != 0 ? new RuntimeException (String . format(" Build failed with exitCode %s" , exitValue)) : null
176- }
177- }
178-
179- static String readAllInput (InputStream inputStream ) {
180- try (Stream<String > lines =
181- new BufferedReader (new InputStreamReader (inputStream, StandardCharsets . UTF_8 )). lines()) {
182- return lines. collect(Collectors . joining(" \n " ));
183- }
184- }
185-
186- private ProcessBuilder getProcessBuilder (String ... tasks ) {
187- File initScript = new File (projectDir, " init.gradle" )
188- ClasspathAddingInitScriptBuilder . build(initScript, getBuildPluginClasspathInjector(). toList())
189- List<String > arguments = [" ./gradlew" , " --init-script" , initScript. toPath(). toString()]
190- Arrays . asList(tasks). forEach(arguments ::add)
191- ProcessBuilder processBuilder = new ProcessBuilder ()
192- .command(arguments)
193- .directory(projectDir)
194- .redirectErrorStream(true )
195- return processBuilder
196- }
197-
198- def validJavaFile = ''' \
199- package test;
200-
201- public class Test {
202- void test() {
203- int x = 1;
204- System.out.println("Hello");
205- Optional.of("hello").orElseGet(() -> {
206- return "Hello World";
207- });
208- }
82+ then :
83+ result. output. contains(' BUILD SUCCESSFUL' )
20984 }
210- ''' . stripIndent()
211-
212- def invalidJavaFile = '''
213- package test;
214- import com.java.unused;
215- public class Test { void test() {int x = 1;
216- System.out.println(
217- "Hello"
218- );
219- Optional.of("hello").orElseGet(() -> {
220- return "Hello World";
221- });
222- } }
223- ''' . stripIndent()
224- }
85+ }
0 commit comments