]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-12427 Patterns to ignore all issues in files are not always taken into account
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Fri, 11 Oct 2019 18:57:27 +0000 (13:57 -0500)
committerSonarTech <sonartech@sonarsource.com>
Sat, 12 Oct 2019 18:21:04 +0000 (20:21 +0200)
sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputFile.java
sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/fs/internal/fs/DefaultInputFileTest.java
sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/ignore/IgnoreIssuesFilter.java
sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/ignore/scanner/IssueExclusionsRegexpScanner.java
sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/ignore/scanner/IssueExclusionsRegexpScannerTest.java

index 0d019a6d29ccb2a0806dbe50462e366845dbfdeb..4d8b14c032cb74a1f20381bc66392082f2d463e7 100644 (file)
@@ -403,6 +403,7 @@ public class DefaultInputFile extends DefaultInputComponent implements InputFile
   }
 
   public boolean isIgnoreAllIssues() {
+    checkMetadata();
     return ignoreAllIssues;
   }
 
@@ -418,6 +419,7 @@ public class DefaultInputFile extends DefaultInputComponent implements InputFile
   }
 
   public boolean isIgnoreAllIssuesOnLine(@Nullable Integer line) {
+    checkMetadata();
     if (line == null || ignoreIssuesOnlineRanges == null) {
       return false;
     }
index 63f6b5fb540beec65c4235400835f9ee94d9b6c9..06e341a40b0aaa304abdb9ac77af1b6e2e62605c 100644 (file)
@@ -33,6 +33,7 @@ import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.nio.file.StandardOpenOption;
+import java.util.function.Consumer;
 import java.util.stream.Collectors;
 import org.junit.Before;
 import org.junit.Rule;
@@ -49,6 +50,7 @@ import org.sonar.api.batch.fs.internal.SensorStrategy;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.junit.Assert.fail;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
 
 public class DefaultInputFileTest {
 
index 0a97ff5b3ee8b1c0e038af5657fbae1b60c45806..fe0be23d420e0ba0dc861e02bc3682fd6b86235b 100644 (file)
@@ -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);
index fe2001f062094c791e84a3d3cdce3b2664a6edc4..990cf16b9c8dca4b90035c7f13ebe421de0c11da 100644 (file)
@@ -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;
       }
index ab54b184c7d4c2f75e6a2c018b665757b2b68be8..cfa6d92e616b250c3334e098a489702027c69f61 100644 (file)
@@ -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"))