Skip to content

Commit 993390d

Browse files
committed
Refactored ResourceHarness to a fluent API that avoids newline ambiguities.
1 parent 7958dcc commit 993390d

31 files changed

Lines changed: 237 additions & 343 deletions

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/BumpThisNumberIfACustomStepChangesTest.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@
1717

1818
import java.io.IOException;
1919

20-
import org.junit.Assert;
2120
import org.junit.Test;
2221

2322
public class BumpThisNumberIfACustomStepChangesTest extends GradleIntegrationTest {
2423

2524
private void writeBuildFile(String toInsert) throws IOException {
26-
write("build.gradle",
25+
setFile("build.gradle").toLines(
2726
"plugins {",
2827
" id 'com.diffplug.gradle.spotless'",
2928
"}",
@@ -39,14 +38,13 @@ private void writeBuildFile(String toInsert) throws IOException {
3938
}
4039

4140
private void writeContentWithBadFormatting() throws IOException {
42-
write("README.md", "ABC");
41+
setFile("README.md").toContent("ABC");
4342
}
4443

4544
@Override
4645
protected void applyIsUpToDate(boolean upToDate) throws IOException {
4746
super.applyIsUpToDate(upToDate);
48-
String result = read("README.md");
49-
Assert.assertEquals("abc", result);
47+
assertFile("README.md").hasContent("abc");
5048
}
5149

5250
@Test

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/CustomLazyGroovyTest.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@
1717

1818
import java.io.IOException;
1919

20-
import org.junit.Assert;
2120
import org.junit.Test;
2221

2322
public class CustomLazyGroovyTest extends GradleIntegrationTest {
2423
@Test
2524
public void integration() throws IOException {
26-
write("build.gradle",
25+
setFile("build.gradle").toLines(
2726
"plugins {",
2827
" id 'com.diffplug.gradle.spotless'",
2928
"}",
@@ -35,9 +34,8 @@ public void integration() throws IOException {
3534
" }",
3635
" }",
3736
"}");
38-
write("README.md", "ABC");
37+
setFile("README.md").toContent("ABC");
3938
gradleRunner().withArguments("spotlessApply").build();
40-
String result = read("README.md");
41-
Assert.assertEquals("abc", result);
39+
assertFile("README.md").hasContent("abc");
4240
}
4341
}

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/DiffMessageFormatterTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ protected String getTaskErrorMessage(SpotlessTask task) {
7373

7474
@Test
7575
public void lineEndingProblem() throws Exception {
76-
SpotlessTask task = create(createTestFile("testFile", "A\r\nB\r\nC\r\n"));
76+
SpotlessTask task = create(setFile("testFile").toContent("A\r\nB\r\nC\r\n"));
7777
assertTaskFailure(task,
7878
" testFile",
7979
" @@ -1,3 +1,3 @@",
@@ -87,7 +87,7 @@ public void lineEndingProblem() throws Exception {
8787

8888
@Test
8989
public void whitespaceProblem() throws Exception {
90-
SpotlessTask task = create(createTestFile("testFile", "A \nB\t\nC \n"));
90+
SpotlessTask task = create(setFile("testFile").toContent("A \nB\t\nC \n"));
9191
task.addStep(FormatterStep.createNeverUpToDate("trimTrailing", input -> {
9292
Pattern pattern = Pattern.compile("[ \t]+$", Pattern.UNIX_LINES | Pattern.MULTILINE);
9393
return pattern.matcher(input).replaceAll("");
@@ -106,8 +106,8 @@ public void whitespaceProblem() throws Exception {
106106
@Test
107107
public void multipleFiles() throws Exception {
108108
SpotlessTask task = create(
109-
createTestFile("A", "1\r\n2\r\n"),
110-
createTestFile("B", "3\n4\r\n"));
109+
setFile("A").toContent("1\r\n2\r\n"),
110+
setFile("B").toContent("3\n4\r\n"));
111111
assertTaskFailure(task,
112112
" A",
113113
" @@ -1,2 +1,2 @@",
@@ -126,7 +126,7 @@ public void multipleFiles() throws Exception {
126126
public void manyFiles() throws Exception {
127127
List<File> testFiles = new ArrayList<>();
128128
for (int i = 0; i < 9 + DiffMessageFormatter.MAX_FILES_TO_LIST - 1; ++i) {
129-
testFiles.add(createTestFile(Integer.toString(i) + ".txt", "1\r\n2\r\n"));
129+
testFiles.add(setFile(Integer.toString(i) + ".txt").toContent("1\r\n2\r\n"));
130130
}
131131
SpotlessTask task = create(testFiles);
132132
assertTaskFailure(task,
@@ -199,7 +199,7 @@ public void manyFiles() throws Exception {
199199
public void manyManyFiles() throws Exception {
200200
List<File> testFiles = new ArrayList<>();
201201
for (int i = 0; i < 9 + DiffMessageFormatter.MAX_FILES_TO_LIST; ++i) {
202-
testFiles.add(createTestFile(Integer.toString(i) + ".txt", "1\r\n2\r\n"));
202+
testFiles.add(setFile(Integer.toString(i) + ".txt").toContent("1\r\n2\r\n"));
203203
}
204204
SpotlessTask task = create(testFiles);
205205
assertTaskFailure(task,
@@ -266,7 +266,7 @@ public void longFile() throws Exception {
266266
builder.append(i);
267267
builder.append("\r\n");
268268
}
269-
SpotlessTask task = create(createTestFile("testFile", builder.toString()));
269+
SpotlessTask task = create(setFile("testFile").toContent(builder.toString()));
270270
assertTaskFailure(task,
271271
" testFile",
272272
" @@ -1,1000 +1,1000 @@",

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/EncodingTest.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,12 @@
1717

1818
import java.nio.charset.Charset;
1919

20-
import org.junit.Assert;
2120
import org.junit.Test;
2221

23-
import com.diffplug.spotless.LineEnding;
24-
2522
public class EncodingTest extends GradleIntegrationTest {
2623
@Test
2724
public void defaultIsUtf8() throws Exception {
28-
write("build.gradle",
25+
setFile("build.gradle").toLines(
2926
"plugins {",
3027
" id 'com.diffplug.gradle.spotless'",
3128
"}",
@@ -35,14 +32,14 @@ public void defaultIsUtf8() throws Exception {
3532
" custom 'replaceMicro', { it.replace('µ', 'A') }",
3633
" }",
3734
"}");
38-
write("test.java", "µ");
35+
setFile("test.java").toContent("µ");
3936
gradleRunner().withArguments("spotlessApply").build();
40-
Assert.assertEquals("A", read("test.java"));
37+
assertFile("test.java").hasContent("A");
4138
}
4239

4340
@Test
4441
public void globalIsRespected() throws Exception {
45-
write("build.gradle",
42+
setFile("build.gradle").toLines(
4643
"plugins {",
4744
" id 'com.diffplug.gradle.spotless'",
4845
"}",
@@ -53,14 +50,14 @@ public void globalIsRespected() throws Exception {
5350
" }",
5451
" encoding 'US-ASCII'",
5552
"}");
56-
write("test.java", "µ");
53+
setFile("test.java").toContent("µ");
5754
gradleRunner().withArguments("spotlessApply").build();
58-
Assert.assertEquals("??", read("test.java"));
55+
assertFile("test.java").hasContent("??");
5956
}
6057

6158
@Test
6259
public void globalIsRespectedButCanBeOverridden() throws Exception {
63-
write("build.gradle",
60+
setFile("build.gradle").toLines(
6461
"plugins {",
6562
" id 'com.diffplug.gradle.spotless'",
6663
"}",
@@ -76,12 +73,10 @@ public void globalIsRespectedButCanBeOverridden() throws Exception {
7673
" }",
7774
" encoding 'US-ASCII'",
7875
"}");
79-
write("test.java", "µ");
80-
write("utf32.encoded", LineEnding.UNIX, Charset.forName("UTF-32"), "µ");
81-
Assert.assertEquals("µ", read("utf32.encoded", Charset.forName("UTF-32")));
82-
76+
setFile("test.java").toContent("µ");
77+
setFile("utf32.encoded").toContent("µ", Charset.forName("UTF-32"));
8378
gradleRunner().withArguments("spotlessApply").build();
84-
Assert.assertEquals("??", read("test.java"));
85-
Assert.assertEquals("A", read("utf32.encoded", Charset.forName("UTF-32")));
79+
assertFile("test.java").hasContent("??");
80+
assertFile("utf32.encoded").hasContent("A", Charset.forName("UTF-32"));
8681
}
8782
}

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/ErrorShouldRethrow.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ private void writeBuild(String... toInsert) throws IOException {
4949
lines.add(" }");
5050
lines.add(" }");
5151
lines.addAll(Arrays.asList(toInsert));
52-
write("build.gradle", lines.toArray(new String[lines.size()]));
52+
setFile("build.gradle").toContent(lines.stream().collect(Collectors.joining("\n")));
5353
}
5454

5555
@Test
5656
public void passesIfNoException() throws Exception {
5757
writeBuild(
5858
" } // format",
5959
"} // spotless");
60-
write("README.md", "This code is fun.");
60+
setFile("README.md").toContent("This code is fun.");
6161
runWithSuccess(":spotlessMisc");
6262
}
6363

@@ -66,7 +66,7 @@ public void anyExceptionShouldFail() throws Exception {
6666
writeBuild(
6767
" } // format",
6868
"} // spotless");
69-
write("README.md", "This code is fubar.");
69+
setFile("README.md").toContent("This code is fubar.");
7070
runWithFailure(
7171
":spotlessMiscStep 'no swearing' found problem in 'README.md':",
7272
"No swearing!",
@@ -79,7 +79,7 @@ public void unlessEnforceCheckIsFalse() throws Exception {
7979
" } // format",
8080
" enforceCheck false",
8181
"} // spotless");
82-
write("README.md", "This code is fubar.");
82+
setFile("README.md").toContent("This code is fubar.");
8383
runWithSuccess(":compileJava UP-TO-DATE");
8484
}
8585

@@ -89,7 +89,7 @@ public void unlessExemptedByStep() throws Exception {
8989
" ignoreErrorForStep 'no swearing'",
9090
" } // format",
9191
"} // spotless");
92-
write("README.md", "This code is fubar.");
92+
setFile("README.md").toContent("This code is fubar.");
9393
runWithSuccess(":spotlessMisc",
9494
"Unable to apply step 'no swearing' to 'README.md'");
9595
}
@@ -100,7 +100,7 @@ public void unlessExemptedByPath() throws Exception {
100100
" ignoreErrorForPath 'README.md'",
101101
" } // format",
102102
"} // spotless");
103-
write("README.md", "This code is fubar.");
103+
setFile("README.md").toContent("This code is fubar.");
104104
runWithSuccess(":spotlessMisc",
105105
"Unable to apply step 'no swearing' to 'README.md'");
106106
}
@@ -112,7 +112,7 @@ public void failsIfNeitherStepNorFileExempted() throws Exception {
112112
" ignoreErrorForPath 'nope'",
113113
" } // format",
114114
"} // spotless");
115-
write("README.md", "This code is fubar.");
115+
setFile("README.md").toContent("This code is fubar.");
116116
runWithFailure(
117117
":spotlessMiscStep 'no swearing' found problem in 'README.md':",
118118
"No swearing!",

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/FileTreeTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ public void fileTree() throws IOException {
4343

4444
@Test
4545
public void absolutePathDoesntWork() throws IOException {
46-
File someFile = write("someFolder/someFile");
46+
File someFile = setFile("someFolder/someFile").toContent("");
4747
File someFolder = someFile.getParentFile();
4848
fileTree.exclude(someFolder.getAbsolutePath());
4949
Assertions.assertThat(fileTree).containsExactlyInAnyOrder(someFile);
5050
}
5151

5252
@Test
5353
public void relativePathDoes() throws IOException {
54-
File someFile = write("someFolder/someFile");
54+
File someFile = setFile("someFolder/someFile").toContent("");
5555
File someFolder = someFile.getParentFile();
5656
fileTree.exclude(relativize(rootFolder(), someFolder));
5757
Assertions.assertThat(fileTree).containsExactlyInAnyOrder();

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/FormatTaskTest.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,31 +46,31 @@ public void createTask() {
4646
@Test(expected = GradleException.class)
4747
public void testLineEndingsCheckFail() throws IOException {
4848
checkTask.setLineEndingsPolicy(LineEnding.UNIX.createPolicy());
49-
checkTask.setTarget(Collections.singleton(createTestFile("testFile", "\r\n")));
49+
checkTask.setTarget(Collections.singleton(setFile("testFile").toContent("\r\n")));
5050
checkTask.execute();
5151
}
5252

5353
@Test
5454
public void testLineEndingsCheckPass() throws IOException {
5555
checkTask.setLineEndingsPolicy(LineEnding.UNIX.createPolicy());
56-
checkTask.setTarget(Collections.singleton(createTestFile("testFile", "\n")));
56+
checkTask.setTarget(Collections.singleton(setFile("testFile").toContent("\n")));
5757
checkTask.execute();
5858
}
5959

6060
@Test
6161
public void testLineEndingsApply() throws IOException {
62-
File testFile = createTestFile("testFile", "\r\n");
62+
File testFile = setFile("testFile").toContent("\r\n");
6363

6464
applyTask.setLineEndingsPolicy(LineEnding.UNIX.createPolicy());
6565
applyTask.setTarget(Collections.singleton(testFile));
6666
applyTask.execute();
6767

68-
assertFileContent("\n", testFile);
68+
assertFile(testFile).hasContent("\n");
6969
}
7070

7171
@Test
7272
public void testStepCheckFail() throws IOException {
73-
File testFile = createTestFile("testFile", "apple");
73+
File testFile = setFile("testFile").toContent("apple");
7474
checkTask.setTarget(Collections.singleton(testFile));
7575

7676
checkTask.addStep(FormatterStep.createNeverUpToDate("double-p", content -> content.replace("pp", "p")));
@@ -81,28 +81,28 @@ public void testStepCheckFail() throws IOException {
8181
" +aple");
8282
Assertions.assertThatThrownBy(() -> checkTask.execute()).hasStackTraceContaining(diff);
8383

84-
assertFileContent("apple", testFile);
84+
assertFile(testFile).hasContent("apple");
8585
}
8686

8787
@Test
8888
public void testStepCheckPass() throws IOException {
89-
File testFile = createTestFile("testFile", "aple");
89+
File testFile = setFile("testFile").toContent("aple");
9090
checkTask.setTarget(Collections.singleton(testFile));
9191

9292
checkTask.addStep(FormatterStep.createNeverUpToDate("double-p", content -> content.replace("pp", "p")));
9393
checkTask.execute();
9494

95-
assertFileContent("aple", testFile);
95+
assertFile(testFile).hasContent("aple");
9696
}
9797

9898
@Test
9999
public void testStepApply() throws IOException {
100-
File testFile = createTestFile("testFile", "apple");
100+
File testFile = setFile("testFile").toContent("apple");
101101
applyTask.setTarget(Collections.singleton(testFile));
102102

103103
applyTask.addStep(FormatterStep.createNeverUpToDate("double-p", content -> content.replace("pp", "p")));
104104
applyTask.execute();
105105

106-
assertFileContent("aple", testFile);
106+
assertFile(testFile).hasContent("aple");
107107
}
108108
}

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/FreshMarkExtensionTest.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@
1717

1818
import java.io.IOException;
1919

20-
import org.junit.Assert;
2120
import org.junit.Test;
2221

2322
public class FreshMarkExtensionTest extends GradleIntegrationTest {
2423
@Test
2524
public void integration() throws IOException {
26-
write("build.gradle",
25+
setFile("build.gradle").toLines(
2726
"buildscript { repositories { mavenCentral() } }",
2827
"plugins {",
2928
" id 'java'",
@@ -37,11 +36,8 @@ public void integration() throws IOException {
3736
" }",
3837
" }",
3938
"}");
40-
String unformatted = getTestResource("freshmark/FreshMarkUnformatted.test");
41-
write("README.md", unformatted);
39+
setFile("README.md").toResource("freshmark/FreshMarkUnformatted.test");
4240
gradleRunner().withArguments("spotlessApply").build();
43-
String result = read("README.md");
44-
String formatted = getTestResource("freshmark/FreshMarkFormatted.test");
45-
Assert.assertEquals(formatted, result);
41+
assertFile("README.md").sameAsResource("freshmark/FreshMarkFormatted.test");
4642
}
4743
}

0 commit comments

Comments
 (0)