Skip to content

Commit

Permalink
SONARCS-564 Enable every rule in the exported SonarLint .ruleset
Browse files Browse the repository at this point in the history
  • Loading branch information
dbolkensteyn committed Dec 4, 2015
1 parent 9aef340 commit ba632ca
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

public class CSharpSonarRulesDefinition implements RulesDefinition, BatchExtension {

private Set<String> parameterlessRuleKeys = null;
private Set<String> allRuleKeys = null;

@Override
public void define(Context context) {
Expand All @@ -46,18 +46,16 @@ public void define(Context context) {

ImmutableSet.Builder<String> builder = ImmutableSet.builder();
for (NewRule rule : repository.rules()) {
if (rule.params().isEmpty()) {
builder.add(rule.key());
}
builder.add(rule.key());
}
parameterlessRuleKeys = builder.build();
allRuleKeys = builder.build();

repository.done();
}

public Set<String> parameterlessRuleKeys() {
Preconditions.checkNotNull(parameterlessRuleKeys);
return parameterlessRuleKeys;
public Set<String> allRuleKeys() {
Preconditions.checkNotNull(allRuleKeys);
return allRuleKeys;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public SonarLintProfileExporter(CSharpSonarRulesDefinition csharpRulesDefinition
@Override
public void exportProfile(RulesProfile ruleProfile, Writer writer) {
Set<String> disabledRuleKeys = Sets.newHashSet();
disabledRuleKeys.addAll(csharpRulesDefinition.parameterlessRuleKeys());
disabledRuleKeys.addAll(csharpRulesDefinition.allRuleKeys());

appendLine(writer, "<?xml version=\"1.0\" encoding=\"utf-8\"?>");
appendLine(writer, "<RuleSet Name=\"Rules for SonarLint\" Description=\"This rule set was automatically generated from SonarQube.\" ToolsVersion=\"14.0\">");
Expand All @@ -53,10 +53,7 @@ public void exportProfile(RulesProfile ruleProfile, Writer writer) {
Rule rule = activeRule.getRule();
disabledRuleKeys.remove(rule.getKey());

if (rule.getParams().isEmpty() && rule.getTemplate() == null && !activeRule.getSeverity().equals(rule.getSeverity())) {
// Rule is from SonarLint and severity is non-default, explicitly enable
appendLine(writer, " <Rule Id=\"" + rule.getKey() + "\" Action=\"Warning\" />");
}
appendLine(writer, " <Rule Id=\"" + rule.getKey() + "\" Action=\"Warning\" />");
}

for (String disableRuleKey : disabledRuleKeys) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public void test() {
assertThat(context.repositories()).hasSize(1);
assertThat(context.repository("csharpsquid").rules()).isNotEmpty();

Set<String> sonarLintRules = csharpRulesDefinition.parameterlessRuleKeys();
assertThat(sonarLintRules.contains("S100")).isFalse();
Set<String> sonarLintRules = csharpRulesDefinition.allRuleKeys();
assertThat(sonarLintRules.contains("S100")).isTrue();
assertThat(sonarLintRules.size()).isGreaterThan(50);
assertThat(sonarLintRules.size()).isLessThanOrEqualTo(context.repository("csharpsquid").rules().size());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class SonarLintProfileExporterTest {

@Test
public void test() {
// S1000 has parameters and is enabled -> should not be in exported rule set
// S1000 has parameters and is enabled
Rule ruleS1000 = mock(Rule.class);
when(ruleS1000.getKey()).thenReturn("S1000");
RuleParam ruleParam = mock(RuleParam.class);
Expand All @@ -50,52 +50,18 @@ public void test() {
when(activeRuleS1000.getRule()).thenReturn(ruleS1000);
when(activeRuleS1000.getSeverity()).thenReturn(RulePriority.BLOCKER);

// S1001 is a template rule and is enabled -> should not be in exported rule set
// S1001 is a SonarLint rule and disabled -> should be disabled in exported rule set
Rule ruleS1001 = mock(Rule.class);
when(ruleS1001.getKey()).thenReturn("S1001");
when(ruleS1001.getParams()).thenReturn(ImmutableList.<RuleParam>of());
Rule baseTemplateRule = mock(Rule.class);
when(ruleS1001.getTemplate()).thenReturn(baseTemplateRule);
when(ruleS1001.getTemplate()).thenReturn(null);
when(ruleS1001.getSeverity()).thenReturn(RulePriority.MAJOR);
org.sonar.api.rules.ActiveRule activeRuleS1001 = mock(ActiveRule.class);
when(activeRuleS1001.getRule()).thenReturn(ruleS1001);
when(activeRuleS1001.getSeverity()).thenReturn(RulePriority.BLOCKER);

// S1002 is a SonarLint rule and disabled -> should be disabled in exported rule set
Rule ruleS1002 = mock(Rule.class);
when(ruleS1002.getKey()).thenReturn("S1002");
when(ruleS1002.getParams()).thenReturn(ImmutableList.<RuleParam>of());
when(ruleS1002.getTemplate()).thenReturn(null);
when(ruleS1002.getSeverity()).thenReturn(RulePriority.MAJOR);

// S1003 is a SonarLint rule and enabled at default severity -> should not be in exported rule set
Rule ruleS1003 = mock(Rule.class);
when(ruleS1003.getKey()).thenReturn("S1003");
when(ruleS1003.getParams()).thenReturn(ImmutableList.<RuleParam>of());
when(ruleS1003.getTemplate()).thenReturn(null);
when(ruleS1003.getSeverity()).thenReturn(RulePriority.MAJOR);
org.sonar.api.rules.ActiveRule activeRuleS1003 = mock(ActiveRule.class);
when(activeRuleS1003.getRule()).thenReturn(ruleS1003);
when(activeRuleS1003.getSeverity()).thenReturn(RulePriority.MAJOR);

// S1004 is a SonarLint rule and enabled at different severity -> should be in exported rule set
Rule ruleS1004 = mock(Rule.class);
when(ruleS1004.getKey()).thenReturn("S1004");
when(ruleS1004.getParams()).thenReturn(ImmutableList.<RuleParam>of());
when(ruleS1004.getTemplate()).thenReturn(null);
when(ruleS1004.getSeverity()).thenReturn(RulePriority.MAJOR);
org.sonar.api.rules.ActiveRule activeRuleS1004 = mock(ActiveRule.class);
when(activeRuleS1004.getRule()).thenReturn(ruleS1004);
when(activeRuleS1004.getSeverity()).thenReturn(RulePriority.BLOCKER);

Set<String> allRules = ImmutableSet.of(
ruleS1000.getKey(),
ruleS1001.getKey(),
ruleS1002.getKey(),
ruleS1003.getKey(),
ruleS1004.getKey());
ruleS1001.getKey());
CSharpSonarRulesDefinition csharpRulesDefinition = mock(CSharpSonarRulesDefinition.class);
when(csharpRulesDefinition.parameterlessRuleKeys()).thenReturn(allRules);
when(csharpRulesDefinition.allRuleKeys()).thenReturn(allRules);

SonarLintProfileExporter exporter = new SonarLintProfileExporter(csharpRulesDefinition);
assertThat(exporter.getKey()).isEqualTo("sonarlint-vs-cs");
Expand All @@ -104,19 +70,14 @@ public void test() {

StringWriter writer = new StringWriter();
RulesProfile rulesProfile = mock(RulesProfile.class);
when(rulesProfile.getActiveRulesByRepository(CSharpPlugin.REPOSITORY_KEY)).thenReturn(
ImmutableList.of(
activeRuleS1000,
activeRuleS1001,
activeRuleS1003,
activeRuleS1004));
when(rulesProfile.getActiveRulesByRepository(CSharpPlugin.REPOSITORY_KEY)).thenReturn(ImmutableList.of(activeRuleS1000));
exporter.exportProfile(rulesProfile, writer);
assertThat(writer.toString()).isEqualTo(
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" +
"<RuleSet Name=\"Rules for SonarLint\" Description=\"This rule set was automatically generated from SonarQube.\" ToolsVersion=\"14.0\">\r\n" +
" <Rules AnalyzerId=\"SonarLint.CSharp\" RuleNamespace=\"SonarLint.CSharp\">\r\n" +
" <Rule Id=\"S1004\" Action=\"Warning\" />\r\n" +
" <Rule Id=\"S1002\" Action=\"None\" />\r\n" +
" <Rule Id=\"S1000\" Action=\"Warning\" />\r\n" +
" <Rule Id=\"S1001\" Action=\"None\" />\r\n" +
" </Rules>\r\n" +
"</RuleSet>\r\n");
}
Expand Down

0 comments on commit ba632ca

Please sign in to comment.