Search in sources :

Example 11 with IssuePattern

use of org.sonar.scanner.issue.ignore.pattern.IssuePattern in project sonarqube by SonarSource.

the class PatternMatcherTest method shouldReturnExtraPatternForResource.

@Test
public void shouldReturnExtraPatternForResource() {
    String file = "foo";
    patternMatcher.addPatternToExcludeResource(file);
    IssuePattern extraPattern = patternMatcher.getPatternsForComponent(file).iterator().next();
    assertThat(extraPattern.matchResource(file)).isTrue();
    assertThat(extraPattern.isCheckLines()).isFalse();
}
Also used : IssuePattern(org.sonar.scanner.issue.ignore.pattern.IssuePattern) Test(org.junit.Test)

Example 12 with IssuePattern

use of org.sonar.scanner.issue.ignore.pattern.IssuePattern in project sonarqube by SonarSource.

the class EnforceIssuesFilter method accept.

@Override
public boolean accept(FilterableIssue issue, IssueFilterChain chain) {
    boolean atLeastOneRuleMatched = false;
    boolean atLeastOnePatternFullyMatched = false;
    IssuePattern matchingPattern = null;
    for (IssuePattern pattern : multicriteriaPatterns) {
        if (ruleMatches(pattern, issue.ruleKey())) {
            atLeastOneRuleMatched = true;
            InputComponent component = ((DefaultFilterableIssue) issue).getComponent();
            if (component.isFile()) {
                DefaultInputFile file = (DefaultInputFile) component;
                if (pattern.matchFile(file.getProjectRelativePath())) {
                    atLeastOnePatternFullyMatched = true;
                    matchingPattern = pattern;
                } else if (pattern.matchFile(file.getModuleRelativePath())) {
                    warnOnceDeprecatedIssuePattern("Specifying module-relative paths at project level in property '" + IssueInclusionPatternInitializer.CONFIG_KEY + "' is deprecated. " + "To continue matching files like '" + file.getProjectRelativePath() + "', update this property so that patterns refer to project-relative paths.");
                    atLeastOnePatternFullyMatched = true;
                    matchingPattern = pattern;
                }
            }
        }
    }
    if (atLeastOneRuleMatched) {
        if (atLeastOnePatternFullyMatched) {
            LOG.debug("Issue '{}' enforced by pattern '{}'", issue, matchingPattern);
        }
        return atLeastOnePatternFullyMatched;
    } else {
        return chain.accept(issue);
    }
}
Also used : InputComponent(org.sonar.api.batch.fs.InputComponent) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) IssuePattern(org.sonar.scanner.issue.ignore.pattern.IssuePattern) DefaultFilterableIssue(org.sonar.scanner.issue.DefaultFilterableIssue)

Example 13 with IssuePattern

use of org.sonar.scanner.issue.ignore.pattern.IssuePattern in project sonarqube by SonarSource.

the class EnforceIssuesFilterTest method shouldRefuseIssueIfRuleMatchesButNotPath.

@Test
public void shouldRefuseIssueIfRuleMatchesButNotPath() {
    DefaultActiveRules activeRules = new DefaultActiveRules(ImmutableSet.of());
    String path = "org/sonar/api/Issue.java";
    String componentKey = "org.sonar.api.Issue";
    RuleKey ruleKey = RuleKey.of("repo", "rule");
    when(issue.ruleKey()).thenReturn(ruleKey);
    when(issue.componentKey()).thenReturn(componentKey);
    IssuePattern matching = new IssuePattern("no match", "repo:rule");
    when(exclusionPatternInitializer.getMulticriteriaPatterns()).thenReturn(ImmutableList.of(matching));
    when(issue.getComponent()).thenReturn(createComponentWithPath(path));
    ignoreFilter = new EnforceIssuesFilter(exclusionPatternInitializer, analysisWarnings, activeRules);
    assertThat(ignoreFilter.accept(issue, chain)).isFalse();
    verifyNoInteractions(chain, analysisWarnings);
}
Also used : DefaultActiveRules(org.sonar.api.batch.rule.internal.DefaultActiveRules) RuleKey(org.sonar.api.rule.RuleKey) IssuePattern(org.sonar.scanner.issue.ignore.pattern.IssuePattern) Test(org.junit.Test)

