|
| 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 | + |
| 17 | +package com.palantir.javaformat.gradle; |
| 18 | + |
| 19 | +import static com.palantir.gradle.testing.assertion.GradlePluginTestAssertions.assertThat; |
| 20 | + |
| 21 | +import com.palantir.gradle.testing.execution.GradleInvoker; |
| 22 | +import com.palantir.gradle.testing.execution.InvocationResult; |
| 23 | +import com.palantir.gradle.testing.files.gradle.GradleFile; |
| 24 | +import com.palantir.gradle.testing.junit.GradlePluginTests; |
| 25 | +import com.palantir.gradle.testing.project.RootProject; |
| 26 | +import java.io.File; |
| 27 | +import java.io.IOException; |
| 28 | +import org.junit.jupiter.params.ParameterizedTest; |
| 29 | +import org.junit.jupiter.params.provider.CsvSource; |
| 30 | + |
| 31 | +@GradlePluginTests |
| 32 | +class PalantirJavaFormatPluginTest { |
| 33 | + |
| 34 | + /** ./gradlew writeImplClasspath generates this file. */ |
| 35 | + private static final String CLASSPATH_FILE = new File("build/impl.classpath").getAbsolutePath(); |
| 36 | + |
| 37 | + private static final String NATIVE_IMAGE_FILE = new File("build/nativeImage.path").getAbsolutePath(); |
| 38 | + |
| 39 | + private static final String NATIVE_CONFIG = |
| 40 | + "palantirJavaFormatNative files(file(\"" + NATIVE_IMAGE_FILE + "\").text)"; |
| 41 | + |
| 42 | + @ParameterizedTest |
| 43 | + @CsvSource( |
| 44 | + delimiter = '|', |
| 45 | + value = { |
| 46 | + " | Using the Java-based formatter", |
| 47 | + "palantir.native.formatter=true | Using the native-image formatter" |
| 48 | + }) |
| 49 | + void formatDiff_updates_only_lines_changed_in_git_diff( |
| 50 | + String extraGradleProperties, String expectedOutput, GradleInvoker gradle, RootProject project) |
| 51 | + throws IOException, InterruptedException { |
| 52 | + if (extraGradleProperties != null && !extraGradleProperties.isBlank()) { |
| 53 | + project.gradlePropertiesFile().append("%s\n", extraGradleProperties); |
| 54 | + } |
| 55 | + |
| 56 | + String extraDependencies = |
| 57 | + (extraGradleProperties == null || extraGradleProperties.isBlank()) ? "" : NATIVE_CONFIG; |
| 58 | + |
| 59 | + standardBuildFile(project, extraDependencies); |
| 60 | + |
| 61 | + // Add jvm args to allow spotless and formatter gradle plugins to run with Java 16+ |
| 62 | + project.gradlePropertiesFile().append(""" |
| 63 | + org.gradle.jvmargs=--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED \ |
| 64 | + --add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED \ |
| 65 | + --add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED \ |
| 66 | + --add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED \ |
| 67 | + --add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED |
| 68 | + """); |
| 69 | + |
| 70 | + executeGitCommand(project, "git", "init"); |
| 71 | + executeGitCommand(project, "git", "config", "user.name", "Foo"); |
| 72 | + executeGitCommand(project, "git", "config", "user.email", "foo@bar.com"); |
| 73 | + |
| 74 | + project.mainSourceSet().java().writeClass(""" |
| 75 | + class Main { |
| 76 | + public static void crazyExistingFormatting ( String... args) { |
| 77 | +
|
| 78 | + } |
| 79 | + } |
| 80 | + """); |
| 81 | + |
| 82 | + executeGitCommand(project, "git", "add", "."); |
| 83 | + executeGitCommand(project, "git", "commit", "-m", "Commit"); |
| 84 | + |
| 85 | + project.mainSourceSet().java().fileByClassName("Main").overwrite(""" |
| 86 | + class Main { |
| 87 | + public static void crazyExistingFormatting ( String... args) { |
| 88 | + System.out.println("Reformat me please"); |
| 89 | + // some comments |
| 90 | + System.out.println("Reformat me again please"); |
| 91 | + } |
| 92 | + } |
| 93 | + """); |
| 94 | + |
| 95 | + InvocationResult result = gradle.withArgs("formatDiff", "--info").buildsSuccessfully(); |
| 96 | + |
| 97 | + assertThat(result).output().contains(expectedOutput); |
| 98 | + |
| 99 | + String expectedMainJava = """ |
| 100 | + class Main { |
| 101 | + public static void crazyExistingFormatting ( String... args) { |
| 102 | + System.out.println("Reformat me please"); |
| 103 | + // some comments |
| 104 | + System.out.println("Reformat me again please"); |
| 105 | + } |
| 106 | + } |
| 107 | + """; |
| 108 | + |
| 109 | + project.mainSourceSet() |
| 110 | + .java() |
| 111 | + .fileByClassName("Main") |
| 112 | + .assertThat() |
| 113 | + .content() |
| 114 | + .isEqualTo(expectedMainJava); |
| 115 | + } |
| 116 | + |
| 117 | + private GradleFile standardBuildFile(RootProject project, String extraDependencies) { |
| 118 | + project.buildGradle() |
| 119 | + .plugins() |
| 120 | + .add("java") |
| 121 | + .add("com.palantir.java-format") |
| 122 | + .add("idea"); |
| 123 | + |
| 124 | + project.buildGradle().append(""" |
| 125 | + dependencies { |
| 126 | + palantirJavaFormat files(file("%s").text.split(':')) |
| 127 | + %s |
| 128 | + } |
| 129 | + """, CLASSPATH_FILE, extraDependencies); |
| 130 | + |
| 131 | + return project.buildGradle(); |
| 132 | + } |
| 133 | + |
| 134 | + private void executeGitCommand(RootProject project, String... command) throws IOException, InterruptedException { |
| 135 | + ProcessBuilder pb = new ProcessBuilder(command); |
| 136 | + pb.directory(project.path().toFile()); |
| 137 | + Process process = pb.start(); |
| 138 | + int exitCode = process.waitFor(); |
| 139 | + if (exitCode != 0) { |
| 140 | + throw new RuntimeException("Git command failed with exit code " + exitCode); |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments