Skip to content

Commit

Permalink
Issue checkstyle#6508: Do not fail NewlineAtEndOfFile check with LF_C…
Browse files Browse the repository at this point in the history
…R_CRLF if file consists only of a newline character
  • Loading branch information
Vampire committed May 13, 2019
1 parent 31c1705 commit ff59142
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Expand Up @@ -2,5 +2,6 @@
/src/test/resources/com/puppycrawl/tools/checkstyle/checks/newlineatendoffile/InputNewlineAtEndOfFileLf.java eol=lf
/src/test/resources/com/puppycrawl/tools/checkstyle/checks/newlineatendoffile/InputNewlineAtEndOfFileCrlf.java eol=crlf
/src/test/resources/com/puppycrawl/tools/checkstyle/checks/newlineatendoffile/InputNewlineAtEndOfFileCr.java -text
/src/test/resources/com/puppycrawl/tools/checkstyle/checks/newlineatendoffile/InputNewlineAtEndOfFileNewlineAtEndLf.txt eol=lf
/src/test/resources/com/puppycrawl/tools/checkstyle/grammar/InputNewlineCrAtEndOfFile.java -text
/src/test/resources/com/puppycrawl/tools/checkstyle/grammar/javadoc/javadocTags/InputSpaceBeforeDescriptionInBlockJavadocTags.javadoc eol=lf
2 changes: 1 addition & 1 deletion config/checkstyle_non_main_files_suppressions.xml
Expand Up @@ -8,7 +8,7 @@

<!-- Input files for NewlineAtEndOfFileCheckTest, intentional no new line at the end. -->
<suppress checks="NewlineAtEndOfFile"
files="[\\/]test[\\/].*[\\/]InputNewlineAtEndOfFile.*\.java"/>
files="[\\/]test[\\/].*[\\/]InputNewlineAtEndOfFile.*\.(java|txt)"/>

<!-- Intentional no new line at the end. -->
<suppress checks="NewlineAtEndOfFile"
Expand Down
Expand Up @@ -42,7 +42,7 @@ public enum LineSeparatorOption {
* Matches CR, LF and CRLF line separators.
* Only the length is used - the actual value is ignored.
*/
LF_CR_CRLF("##"),
LF_CR_CRLF("#"),

/** System default line separators. **/
SYSTEM(System.getProperty("line.separator"));
Expand All @@ -69,8 +69,7 @@ public boolean matches(byte... bytes) {
if (this == LF_CR_CRLF) {
// this silently assumes LF and CR are of length 1
// CRLF always matches LF, so CRLF isn't tested
result = LF.matches(Arrays.copyOfRange(bytes, 1, 2))
|| CR.matches(Arrays.copyOfRange(bytes, 1, 2));
result = LF.matches(bytes) || CR.matches(bytes);
}
else {
result = Arrays.equals(bytes, lineSeparator);
Expand Down
Expand Up @@ -180,6 +180,18 @@ public void testFileWithEmptyLineOnly() throws Exception {
expected);
}

@Test
public void testFileWithEmptyLineOnlyWithLfCrCrlf() throws Exception {
final DefaultConfiguration checkConfig =
createModuleConfig(NewlineAtEndOfFileCheck.class);
checkConfig.addAttribute("lineSeparator", LineSeparatorOption.LF_CR_CRLF.toString());
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
verify(
createChecker(checkConfig),
getPath("InputNewlineAtEndOfFileNewlineAtEndLf.txt"),
expected);
}

@Test
public void testWrongFile() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(NewlineAtEndOfFileCheck.class);
Expand Down
@@ -0,0 +1 @@

0 comments on commit ff59142

Please sign in to comment.