Example 14 with IssuePattern

use of org.sonar.scanner.issue.ignore.pattern.IssuePattern in project sonarqube by SonarSource.

the class EnforceIssuesFilterTest method shouldPassToChainIfRuleDoesNotMatch.

@Test
public void shouldPassToChainIfRuleDoesNotMatch() {
    DefaultActiveRules activeRules = new DefaultActiveRules(ImmutableSet.of());
    RuleKey ruleKey = RuleKey.of("repo", "rule");
    when(issue.ruleKey()).thenReturn(ruleKey);
    IssuePattern matching = new IssuePattern("**", "unknown");
    when(exclusionPatternInitializer.getMulticriteriaPatterns()).thenReturn(ImmutableList.of(matching));
    ignoreFilter = new EnforceIssuesFilter(exclusionPatternInitializer, analysisWarnings, activeRules);
    assertThat(ignoreFilter.accept(issue, chain)).isTrue();
    verify(chain).accept(issue);
}
Also used : DefaultActiveRules(org.sonar.api.batch.rule.internal.DefaultActiveRules) RuleKey(org.sonar.api.rule.RuleKey) IssuePattern(org.sonar.scanner.issue.ignore.pattern.IssuePattern) Test(org.junit.Test)

Example 15 with IssuePattern

use of org.sonar.scanner.issue.ignore.pattern.IssuePattern in project sonarqube by SonarSource.

the class EnforceIssuesFilterTest method shouldRefuseIssueIfRuleMatchesAndNotFile.

@Test
public void shouldRefuseIssueIfRuleMatchesAndNotFile() throws IOException {
    DefaultActiveRules activeRules = new DefaultActiveRules(ImmutableSet.of());
    String path = "org/sonar/api/Issue.java";
    RuleKey ruleKey = RuleKey.of("repo", "key");
    when(issue.ruleKey()).thenReturn(ruleKey);
    IssuePattern matching = new IssuePattern(path, ruleKey.toString());
    when(exclusionPatternInitializer.getMulticriteriaPatterns()).thenReturn(ImmutableList.of(matching));
    when(issue.getComponent()).thenReturn(TestInputFileBuilder.newDefaultInputProject("foo", tempFolder.newFolder()));
    ignoreFilter = new EnforceIssuesFilter(exclusionPatternInitializer, analysisWarnings, activeRules);
    assertThat(ignoreFilter.accept(issue, chain)).isFalse();
    verifyNoInteractions(chain, analysisWarnings);
}
Also used : DefaultActiveRules(org.sonar.api.batch.rule.internal.DefaultActiveRules) RuleKey(org.sonar.api.rule.RuleKey) IssuePattern(org.sonar.scanner.issue.ignore.pattern.IssuePattern) Test(org.junit.Test)

Aggregations

IssuePattern (org.sonar.scanner.issue.ignore.pattern.IssuePattern)17 Test (org.junit.Test)16 RuleKey (org.sonar.api.rule.RuleKey)6 DefaultActiveRules (org.sonar.api.batch.rule.internal.DefaultActiveRules)5 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)2 InputComponent (org.sonar.api.batch.fs.InputComponent)1 NewActiveRule (org.sonar.api.batch.rule.internal.NewActiveRule)1 AnalysisWarnings (org.sonar.api.notifications.AnalysisWarnings)1 WildcardPattern (org.sonar.api.utils.WildcardPattern)1 DefaultFilterableIssue (org.sonar.scanner.issue.DefaultFilterableIssue)1 LineRange (org.sonar.scanner.issue.ignore.pattern.LineRange)1