From ba632ca679434861c2070655f4bc6ae0bb1d95b1 Mon Sep 17 00:00:00 2001 From: Dinesh Bolkensteyn Date: Fri, 4 Dec 2015 17:28:37 +0100 Subject: [PATCH] SONARCS-564 Enable every rule in the exported SonarLint .ruleset --- .../csharp/CSharpSonarRulesDefinition.java | 14 ++--- .../csharp/SonarLintProfileExporter.java | 7 +-- .../CSharpSonarRulesDefinitionTest.java | 4 +- .../csharp/SonarLintProfileExporterTest.java | 55 +++---------------- 4 files changed, 18 insertions(+), 62 deletions(-) diff --git a/src/main/java/org/sonar/plugins/csharp/CSharpSonarRulesDefinition.java b/src/main/java/org/sonar/plugins/csharp/CSharpSonarRulesDefinition.java index 9a3cf5f903b..c23191a828c 100644 --- a/src/main/java/org/sonar/plugins/csharp/CSharpSonarRulesDefinition.java +++ b/src/main/java/org/sonar/plugins/csharp/CSharpSonarRulesDefinition.java @@ -32,7 +32,7 @@ public class CSharpSonarRulesDefinition implements RulesDefinition, BatchExtension { - private Set parameterlessRuleKeys = null; + private Set allRuleKeys = null; @Override public void define(Context context) { @@ -46,18 +46,16 @@ public void define(Context context) { ImmutableSet.Builder 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 parameterlessRuleKeys() { - Preconditions.checkNotNull(parameterlessRuleKeys); - return parameterlessRuleKeys; + public Set allRuleKeys() { + Preconditions.checkNotNull(allRuleKeys); + return allRuleKeys; } } diff --git a/src/main/java/org/sonar/plugins/csharp/SonarLintProfileExporter.java b/src/main/java/org/sonar/plugins/csharp/SonarLintProfileExporter.java index 5f0c98ef75c..5d826373ab1 100644 --- a/src/main/java/org/sonar/plugins/csharp/SonarLintProfileExporter.java +++ b/src/main/java/org/sonar/plugins/csharp/SonarLintProfileExporter.java @@ -43,7 +43,7 @@ public SonarLintProfileExporter(CSharpSonarRulesDefinition csharpRulesDefinition @Override public void exportProfile(RulesProfile ruleProfile, Writer writer) { Set disabledRuleKeys = Sets.newHashSet(); - disabledRuleKeys.addAll(csharpRulesDefinition.parameterlessRuleKeys()); + disabledRuleKeys.addAll(csharpRulesDefinition.allRuleKeys()); appendLine(writer, ""); appendLine(writer, ""); @@ -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, " "); - } + appendLine(writer, " "); } for (String disableRuleKey : disabledRuleKeys) { diff --git a/src/test/java/org/sonar/plugins/csharp/CSharpSonarRulesDefinitionTest.java b/src/test/java/org/sonar/plugins/csharp/CSharpSonarRulesDefinitionTest.java index f669b42f38c..e7ea4a9b5d0 100644 --- a/src/test/java/org/sonar/plugins/csharp/CSharpSonarRulesDefinitionTest.java +++ b/src/test/java/org/sonar/plugins/csharp/CSharpSonarRulesDefinitionTest.java @@ -39,8 +39,8 @@ public void test() { assertThat(context.repositories()).hasSize(1); assertThat(context.repository("csharpsquid").rules()).isNotEmpty(); - Set sonarLintRules = csharpRulesDefinition.parameterlessRuleKeys(); - assertThat(sonarLintRules.contains("S100")).isFalse(); + Set sonarLintRules = csharpRulesDefinition.allRuleKeys(); + assertThat(sonarLintRules.contains("S100")).isTrue(); assertThat(sonarLintRules.size()).isGreaterThan(50); assertThat(sonarLintRules.size()).isLessThanOrEqualTo(context.repository("csharpsquid").rules().size()); } diff --git a/src/test/java/org/sonar/plugins/csharp/SonarLintProfileExporterTest.java b/src/test/java/org/sonar/plugins/csharp/SonarLintProfileExporterTest.java index 7351ec36572..86afc171fc7 100644 --- a/src/test/java/org/sonar/plugins/csharp/SonarLintProfileExporterTest.java +++ b/src/test/java/org/sonar/plugins/csharp/SonarLintProfileExporterTest.java @@ -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); @@ -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.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.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.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.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 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"); @@ -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( "\r\n" + "\r\n" + " \r\n" + - " \r\n" + - " \r\n" + + " \r\n" + + " \r\n" + " \r\n" + "\r\n"); }