|
| 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.google.common.base.Splitter; |
| 19 | +import com.palantir.gradle.testing.execution.GradleInvoker; |
| 20 | +import com.palantir.gradle.testing.files.gradle.GradleFile; |
| 21 | +import com.palantir.gradle.testing.junit.GradlePluginTests; |
| 22 | +import com.palantir.gradle.testing.project.RootProject; |
| 23 | +import java.io.File; |
| 24 | +import java.io.IOException; |
| 25 | +import java.nio.file.Files; |
| 26 | +import java.util.List; |
| 27 | +import org.junit.jupiter.api.BeforeEach; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | + |
| 30 | +/** |
| 31 | + * When we were getting gradle-baseline to support the configuration cache, spotless had some poorly written tasks |
| 32 | + * which caused issues with the configuration cache. |
| 33 | + * |
| 34 | + * Bumping spotless to 6.22.0 fixed this, but revealed a new error — the {@code palantirJavaFormat} configuration was |
| 35 | + * being <a href="https://github.com/palantir/palantir-java-format/blob/b7b5995df3be690780939c0d0cb2ec49b99c68c8/gradle-palantir-java-format/src/main/java/com/palantir/javaformat/gradle/spotless/NativePalantirJavaFormatStep.java#L45"> resolved eagerly</a>. |
| 36 | + * |
| 37 | + * gradle-consistent-versions enforces against resolving configurations at configuration time, and throws an error. |
| 38 | + * |
| 39 | + * This test forces creation of the spotless steps, which will reveal any eager resolution of configurations. |
| 40 | + */ |
| 41 | +@GradlePluginTests |
| 42 | +class SupportsSpotless622 { |
| 43 | + private static final String CLASSPATH_FILE = new File("build/impl.classpath").getAbsolutePath(); |
| 44 | + |
| 45 | + @BeforeEach |
| 46 | + void setup(RootProject rootProject) throws IOException { |
| 47 | + rootProject.buildGradle().overwrite(""" |
| 48 | + buildscript { |
| 49 | + repositories { |
| 50 | + mavenCentral() { metadataSources { mavenPom(); ignoreGradleMetadataRedirection() } } |
| 51 | + gradlePluginPortal() { metadataSources { mavenPom(); ignoreGradleMetadataRedirection() } } |
| 52 | + } |
| 53 | + dependencies { |
| 54 | + classpath 'com.palantir.gradle.consistentversions:gradle-consistent-versions:2.34.0' |
| 55 | + classpath 'com.diffplug.spotless:spotless-plugin-gradle:6.22.0' |
| 56 | + } |
| 57 | + } |
| 58 | +
|
| 59 | + version = '0.1.0' |
| 60 | + """); |
| 61 | + |
| 62 | + // Apply plugins using the old syntax because these are external buildscript plugins |
| 63 | + // not available via the plugins {} DSL in tests |
| 64 | + // Suppressing GradleTestPluginsBlock because this test specifically tests buildscript-loaded plugins |
| 65 | + @SuppressWarnings("GradleTestPluginsBlock") |
| 66 | + GradleFile unused = rootProject.buildGradle().append(""" |
| 67 | + apply plugin: 'java' |
| 68 | + apply plugin: 'com.palantir.java-format' |
| 69 | + apply plugin: 'com.palantir.consistent-versions' |
| 70 | + apply plugin: 'com.diffplug.spotless' |
| 71 | + """); |
| 72 | + |
| 73 | + rootProject.file("versions.props").createEmpty(); |
| 74 | + rootProject.file("versions.lock").createEmpty(); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void palantir_java_format_plugin_works_with_spotless_6_22_0(GradleInvoker gradle, RootProject rootProject) |
| 79 | + throws IOException { |
| 80 | + // First, generate the wrapper |
| 81 | + gradle.withArgs("wrapper").buildsSuccessfully(); |
| 82 | + |
| 83 | + // Read the impl.classpath file and add the palantirJavaFormat dependency |
| 84 | + String classpathContent = Files.readString(new File(CLASSPATH_FILE).toPath()); |
| 85 | + List<String> classpathEntries = Splitter.on(':').splitToList(classpathContent); |
| 86 | + |
| 87 | + StringBuilder dependenciesBlock = new StringBuilder("dependencies {\n"); |
| 88 | + dependenciesBlock.append(" palantirJavaFormat files(\n"); |
| 89 | + for (int i = 0; i < classpathEntries.size(); i++) { |
| 90 | + dependenciesBlock |
| 91 | + .append(" '") |
| 92 | + .append(classpathEntries.get(i)) |
| 93 | + .append("'"); |
| 94 | + if (i < classpathEntries.size() - 1) { |
| 95 | + dependenciesBlock.append(","); |
| 96 | + } |
| 97 | + dependenciesBlock.append("\n"); |
| 98 | + } |
| 99 | + dependenciesBlock.append(" )\n"); |
| 100 | + dependenciesBlock.append("}\n"); |
| 101 | + |
| 102 | + rootProject.buildGradle().append(dependenciesBlock.toString()); |
| 103 | + |
| 104 | + rootProject.buildGradle().append(""" |
| 105 | +
|
| 106 | + // This forces the realization of the spotlessJava task, creating the spotless steps. |
| 107 | + // If any configurations are eagerly resolved in the spotless steps, |
| 108 | + // consistent-versions should catch it and throw here. |
| 109 | + project.getTasks().getByName("spotlessJava") |
| 110 | + """); |
| 111 | + |
| 112 | + // The test passes if the build succeeds, meaning consistent-versions did not catch |
| 113 | + // eager configuration resolution, which would have caused a build failure |
| 114 | + gradle.withArgs("classes", "--info").buildsSuccessfully(); |
| 115 | + } |
| 116 | +} |
0 commit comments