Skip to content

Commit 19e26c8

Browse files
committed
Normalize newlines when appending to file in CheckTaskTests
1 parent 97ecbcc commit 19e26c8

File tree

1 file changed

+13
-3
lines changed
  • spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/test/java/io/spring/javaformat/gradle

1 file changed

+13
-3
lines changed

spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/test/java/io/spring/javaformat/gradle/CheckTaskTests.java

+13-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import java.nio.file.StandardCopyOption;
2424
import java.nio.file.StandardOpenOption;
2525
import java.util.Arrays;
26-
import java.util.Collections;
26+
import java.util.List;
2727
import java.util.stream.Stream;
2828

2929
import org.gradle.testkit.runner.BuildResult;
@@ -72,8 +72,8 @@ public void whenFirstInvocationSucceedsAndSourceIsModifiedThenSecondInvocationSu
7272
GradleBuild gradleBuild = this.gradleBuild.source(this.temp);
7373
BuildResult result = gradleBuild.build("check");
7474
assertThat(result.task(":checkFormatMain").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
75-
Files.write(new File(this.temp, "src/main/java/simple/Simple.java").toPath(),
76-
Collections.singletonList("// A change to the file"), StandardOpenOption.APPEND);
75+
appendToFileNormalizingNewlines(new File(this.temp, "src/main/java/simple/Simple.java").toPath(),
76+
"// A change to the file");
7777
result = gradleBuild.build("--debug", "check");
7878
assertThat(result.task(":checkFormatMain").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
7979
}
@@ -145,4 +145,14 @@ private void copyFolder(Path source, Path target) throws IOException {
145145
}
146146
}
147147

148+
/**
149+
* Uses a read/modify/truncate approach to append a line to a file.
150+
* This avoids issues where the standard append option results in mixed line-endings.
151+
*/
152+
private void appendToFileNormalizingNewlines(Path sourceFilePath, String lineToAppend) throws IOException {
153+
List<String> lines = Files.readAllLines(sourceFilePath);
154+
lines.add(lineToAppend);
155+
Files.write(sourceFilePath, lines, StandardOpenOption.TRUNCATE_EXISTING);
156+
}
157+
148158
}

0 commit comments

Comments
 (0)