Skip to content

Commit

Permalink
Categories based on sonar's tags (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
filipesperandio authored Oct 23, 2017
1 parent 5bee389 commit d5fe9f6
Showing 6 changed files with 195 additions and 17 deletions.
16 changes: 10 additions & 6 deletions src/main/java/cc/analysis/SonarLintFactory.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
85 changes: 77 additions & 8 deletions src/main/java/cc/models/Categories.java
Original file line number Diff line number Diff line change
@@ -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<String> {
public Categories(String ruleType) {
String category;
switch (ruleType) {
import static cc.models.Categories.Category.*;

class Categories extends ArrayList<Categories.Category> {
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<String> 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);
}
}
85 changes: 85 additions & 0 deletions src/test/java/cc/models/CategoriesTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
5 changes: 3 additions & 2 deletions src/test/java/cc/models/CodeClimateIssueTest.java
Original file line number Diff line number Diff line change
@@ -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)
);
}
}
19 changes: 19 additions & 0 deletions src/test/java/support/Factory.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
2 changes: 1 addition & 1 deletion src/test/resources/sanity_check_expected_issues.json
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@
}
},
"categories": [
"Bug Risk"
"Clarity"
]
},
{

0 comments on commit d5fe9f6

Please sign in to comment.