Skip to content

Commit

Permalink
Normalize newlines when appending to file in CheckTaskTests
Browse files Browse the repository at this point in the history
  • Loading branch information
ParkerM committed Aug 24, 2022
1 parent 6c3a771 commit 3889f1c
Showing 1 changed file with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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<String> lines = Files.readAllLines(sourceFilePath);
lines.add(lineToAppend);
Files.write(sourceFilePath, lines, StandardOpenOption.TRUNCATE_EXISTING);
}

}

0 comments on commit 3889f1c

Please sign in to comment.