From d82d0c58bd376941d1de7383aa622b55e71d3c00 Mon Sep 17 00:00:00 2001 From: Parker Mauney Date: Thu, 25 Nov 2021 13:09:35 -0600 Subject: [PATCH 1/6] Add line separator fixtures + Add .gitattributes for fixtures with a specific eol. --- .../src/test/resources/.gitattributes | 4 ++++ .../src/test/resources/expected/correct-cr.txt | 1 + .../src/test/resources/expected/correct-crlf.txt | 9 +++++++++ .../src/test/resources/expected/correct-lf.txt | 9 +++++++++ .../src/test/resources/source/correct-cr.txt | 1 + .../src/test/resources/source/correct-crlf.txt | 9 +++++++++ .../src/test/resources/source/correct-lf.txt | 9 +++++++++ 7 files changed, 42 insertions(+) create mode 100644 spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/.gitattributes create mode 100644 spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/expected/correct-cr.txt create mode 100644 spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/expected/correct-crlf.txt create mode 100644 spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/expected/correct-lf.txt create mode 100644 spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/source/correct-cr.txt create mode 100644 spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/source/correct-crlf.txt create mode 100644 spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/source/correct-lf.txt diff --git a/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/.gitattributes b/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/.gitattributes new file mode 100644 index 00000000..95175246 --- /dev/null +++ b/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/.gitattributes @@ -0,0 +1,4 @@ +# Test resources that need specific eol +**/correct-crlf.txt eol=crlf +**/correct-cr.txt eol=cr +**/correct-lf.txt eol=lf diff --git a/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/expected/correct-cr.txt b/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/expected/correct-cr.txt new file mode 100644 index 00000000..a72e4bf3 --- /dev/null +++ b/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/expected/correct-cr.txt @@ -0,0 +1 @@ +package correct; public class CorrectCr { public static void main(String[] args) throws Exception { // FIXME } } \ No newline at end of file diff --git a/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/expected/correct-crlf.txt b/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/expected/correct-crlf.txt new file mode 100644 index 00000000..5a609678 --- /dev/null +++ b/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/expected/correct-crlf.txt @@ -0,0 +1,9 @@ +package correct; + +public class CorrectCrlf { + + public static void main(String[] args) throws Exception { + // FIXME + } + +} diff --git a/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/expected/correct-lf.txt b/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/expected/correct-lf.txt new file mode 100644 index 00000000..3b6208a2 --- /dev/null +++ b/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/expected/correct-lf.txt @@ -0,0 +1,9 @@ +package correct; + +public class CorrectLf { + + public static void main(String[] args) throws Exception { + // FIXME + } + +} diff --git a/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/source/correct-cr.txt b/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/source/correct-cr.txt new file mode 100644 index 00000000..a72e4bf3 --- /dev/null +++ b/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/source/correct-cr.txt @@ -0,0 +1 @@ +package correct; public class CorrectCr { public static void main(String[] args) throws Exception { // FIXME } } \ No newline at end of file diff --git a/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/source/correct-crlf.txt b/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/source/correct-crlf.txt new file mode 100644 index 00000000..5a609678 --- /dev/null +++ b/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/source/correct-crlf.txt @@ -0,0 +1,9 @@ +package correct; + +public class CorrectCrlf { + + public static void main(String[] args) throws Exception { + // FIXME + } + +} diff --git a/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/source/correct-lf.txt b/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/source/correct-lf.txt new file mode 100644 index 00000000..3b6208a2 --- /dev/null +++ b/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/source/correct-lf.txt @@ -0,0 +1,9 @@ +package correct; + +public class CorrectLf { + + public static void main(String[] args) throws Exception { + // FIXME + } + +} From b0c889f48342ac035d700f1c53752267582a3818 Mon Sep 17 00:00:00 2001 From: Parker Mauney Date: Thu, 25 Nov 2021 13:11:29 -0600 Subject: [PATCH 2/6] Preserve existing eol by default --- .../javaformat/formatter/Formatter.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/spring-javaformat/spring-javaformat-formatter/src/main/java/io/spring/javaformat/formatter/Formatter.java b/spring-javaformat/spring-javaformat-formatter/src/main/java/io/spring/javaformat/formatter/Formatter.java index 83f6406c..c76873cf 100644 --- a/spring-javaformat/spring-javaformat-formatter/src/main/java/io/spring/javaformat/formatter/Formatter.java +++ b/spring-javaformat/spring-javaformat-formatter/src/main/java/io/spring/javaformat/formatter/Formatter.java @@ -17,6 +17,8 @@ package io.spring.javaformat.formatter; import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.eclipse.jface.text.IRegion; import org.eclipse.text.edits.TextEdit; @@ -56,6 +58,11 @@ public class Formatter { */ private static final int DEFAULT_INDENTATION_LEVEL = 0; + /** + * Pattern that matches all line separators into named-capturing group "sep". + */ + private static final Pattern LINE_SEPARATOR_PATTERN = Pattern.compile("(?(\r\n|\r|\n))"); + /** * The default line separator. */ @@ -123,6 +130,9 @@ public TextEdit format(String source, int offset, int length, String lineSeparat public TextEdit format(int kind, String source, int offset, int length, int indentationLevel, String lineSeparator) { + if (lineSeparator == null) { + lineSeparator = detectLineSeparator(source); + } return this.delegate.format(kind, source, offset, length, indentationLevel, lineSeparator); } @@ -148,6 +158,9 @@ public TextEdit format(String source, IRegion[] regions, String lineSeparator) { } public TextEdit format(int kind, String source, IRegion[] regions, int indentationLevel, String lineSeparator) { + if (lineSeparator == null) { + lineSeparator = detectLineSeparator(source); + } return this.delegate.format(kind, source, regions, indentationLevel, lineSeparator); } @@ -159,4 +172,17 @@ public void setOptions(Map options) { this.delegate.setOptions(options); } + private String detectLineSeparator(String contents) { + Matcher matcher = LINE_SEPARATOR_PATTERN.matcher(contents); + if (!matcher.find()) { + return DEFAULT_LINE_SEPARATOR; + } + String firstMatch = matcher.group("sep"); + while (matcher.find()) { + if (!matcher.group("sep").equals(firstMatch)) { + return DEFAULT_LINE_SEPARATOR; + } + } + return firstMatch; + } } From ca1b76685e9c3c11509d342af20019d1e2ef1234 Mon Sep 17 00:00:00 2001 From: Parker Mauney Date: Fri, 26 Nov 2021 16:30:44 -0600 Subject: [PATCH 3/6] Enforce LF on VerifyApply fixtures to keep tests OS-agnostic --- .../spring-javaformat-maven-plugin/src/it/.gitattributes | 2 ++ .../src/test/java/io/spring/format/maven/VerifyApply.java | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 spring-javaformat-maven/spring-javaformat-maven-plugin/src/it/.gitattributes diff --git a/spring-javaformat-maven/spring-javaformat-maven-plugin/src/it/.gitattributes b/spring-javaformat-maven/spring-javaformat-maven-plugin/src/it/.gitattributes new file mode 100644 index 00000000..525113da --- /dev/null +++ b/spring-javaformat-maven/spring-javaformat-maven-plugin/src/it/.gitattributes @@ -0,0 +1,2 @@ +# Test resources that need a predictable eol +apply*/src/main/java/simple/Simple.java eol=lf diff --git a/spring-javaformat-maven/spring-javaformat-maven-plugin/src/test/java/io/spring/format/maven/VerifyApply.java b/spring-javaformat-maven/spring-javaformat-maven-plugin/src/test/java/io/spring/format/maven/VerifyApply.java index 65fc2ec0..0c21dc7e 100644 --- a/spring-javaformat-maven/spring-javaformat-maven-plugin/src/test/java/io/spring/format/maven/VerifyApply.java +++ b/spring-javaformat-maven/spring-javaformat-maven-plugin/src/test/java/io/spring/format/maven/VerifyApply.java @@ -30,7 +30,7 @@ */ public class VerifyApply { - private static final String LF = System.lineSeparator(); + private static final String LF = "\n"; private static final String JAVA_FILE = "src/main/java/simple/Simple.java"; From a9ee7e8a27cb358adac601d98e8e4dd9a2f40e23 Mon Sep 17 00:00:00 2001 From: Parker Mauney Date: Fri, 26 Nov 2021 18:51:05 -0600 Subject: [PATCH 4/6] Normalize newlines in eclipse ProjectSettingsFilesTests assertion This test hits Properties#store, in which BufferedWriter#newLine applies system default line separators. --- .../eclipse/projectsettings/ProjectSettingsFilesTests.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spring-javaformat-eclipse/io.spring.javaformat.eclipse.tests/src/io/spring/javaformat/eclipse/projectsettings/ProjectSettingsFilesTests.java b/spring-javaformat-eclipse/io.spring.javaformat.eclipse.tests/src/io/spring/javaformat/eclipse/projectsettings/ProjectSettingsFilesTests.java index a75b92b1..20c06744 100644 --- a/spring-javaformat-eclipse/io.spring.javaformat.eclipse.tests/src/io/spring/javaformat/eclipse/projectsettings/ProjectSettingsFilesTests.java +++ b/spring-javaformat-eclipse/io.spring.javaformat.eclipse.tests/src/io/spring/javaformat/eclipse/projectsettings/ProjectSettingsFilesTests.java @@ -95,7 +95,8 @@ void applyToProjectWithFileMergesToDotSettings() throws Exception { }).given(projectFile).setContents((InputStream) any(), anyInt(), any()); files.applyToProject(project, monitor); verify(projectFile).setContents((InputStream) any(), eq(1), eq(monitor)); - assertThat(out.toString(StandardCharsets.UTF_8)).isEqualTo("a=b\ny=z\n"); + assertThat(out.toString(StandardCharsets.UTF_8)) + .isEqualToNormalizingNewlines("a=b\ny=z\n"); } private ProjectSettingsFile createPrefsFile() throws IOException { From e406e367154338389fa5ab61cf78eae8ddf47ffd Mon Sep 17 00:00:00 2001 From: Parker Mauney Date: Sat, 5 Mar 2022 19:28:56 -0600 Subject: [PATCH 5/6] Normalize newlines when appending to file in CheckTaskTests --- .../spring/javaformat/gradle/CheckTaskTests.java | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) 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 a5554177..0420e1c2 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 @@ void whenFirstInvocationSucceedsAndSourceIsModifiedThenSecondInvocationSucceeds( 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); } @@ -146,4 +146,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); + } + } From 8eaadf29d06127bd948582b42d48a7108e35f88f Mon Sep 17 00:00:00 2001 From: Parker Mauney Date: Wed, 15 Feb 2023 18:03:34 -0500 Subject: [PATCH 6/6] Use eclipse util method to detect line separator --- .../pom.xml | 1 + .../pom.xml | 1 + .../javaformat/formatter/Formatter.java | 25 +++---------------- 3 files changed, 5 insertions(+), 22 deletions(-) diff --git a/spring-javaformat/spring-javaformat-formatter-eclipse-jdk11/pom.xml b/spring-javaformat/spring-javaformat-formatter-eclipse-jdk11/pom.xml index 9d7f1a3d..4cfa7455 100644 --- a/spring-javaformat/spring-javaformat-formatter-eclipse-jdk11/pom.xml +++ b/spring-javaformat/spring-javaformat-formatter-eclipse-jdk11/pom.xml @@ -173,6 +173,7 @@ + diff --git a/spring-javaformat/spring-javaformat-formatter-eclipse-jdk8/pom.xml b/spring-javaformat/spring-javaformat-formatter-eclipse-jdk8/pom.xml index 60417036..f3136138 100644 --- a/spring-javaformat/spring-javaformat-formatter-eclipse-jdk8/pom.xml +++ b/spring-javaformat/spring-javaformat-formatter-eclipse-jdk8/pom.xml @@ -173,6 +173,7 @@ + diff --git a/spring-javaformat/spring-javaformat-formatter/src/main/java/io/spring/javaformat/formatter/Formatter.java b/spring-javaformat/spring-javaformat-formatter/src/main/java/io/spring/javaformat/formatter/Formatter.java index c76873cf..90a7f091 100644 --- a/spring-javaformat/spring-javaformat-formatter/src/main/java/io/spring/javaformat/formatter/Formatter.java +++ b/spring-javaformat/spring-javaformat-formatter/src/main/java/io/spring/javaformat/formatter/Formatter.java @@ -17,10 +17,9 @@ package io.spring.javaformat.formatter; import java.util.Map; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import org.eclipse.jface.text.IRegion; +import org.eclipse.jface.text.TextUtilities; import org.eclipse.text.edits.TextEdit; import io.spring.javaformat.config.JavaBaseline; @@ -58,11 +57,6 @@ public class Formatter { */ private static final int DEFAULT_INDENTATION_LEVEL = 0; - /** - * Pattern that matches all line separators into named-capturing group "sep". - */ - private static final Pattern LINE_SEPARATOR_PATTERN = Pattern.compile("(?(\r\n|\r|\n))"); - /** * The default line separator. */ @@ -131,7 +125,7 @@ public TextEdit format(String source, int offset, int length, String lineSeparat public TextEdit format(int kind, String source, int offset, int length, int indentationLevel, String lineSeparator) { if (lineSeparator == null) { - lineSeparator = detectLineSeparator(source); + lineSeparator = TextUtilities.determineLineDelimiter(source, DEFAULT_LINE_SEPARATOR); } return this.delegate.format(kind, source, offset, length, indentationLevel, lineSeparator); } @@ -159,7 +153,7 @@ public TextEdit format(String source, IRegion[] regions, String lineSeparator) { public TextEdit format(int kind, String source, IRegion[] regions, int indentationLevel, String lineSeparator) { if (lineSeparator == null) { - lineSeparator = detectLineSeparator(source); + lineSeparator = TextUtilities.determineLineDelimiter(source, DEFAULT_LINE_SEPARATOR); } return this.delegate.format(kind, source, regions, indentationLevel, lineSeparator); } @@ -172,17 +166,4 @@ public void setOptions(Map options) { this.delegate.setOptions(options); } - private String detectLineSeparator(String contents) { - Matcher matcher = LINE_SEPARATOR_PATTERN.matcher(contents); - if (!matcher.find()) { - return DEFAULT_LINE_SEPARATOR; - } - String firstMatch = matcher.group("sep"); - while (matcher.find()) { - if (!matcher.group("sep").equals(firstMatch)) { - return DEFAULT_LINE_SEPARATOR; - } - } - return firstMatch; - } }