|
| 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 | +} |
0 commit comments