Search in sources :

Example 1 with WildcardPattern

use of org.sonar.api.utils.WildcardPattern in project sonarqube by SonarSource.

the class IgnoreIssuesFilter method hasRuleMatchFor.

private boolean hasRuleMatchFor(InputComponent component, FilterableIssue issue) {
    for (WildcardPattern pattern : rulePatternByComponent.getOrDefault(component, Collections.emptyList())) {
        if (pattern.match(issue.ruleKey().toString())) {
            LOG.debug("Issue '{}' ignored by exclusion pattern '{}'", issue, pattern);
            return true;
        }
        RuleKey ruleKey = issue.ruleKey();
        if (activeRules.matchesDeprecatedKeys(ruleKey, pattern)) {
            String msg = String.format("A multicriteria issue exclusion uses the rule key '%s' that has been changed. The pattern should be updated to '%s'", pattern, ruleKey);
            analysisWarnings.addUnique(msg);
            if (warnedDeprecatedRuleKeys.add(ruleKey)) {
                LOG.warn(msg);
            }
            LOG.debug("Issue '{}' ignored by exclusion pattern '{}' matching a deprecated rule key", issue, pattern);
            return true;
        }
    }
    return false;
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) WildcardPattern(org.sonar.api.utils.WildcardPattern)

Example 2 with WildcardPattern

use of org.sonar.api.utils.WildcardPattern in project sonarqube by SonarSource.

the class EnforceIssuesFilterTest method shouldRefuseIssueIfRuleMatchesAndPathUnknown.

@Test
public void shouldRefuseIssueIfRuleMatchesAndPathUnknown() {
    String rule = "rule";
    String path = "org/sonar/api/Issue.java";
    String componentKey = "org.sonar.api.Issue";
    RuleKey ruleKey = mock(RuleKey.class);
    when(ruleKey.toString()).thenReturn(rule);
    when(issue.ruleKey()).thenReturn(ruleKey);
    when(issue.componentKey()).thenReturn(componentKey);
    IssuePattern matching = mock(IssuePattern.class);
    WildcardPattern rulePattern = mock(WildcardPattern.class);
    when(matching.getRulePattern()).thenReturn(rulePattern);
    when(rulePattern.match(rule)).thenReturn(true);
    WildcardPattern pathPattern = mock(WildcardPattern.class);
    when(matching.getResourcePattern()).thenReturn(pathPattern);
    when(pathPattern.match(path)).thenReturn(false);
    when(exclusionPatternInitializer.getMulticriteriaPatterns()).thenReturn(ImmutableList.of(matching));
    when(exclusionPatternInitializer.getPathForComponent(componentKey)).thenReturn(null);
    assertThat(ignoreFilter.accept(issue, chain)).isFalse();
    verifyZeroInteractions(chain);
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) IssuePattern(org.sonar.scanner.issue.ignore.pattern.IssuePattern) WildcardPattern(org.sonar.api.utils.WildcardPattern) Test(org.junit.Test)

Example 3 with WildcardPattern

use of org.sonar.api.utils.WildcardPattern in project sonarqube by SonarSource.

the class IgnoreIssuesFilterTest method shouldRejectIfRulePatternMatches.

@Test
public void shouldRejectIfRulePatternMatches() {
    DefaultActiveRules activeRules = new DefaultActiveRules(ImmutableSet.of());
    IgnoreIssuesFilter underTest = new IgnoreIssuesFilter(activeRules, analysisWarnings);
    WildcardPattern pattern = mock(WildcardPattern.class);
    when(pattern.match(ruleKey.toString())).thenReturn(true);
    underTest.addRuleExclusionPatternForComponent(component, pattern);
    assertThat(underTest.accept(issue, chain)).isFalse();
    verifyNoInteractions(analysisWarnings);
}
Also used : DefaultActiveRules(org.sonar.api.batch.rule.internal.DefaultActiveRules) WildcardPattern(org.sonar.api.utils.WildcardPattern) Test(org.junit.Test)

Example 4 with WildcardPattern

use of org.sonar.api.utils.WildcardPattern in project sonarqube by SonarSource.

the class IgnoreIssuesFilterTest method shouldAcceptIfRulePatternDoesNotMatch.

@Test
public void shouldAcceptIfRulePatternDoesNotMatch() {
    DefaultActiveRules activeRules = new DefaultActiveRules(ImmutableSet.of());
    IgnoreIssuesFilter underTest = new IgnoreIssuesFilter(activeRules, analysisWarnings);
    WildcardPattern pattern = mock(WildcardPattern.class);
    when(pattern.match(ruleKey.toString())).thenReturn(false);
    underTest.addRuleExclusionPatternForComponent(component, pattern);
    assertThat(underTest.accept(issue, chain)).isFalse();
}
Also used : DefaultActiveRules(org.sonar.api.batch.rule.internal.DefaultActiveRules) WildcardPattern(org.sonar.api.utils.WildcardPattern) Test(org.junit.Test)

Example 5 with WildcardPattern

use of org.sonar.api.utils.WildcardPattern in project sonarqube by SonarSource.

the class IgnoreIssuesFilterTest method shouldRejectIfRulePatternMatchesDeprecatedRule.

@Test
public void shouldRejectIfRulePatternMatchesDeprecatedRule() {
    DefaultActiveRules activeRules = new DefaultActiveRules(ImmutableSet.of(new NewActiveRule.Builder().setRuleKey(ruleKey).setDeprecatedKeys(singleton(RuleKey.of("repo", "rule"))).build()));
    IgnoreIssuesFilter underTest = new IgnoreIssuesFilter(activeRules, analysisWarnings);
    WildcardPattern pattern = WildcardPattern.create("repo:rule");
    underTest.addRuleExclusionPatternForComponent(component, pattern);
    assertThat(underTest.accept(issue, chain)).isFalse();
    verify(analysisWarnings).addUnique("A multicriteria issue exclusion uses the rule key 'repo:rule' that has been changed. The pattern should be updated to 'foo:bar'");
    assertThat(logTester.logs()).contains("A multicriteria issue exclusion uses the rule key 'repo:rule' that has been changed. The pattern should be updated to 'foo:bar'");
}
Also used : DefaultActiveRules(org.sonar.api.batch.rule.internal.DefaultActiveRules) NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) WildcardPattern(org.sonar.api.utils.WildcardPattern) Test(org.junit.Test)

Aggregations

WildcardPattern (org.sonar.api.utils.WildcardPattern)5 Test (org.junit.Test)4 DefaultActiveRules (org.sonar.api.batch.rule.internal.DefaultActiveRules)3 RuleKey (org.sonar.api.rule.RuleKey)2 NewActiveRule (org.sonar.api.batch.rule.internal.NewActiveRule)1 IssuePattern (org.sonar.scanner.issue.ignore.pattern.IssuePattern)1