diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2019-10-11 13:57:27 -0500 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2019-10-12 20:21:04 +0200 |
commit | 4876424dd7c24074f4fa9fa073d7310295614179 (patch) | |
tree | f48fb58d75d644d3c1caeba756de5e9d4208abf4 /sonar-scanner-engine/src | |
parent | 403901698010a7e3899859dc3eaed32b8e9196e0 (diff) | |
download | sonarqube-4876424dd7c24074f4fa9fa073d7310295614179.tar.gz sonarqube-4876424dd7c24074f4fa9fa073d7310295614179.zip |
SONAR-12427 Patterns to ignore all issues in files are not always taken into account
Diffstat (limited to 'sonar-scanner-engine/src')
3 files changed, 18 insertions, 14 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/ignore/IgnoreIssuesFilter.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/ignore/IgnoreIssuesFilter.java index 0a97ff5b3ee..fe0be23d420 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/ignore/IgnoreIssuesFilter.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/ignore/IgnoreIssuesFilter.java @@ -43,18 +43,25 @@ public class IgnoreIssuesFilter implements IssueFilter { @Override public boolean accept(FilterableIssue issue, IssueFilterChain chain) { InputComponent component = ((DefaultFilterableIssue) issue).getComponent(); - if (component.isFile() && ((DefaultInputFile) component).isIgnoreAllIssues()) { - return false; - } - if (component.isFile() && ((DefaultInputFile) component).isIgnoreAllIssuesOnLine(issue.line())) { + + if (isIgnoreIssue(component, issue)) { return false; } + if (hasRuleMatchFor(component, issue)) { return false; } return chain.accept(issue); } + private static boolean isIgnoreIssue(InputComponent component, FilterableIssue issue) { + if (component.isFile()) { + DefaultInputFile inputFile = (DefaultInputFile) component; + return inputFile.isIgnoreAllIssues() || inputFile.isIgnoreAllIssuesOnLine(issue.line()); + } + return false; + } + public void addRuleExclusionPatternForComponent(DefaultInputFile inputFile, WildcardPattern rulePattern) { if ("*".equals(rulePattern.toString())) { inputFile.setIgnoreAllIssues(true); diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/ignore/scanner/IssueExclusionsRegexpScanner.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/ignore/scanner/IssueExclusionsRegexpScanner.java index fe2001f0620..990cf16b9c8 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/ignore/scanner/IssueExclusionsRegexpScanner.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/ignore/scanner/IssueExclusionsRegexpScanner.java @@ -45,6 +45,7 @@ public class IssueExclusionsRegexpScanner extends CharHandler { private LineExclusion currentLineExclusion = null; private int fileLength = 0; private DoubleRegexpMatcher currentMatcher; + private boolean ignoreAllIssues; IssueExclusionsRegexpScanner(DefaultInputFile inputFile, List<Pattern> allFilePatterns, List<DoubleRegexpMatcher> blockMatchers) { this.allFilePatterns = allFilePatterns; @@ -55,7 +56,7 @@ public class IssueExclusionsRegexpScanner extends CharHandler { @Override public void handleIgnoreEoL(char c) { - if (inputFile.isIgnoreAllIssues()) { + if (ignoreAllIssues) { return; } sb.append(c); @@ -63,7 +64,7 @@ public class IssueExclusionsRegexpScanner extends CharHandler { @Override public void newLine() { - if (inputFile.isIgnoreAllIssues()) { + if (ignoreAllIssues) { return; } processLine(sb.toString()); @@ -73,7 +74,7 @@ public class IssueExclusionsRegexpScanner extends CharHandler { @Override public void eof() { - if (inputFile.isIgnoreAllIssues()) { + if (ignoreAllIssues) { return; } processLine(sb.toString()); @@ -102,6 +103,7 @@ public class IssueExclusionsRegexpScanner extends CharHandler { if (pattern.matcher(line).find()) { // nothing more to do on this file LOG.debug(" - Exclusion pattern '{}': all issues in this file will be ignored.", pattern); + ignoreAllIssues = true; inputFile.setIgnoreAllIssues(true); return; } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/ignore/scanner/IssueExclusionsRegexpScannerTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/ignore/scanner/IssueExclusionsRegexpScannerTest.java index ab54b184c7d..cfa6d92e616 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/ignore/scanner/IssueExclusionsRegexpScannerTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/ignore/scanner/IssueExclusionsRegexpScannerTest.java @@ -32,8 +32,6 @@ import java.util.regex.Pattern; import java.util.stream.IntStream; import org.junit.Before; import org.junit.Test; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.fs.internal.FileMetadata; import org.sonar.api.batch.fs.internal.TestInputFileBuilder; @@ -42,12 +40,11 @@ import org.sonar.scanner.issue.ignore.scanner.IssueExclusionsLoader.DoubleRegexp import static java.nio.charset.StandardCharsets.UTF_8; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.mockito.Mockito.mock; public class IssueExclusionsRegexpScannerTest { private DefaultInputFile javaFile; - - @Mock - private IssueExclusionPatternInitializer patternsInitializer; + private IssueExclusionPatternInitializer patternsInitializer = mock(IssueExclusionPatternInitializer.class); private List<Pattern> allFilePatterns; private List<DoubleRegexpMatcher> blockPatterns; @@ -56,8 +53,6 @@ public class IssueExclusionsRegexpScannerTest { @Before public void init() { - MockitoAnnotations.initMocks(this); - blockPatterns = Arrays.asList(new DoubleRegexpMatcher[] { new DoubleRegexpMatcher(Pattern.compile("// SONAR-OFF"), Pattern.compile("// SONAR-ON")), new DoubleRegexpMatcher(Pattern.compile("// FOO-OFF"), Pattern.compile("// FOO-ON")) |