Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ buildscript {
classpath 'com.palantir.gradle.idea-language-injector:gradle-idea-language-injector:0.2.0'
classpath 'com.palantir.gradle.jdks:gradle-jdks:0.69.0'
classpath 'com.palantir.gradle.jdkslatest:gradle-jdks-latest:0.24.0'
classpath 'com.palantir.gradle.plugintesting:gradle-plugin-testing:0.6.0'
classpath 'com.palantir.gradle.plugintesting:gradle-plugin-testing:0.39.0'
classpath 'com.palantir.javaformat:gradle-palantir-java-format:2.82.0'
classpath 'com.palantir.suppressible-error-prone:gradle-suppressible-error-prone:2.26.0'
classpath 'me.champeau.jmh:jmh-gradle-plugin:0.7.3'
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* (c) Copyright 2019 Palantir Technologies Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.palantir.javaformat.gradle;

import static com.palantir.gradle.testing.assertion.GradlePluginTestAssertions.assertThat;

import com.palantir.gradle.testing.execution.GradleInvoker;
import com.palantir.gradle.testing.execution.InvocationResult;
import com.palantir.gradle.testing.files.gradle.GradleFile;
import com.palantir.gradle.testing.junit.GradlePluginTests;
import com.palantir.gradle.testing.project.RootProject;
import java.io.File;
import java.io.IOException;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

@GradlePluginTests
class PalantirJavaFormatPluginTest {

/** ./gradlew writeImplClasspath generates this file. */
private static final String CLASSPATH_FILE = new File("build/impl.classpath").getAbsolutePath();

private static final String NATIVE_IMAGE_FILE = new File("build/nativeImage.path").getAbsolutePath();

private static final String NATIVE_CONFIG =
"palantirJavaFormatNative files(file(\"" + NATIVE_IMAGE_FILE + "\").text)";

private GradleFile standardBuildFile(RootProject rootProject, String extraDependencies) {
rootProject
.buildGradle()
.plugins()
.add("java")
.add("com.palantir.java-format")
.add("idea");

return rootProject.buildGradle().append("""
dependencies {
palantirJavaFormat files(file("%s").text.split(':'))
%s
}
""", CLASSPATH_FILE, extraDependencies);
}

@ParameterizedTest
@CsvSource(delimiter = '|', textBlock = """
# extraGradleProperties | expectedOutput
'' | Using the Java-based formatter
'palantir.native.formatter=true' | Using the native-image formatter
""")
void format_diff_updates_only_lines_changed_in_git_diff(
String extraGradleProperties, String expectedOutput, RootProject rootProject, GradleInvoker gradle)
throws IOException, InterruptedException {

if (!extraGradleProperties.isEmpty()) {
rootProject.gradlePropertiesFile().appendProperty("palantir.native.formatter", "true");
}

String extraDependencies = extraGradleProperties.isEmpty() ? "" : NATIVE_CONFIG;
standardBuildFile(rootProject, extraDependencies);

// Add jvm args to allow spotless and formatter gradle plugins to run with Java 16+
rootProject.gradlePropertiesFile().append("""
org.gradle.jvmargs=--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED \
--add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED \
--add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED \
--add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED \
--add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
""");

executeGitCommand(rootProject, "git", "init");
executeGitCommand(rootProject, "git", "config", "user.name", "Foo");
executeGitCommand(rootProject, "git", "config", "user.email", "foo@bar.com");

rootProject.mainSourceSet().java().writeClass("""
class Main {
public static void crazyExistingFormatting ( String... args) {

}
}
""");

executeGitCommand(rootProject, "git", "add", ".");
executeGitCommand(rootProject, "git", "commit", "-m", "Commit");

rootProject.mainSourceSet().java().fileByClassName("Main").overwrite("""
class Main {
public static void crazyExistingFormatting ( String... args) {
System.out.println("Reformat me please");
// some comments
System.out.println("Reformat me again please");
}
}
""");

InvocationResult result = gradle.withArgs("formatDiff", "--info").buildsSuccessfully();

assertThat(result).output().contains(expectedOutput);

rootProject.mainSourceSet().java().fileByClassName("Main").assertThat().hasContent("""
class Main {
public static void crazyExistingFormatting ( String... args) {
System.out.println("Reformat me please");
// some comments
System.out.println("Reformat me again please");
}
}
""");
}

private static void executeGitCommand(RootProject rootProject, String... command)
throws IOException, InterruptedException {
ProcessBuilder pb = new ProcessBuilder(command);
pb.directory(rootProject.path().toFile());
pb.environment().putAll(System.getenv());
Process process = pb.start();
int exitCode = process.waitFor();
if (exitCode != 0) {
throw new RuntimeException("Git command failed with exit code " + exitCode);
}
}
}
Loading