Skip to content

Commit ba632ca

Browse files
committed
SONARCS-564 Enable every rule in the exported SonarLint .ruleset
1 parent 9aef340 commit ba632ca

File tree

4 files changed

+18
-62
lines changed

4 files changed

+18
-62
lines changed

src/main/java/org/sonar/plugins/csharp/CSharpSonarRulesDefinition.java

+6-8
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
public class CSharpSonarRulesDefinition implements RulesDefinition, BatchExtension {
3434

35-
private Set<String> parameterlessRuleKeys = null;
35+
private Set<String> allRuleKeys = null;
3636

3737
@Override
3838
public void define(Context context) {
@@ -46,18 +46,16 @@ public void define(Context context) {
4646

4747
ImmutableSet.Builder<String> builder = ImmutableSet.builder();
4848
for (NewRule rule : repository.rules()) {
49-
if (rule.params().isEmpty()) {
50-
builder.add(rule.key());
51-
}
49+
builder.add(rule.key());
5250
}
53-
parameterlessRuleKeys = builder.build();
51+
allRuleKeys = builder.build();
5452

5553
repository.done();
5654
}
5755

58-
public Set<String> parameterlessRuleKeys() {
59-
Preconditions.checkNotNull(parameterlessRuleKeys);
60-
return parameterlessRuleKeys;
56+
public Set<String> allRuleKeys() {
57+
Preconditions.checkNotNull(allRuleKeys);
58+
return allRuleKeys;
6159
}
6260

6361
}

src/main/java/org/sonar/plugins/csharp/SonarLintProfileExporter.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public SonarLintProfileExporter(CSharpSonarRulesDefinition csharpRulesDefinition
4343
@Override
4444
public void exportProfile(RulesProfile ruleProfile, Writer writer) {
4545
Set<String> disabledRuleKeys = Sets.newHashSet();
46-
disabledRuleKeys.addAll(csharpRulesDefinition.parameterlessRuleKeys());
46+
disabledRuleKeys.addAll(csharpRulesDefinition.allRuleKeys());
4747

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

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

6259
for (String disableRuleKey : disabledRuleKeys) {

src/test/java/org/sonar/plugins/csharp/CSharpSonarRulesDefinitionTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ public void test() {
3939
assertThat(context.repositories()).hasSize(1);
4040
assertThat(context.repository("csharpsquid").rules()).isNotEmpty();
4141

42-
Set<String> sonarLintRules = csharpRulesDefinition.parameterlessRuleKeys();
43-
assertThat(sonarLintRules.contains("S100")).isFalse();
42+
Set<String> sonarLintRules = csharpRulesDefinition.allRuleKeys();
43+
assertThat(sonarLintRules.contains("S100")).isTrue();
4444
assertThat(sonarLintRules.size()).isGreaterThan(50);
4545
assertThat(sonarLintRules.size()).isLessThanOrEqualTo(context.repository("csharpsquid").rules().size());
4646
}

src/test/java/org/sonar/plugins/csharp/SonarLintProfileExporterTest.java

+8-47
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class SonarLintProfileExporterTest {
3939

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

53-
// S1001 is a template rule and is enabled -> should not be in exported rule set
53+
// S1001 is a SonarLint rule and disabled -> should be disabled in exported rule set
5454
Rule ruleS1001 = mock(Rule.class);
5555
when(ruleS1001.getKey()).thenReturn("S1001");
5656
when(ruleS1001.getParams()).thenReturn(ImmutableList.<RuleParam>of());
57-
Rule baseTemplateRule = mock(Rule.class);
58-
when(ruleS1001.getTemplate()).thenReturn(baseTemplateRule);
57+
when(ruleS1001.getTemplate()).thenReturn(null);
5958
when(ruleS1001.getSeverity()).thenReturn(RulePriority.MAJOR);
60-
org.sonar.api.rules.ActiveRule activeRuleS1001 = mock(ActiveRule.class);
61-
when(activeRuleS1001.getRule()).thenReturn(ruleS1001);
62-
when(activeRuleS1001.getSeverity()).thenReturn(RulePriority.BLOCKER);
63-
64-
// S1002 is a SonarLint rule and disabled -> should be disabled in exported rule set
65-
Rule ruleS1002 = mock(Rule.class);
66-
when(ruleS1002.getKey()).thenReturn("S1002");
67-
when(ruleS1002.getParams()).thenReturn(ImmutableList.<RuleParam>of());
68-
when(ruleS1002.getTemplate()).thenReturn(null);
69-
when(ruleS1002.getSeverity()).thenReturn(RulePriority.MAJOR);
70-
71-
// S1003 is a SonarLint rule and enabled at default severity -> should not be in exported rule set
72-
Rule ruleS1003 = mock(Rule.class);
73-
when(ruleS1003.getKey()).thenReturn("S1003");
74-
when(ruleS1003.getParams()).thenReturn(ImmutableList.<RuleParam>of());
75-
when(ruleS1003.getTemplate()).thenReturn(null);
76-
when(ruleS1003.getSeverity()).thenReturn(RulePriority.MAJOR);
77-
org.sonar.api.rules.ActiveRule activeRuleS1003 = mock(ActiveRule.class);
78-
when(activeRuleS1003.getRule()).thenReturn(ruleS1003);
79-
when(activeRuleS1003.getSeverity()).thenReturn(RulePriority.MAJOR);
80-
81-
// S1004 is a SonarLint rule and enabled at different severity -> should be in exported rule set
82-
Rule ruleS1004 = mock(Rule.class);
83-
when(ruleS1004.getKey()).thenReturn("S1004");
84-
when(ruleS1004.getParams()).thenReturn(ImmutableList.<RuleParam>of());
85-
when(ruleS1004.getTemplate()).thenReturn(null);
86-
when(ruleS1004.getSeverity()).thenReturn(RulePriority.MAJOR);
87-
org.sonar.api.rules.ActiveRule activeRuleS1004 = mock(ActiveRule.class);
88-
when(activeRuleS1004.getRule()).thenReturn(ruleS1004);
89-
when(activeRuleS1004.getSeverity()).thenReturn(RulePriority.BLOCKER);
9059

9160
Set<String> allRules = ImmutableSet.of(
9261
ruleS1000.getKey(),
93-
ruleS1001.getKey(),
94-
ruleS1002.getKey(),
95-
ruleS1003.getKey(),
96-
ruleS1004.getKey());
62+
ruleS1001.getKey());
9763
CSharpSonarRulesDefinition csharpRulesDefinition = mock(CSharpSonarRulesDefinition.class);
98-
when(csharpRulesDefinition.parameterlessRuleKeys()).thenReturn(allRules);
64+
when(csharpRulesDefinition.allRuleKeys()).thenReturn(allRules);
9965

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

10571
StringWriter writer = new StringWriter();
10672
RulesProfile rulesProfile = mock(RulesProfile.class);
107-
when(rulesProfile.getActiveRulesByRepository(CSharpPlugin.REPOSITORY_KEY)).thenReturn(
108-
ImmutableList.of(
109-
activeRuleS1000,
110-
activeRuleS1001,
111-
activeRuleS1003,
112-
activeRuleS1004));
73+
when(rulesProfile.getActiveRulesByRepository(CSharpPlugin.REPOSITORY_KEY)).thenReturn(ImmutableList.of(activeRuleS1000));
11374
exporter.exportProfile(rulesProfile, writer);
11475
assertThat(writer.toString()).isEqualTo(
11576
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" +
11677
"<RuleSet Name=\"Rules for SonarLint\" Description=\"This rule set was automatically generated from SonarQube.\" ToolsVersion=\"14.0\">\r\n" +
11778
" <Rules AnalyzerId=\"SonarLint.CSharp\" RuleNamespace=\"SonarLint.CSharp\">\r\n" +
118-
" <Rule Id=\"S1004\" Action=\"Warning\" />\r\n" +
119-
" <Rule Id=\"S1002\" Action=\"None\" />\r\n" +
79+
" <Rule Id=\"S1000\" Action=\"Warning\" />\r\n" +
80+
" <Rule Id=\"S1001\" Action=\"None\" />\r\n" +
12081
" </Rules>\r\n" +
12182
"</RuleSet>\r\n");
12283
}

0 commit comments

Comments
 (0)