|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | + |
| 15 | +package com.palantir.javaformat.gradle; |
| 16 | + |
| 17 | +import static org.assertj.core.api.Assertions.assertThat; |
| 18 | + |
| 19 | +import com.palantir.javaformat.gradle.testing.GradleTestProject; |
| 20 | +import java.io.File; |
| 21 | +import java.io.IOException; |
| 22 | +import java.net.URISyntaxException; |
| 23 | +import java.nio.file.Files; |
| 24 | +import java.nio.file.Path; |
| 25 | +import org.gradle.testkit.runner.TaskOutcome; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | +import org.junit.jupiter.api.io.TempDir; |
| 28 | +import org.junit.jupiter.params.Parameter; |
| 29 | +import org.junit.jupiter.params.ParameterizedClass; |
| 30 | +import org.junit.jupiter.params.provider.ValueSource; |
| 31 | + |
| 32 | +/** |
| 33 | + * spotlessJava is cacheable, and the open-java-format step is part of its cache key: the key changes with the |
| 34 | + * formatter's jars, and not with the directory the project is in. |
| 35 | + * |
| 36 | + * <p>Every build here is spotlessApply, after the source is written again. The formatted file is what shows that |
| 37 | + * spotlessJava's output, whether it ran, was up to date or came from the cache, holds the source formatted: a step that |
| 38 | + * leaves the source as it is passes a spotlessCheck of a formatted file just as well. Writing the same source again is |
| 39 | + * the change that must not count, as only its time changes. |
| 40 | + */ |
| 41 | +@ParameterizedClass(name = "configuration cache {0}") |
| 42 | +@ValueSource(booleans = {true, false}) |
| 43 | +final class BuildCacheTest { |
| 44 | + |
| 45 | + /** ./gradlew writeImplClasspath generates this file. */ |
| 46 | + private static final Path CLASSPATH_FILE = Path.of("build/impl.classpath"); |
| 47 | + |
| 48 | + private static final String MAIN_JAVA = "src/main/java/Main.java"; |
| 49 | + private static final String UNFORMATTED = "class Main {\n int x;\n}\n"; |
| 50 | + private static final String FORMATTED = "class Main {\n int x;\n}\n"; |
| 51 | + private static final String OTHER_UNFORMATTED = "class Main {\n int y;\n}\n"; |
| 52 | + private static final String OTHER_FORMATTED = "class Main {\n int y;\n}\n"; |
| 53 | + |
| 54 | + @Parameter |
| 55 | + private boolean configurationCache; |
| 56 | + |
| 57 | + @TempDir |
| 58 | + private Path projectDir; |
| 59 | + |
| 60 | + @TempDir |
| 61 | + private Path otherProjectDir; |
| 62 | + |
| 63 | + @TempDir |
| 64 | + private Path buildCacheDir; |
| 65 | + |
| 66 | + @Test |
| 67 | + void spotlessJava_is_up_to_date_when_the_source_is_written_again_unchanged() throws IOException { |
| 68 | + var project = project(projectDir); |
| 69 | + assertThat(spotlessApply(project, UNFORMATTED)).isEqualTo(TaskOutcome.SUCCESS); |
| 70 | + assertThat(project.readFile(MAIN_JAVA)).isEqualToNormalizingNewlines(FORMATTED); |
| 71 | + |
| 72 | + assertThat(spotlessApply(project, UNFORMATTED)).isEqualTo(TaskOutcome.UP_TO_DATE); |
| 73 | + assertThat(project.readFile(MAIN_JAVA)).isEqualToNormalizingNewlines(FORMATTED); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void spotlessJava_runs_again_when_the_source_changes() throws IOException { |
| 78 | + var project = project(projectDir); |
| 79 | + assertThat(spotlessApply(project, UNFORMATTED)).isEqualTo(TaskOutcome.SUCCESS); |
| 80 | + |
| 81 | + assertThat(spotlessApply(project, OTHER_UNFORMATTED)).isEqualTo(TaskOutcome.SUCCESS); |
| 82 | + assertThat(project.readFile(MAIN_JAVA)).isEqualToNormalizingNewlines(OTHER_FORMATTED); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + void spotlessJava_comes_from_the_cache_when_the_source_changes_back() throws IOException { |
| 87 | + var project = project(projectDir); |
| 88 | + assertThat(spotlessApply(project, UNFORMATTED)).isEqualTo(TaskOutcome.SUCCESS); |
| 89 | + assertThat(spotlessApply(project, OTHER_UNFORMATTED)).isEqualTo(TaskOutcome.SUCCESS); |
| 90 | + |
| 91 | + assertThat(spotlessApply(project, UNFORMATTED)).isEqualTo(TaskOutcome.FROM_CACHE); |
| 92 | + assertThat(project.readFile(MAIN_JAVA)).isEqualToNormalizingNewlines(FORMATTED); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + void spotlessJava_comes_from_the_cache_after_the_build_directory_is_deleted() throws IOException { |
| 97 | + var project = project(projectDir); |
| 98 | + assertThat(spotlessApply(project, UNFORMATTED)).isEqualTo(TaskOutcome.SUCCESS); |
| 99 | + |
| 100 | + project.delete("build"); |
| 101 | + |
| 102 | + assertThat(spotlessApply(project, UNFORMATTED)).isEqualTo(TaskOutcome.FROM_CACHE); |
| 103 | + assertThat(project.readFile(MAIN_JAVA)).isEqualToNormalizingNewlines(FORMATTED); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + void spotlessJava_comes_from_the_cache_in_another_directory() throws IOException { |
| 108 | + assertThat(spotlessApply(project(projectDir), UNFORMATTED)).isEqualTo(TaskOutcome.SUCCESS); |
| 109 | + |
| 110 | + // The same project somewhere else, as another checkout or another CI machine has it. |
| 111 | + var moved = project(otherProjectDir); |
| 112 | + |
| 113 | + assertThat(spotlessApply(moved, UNFORMATTED)).isEqualTo(TaskOutcome.FROM_CACHE); |
| 114 | + assertThat(moved.readFile(MAIN_JAVA)).isEqualToNormalizingNewlines(FORMATTED); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + void spotlessJava_runs_again_when_the_formatter_jars_change() throws IOException, URISyntaxException { |
| 119 | + var project = project(projectDir); |
| 120 | + assertThat(spotlessApply(project, UNFORMATTED)).isEqualTo(TaskOutcome.SUCCESS); |
| 121 | + |
| 122 | + // Spotless signs the jars by name, size and content, as another formatter version changes them. The extra jar |
| 123 | + // is JUnit's, not one written into projectDir: the daemon keeps the formatter's jars open after the build, and |
| 124 | + // Windows does not delete an open file. Without the build directory, only the cache could skip the task. |
| 125 | + var extraJar = Path.of( |
| 126 | + Test.class.getProtectionDomain().getCodeSource().getLocation().toURI()); |
| 127 | + project.writeFile("formatter.classpath", formatterClasspath() + File.pathSeparator + extraJar) |
| 128 | + .delete("build"); |
| 129 | + |
| 130 | + assertThat(spotlessApply(project, UNFORMATTED)).isEqualTo(TaskOutcome.SUCCESS); |
| 131 | + assertThat(project.readFile(MAIN_JAVA)).isEqualToNormalizingNewlines(FORMATTED); |
| 132 | + } |
| 133 | + |
| 134 | + private GradleTestProject project(Path dir) throws IOException { |
| 135 | + return new GradleTestProject(dir) |
| 136 | + .configurationCache(configurationCache) |
| 137 | + .withBuildCache(buildCacheDir) |
| 138 | + .plugins("java", "dev.openjavaformat.java-format", "com.diffplug.spotless") |
| 139 | + .withJavacInternalExports() |
| 140 | + .writeFile("formatter.classpath", formatterClasspath()) |
| 141 | + .buildGradle(""" |
| 142 | + dependencies { |
| 143 | + palantirJavaFormat files(file("formatter.classpath").text.split(File.pathSeparator)) |
| 144 | + } |
| 145 | + """); |
| 146 | + } |
| 147 | + |
| 148 | + private static String formatterClasspath() throws IOException { |
| 149 | + return Files.readString(CLASSPATH_FILE).trim(); |
| 150 | + } |
| 151 | + |
| 152 | + /** Writes the source and runs spotlessApply; returns what spotlessJava did for it. */ |
| 153 | + private static TaskOutcome spotlessApply(GradleTestProject project, String source) { |
| 154 | + var task = |
| 155 | + project.writeFile(MAIN_JAVA, source).succeeds("spotlessApply").task(":spotlessJava"); |
| 156 | + assertThat(task).isNotNull(); |
| 157 | + return task.getOutcome(); |
| 158 | + } |
| 159 | +} |
0 commit comments