|
23 | 23 | import java.nio.file.StandardCopyOption;
|
24 | 24 | import java.nio.file.StandardOpenOption;
|
25 | 25 | import java.util.Arrays;
|
26 |
| -import java.util.Collections; |
| 26 | +import java.util.List; |
27 | 27 | import java.util.stream.Stream;
|
28 | 28 |
|
29 | 29 | import org.gradle.testkit.runner.BuildResult;
|
@@ -72,8 +72,8 @@ public void whenFirstInvocationSucceedsAndSourceIsModifiedThenSecondInvocationSu
|
72 | 72 | GradleBuild gradleBuild = this.gradleBuild.source(this.temp);
|
73 | 73 | BuildResult result = gradleBuild.build("check");
|
74 | 74 | 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"); |
77 | 77 | result = gradleBuild.build("--debug", "check");
|
78 | 78 | assertThat(result.task(":checkFormatMain").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
|
79 | 79 | }
|
@@ -145,4 +145,14 @@ private void copyFolder(Path source, Path target) throws IOException {
|
145 | 145 | }
|
146 | 146 | }
|
147 | 147 |
|
| 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 | + |
148 | 158 | }
|
0 commit comments