Skip to content

Commit 7a0a5e6

Browse files
committed
Test the configuration cache after the extracted formatter is gone
With the formatter inside the plugin, the Spotless step signs the extracted jars, and Spotless stores that signature in the configuration cache entry: the jars are extracted, and checked for, while the entry is stored. Gradle takes those checks for inputs, so a build after the directory is deleted, or after the first extraction, cannot reuse the entry and stores another; on a new machine or a new plugin version that is one more configuration of every project. spotlessApply_reuses_the_entry_after_the_extracted_formatter_is_deleted fails on that, and is left out on Windows, which does not delete the jars a daemon holds open.
1 parent d70aab2 commit 7a0a5e6

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

‎gradle-open-java-format/src/test/java/com/palantir/javaformat/gradle/ConfigurationCacheTest.java‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import org.gradle.testkit.runner.BuildTask;
2828
import org.gradle.testkit.runner.TaskOutcome;
2929
import org.junit.jupiter.api.Test;
30+
import org.junit.jupiter.api.condition.DisabledOnOs;
31+
import org.junit.jupiter.api.condition.OS;
3032
import org.junit.jupiter.api.io.TempDir;
3133

3234
/**
@@ -96,6 +98,37 @@ void spotlessCheck_reports_the_violation_from_a_reused_entry() throws IOExceptio
9698
}
9799
}
98100

101+
@Test
102+
@DisabledOnOs(
103+
value = OS.WINDOWS,
104+
disabledReason = "The daemon keeps the extracted jars open, and Windows does not delete an open file")
105+
void spotlessApply_reuses_the_entry_after_the_extracted_formatter_is_deleted() throws IOException {
106+
// The formatter inside the plugin, not the jars this build made: nothing in palantirJavaFormat.
107+
var project = new GradleTestProject(projectDir)
108+
.configurationCache(true)
109+
.plugins("java", "dev.openjavaformat.java-format", "com.diffplug.spotless")
110+
.withJavacInternalExports()
111+
.buildGradle("println \"gradleUserHome=${gradle.gradleUserHomeDir}\"")
112+
.writeFile(MAIN_JAVA, UNFORMATTED);
113+
var stored = project.succeeds("spotlessApply");
114+
assertThat(stored.getOutput()).contains(STORED);
115+
116+
// Storing the entry must not depend on the extracted jars, which a later build may find gone.
117+
var gradleUserHome = stored.getOutput()
118+
.lines()
119+
.filter(line -> line.startsWith("gradleUserHome="))
120+
.map(line -> Path.of(line.substring("gradleUserHome=".length())))
121+
.findFirst()
122+
.orElseThrow();
123+
new GradleTestProject(gradleUserHome).delete("open-java-format-caches/" + BundledFormatter.hash());
124+
project.writeFile(MAIN_JAVA, UNFORMATTED);
125+
126+
var reused = project.succeeds("spotlessApply");
127+
128+
assertThat(reused.getOutput()).contains(REUSED);
129+
assertThat(project.readFile(MAIN_JAVA)).isEqualToNormalizingNewlines(FORMATTED);
130+
}
131+
99132
@Test
100133
void spotlessJava_is_up_to_date_until_the_formatter_jars_change() throws IOException, URISyntaxException {
101134
var jars = Files.readString(CLASSPATH_FILE).trim();

0 commit comments

Comments
 (0)