diff --git a/src/main/java/cc/analysis/SonarLintFactory.java b/src/main/java/cc/analysis/SonarLintFactory.java index 6826672..94aa81b 100644 --- a/src/main/java/cc/analysis/SonarLintFactory.java +++ b/src/main/java/cc/analysis/SonarLintFactory.java @@ -21,13 +21,17 @@ public SonarLintFactory(ConfigurationReader reader, Path workDir) { @Override public SonarLint createSonarLint(Path projectHome, boolean mustBeConnected, boolean verbose) { LogOutputWrapper logWrapper = new LogOutputWrapper(verbose); + return new StandaloneSonarLint(createEngine(logWrapper), workDir, logWrapper); + } - StandaloneGlobalConfiguration config = StandaloneGlobalConfiguration.builder() - .addPlugins(plugins()) - .setLogOutput(logWrapper) - .build(); + public StandaloneSonarLintEngineImpl createEngine(LogOutputWrapper logWrapper) { + return new StandaloneSonarLintEngineImpl(config(logWrapper)); + } - StandaloneSonarLintEngine engine = new StandaloneSonarLintEngineImpl(config); - return new StandaloneSonarLint(engine, workDir, logWrapper); + public StandaloneGlobalConfiguration config(LogOutputWrapper logWrapper) { + return StandaloneGlobalConfiguration.builder() + .addPlugins(plugins()) + .setLogOutput(logWrapper) + .build(); } } diff --git a/src/main/java/cc/models/Categories.java b/src/main/java/cc/models/Categories.java index 568e39d..4e076c0 100644 --- a/src/main/java/cc/models/Categories.java +++ b/src/main/java/cc/models/Categories.java @@ -1,26 +1,95 @@ package cc.models; +import com.google.gson.annotations.SerializedName; import org.sonarsource.sonarlint.core.client.api.common.RuleDetails; import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; -class Categories extends ArrayList { - public Categories(String ruleType) { - String category; - switch (ruleType) { +import static cc.models.Categories.Category.*; + +class Categories extends ArrayList { + public enum Category { + @SerializedName("Bug Risk") + BUG_RISK, + + @SerializedName("Clarity") + CLARITY, + + @SerializedName("Compatibility") + COMPATIBILITY, + + @SerializedName("Complexity") + COMPLEXITY, + + @SerializedName("Duplication") + DUPLICATION, + + @SerializedName("Performance") + PERFORMANCE, + + @SerializedName("Security") + SECURITY, + + @SerializedName("Style") + STYLE; + } + + public Categories(RuleDetails rule) { + switch (rule.getType()) { case "VULNERABILITY": { - category = "Security"; + add(SECURITY); break; } + case "BUG": { + add(BUG_RISK); + break; + } + case "CODE_SMELL": default: { - category = "Bug Risk"; + Category category = fromTags(rule.getTags()); + add(category); break; } } - add(category); + } + + private Category fromTags(String[] tags) { + List tagList = Arrays.asList(tags); + if (tagList.contains("brain-overload")) { + return COMPLEXITY; + } + + if (tagList.contains("duplicate")) { + return DUPLICATION; + } + + if (tagList.contains("deadlock") || tagList.contains("unpredictable") + || tagList.contains("bad-practice") || tagList.contains("suspicious")) { + return BUG_RISK; + } + + if (tagList.contains("maven")) { + return COMPATIBILITY; + } + + if (tagList.contains("performance")) { + return PERFORMANCE; + } + + if (tagList.contains("convention") || tagList.contains("style")) { + return STYLE; + } + + if (tagList.contains("confusing")) { + return CLARITY; + } + + return CLARITY; } public static Categories from(RuleDetails ruleDetails) { - return new Categories(ruleDetails.getType()); + return new Categories(ruleDetails); } } diff --git a/src/test/java/cc/models/CategoriesTest.java b/src/test/java/cc/models/CategoriesTest.java new file mode 100644 index 0000000..309e958 --- /dev/null +++ b/src/test/java/cc/models/CategoriesTest.java @@ -0,0 +1,85 @@ +package cc.models; + +import org.junit.BeforeClass; +import org.junit.Test; +import org.sonar.api.batch.rule.Rule; +import org.sonarsource.sonarlint.core.client.api.common.RuleDetails; +import support.Factory; + +import java.util.Collection; + +import static cc.models.Categories.Category.*; +import static org.assertj.core.api.Assertions.assertThat; + +public class CategoriesTest { + + @BeforeClass + public static void beforeAll() throws Exception { + System.setProperty("sonarlint.home", "build"); + } + + @Test + public void set_clarity_for_generic_code_smells() throws Exception { + assertThat(getCategoriesForRule("S1602")).contains(CLARITY); + } + + @Test + public void set_complexity_for_brain_overload_code_smells() throws Exception { + assertThat(getCategoriesForRule("S1067")).contains(COMPLEXITY); + } + + @Test + public void set_duplication_for_duplicate_code_smells() throws Exception { + assertThat(getCategoriesForRule("S4144")).contains(DUPLICATION); + } + + @Test + public void set_clarity_for_confusing_code_smells() throws Exception { + assertThat(getCategoriesForRule("S1141")).contains(CLARITY); + } + + @Test + public void set_bug_risk_for_deadlock_code_smells() throws Exception { + assertThat(getCategoriesForRule("S3046")).contains(BUG_RISK); + } + + @Test + public void set_compatibility_for_maven_code_smells() throws Exception { + assertThat(getCategoriesForRule("S3423")).contains(COMPATIBILITY); + } + + @Test + public void set_performance_for_performance_code_smells() throws Exception { + assertThat(getCategoriesForRule("S1149")).contains(PERFORMANCE); + } + + @Test + public void set_style_for_convention_code_smells() throws Exception { + assertThat(getCategoriesForRule("S00115")).contains(Categories.Category.STYLE); + } + + @Test + public void set_style_for_style_code_smells() throws Exception { + assertThat(getCategoriesForRule("S00122")).contains(Categories.Category.STYLE); + } + + @Test + public void set_bug_risk_for_unpredictable_code_smells() throws Exception { + assertThat(getCategoriesForRule("S1215")).contains(Categories.Category.BUG_RISK); + } + + @Test + public void set_bug_risk_for_bad_practice_code_smells() throws Exception { + assertThat(getCategoriesForRule("S106")).contains(Categories.Category.BUG_RISK); + } + + @Test + public void set_bug_risk_for_suspicious_code_smells() throws Exception { + assertThat(getCategoriesForRule("S1186")).contains(Categories.Category.BUG_RISK); + } + + private Categories getCategoriesForRule(String key) { + RuleDetails rule = Factory.createRule(key); + return Categories.from(rule); + } +} \ No newline at end of file diff --git a/src/test/java/cc/models/CodeClimateIssueTest.java b/src/test/java/cc/models/CodeClimateIssueTest.java index 7e1d5d0..d222988 100644 --- a/src/test/java/cc/models/CodeClimateIssueTest.java +++ b/src/test/java/cc/models/CodeClimateIssueTest.java @@ -22,13 +22,14 @@ public void properly_serialize_severity() throws Exception { } private CodeClimateIssue createIssueForSeverity(String severity) { + FakeRuleDetails rule = new FakeRuleDetails(severity); return new CodeClimateIssue( "check", - Severity.from(new FakeRuleDetails(severity)), + Severity.from(rule), "desc", new Content(""), new Location("/tmp", "path", new Lines(0, 1)), - new Categories("VULNERABILITY") + new Categories(rule) ); } } \ No newline at end of file diff --git a/src/test/java/support/Factory.java b/src/test/java/support/Factory.java new file mode 100644 index 0000000..e4e28cf --- /dev/null +++ b/src/test/java/support/Factory.java @@ -0,0 +1,19 @@ +package support; + +import cc.analysis.SonarLintFactory; +import org.sonarlint.cli.analysis.LogOutputWrapper; +import org.sonarsource.sonarlint.core.StandaloneSonarLintEngineImpl; +import org.sonarsource.sonarlint.core.client.api.common.RuleDetails; + +import java.nio.file.Paths; + +public class Factory { + public static StandaloneSonarLintEngineImpl sonarlint() { + SonarLintFactory factory = new SonarLintFactory(null, Paths.get("/tmp/sonarlint")); + return factory.createEngine(new LogOutputWrapper(false)); + } + + public static RuleDetails createRule(String key) { + return sonarlint().getRuleDetails("squid:" + key); + } +} \ No newline at end of file diff --git a/src/test/resources/sanity_check_expected_issues.json b/src/test/resources/sanity_check_expected_issues.json index 15d843e..5ed936e 100644 --- a/src/test/resources/sanity_check_expected_issues.json +++ b/src/test/resources/sanity_check_expected_issues.json @@ -34,7 +34,7 @@ } }, "categories": [ - "Bug Risk" + "Clarity" ] }, {