From 27260a355473c62d6c36b73cd5b89490215add2d Mon Sep 17 00:00:00 2001 From: Jonathan Bluett-Duncan Date: Mon, 4 Dec 2017 19:50:17 +0000 Subject: [PATCH 1/3] Attempt to fix seemingly test-related bug on Windows --- .../com/diffplug/gradle/spotless/UpToDateTest.java | 10 +++++----- .../java/com/diffplug/spotless/ResourceHarness.java | 10 ++++++---- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/UpToDateTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/UpToDateTest.java index aa12d7e406..bb0765a47b 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/UpToDateTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/UpToDateTest.java @@ -43,7 +43,7 @@ public void testNormalCase() throws IOException { write("README.md", "ABC"); // first time, the task runs as expected applyIsUpToDate(false); - assertFile("README.md").hasContent("abc"); + assertThatNewFile("README.md").hasContent("abc"); // because a file was changed (by spotless), // up-to-date is false, even though nothing is // going to change during this run. This second @@ -61,13 +61,13 @@ public void testNearPathologicalCase() throws IOException { write("README.md", "ABC"); // first time, up-to-date is false applyIsUpToDate(false); - assertFile("README.md").hasContent("abc"); + assertThatNewFile("README.md").hasContent("abc"); // now we'll change the file write("README.md", "AB"); // as expected, the task will run again applyIsUpToDate(false); - assertFile("README.md").hasContent("ab"); + assertThatNewFile("README.md").hasContent("ab"); // and it'll take two more runs to get to up-to-date applyIsUpToDate(false); applyIsUpToDate(true); @@ -79,14 +79,14 @@ public void testPathologicalCase() throws IOException { write("README.md", "ABC"); // first time, up-to-date is false applyIsUpToDate(false); - assertFile("README.md").hasContent("abc"); + assertThatNewFile("README.md").hasContent("abc"); // now we'll change the file back to EXACTLY its original content write("README.md", "ABC"); // the task should run again, but instead the next line will // fail an assertion, because the task is actually reported as up-to-date applyIsUpToDate(false); - assertFile("README.md").hasContent("abc"); + assertThatNewFile("README.md").hasContent("abc"); // and it'll take two more runs to get to up-to-date applyIsUpToDate(false); applyIsUpToDate(true); diff --git a/testlib/src/main/java/com/diffplug/spotless/ResourceHarness.java b/testlib/src/main/java/com/diffplug/spotless/ResourceHarness.java index d75c4acdc3..dba89322cf 100644 --- a/testlib/src/main/java/com/diffplug/spotless/ResourceHarness.java +++ b/testlib/src/main/java/com/diffplug/spotless/ResourceHarness.java @@ -106,10 +106,14 @@ protected File write(String path, LineEnding ending, Charset encoding, String... return target.toFile(); } - protected AbstractFileAssert assertFile(String path) throws IOException { + protected AbstractFileAssert assertThatNewFile(String path) throws IOException { return Assertions.assertThat(newFile(path)).usingCharset(StandardCharsets.UTF_8); } + protected AbstractFileAssert assertThatExistingFile(File path) { + return Assertions.assertThat(path).usingCharset(StandardCharsets.UTF_8); + } + protected String read(Path path) throws IOException { return read(path, LineEnding.UNIX); } @@ -184,9 +188,7 @@ protected File createTestFile(String filename, String content) throws IOExceptio /** Asserts that the given resource from the src/test/resources directory has the same content as the given file. */ protected void assertFileContent(String expectedContent, File actual) throws IOException { - // This line thing is necessary for the tests to pass when Windows git screws up the line-endings - String actualContent = new String(Files.readAllBytes(actual.toPath()), StandardCharsets.UTF_8); - Assert.assertEquals(expectedContent, actualContent); + assertThatExistingFile(actual).hasContent(expectedContent); } /** Reads the given resource from "before", applies the step, and makes sure the result is "after". */ From 324183dfd72144e3918843d54984623905b86081 Mon Sep 17 00:00:00 2001 From: Jonathan Bluett-Duncan Date: Tue, 5 Dec 2017 10:45:49 +0000 Subject: [PATCH 2/3] Revert "Attempt to fix seemingly test-related bug on Windows" This reverts commit 27260a3 --- .../com/diffplug/gradle/spotless/UpToDateTest.java | 10 +++++----- .../java/com/diffplug/spotless/ResourceHarness.java | 10 ++++------ 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/UpToDateTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/UpToDateTest.java index bb0765a47b..aa12d7e406 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/UpToDateTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/UpToDateTest.java @@ -43,7 +43,7 @@ public void testNormalCase() throws IOException { write("README.md", "ABC"); // first time, the task runs as expected applyIsUpToDate(false); - assertThatNewFile("README.md").hasContent("abc"); + assertFile("README.md").hasContent("abc"); // because a file was changed (by spotless), // up-to-date is false, even though nothing is // going to change during this run. This second @@ -61,13 +61,13 @@ public void testNearPathologicalCase() throws IOException { write("README.md", "ABC"); // first time, up-to-date is false applyIsUpToDate(false); - assertThatNewFile("README.md").hasContent("abc"); + assertFile("README.md").hasContent("abc"); // now we'll change the file write("README.md", "AB"); // as expected, the task will run again applyIsUpToDate(false); - assertThatNewFile("README.md").hasContent("ab"); + assertFile("README.md").hasContent("ab"); // and it'll take two more runs to get to up-to-date applyIsUpToDate(false); applyIsUpToDate(true); @@ -79,14 +79,14 @@ public void testPathologicalCase() throws IOException { write("README.md", "ABC"); // first time, up-to-date is false applyIsUpToDate(false); - assertThatNewFile("README.md").hasContent("abc"); + assertFile("README.md").hasContent("abc"); // now we'll change the file back to EXACTLY its original content write("README.md", "ABC"); // the task should run again, but instead the next line will // fail an assertion, because the task is actually reported as up-to-date applyIsUpToDate(false); - assertThatNewFile("README.md").hasContent("abc"); + assertFile("README.md").hasContent("abc"); // and it'll take two more runs to get to up-to-date applyIsUpToDate(false); applyIsUpToDate(true); diff --git a/testlib/src/main/java/com/diffplug/spotless/ResourceHarness.java b/testlib/src/main/java/com/diffplug/spotless/ResourceHarness.java index dba89322cf..d75c4acdc3 100644 --- a/testlib/src/main/java/com/diffplug/spotless/ResourceHarness.java +++ b/testlib/src/main/java/com/diffplug/spotless/ResourceHarness.java @@ -106,14 +106,10 @@ protected File write(String path, LineEnding ending, Charset encoding, String... return target.toFile(); } - protected AbstractFileAssert assertThatNewFile(String path) throws IOException { + protected AbstractFileAssert assertFile(String path) throws IOException { return Assertions.assertThat(newFile(path)).usingCharset(StandardCharsets.UTF_8); } - protected AbstractFileAssert assertThatExistingFile(File path) { - return Assertions.assertThat(path).usingCharset(StandardCharsets.UTF_8); - } - protected String read(Path path) throws IOException { return read(path, LineEnding.UNIX); } @@ -188,7 +184,9 @@ protected File createTestFile(String filename, String content) throws IOExceptio /** Asserts that the given resource from the src/test/resources directory has the same content as the given file. */ protected void assertFileContent(String expectedContent, File actual) throws IOException { - assertThatExistingFile(actual).hasContent(expectedContent); + // This line thing is necessary for the tests to pass when Windows git screws up the line-endings + String actualContent = new String(Files.readAllBytes(actual.toPath()), StandardCharsets.UTF_8); + Assert.assertEquals(expectedContent, actualContent); } /** Reads the given resource from "before", applies the step, and makes sure the result is "after". */ From 5ae5e9f7be20ae90675eaadb7d33b6a6f586b8f4 Mon Sep 17 00:00:00 2001 From: Jonathan Bluett-Duncan Date: Tue, 5 Dec 2017 10:57:08 +0000 Subject: [PATCH 3/3] Second attempt to fix seemingly test-related Windows bug --- testlib/src/main/java/com/diffplug/spotless/StepHarness.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/testlib/src/main/java/com/diffplug/spotless/StepHarness.java b/testlib/src/main/java/com/diffplug/spotless/StepHarness.java index d5b1db148a..7b4be1e1bd 100644 --- a/testlib/src/main/java/com/diffplug/spotless/StepHarness.java +++ b/testlib/src/main/java/com/diffplug/spotless/StepHarness.java @@ -29,7 +29,8 @@ public class StepHarness { /** Creates a StepHarness around the given FormatterFunc. */ public StepHarness(FormatterFunc formatter) { - this.formatter = Objects.requireNonNull(formatter); + Objects.requireNonNull(formatter); + this.formatter = input -> LineEnding.toUnix(formatter.apply(input)); } /** Creates a harness for testing steps which don't depend on the file. */