Skip to content

Commit 3ebb666

Browse files
authored
Merge pull request #180 from diffplug/feature/windowsNewlines
Fixed some newline issues for running tests on Windows
2 parents 66c2a4b + a0c97af commit 3ebb666

File tree

5 files changed

+22
-27
lines changed

5 files changed

+22
-27
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ public void globalIsRespectedButCanBeOverridden() throws Exception {
7878
"}");
7979
write("test.java", "µ");
8080
write("utf32.encoded", LineEnding.UNIX, Charset.forName("UTF-32"), "µ");
81-
Assert.assertEquals(\n", read("utf32.encoded", LineEnding.UNIX, Charset.forName("UTF-32")));
81+
Assert.assertEquals(\n", read("utf32.encoded", Charset.forName("UTF-32")));
8282

8383
gradleRunner().withArguments("spotlessApply").build();
8484
Assert.assertEquals("??\n", read("test.java"));
85-
Assert.assertEquals("A\n", read("utf32.encoded", LineEnding.UNIX, Charset.forName("UTF-32")));
85+
Assert.assertEquals("A\n", read("utf32.encoded", Charset.forName("UTF-32")));
8686
}
8787
}

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,8 @@
2727

2828
import com.diffplug.common.base.Errors;
2929
import com.diffplug.common.base.StringPrinter;
30-
import com.diffplug.spotless.LineEnding;
3130

3231
public class GradleIncrementalResolutionTest extends GradleIntegrationTest {
33-
private static final boolean IS_UNIX = LineEnding.PLATFORM_NATIVE.str().equals("\n");
34-
3532
@Test
3633
public void failureDoesntTriggerAll() throws IOException {
3734
write("build.gradle",
@@ -60,9 +57,9 @@ public void failureDoesntTriggerAll() throws IOException {
6057
checkRanAgainst("abc");
6158
// apply will run against all three the first time
6259
applyRanAgainst("abc");
63-
// for some reason, it appears unix has higher resolution on which files need to be checked
64-
applyRanAgainst(IS_UNIX ? "b" : "abc");
65-
// but nobody the last time
60+
// the second time, it will only run on the file that was changes
61+
applyRanAgainst("b");
62+
// and nobody the last time
6663
applyRanAgainst("");
6764

6865
// if we change just one file

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.gradle.testkit.runner.GradleRunner;
2828
import org.gradle.testkit.runner.TaskOutcome;
2929
import org.junit.Assert;
30+
import org.junit.Before;
3031

3132
import com.diffplug.common.base.Errors;
3233
import com.diffplug.common.base.StringPrinter;
@@ -36,6 +37,11 @@
3637
import com.diffplug.spotless.ResourceHarness;
3738

3839
public class GradleIntegrationTest extends ResourceHarness {
40+
@Before
41+
public void gitAttributes() throws IOException {
42+
write(".gitattributes", "* text eol=lf");
43+
}
44+
3945
protected GradleRunner gradleRunner() throws IOException {
4046
return GradleRunner.create().withProjectDir(rootFolder()).withPluginClasspath();
4147
}

testlib/src/main/java/com/diffplug/spotless/ResourceHarness.java

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -110,31 +110,20 @@ protected AbstractFileAssert<?> assertFile(String path) throws IOException {
110110
return Assertions.assertThat(newFile(path)).usingCharset(StandardCharsets.UTF_8);
111111
}
112112

113-
protected String read(Path path) throws IOException {
114-
return read(path, LineEnding.UNIX);
115-
}
116-
117113
protected String read(String path) throws IOException {
118-
return read(path, LineEnding.UNIX);
114+
return read(newFile(path).toPath());
119115
}
120116

121-
protected String read(String path, LineEnding ending) throws IOException {
122-
return read(path, ending, StandardCharsets.UTF_8);
123-
}
124-
125-
protected String read(Path path, LineEnding ending) throws IOException {
126-
return read(path, ending, StandardCharsets.UTF_8);
117+
protected String read(Path path) throws IOException {
118+
return read(path, StandardCharsets.UTF_8);
127119
}
128120

129-
protected String read(String path, LineEnding ending, Charset encoding) throws IOException {
130-
Path target = newFile(path).toPath();
131-
return read(target, ending, encoding);
121+
protected String read(String path, Charset encoding) throws IOException {
122+
return read(newFile(path).toPath(), encoding);
132123
}
133124

134-
protected String read(Path path, LineEnding ending, Charset encoding) throws IOException {
135-
String content = new String(Files.readAllBytes(path), encoding);
136-
String allUnixNewline = LineEnding.toUnix(content);
137-
return allUnixNewline.replace("\n", ending.str());
125+
protected String read(Path path, Charset encoding) throws IOException {
126+
return new String(Files.readAllBytes(path), encoding);
138127
}
139128

140129
protected void replace(String path, String toReplace, String replaceWith) throws IOException {

testlib/src/main/java/com/diffplug/spotless/StepHarness.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ public StepHarness(FormatterFunc formatter) {
3434

3535
/** Creates a harness for testing steps which don't depend on the file. */
3636
public static StepHarness forStep(FormatterStep step) {
37-
return new StepHarness(input -> step.format(input, new File("")));
37+
// We don't care if an individual FormatterStep is misbehaving on line-endings, because
38+
// Formatter fixes that. No reason to care in tests either. It's likely to pop up when
39+
// running tests on Windows from time-to-time
40+
return new StepHarness(input -> LineEnding.toUnix(step.format(input, new File(""))));
3841
}
3942

4043
/** Creates a harness for testing a formatter whose steps don't depend on the file. */

0 commit comments

Comments
 (0)