diff --git a/spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/test/java/io/spring/javaformat/gradle/CheckTaskTests.java b/spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/test/java/io/spring/javaformat/gradle/CheckTaskTests.java index 6e38db7b..7a58d00c 100644 --- a/spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/test/java/io/spring/javaformat/gradle/CheckTaskTests.java +++ b/spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/test/java/io/spring/javaformat/gradle/CheckTaskTests.java @@ -23,7 +23,7 @@ import java.nio.file.StandardCopyOption; import java.nio.file.StandardOpenOption; import java.util.Arrays; -import java.util.Collections; +import java.util.List; import java.util.stream.Stream; import org.gradle.testkit.runner.BuildResult; @@ -72,8 +72,8 @@ public void whenFirstInvocationSucceedsAndSourceIsModifiedThenSecondInvocationSu GradleBuild gradleBuild = this.gradleBuild.source(this.temp); BuildResult result = gradleBuild.build("check"); assertThat(result.task(":checkFormatMain").getOutcome()).isEqualTo(TaskOutcome.SUCCESS); - Files.write(new File(this.temp, "src/main/java/simple/Simple.java").toPath(), - Collections.singletonList("// A change to the file"), StandardOpenOption.APPEND); + appendToFileNormalizingNewlines(new File(this.temp, "src/main/java/simple/Simple.java").toPath(), + "// A change to the file"); result = gradleBuild.build("--debug", "check"); assertThat(result.task(":checkFormatMain").getOutcome()).isEqualTo(TaskOutcome.SUCCESS); } @@ -145,4 +145,14 @@ private void copyFolder(Path source, Path target) throws IOException { } } + /** + * Uses a read/modify/truncate approach to append a line to a file. + * This avoids issues where the standard append option results in mixed line-endings. + */ + private void appendToFileNormalizingNewlines(Path sourceFilePath, String lineToAppend) throws IOException { + List lines = Files.readAllLines(sourceFilePath); + lines.add(lineToAppend); + Files.write(sourceFilePath, lines, StandardOpenOption.TRUNCATE_EXISTING); + } + }