Skip to content

Commit

Permalink
Issue checkstyle#4735: make RegexpHeaderCheck detect and enforce rege…
Browse files Browse the repository at this point in the history
…x having '\n\n'
  • Loading branch information
Vivek Rao committed Aug 2, 2017
1 parent cf436f1 commit b7aa926
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 2 deletions.
Expand Up @@ -170,10 +170,13 @@ private void loadHeader(final Reader headerReader) throws IOException {
final LineNumberReader lnr = new LineNumberReader(headerReader);
try {
while (true) {
final String line = lnr.readLine();
String line = lnr.readLine();
if (line == null) {
break;
}
if (line.isEmpty()) {
line = "^$";
}
readerLines.add(line);
}
postProcessHeaderLines();
Expand Down
Expand Up @@ -211,6 +211,29 @@ public void testFailureForMultilineRegexp() throws Exception {
}
}

@Test
public void testInlineRegexpHeaderConsecutiveNewlines() throws Exception {
final DefaultConfiguration checkConfig =
createCheckConfig(RegexpHeaderCheck.class);
checkConfig.addAttribute("header", "^/*$\\n// .*\\n\\n// Created: 2017\\n^//.*");
final String[] expected = {
"3: " + getCheckMessage(MSG_HEADER_MISMATCH, "^$"),
};
verify(checkConfig, getPath("InputRegexpHeaderConsecutiveNewLines.java"), expected);
}

@Test
public void testInlineRegexpHeaderConsecutiveNewlinesThroughConfigFile() throws Exception {
final DefaultConfiguration checkConfig =
createCheckConfig(RegexpHeaderCheck.class);
final URI uri = new File(getPath("InputRegexpHeaderNewLines.header")).toURI();
checkConfig.addAttribute("headerFile", uri.toString());
final String[] expected = {
"3: " + getCheckMessage(MSG_HEADER_MISMATCH, "^$"),
};
verify(checkConfig, getPath("InputRegexpHeaderConsecutiveNewLines.java"), expected);
}

@Test
public void testRegexpHeaderIgnore() throws Exception {
final DefaultConfiguration checkConfig =
Expand Down
@@ -0,0 +1,13 @@
////////////////////////////////////////////////////////////////////////////////
// Test case file for checkstyle.
/* */
// Created: 2017
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.checks.header.regexpheader;

/**
* @author Vivek Rao
*/
public class InputRegexpHeaderConsecutiveNewLines
{
}
@@ -0,0 +1,5 @@
^/*$
// .*

// Created: 2017
^.*$
3 changes: 2 additions & 1 deletion src/xdocs/config_header.xml
Expand Up @@ -225,7 +225,8 @@ line 14: ^ \*/
four digit year. Line 5 is an example how to enforce revision
control keywords in a file header. Lines 12-14 is a template for
javadoc (line 13 is so complicated to remove conflict with and of
javadoc comment).
javadoc comment). Lines 7, 9 and 11 will be treated as '^$' and
will forcefully expect the line to be empty.
</p>

<p>
Expand Down

0 comments on commit b7aa926

Please sign in to comment.