Skip to content

Commit 7160468

Browse files
committed
Check the formatted files in the Spotless tests
A Spotless test passed as long as its task did: three of them would have passed with a step that formats nothing, as they did while the configuration cache lost the formatter. spotlessJava_is_up_to_date_until_the_formatter_jars_change ran spotlessCheck on a formatted file, which a step that changes nothing passes. It now runs spotlessApply on the unformatted source each time and checks the file it leaves; line endings no longer matter, so the build no longer sets lineEndings to PRESERVE. format_ignores_excluded_directories passed when nothing was formatted at all. It now runs spotlessApply with a file outside the excluded directory too, and checks that the one is formatted and the other is left as it was. format_checks_non_generated_files passed on any failure of the check, a lint error from a failing formatter as well; it now looks for the format violation. With the step's formatter returning its input, every test that runs a Spotless task now fails.
1 parent 6b1cd06 commit 7160468

2 files changed

Lines changed: 35 additions & 12 deletions

File tree

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,22 +104,20 @@ void spotlessJava_is_up_to_date_until_the_formatter_jars_change() throws IOExcep
104104
.plugins("java", "dev.openjavaformat.java-format", "com.diffplug.spotless")
105105
.withJavacInternalExports()
106106
.writeFile("formatter.classpath", jars)
107-
// By default Spotless checks for the platform's line endings, CRLF on Windows; PRESERVE keeps the
108-
// file's own.
109107
.buildGradle("""
110108
dependencies {
111109
palantirJavaFormat files(file("formatter.classpath").text.split(File.pathSeparator))
112110
}
113-
spotless {
114-
lineEndings = 'PRESERVE'
115-
}
116-
""")
117-
.writeFile(MAIN_JAVA, FORMATTED);
111+
""");
118112

119-
assertThat(spotlessJava(project.succeeds("spotlessCheck"))).isEqualTo(TaskOutcome.SUCCESS);
120-
var unchanged = project.succeeds("spotlessCheck");
113+
// spotlessApply of the same source each time, and the file it leaves: a step that changes nothing is up to
114+
// date just as well, and passes a spotlessCheck of a formatted file.
115+
assertThat(spotlessJava(spotlessApply(project))).isEqualTo(TaskOutcome.SUCCESS);
116+
assertThat(project.readFile(MAIN_JAVA)).isEqualToNormalizingNewlines(FORMATTED);
117+
var unchanged = spotlessApply(project);
121118
assertThat(unchanged.getOutput()).contains(REUSED);
122119
assertThat(spotlessJava(unchanged)).isEqualTo(TaskOutcome.UP_TO_DATE);
120+
assertThat(project.readFile(MAIN_JAVA)).isEqualToNormalizingNewlines(FORMATTED);
123121

124122
// Spotless signs the jars by name, size and content, as another formatter version changes them. The extra jar
125123
// is JUnit's, not one written into projectDir: the daemon keeps the formatter's jars open after the build, and
@@ -128,7 +126,13 @@ palantirJavaFormat files(file("formatter.classpath").text.split(File.pathSeparat
128126
Test.class.getProtectionDomain().getCodeSource().getLocation().toURI());
129127
project.writeFile("formatter.classpath", jars + File.pathSeparator + extraJar);
130128

131-
assertThat(spotlessJava(project.succeeds("spotlessCheck"))).isEqualTo(TaskOutcome.SUCCESS);
129+
assertThat(spotlessJava(spotlessApply(project))).isEqualTo(TaskOutcome.SUCCESS);
130+
assertThat(project.readFile(MAIN_JAVA)).isEqualToNormalizingNewlines(FORMATTED);
131+
}
132+
133+
/** spotlessApply of the unformatted source, written again: only its time changes. */
134+
private static BuildResult spotlessApply(GradleTestProject project) {
135+
return project.writeFile(MAIN_JAVA, UNFORMATTED).succeeds("spotlessApply");
132136
}
133137

134138
@Test

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ final class SpotlessExcludesTest {
4343
public class Test { Void test() { return null; } }
4444
""";
4545

46+
/**
47+
* Formatted next to a file in an excluded directory. If it stays as it is, the formatter did not run, and the
48+
* excluded file staying as it is proves nothing.
49+
*/
50+
private static final String MAIN_JAVA = "src/main/java/Main.java";
51+
52+
private static final String UNFORMATTED_MAIN = "class Main {\n int x;\n}\n";
53+
private static final String FORMATTED_MAIN = "class Main {\n int x;\n}\n";
54+
4655
@Parameter
4756
private boolean configurationCache;
4857

@@ -81,12 +90,17 @@ void format_ignores_excluded_directories(String srcDir) {
8190
java { srcDir '%s' }
8291
}
8392
}
84-
""", srcDir).writeFile(srcDir + "/test/Test.java", SOURCE_FILE);
93+
""", srcDir)
94+
.writeFile(srcDir + "/test/Test.java", SOURCE_FILE)
95+
.writeFile(MAIN_JAVA, UNFORMATTED_MAIN);
8596

86-
BuildResult result = project.succeeds("spotlessJavaCheck");
97+
BuildResult result = project.succeeds("spotlessApply");
8798

8899
assertThat(result.task(":spotlessJava")).isNotNull().extracting(task -> task.getOutcome())
89100
.isEqualTo(TaskOutcome.SUCCESS);
101+
assertThat(project.readFile(srcDir + "/test/Test.java")).isEqualTo(SOURCE_FILE);
102+
// Spotless writes the platform's line endings, CRLF on Windows.
103+
assertThat(project.readFile(MAIN_JAVA)).isEqualToNormalizingNewlines(FORMATTED_MAIN);
90104
}
91105

92106
@ParameterizedTest
@@ -108,5 +122,10 @@ void format_checks_non_generated_files(String srcDir) {
108122

109123
assertThat(result.task(":spotlessJava")).isNotNull().extracting(task -> task.getOutcome())
110124
.isEqualTo(TaskOutcome.SUCCESS);
125+
// A formatter that fails reports a lint error instead, which fails the check just as well.
126+
assertThat(result.getOutput())
127+
.contains("format violations")
128+
.contains("Test.java")
129+
.doesNotContain("lint error");
111130
}
112131
}

0 commit comments

Comments
 (0)