Skip to content

Commit 9dfbab8

Browse files
committed
Run the Spotless tests with the formatter inside the plugin
The Spotless tests declared the jars this build made in palantirJavaFormat, which was the only way they could format: under TestKit the default dependency had no version. They now run with nothing declared, as a build that applies the plugin does, through the jars the plugin carries. BuildCacheTest keeps two tests of a declared formatter: declaring one makes spotlessJava run again, and so does a change of the declared jars. ConfigurationCacheTest keeps its test of the declared jars too, and MultiProjectTest stays on a declared formatter, as resolving that in the subprojects is what it tests. With the step's formatter returning its input, every test that runs a Spotless task still fails.
1 parent ea0283f commit 9dfbab8

5 files changed

Lines changed: 33 additions & 48 deletions

File tree

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

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
/**
3333
* 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.
34+
* formatter, the one inside the plugin or the one a build declares, and not with the directory the project is in.
3535
*
3636
* <p>Every build here is spotlessApply, after the source is written again. The formatted file is what shows that
3737
* spotlessJava's output, whether it ran, was up to date or came from the cache, holds the source formatted: a step that
@@ -42,7 +42,7 @@
4242
@ValueSource(booleans = {true, false})
4343
final class BuildCacheTest {
4444

45-
/** ./gradlew writeImplClasspath generates this file. */
45+
/** ./gradlew writeImplClasspath generates this file: the jars this build made, to declare as an override. */
4646
private static final Path CLASSPATH_FILE = Path.of("build/impl.classpath");
4747

4848
private static final String MAIN_JAVA = "src/main/java/Main.java";
@@ -64,7 +64,7 @@ final class BuildCacheTest {
6464
private Path buildCacheDir;
6565

6666
@Test
67-
void spotlessJava_is_up_to_date_when_the_source_is_written_again_unchanged() throws IOException {
67+
void spotlessJava_is_up_to_date_when_the_source_is_written_again_unchanged() {
6868
var project = project(projectDir);
6969
assertThat(spotlessApply(project, UNFORMATTED)).isEqualTo(TaskOutcome.SUCCESS);
7070
assertThat(project.readFile(MAIN_JAVA)).isEqualToNormalizingNewlines(FORMATTED);
@@ -74,7 +74,7 @@ void spotlessJava_is_up_to_date_when_the_source_is_written_again_unchanged() thr
7474
}
7575

7676
@Test
77-
void spotlessJava_runs_again_when_the_source_changes() throws IOException {
77+
void spotlessJava_runs_again_when_the_source_changes() {
7878
var project = project(projectDir);
7979
assertThat(spotlessApply(project, UNFORMATTED)).isEqualTo(TaskOutcome.SUCCESS);
8080

@@ -83,7 +83,7 @@ void spotlessJava_runs_again_when_the_source_changes() throws IOException {
8383
}
8484

8585
@Test
86-
void spotlessJava_comes_from_the_cache_when_the_source_changes_back() throws IOException {
86+
void spotlessJava_comes_from_the_cache_when_the_source_changes_back() {
8787
var project = project(projectDir);
8888
assertThat(spotlessApply(project, UNFORMATTED)).isEqualTo(TaskOutcome.SUCCESS);
8989
assertThat(spotlessApply(project, OTHER_UNFORMATTED)).isEqualTo(TaskOutcome.SUCCESS);
@@ -93,7 +93,7 @@ void spotlessJava_comes_from_the_cache_when_the_source_changes_back() throws IOE
9393
}
9494

9595
@Test
96-
void spotlessJava_comes_from_the_cache_after_the_build_directory_is_deleted() throws IOException {
96+
void spotlessJava_comes_from_the_cache_after_the_build_directory_is_deleted() {
9797
var project = project(projectDir);
9898
assertThat(spotlessApply(project, UNFORMATTED)).isEqualTo(TaskOutcome.SUCCESS);
9999

@@ -104,7 +104,7 @@ void spotlessJava_comes_from_the_cache_after_the_build_directory_is_deleted() th
104104
}
105105

106106
@Test
107-
void spotlessJava_comes_from_the_cache_in_another_directory() throws IOException {
107+
void spotlessJava_comes_from_the_cache_in_another_directory() {
108108
assertThat(spotlessApply(project(projectDir), UNFORMATTED)).isEqualTo(TaskOutcome.SUCCESS);
109109

110110
// The same project somewhere else, as another checkout or another CI machine has it.
@@ -115,13 +115,25 @@ void spotlessJava_comes_from_the_cache_in_another_directory() throws IOException
115115
}
116116

117117
@Test
118-
void spotlessJava_runs_again_when_the_formatter_jars_change() throws IOException, URISyntaxException {
118+
void spotlessJava_runs_again_when_the_build_declares_a_formatter() throws IOException {
119119
var project = project(projectDir);
120120
assertThat(spotlessApply(project, UNFORMATTED)).isEqualTo(TaskOutcome.SUCCESS);
121121

122+
// Without the build directory, only the cache could skip the task.
123+
project.writeFile("formatter.classpath", formatterClasspath()).delete("build");
124+
125+
assertThat(spotlessApply(project, UNFORMATTED)).isEqualTo(TaskOutcome.SUCCESS);
126+
assertThat(project.readFile(MAIN_JAVA)).isEqualToNormalizingNewlines(FORMATTED);
127+
}
128+
129+
@Test
130+
void spotlessJava_runs_again_when_the_declared_jars_change() throws IOException, URISyntaxException {
131+
var project = project(projectDir).writeFile("formatter.classpath", formatterClasspath());
132+
assertThat(spotlessApply(project, UNFORMATTED)).isEqualTo(TaskOutcome.SUCCESS);
133+
122134
// Spotless signs the jars by name, size and content, as another formatter version changes them. The extra jar
123135
// 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.
136+
// Windows does not delete an open file.
125137
var extraJar = Path.of(
126138
Test.class.getProtectionDomain().getCodeSource().getLocation().toURI());
127139
project.writeFile("formatter.classpath", formatterClasspath() + File.pathSeparator + extraJar)
@@ -131,16 +143,18 @@ void spotlessJava_runs_again_when_the_formatter_jars_change() throws IOException
131143
assertThat(project.readFile(MAIN_JAVA)).isEqualToNormalizingNewlines(FORMATTED);
132144
}
133145

134-
private GradleTestProject project(Path dir) throws IOException {
146+
/** The formatter inside the plugin, unless the test writes formatter.classpath, which the build then declares. */
147+
private GradleTestProject project(Path dir) {
135148
return new GradleTestProject(dir)
136149
.configurationCache(configurationCache)
137150
.withBuildCache(buildCacheDir)
138151
.plugins("java", "dev.openjavaformat.java-format", "com.diffplug.spotless")
139152
.withJavacInternalExports()
140-
.writeFile("formatter.classpath", formatterClasspath())
141153
.buildGradle("""
142-
dependencies {
143-
palantirJavaFormat files(file("formatter.classpath").text.split(File.pathSeparator))
154+
if (file("formatter.classpath").exists()) {
155+
dependencies {
156+
palantirJavaFormat files(file("formatter.classpath").text.split(File.pathSeparator))
157+
}
144158
}
145159
""");
146160
}

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
*/
3939
final class ConfigurationCacheTest {
4040

41-
/** ./gradlew writeImplClasspath generates this file. */
41+
/** ./gradlew writeImplClasspath generates this file: the jars this build made, to declare as an override. */
4242
private static final Path CLASSPATH_FILE = Path.of("build/impl.classpath");
4343

4444
private static final String STORED = "Configuration cache entry stored.";
@@ -201,17 +201,12 @@ void required_idea_plugin_is_listed_from_a_reused_entry() {
201201
}
202202
}
203203

204+
/** With the formatter inside the plugin. */
204205
private GradleTestProject project(String... plugins) {
205-
// Forward slashes: the path goes into a Groovy string, where a Windows backslash would start an escape.
206206
return new GradleTestProject(projectDir)
207207
.configurationCache(true)
208208
.plugins(plugins)
209-
.withJavacInternalExports()
210-
.buildGradle("""
211-
dependencies {
212-
palantirJavaFormat files(file("%s").text.split(File.pathSeparator))
213-
}
214-
""", CLASSPATH_FILE.toAbsolutePath().toString().replace('\\', '/'));
209+
.withJavacInternalExports();
215210
}
216211

217212
private static TaskOutcome spotlessJava(BuildResult result) {

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,7 @@
3232
@ValueSource(booleans = {true, false})
3333
final class PalantirJavaFormatSpotlessPluginTest {
3434

35-
/** ./gradlew writeImplClasspath generates this file. */
3635
// Forward slashes: the path goes into a Groovy string, where a Windows backslash would start an escape.
37-
private static final String CLASSPATH_FILE =
38-
new File("build/impl.classpath").getAbsolutePath().replace('\\', '/');
39-
4036
private static final String NATIVE_IMAGE_FILE =
4137
new File("build/nativeImage.path").getAbsolutePath().replace('\\', '/');
4238

@@ -81,10 +77,9 @@ void formats_with_spotless_when_spotless_is_applied(
8177
""", javaVersion)
8278
.buildGradle("""
8379
dependencies {
84-
palantirJavaFormat files(file("%s").text.split(File.pathSeparator))
8580
%s
8681
}
87-
""", CLASSPATH_FILE, extraDependencies)
82+
""", extraDependencies)
8883
.writeFile(MAIN_JAVA, invalidJavaFile());
8984

9085
BuildResult result = project.succeeds("spotlessApply", "--info");

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import static org.assertj.core.api.Assertions.assertThat;
1919

2020
import com.palantir.javaformat.gradle.testing.GradleTestProject;
21-
import java.io.File;
2221
import java.nio.file.Path;
2322
import org.gradle.testkit.runner.BuildResult;
2423
import org.gradle.testkit.runner.TaskOutcome;
@@ -33,10 +32,6 @@
3332
@ValueSource(booleans = {true, false})
3433
final class SpotlessExcludesTest {
3534

36-
// Forward slashes: the path goes into a Groovy string, where a Windows backslash would start an escape.
37-
private static final String CLASSPATH_FILE =
38-
new File("build/impl.classpath").getAbsolutePath().replace('\\', '/');
39-
4035
private static final String SOURCE_FILE = """
4136
package test;
4237
import java.lang.Void;
@@ -65,12 +60,7 @@ void setup() {
6560
project = new GradleTestProject(projectDir)
6661
.configurationCache(configurationCache)
6762
.plugins("java", "dev.openjavaformat.java-format", "com.diffplug.spotless")
68-
.withJavacInternalExports()
69-
.buildGradle("""
70-
dependencies {
71-
palantirJavaFormat files(file("%s").text.split(File.pathSeparator))
72-
}
73-
""", CLASSPATH_FILE);
63+
.withJavacInternalExports();
7464
}
7565

7666
@ParameterizedTest

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package com.palantir.javaformat.gradle;
1717

1818
import com.palantir.javaformat.gradle.testing.GradleTestProject;
19-
import java.io.File;
2019
import java.nio.file.Path;
2120
import org.junit.jupiter.api.Test;
2221
import org.junit.jupiter.api.io.TempDir;
@@ -33,10 +32,6 @@
3332
*/
3433
final class SupportsCurrentSpotlessTest {
3534

36-
// Forward slashes: the path goes into a Groovy string, where a Windows backslash would start an escape.
37-
private static final String CLASSPATH_FILE =
38-
new File("build/impl.classpath").getAbsolutePath().replace('\\', '/');
39-
4035
@TempDir
4136
private Path projectDir;
4237

@@ -47,14 +42,10 @@ void palantirjavaformatplugin_works_with_current_spotless() {
4742
.plugins("java", "dev.openjavaformat.java-format", "com.diffplug.spotless")
4843
.withJavacInternalExports()
4944
.buildGradle("""
50-
dependencies {
51-
palantirJavaFormat files(file("%s").text.split(File.pathSeparator))
52-
}
53-
5445
// Forces realization of the spotlessJava task, creating the spotless steps. Any
5546
// configuration resolved eagerly in them fails the configuration cache here.
5647
project.getTasks().getByName("spotlessJava")
57-
""", CLASSPATH_FILE)
48+
""")
5849
.succeeds("classes", "--info");
5950
}
6051
}

0 commit comments

Comments
 (0)