|
| 1 | +/* |
| 2 | + * (c) Copyright 2026 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 static org.assertj.core.api.Assertions.assertThat; |
| 19 | + |
| 20 | +import com.palantir.javaformat.gradle.testing.GradleTestProject; |
| 21 | +import java.io.File; |
| 22 | +import java.io.IOException; |
| 23 | +import java.nio.file.Path; |
| 24 | +import org.gradle.testkit.runner.BuildResult; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | +import org.junit.jupiter.api.io.TempDir; |
| 27 | + |
| 28 | +/** |
| 29 | + * A build without the JVM flags used to fail on the first file with a raw IllegalAccessError from the formatter's |
| 30 | + * internals (#19). It fails before the first file now, naming the two settings that make the formatter run. This |
| 31 | + * class runs in the default test task; PalantirJavaFormatPluginTest does not, because it needs the native binary. |
| 32 | + */ |
| 33 | +class FormatDiffWithoutJavacExportsTest { |
| 34 | + |
| 35 | + /** ./gradlew writeImplClasspath generates this file. Forward slashes: the path goes into a Groovy string. */ |
| 36 | + private static final String CLASSPATH_FILE = |
| 37 | + new File("build/impl.classpath").getAbsolutePath().replace('\\', '/'); |
| 38 | + |
| 39 | + private static final String MAIN_JAVA = "src/main/java/Main.java"; |
| 40 | + |
| 41 | + @TempDir |
| 42 | + private Path projectDir; |
| 43 | + |
| 44 | + @Test |
| 45 | + void formatDiff_names_both_settings_when_the_gradle_jvm_does_not_export_javac() |
| 46 | + throws IOException, InterruptedException { |
| 47 | + GradleTestProject project = new GradleTestProject(projectDir) |
| 48 | + .plugins("java", "dev.openjavaformat.java-format") |
| 49 | + .buildGradle( |
| 50 | + """ |
| 51 | + dependencies { |
| 52 | + palantirJavaFormat files(file("%s").text.split(File.pathSeparator)) |
| 53 | + } |
| 54 | + """, |
| 55 | + CLASSPATH_FILE); |
| 56 | + |
| 57 | + git(project, "init"); |
| 58 | + git(project, "config", "user.name", "Foo"); |
| 59 | + git(project, "config", "user.email", "foo@bar.com"); |
| 60 | + // The repository this runs in may sign commits; a throwaway one has no key to sign with. |
| 61 | + git(project, "config", "commit.gpgsign", "false"); |
| 62 | + project.writeFile(MAIN_JAVA, "class Main {}\n"); |
| 63 | + git(project, "add", "."); |
| 64 | + git(project, "commit", "-m", "Commit"); |
| 65 | + project.writeFile(MAIN_JAVA, "class Main { int x; }\n"); |
| 66 | + |
| 67 | + BuildResult result = project.fails("formatDiff"); |
| 68 | + |
| 69 | + // Which of the five packages the message lists depends on what the Gradle daemon exports on its own; |
| 70 | + // com.sun.tools.javac.parser, the one the old error named, is never among those. |
| 71 | + assertThat(result.getOutput()) |
| 72 | + .contains("open-java-format cannot run inside this Gradle JVM: module jdk.compiler does not export ") |
| 73 | + .contains("com.sun.tools.javac.parser") |
| 74 | + .contains("openjavaformat.native.formatter=true") |
| 75 | + .contains("org.gradle.jvmargs=--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED" |
| 76 | + + " --add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED" |
| 77 | + + " --add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED" |
| 78 | + + " --add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED" |
| 79 | + + " --add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED") |
| 80 | + .contains("https://openjavaformat.dev/get-started/gradle/#choose-how-the-formatter-runs") |
| 81 | + .doesNotContain("IllegalAccessError") |
| 82 | + .doesNotContain("Formatting "); |
| 83 | + // The check runs before the first file, so nothing was written. |
| 84 | + assertThat(project.readFile(MAIN_JAVA)).isEqualTo("class Main { int x; }\n"); |
| 85 | + } |
| 86 | + |
| 87 | + private static void git(GradleTestProject project, String... args) throws IOException, InterruptedException { |
| 88 | + String[] command = new String[args.length + 1]; |
| 89 | + command[0] = "git"; |
| 90 | + System.arraycopy(args, 0, command, 1, args.length); |
| 91 | + Process process = new ProcessBuilder(command).directory(project.path().toFile()).start(); |
| 92 | + int exitCode = process.waitFor(); |
| 93 | + if (exitCode != 0) { |
| 94 | + throw new RuntimeException("git " + String.join(" ", args) + " failed with exit code " + exitCode); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments