diff options
author | rnveach <rveach02@gmail.com> | 2017-12-30 16:52:03 -0500 |
---|---|---|
committer | Roman Ivanov <romani@users.noreply.github.com> | 2017-12-30 17:38:34 -0800 |
commit | 19211ae6e578e023959e7df2698afa8986857d42 (patch) | |
tree | aa8629535002aa3a9adbc8fc86bce1223b69dc5b | |
parent | 38a3a34a547d55237d08cfc1ff350391b97f8d9a (diff) | |
download | checkstyle-19211ae6e578e023959e7df2698afa8986857d42.tar.gz |
Issue #5405: SuppressWithPlainTextComment should ignore directories
2 files changed, 28 insertions, 7 deletions
diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressWithPlainTextCommentFilter.java b/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressWithPlainTextCommentFilter.java index 3345fd462..ad380845e 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressWithPlainTextCommentFilter.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressWithPlainTextCommentFilter.java @@ -117,8 +117,10 @@ public class SuppressWithPlainTextCommentFilter extends AutomaticBean implements boolean accepted = true; if (event.getLocalizedMessage() != null) { final FileText fileText = getFileText(event.getFileName()); - final List<Suppression> suppressions = getSuppressions(fileText); - accepted = getNearestSuppression(suppressions, event) == null; + if (fileText != null) { + final List<Suppression> suppressions = getSuppressions(fileText); + accepted = getNearestSuppression(suppressions, event) == null; + } } return accepted; } @@ -134,12 +136,20 @@ public class SuppressWithPlainTextCommentFilter extends AutomaticBean implements * @return {@link FileText} instance. */ private static FileText getFileText(String fileName) { - try { - return new FileText(new File(fileName), StandardCharsets.UTF_8.name()); - } - catch (IOException ex) { - throw new IllegalStateException("Cannot read source file: " + fileName, ex); + final File file = new File(fileName); + FileText result = null; + + // some violations can be on a directory, instead of a file + if (!file.isDirectory()) { + try { + result = new FileText(file, StandardCharsets.UTF_8.name()); + } + catch (IOException ex) { + throw new IllegalStateException("Cannot read source file: " + fileName, ex); + } } + + return result; } /** diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/filters/SuppressWithPlainTextCommentFilterTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/filters/SuppressWithPlainTextCommentFilterTest.java index 08642ab04..9f717333f 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/filters/SuppressWithPlainTextCommentFilterTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/filters/SuppressWithPlainTextCommentFilterTest.java @@ -26,6 +26,7 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.io.FileNotFoundException; +import java.io.IOException; import java.util.Arrays; import java.util.Collection; import java.util.stream.Collectors; @@ -456,6 +457,16 @@ public class SuppressWithPlainTextCommentFilterTest extends AbstractModuleTestSu ); } + @Test + public void testFilterWithDirectory() throws IOException { + final SuppressWithPlainTextCommentFilter filter = new SuppressWithPlainTextCommentFilter(); + final AuditEvent event = new AuditEvent(this, getPath(""), new LocalizedMessage(1, 1, + "bundle", "key", null, SeverityLevel.ERROR, "moduleId", getClass(), + "customMessage")); + + assertTrue("filter should accept directory", filter.accept(event)); + } + private void verifySuppressed(String fileNameWithExtension, String[] violationMessages, Configuration... childConfigs) throws Exception { final DefaultConfiguration checkerConfig = createRootConfig(null); |