Skip to content

Commit e406e36

Browse files
committed
Normalize newlines when appending to file in CheckTaskTests
1 parent a9ee7e8 commit e406e36

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

Lines changed: 13 additions & 3 deletions
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 @@ void whenFirstInvocationSucceedsAndSourceIsModifiedThenSecondInvocationSucceeds(
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
}
@@ -146,4 +146,14 @@ private void copyFolder(Path source, Path target) throws IOException {
146146
}
147147
}
148148

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

0 commit comments

Comments
 (0)