From 008968aac6a9d80c382573c689477adc4bb29861 Mon Sep 17 00:00:00 2001 From: Filipe Esperandio Date: Wed, 1 Nov 2017 17:18:44 -0200 Subject: [PATCH 1/3] Delete code moved to sonar-wrapper lib --- fixtures/multiple_paths/Main.java | 9 -- fixtures/multiple_paths/config.json | 7 -- .../src/excluded/java/pkg1/HasIssue.java | 21 ---- .../src/included/java/pkg1/HasIssue.java | 21 ---- .../src/included/java/pkg1/HasNoIssue.java | 4 - .../multiple_paths/src/test/java/Test.java | 2 - .../multiple_paths/src/test2/java/Test.java | 2 - src/main/java/cc/App.java | 50 ---------- src/main/java/cc/Config.java | 85 ---------------- .../java/cc/analysis/SonarLintFactory.java | 37 ------- .../java/cc/analysis/StandaloneSonarLint.java | 46 --------- src/main/java/cc/files/Collector.java | 39 -------- src/main/java/cc/files/Finder.java | 67 ------------- src/main/java/cc/files/Matcher.java | 40 -------- src/main/java/cc/models/Categories.java | 95 ------------------ src/main/java/cc/models/CodeClimateIssue.java | 34 ------- src/main/java/cc/models/Content.java | 15 --- src/main/java/cc/models/Lines.java | 22 ----- src/main/java/cc/models/Location.java | 25 ----- src/main/java/cc/models/Severity.java | 35 ------- src/main/java/cc/report/JsonReport.java | 41 -------- src/main/java/cc/report/ReportFactory.java | 24 ----- .../java/cc/serialization/GsonFactory.java | 13 --- .../java/org/sonarlint/cli/EngineWrapper.java | 40 -------- .../cli/analysis/LogOutputWrapper.java | 10 -- .../cli/analysis/SonarLintFactoryWrapper.java | 19 ---- .../java/org/sonarlint/cli/util/Logger.java | 98 ------------------- src/test/java/cc/ConfigTest.java | 73 -------------- src/test/java/cc/files/FinderTest.java | 83 ---------------- src/test/java/cc/models/CategoriesTest.java | 85 ---------------- .../java/cc/models/CodeClimateIssueTest.java | 35 ------- src/test/java/cc/models/LinesTest.java | 17 ---- src/test/java/cc/models/SeverityTest.java | 16 --- src/test/java/cc/report/JsonReportTest.java | 74 -------------- .../integration/ConfigurationOptionsTest.java | 69 ------------- src/test/java/support/Factory.java | 19 ---- src/test/java/support/OutputHelper.java | 23 ----- src/test/java/support/SystemHelper.java | 38 ------- .../support/fakes/FakeClientInputFile.java | 48 --------- src/test/java/support/fakes/FakeIssue.java | 82 ---------------- .../java/support/fakes/FakeRuleDetails.java | 54 ---------- .../java/support/fakes/FakeTrackable.java | 93 ------------------ 42 files changed, 1710 deletions(-) delete mode 100644 fixtures/multiple_paths/Main.java delete mode 100644 fixtures/multiple_paths/config.json delete mode 100644 fixtures/multiple_paths/src/excluded/java/pkg1/HasIssue.java delete mode 100644 fixtures/multiple_paths/src/included/java/pkg1/HasIssue.java delete mode 100644 fixtures/multiple_paths/src/included/java/pkg1/HasNoIssue.java delete mode 100644 fixtures/multiple_paths/src/test/java/Test.java delete mode 100644 fixtures/multiple_paths/src/test2/java/Test.java delete mode 100644 src/main/java/cc/App.java delete mode 100644 src/main/java/cc/Config.java delete mode 100644 src/main/java/cc/analysis/SonarLintFactory.java delete mode 100644 src/main/java/cc/analysis/StandaloneSonarLint.java delete mode 100644 src/main/java/cc/files/Collector.java delete mode 100644 src/main/java/cc/files/Finder.java delete mode 100644 src/main/java/cc/files/Matcher.java delete mode 100644 src/main/java/cc/models/Categories.java delete mode 100644 src/main/java/cc/models/CodeClimateIssue.java delete mode 100644 src/main/java/cc/models/Content.java delete mode 100644 src/main/java/cc/models/Lines.java delete mode 100644 src/main/java/cc/models/Location.java delete mode 100644 src/main/java/cc/models/Severity.java delete mode 100644 src/main/java/cc/report/JsonReport.java delete mode 100644 src/main/java/cc/report/ReportFactory.java delete mode 100644 src/main/java/cc/serialization/GsonFactory.java delete mode 100644 src/main/java/org/sonarlint/cli/EngineWrapper.java delete mode 100644 src/main/java/org/sonarlint/cli/analysis/LogOutputWrapper.java delete mode 100644 src/main/java/org/sonarlint/cli/analysis/SonarLintFactoryWrapper.java delete mode 100644 src/main/java/org/sonarlint/cli/util/Logger.java delete mode 100644 src/test/java/cc/ConfigTest.java delete mode 100644 src/test/java/cc/files/FinderTest.java delete mode 100644 src/test/java/cc/models/CategoriesTest.java delete mode 100644 src/test/java/cc/models/CodeClimateIssueTest.java delete mode 100644 src/test/java/cc/models/LinesTest.java delete mode 100644 src/test/java/cc/models/SeverityTest.java delete mode 100644 src/test/java/cc/report/JsonReportTest.java delete mode 100644 src/test/java/integration/ConfigurationOptionsTest.java delete mode 100644 src/test/java/support/Factory.java delete mode 100644 src/test/java/support/OutputHelper.java delete mode 100644 src/test/java/support/SystemHelper.java delete mode 100644 src/test/java/support/fakes/FakeClientInputFile.java delete mode 100644 src/test/java/support/fakes/FakeIssue.java delete mode 100644 src/test/java/support/fakes/FakeRuleDetails.java delete mode 100644 src/test/java/support/fakes/FakeTrackable.java diff --git a/fixtures/multiple_paths/Main.java b/fixtures/multiple_paths/Main.java deleted file mode 100644 index ebb7893..0000000 --- a/fixtures/multiple_paths/Main.java +++ /dev/null @@ -1,9 +0,0 @@ -import pkg1; - -public class Main { - public void main(String[] args) { - for (int k = 0; k < 20; i++) { // cause issue - System.out.println(new Class1()); - } - } -} \ No newline at end of file diff --git a/fixtures/multiple_paths/config.json b/fixtures/multiple_paths/config.json deleted file mode 100644 index a3c992c..0000000 --- a/fixtures/multiple_paths/config.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "enabled": true, - "include_paths": [ - "src/included/", - "Main.java" - ] -} diff --git a/fixtures/multiple_paths/src/excluded/java/pkg1/HasIssue.java b/fixtures/multiple_paths/src/excluded/java/pkg1/HasIssue.java deleted file mode 100644 index b1e976f..0000000 --- a/fixtures/multiple_paths/src/excluded/java/pkg1/HasIssue.java +++ /dev/null @@ -1,21 +0,0 @@ -package otherpkg; - -public class HasIssue { - public void method() { - for (int i = 1; i != 10; i += 2) { - for (int k = 0; k < 20; i++) { - System.out.println("Hello"); - } - } - } - - public void doSomething(File file, Lock lock) { - String b = "txt"; - String a = b; - String c = a; - b = c; - - file.delete(); // Noncompliant - lock.tryLock(); // Noncompliant - } -} diff --git a/fixtures/multiple_paths/src/included/java/pkg1/HasIssue.java b/fixtures/multiple_paths/src/included/java/pkg1/HasIssue.java deleted file mode 100644 index dbc3cd7..0000000 --- a/fixtures/multiple_paths/src/included/java/pkg1/HasIssue.java +++ /dev/null @@ -1,21 +0,0 @@ -package pkg1; - -class HasIssue { - public void method() { - for (int i = 0; i < 10; i++) { - for (int k = 0; k < 20; i++) { - System.out.println("Hello"); - } - } - } - - public void doSomething(File file, Lock lock) { - String b = "txt"; - String a = b; - String c = a; - b = c; - - file.delete(); // Noncompliant - lock.tryLock(); // Noncompliant - } -} \ No newline at end of file diff --git a/fixtures/multiple_paths/src/included/java/pkg1/HasNoIssue.java b/fixtures/multiple_paths/src/included/java/pkg1/HasNoIssue.java deleted file mode 100644 index 281d5df..0000000 --- a/fixtures/multiple_paths/src/included/java/pkg1/HasNoIssue.java +++ /dev/null @@ -1,4 +0,0 @@ -package pkg1; - -public class HasNoIssue { -} \ No newline at end of file diff --git a/fixtures/multiple_paths/src/test/java/Test.java b/fixtures/multiple_paths/src/test/java/Test.java deleted file mode 100644 index 0b8bc08..0000000 --- a/fixtures/multiple_paths/src/test/java/Test.java +++ /dev/null @@ -1,2 +0,0 @@ -public class Test { -} \ No newline at end of file diff --git a/fixtures/multiple_paths/src/test2/java/Test.java b/fixtures/multiple_paths/src/test2/java/Test.java deleted file mode 100644 index 0b8bc08..0000000 --- a/fixtures/multiple_paths/src/test2/java/Test.java +++ /dev/null @@ -1,2 +0,0 @@ -public class Test { -} \ No newline at end of file diff --git a/src/main/java/cc/App.java b/src/main/java/cc/App.java deleted file mode 100644 index acdae6c..0000000 --- a/src/main/java/cc/App.java +++ /dev/null @@ -1,50 +0,0 @@ -package cc; - -import cc.analysis.SonarLintFactory; -import cc.files.Finder; -import org.sonarlint.cli.EngineWrapper; -import org.sonarlint.cli.InputFileFinder; -import org.sonarlint.cli.config.ConfigurationReader; -import org.sonarlint.cli.report.ReportFactory; -import org.sonarlint.cli.util.System2; - -import java.nio.charset.Charset; -import java.nio.file.Path; -import java.nio.file.Paths; - -import static org.sonarlint.cli.SonarProperties.PROJECT_HOME; - -public class App { - static final int ERROR = 1; - - public static void main(String[] args) { - execute(System2.INSTANCE); - } - - public static void execute(System2 system) { - try { - Config config = Config.from(system.getProperty("config")); - Charset charset = config.getCharset(); - - InputFileFinder fileFinder = new Finder(config.getIncludePaths(), config.getTestsPatterns(), charset); - ReportFactory reportFactory = new cc.report.ReportFactory(charset, config.getMinimumSeverity()); - ConfigurationReader reader = new ConfigurationReader(); - SonarLintFactory sonarLintFactory = new SonarLintFactory(reader, config.getSonarlintDir()); - Path projectHome = getProjectHome(system); - - int exitCode = new EngineWrapper(config, sonarLintFactory, reportFactory, fileFinder, projectHome).run(); - system.exit(exitCode); - } catch (Exception e) { - e.printStackTrace(System.err); - system.exit(ERROR); - } - } - - private static Path getProjectHome(System2 system) { - String projectHome = system.getProperty(PROJECT_HOME); - if (projectHome == null) { - throw new IllegalStateException("Can't find project home. System property not set: " + PROJECT_HOME); - } - return Paths.get(projectHome); - } -} diff --git a/src/main/java/cc/Config.java b/src/main/java/cc/Config.java deleted file mode 100644 index 8166801..0000000 --- a/src/main/java/cc/Config.java +++ /dev/null @@ -1,85 +0,0 @@ -package cc; - -import cc.models.Severity; -import com.google.gson.FieldNamingPolicy; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.stream.JsonReader; -import org.sonarlint.cli.Options; - -import java.io.FileReader; -import java.nio.charset.Charset; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.*; - -public class Config extends Options { - private List includePaths = Arrays.asList(""); - private Map config = new LinkedHashMap<>(); - - public List getIncludePaths() { - return includePaths; - } - - public Charset getCharset() { - return createCharset(charset()); - } - - @Override - public String charset() { - return (String) config.get("charset"); - } - - public String getTestsPatterns() { - List testsPatterns = (List) config.get("tests_patterns"); - return joinPatterns(testsPatterns); - } - - public Path getSonarlintDir() { - return Paths.get("/tmp/sonarlint"); - } - - public Severity getMinimumSeverity() { - String severity = (String) config.get("minimum_severity"); - return Severity.from(severity, Severity.MAJOR); - } - - @Override - public Properties properties() { - Properties properties = super.properties(); - for (Map.Entry entry : config.entrySet()) { - properties.put(entry.getKey(), entry.getValue().toString()); - } - return properties; - } - - String joinPatterns(List patterns) { - if (patterns == null) { - return null; - } - return "{" + String.join(",", patterns) + "}"; - } - - - Charset createCharset(String charset) { - if (charset != null) { - return Charset.forName(charset); - } else { - return Charset.defaultCharset(); - } - } - - public static Config from(String file) { - try { - return gson().fromJson(new JsonReader(new FileReader(file)), Config.class); - } catch (Exception e) { - return new Config(); - } - } - - static Gson gson() { - return new GsonBuilder() - .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) - .create(); - } -} diff --git a/src/main/java/cc/analysis/SonarLintFactory.java b/src/main/java/cc/analysis/SonarLintFactory.java deleted file mode 100644 index 94aa81b..0000000 --- a/src/main/java/cc/analysis/SonarLintFactory.java +++ /dev/null @@ -1,37 +0,0 @@ -package cc.analysis; - -import org.sonarlint.cli.analysis.LogOutputWrapper; -import org.sonarlint.cli.analysis.SonarLint; -import org.sonarlint.cli.config.ConfigurationReader; -import org.sonarlint.cli.util.Logger; -import org.sonarsource.sonarlint.core.StandaloneSonarLintEngineImpl; -import org.sonarsource.sonarlint.core.client.api.standalone.StandaloneGlobalConfiguration; -import org.sonarsource.sonarlint.core.client.api.standalone.StandaloneSonarLintEngine; - -import java.nio.file.Path; - -public class SonarLintFactory extends org.sonarlint.cli.analysis.SonarLintFactoryWrapper { - private Path workDir; - - public SonarLintFactory(ConfigurationReader reader, Path workDir) { - super(reader); - this.workDir = workDir; - } - - @Override - public SonarLint createSonarLint(Path projectHome, boolean mustBeConnected, boolean verbose) { - LogOutputWrapper logWrapper = new LogOutputWrapper(verbose); - return new StandaloneSonarLint(createEngine(logWrapper), workDir, logWrapper); - } - - public StandaloneSonarLintEngineImpl createEngine(LogOutputWrapper logWrapper) { - return new StandaloneSonarLintEngineImpl(config(logWrapper)); - } - - public StandaloneGlobalConfiguration config(LogOutputWrapper logWrapper) { - return StandaloneGlobalConfiguration.builder() - .addPlugins(plugins()) - .setLogOutput(logWrapper) - .build(); - } -} diff --git a/src/main/java/cc/analysis/StandaloneSonarLint.java b/src/main/java/cc/analysis/StandaloneSonarLint.java deleted file mode 100644 index 139bd66..0000000 --- a/src/main/java/cc/analysis/StandaloneSonarLint.java +++ /dev/null @@ -1,46 +0,0 @@ -package cc.analysis; - -import org.sonarlint.cli.analysis.IssueCollector; -import org.sonarlint.cli.analysis.LogOutputWrapper; -import org.sonarlint.cli.report.ReportFactory; -import org.sonarsource.sonarlint.core.client.api.common.ProgressMonitor; -import org.sonarsource.sonarlint.core.client.api.common.analysis.AnalysisResults; -import org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile; -import org.sonarsource.sonarlint.core.client.api.standalone.StandaloneAnalysisConfiguration; -import org.sonarsource.sonarlint.core.client.api.standalone.StandaloneSonarLintEngine; -import org.sonarsource.sonarlint.core.tracking.IssueTrackable; -import org.sonarsource.sonarlint.core.tracking.Trackable; - -import java.nio.file.Path; -import java.util.Collection; -import java.util.Date; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; - -public class StandaloneSonarLint extends org.sonarlint.cli.analysis.StandaloneSonarLint { - private StandaloneSonarLintEngine engine; - private Path workDir; - private LogOutputWrapper logWrapper; - - public StandaloneSonarLint(StandaloneSonarLintEngine engine, Path workDir, LogOutputWrapper logWrapper) { - super(engine); - this.engine = engine; - this.workDir = workDir; - this.logWrapper = logWrapper; - } - - @Override - protected void doAnalysis(Map properties, ReportFactory reportFactory, List inputFiles, Path baseDirPath) { - Date start = new Date(); - - IssueCollector collector = new IssueCollector(); - StandaloneAnalysisConfiguration config = new StandaloneAnalysisConfiguration(baseDirPath, workDir, inputFiles, properties); - AnalysisResults result = engine.analyze(config, collector, logWrapper, new NoOpProgressMonitor()); - Collection trackables = collector.get().stream().map(IssueTrackable::new).collect(Collectors.toList()); - generateReports(trackables, result, reportFactory, baseDirPath.getFileName().toString(), baseDirPath, start); - } - - private static class NoOpProgressMonitor extends ProgressMonitor { - } -} diff --git a/src/main/java/cc/files/Collector.java b/src/main/java/cc/files/Collector.java deleted file mode 100644 index 2429da5..0000000 --- a/src/main/java/cc/files/Collector.java +++ /dev/null @@ -1,39 +0,0 @@ -package cc.files; - -import java.io.IOException; -import java.nio.file.FileVisitResult; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.SimpleFileVisitor; -import java.nio.file.attribute.BasicFileAttributes; -import java.util.ArrayList; -import java.util.List; - -public class Collector extends SimpleFileVisitor { - final List files; - final Path baseDir; - - public Collector(Path baseDir) { - this.baseDir = baseDir; - this.files = new ArrayList<>(); - } - - public List getFiles() { - return files; - } - - @Override - public FileVisitResult visitFile(final Path file, BasicFileAttributes attrs) throws IOException { - files.add(file); - return super.visitFile(file, attrs); - } - - @Override - public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { - if (Files.isHidden(dir)) { - return FileVisitResult.SKIP_SUBTREE; - } - - return super.preVisitDirectory(dir, attrs); - } -} diff --git a/src/main/java/cc/files/Finder.java b/src/main/java/cc/files/Finder.java deleted file mode 100644 index 6481ce4..0000000 --- a/src/main/java/cc/files/Finder.java +++ /dev/null @@ -1,67 +0,0 @@ -package cc.files; - -import org.sonarlint.cli.InputFileFinder; -import org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile; - -import java.io.IOException; -import java.nio.charset.Charset; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; -import java.util.stream.Collectors; - -import static java.nio.file.Files.isDirectory; - -public class Finder extends InputFileFinder { - final List includedPaths; - final Charset charset; - final Matcher matcher; - - public Finder(List includedPaths, String testsGlobPattern, Charset charset) { - super(null, testsGlobPattern, null, charset); - this.includedPaths = includedPaths; - this.charset = charset; - this.matcher = new Matcher(testsGlobPattern, charset); - } - - @Override - public List collect(Path baseDir) throws IOException { - return findPaths(baseDir).stream() - .map(path -> toClientInputFile(baseDir, path)) - .filter(Objects::nonNull) - .collect(Collectors.toList()); - } - - List findPaths(Path baseDir) throws IOException { - List paths = new ArrayList<>(); - for (String path : includedPaths) { - Path resolvedPath = baseDir.resolve(path); - if (isDirectory(resolvedPath)) { - paths.addAll(collectDir(baseDir, resolvedPath)); - } else { - paths.add(resolvedPath); - } - } - return paths; - } - - ClientInputFile toClientInputFile(Path baseDir, Path path) { - return createInputFile(path, isTest(baseDir, path)); - } - - boolean isTest(Path baseDir, Path path) { - return matcher.isTest(baseDir, path); - } - - ClientInputFile createInputFile(Path resolvedPath, boolean test) { - return new DefaultClientInputFile(resolvedPath, test, charset); - } - - List collectDir(Path baseDir, Path dir) throws IOException { - Collector collector = new Collector(baseDir); - Files.walkFileTree(dir, collector); - return collector.getFiles(); - } -} \ No newline at end of file diff --git a/src/main/java/cc/files/Matcher.java b/src/main/java/cc/files/Matcher.java deleted file mode 100644 index 99b3e2b..0000000 --- a/src/main/java/cc/files/Matcher.java +++ /dev/null @@ -1,40 +0,0 @@ -package cc.files; - -import java.nio.charset.Charset; -import java.nio.file.FileSystems; -import java.nio.file.Path; -import java.nio.file.PathMatcher; - -public class Matcher { - static final String GLOB_PREFIX = "glob:"; - static PathMatcher REFUSE_ALL = p -> false; - - final PathMatcher testsMatcher; - final Charset charset; - - public Matcher(PathMatcher testsMatcher, Charset charset) { - this.testsMatcher = testsMatcher; - this.charset = charset; - } - - public Matcher(String testsGlobPattern, Charset charset) { - this(createPathMatcher(testsGlobPattern, REFUSE_ALL), charset); - } - - public boolean isTest(Path baseDir, Path absoluteFilePath) { - Path relativeFilePath = baseDir.relativize(absoluteFilePath); - return testsMatcher.matches(absoluteFilePath) || testsMatcher.matches(relativeFilePath); - } - - static PathMatcher createPathMatcher(String pattern, PathMatcher defaultMatcher) { - try { - if (pattern != null) { - return FileSystems.getDefault().getPathMatcher(GLOB_PREFIX + pattern); - } else { - return defaultMatcher; - } - } catch (Exception e) { - throw new RuntimeException("Error creating matcher with pattern: " + pattern, e); - } - } -} \ No newline at end of file diff --git a/src/main/java/cc/models/Categories.java b/src/main/java/cc/models/Categories.java deleted file mode 100644 index 4e076c0..0000000 --- a/src/main/java/cc/models/Categories.java +++ /dev/null @@ -1,95 +0,0 @@ -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; - -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": { - add(SECURITY); - break; - } - case "BUG": { - add(BUG_RISK); - break; - } - case "CODE_SMELL": - default: { - Category category = fromTags(rule.getTags()); - add(category); - break; - } - } - } - - 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); - } -} diff --git a/src/main/java/cc/models/CodeClimateIssue.java b/src/main/java/cc/models/CodeClimateIssue.java deleted file mode 100644 index 1797b87..0000000 --- a/src/main/java/cc/models/CodeClimateIssue.java +++ /dev/null @@ -1,34 +0,0 @@ -package cc.models; - -import org.sonarsource.sonarlint.core.client.api.common.RuleDetails; -import org.sonarsource.sonarlint.core.client.api.common.analysis.Issue; - -public class CodeClimateIssue { - public final String type = "issue"; - public final String checkName; - public final Severity severity; - public final String description; - public final Content content; - public final Location location; - public final Categories categories; - - public CodeClimateIssue(String checkName, Severity severity, String description, Content content, Location location, Categories categories) { - this.checkName = checkName; - this.severity = severity; - this.description = description; - this.content = content; - this.location = location; - this.categories = categories; - } - - public static CodeClimateIssue from(Issue issue, RuleDetails ruleDetails, String baseDir) { - String checkName = issue.getRuleKey(); - String description = issue.getMessage(); - Severity severity = Severity.from(ruleDetails); - Content content = Content.from(ruleDetails); - Location location = Location.from(baseDir, issue); - Categories categories = Categories.from(ruleDetails); - return new CodeClimateIssue(checkName, severity, description, content, location, categories); - } - -} diff --git a/src/main/java/cc/models/Content.java b/src/main/java/cc/models/Content.java deleted file mode 100644 index 1840150..0000000 --- a/src/main/java/cc/models/Content.java +++ /dev/null @@ -1,15 +0,0 @@ -package cc.models; - -import org.sonarsource.sonarlint.core.client.api.common.RuleDetails; - -class Content { - String body; - - public Content(String body) { - this.body = body; - } - - public static Content from(RuleDetails ruleDetails) { - return new Content(ruleDetails.getHtmlDescription()); - } -} diff --git a/src/main/java/cc/models/Lines.java b/src/main/java/cc/models/Lines.java deleted file mode 100644 index 1765beb..0000000 --- a/src/main/java/cc/models/Lines.java +++ /dev/null @@ -1,22 +0,0 @@ -package cc.models; - -import org.sonarsource.sonarlint.core.client.api.common.analysis.Issue; - -class Lines { - final Integer begin; - final Integer end; - - public Lines(Integer begin, Integer end) { - this.begin = begin; - this.end = end; - } - - public static Lines from(Issue issue) { - if (issue.getStartLine() == null || issue.getEndLine() == null) { - System.err.println("File location was not provided, defaulting to line 1."); - System.err.println(issue); - return new Lines(1, 1); - } - return new Lines(issue.getStartLine(), issue.getEndLine()); - } -} diff --git a/src/main/java/cc/models/Location.java b/src/main/java/cc/models/Location.java deleted file mode 100644 index f39de09..0000000 --- a/src/main/java/cc/models/Location.java +++ /dev/null @@ -1,25 +0,0 @@ -package cc.models; - -import org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile; -import org.sonarsource.sonarlint.core.client.api.common.analysis.Issue; - -class Location { - final String path; - final Lines lines; - - public Location(String baseDir, String path, Lines lines) { - String regex = ("^" + baseDir + "/").replace("//", "/"); - this.path = path.replaceFirst(regex, ""); - this.lines = lines; - } - - public static Location from(String baseDir, Issue issue) { - ClientInputFile inputFile = issue.getInputFile(); - - if (inputFile == null || inputFile.getPath() == null) { - return null; - } - - return new Location(baseDir, inputFile.getPath(), Lines.from(issue)); - } -} diff --git a/src/main/java/cc/models/Severity.java b/src/main/java/cc/models/Severity.java deleted file mode 100644 index af15c07..0000000 --- a/src/main/java/cc/models/Severity.java +++ /dev/null @@ -1,35 +0,0 @@ -package cc.models; - -import com.google.gson.annotations.SerializedName; -import org.sonarsource.sonarlint.core.client.api.common.RuleDetails; - -import static java.util.Optional.ofNullable; - -public enum Severity { - @SerializedName("info") - INFO, - @SerializedName("minor") - MINOR, - @SerializedName("major") - MAJOR, - @SerializedName("critical") - CRITICAL, - @SerializedName("blocker") - BLOCKER; - - public static Severity from(RuleDetails ruleDetails) { - String severity = ruleDetails.getSeverity(); - return from(severity); - } - - public static Severity from(String severity) { - if (severity == null) { - return null; - } - return valueOf(severity.toUpperCase()); - } - - public static Severity from(String severity, Severity defaultValue) { - return ofNullable(from(severity)).orElse(defaultValue); - } -} diff --git a/src/main/java/cc/report/JsonReport.java b/src/main/java/cc/report/JsonReport.java deleted file mode 100644 index a07e820..0000000 --- a/src/main/java/cc/report/JsonReport.java +++ /dev/null @@ -1,41 +0,0 @@ -package cc.report; - -import cc.models.CodeClimateIssue; -import cc.models.Severity; -import cc.serialization.GsonFactory; -import com.google.gson.Gson; -import org.sonarsource.sonarlint.core.client.api.common.RuleDetails; -import org.sonarsource.sonarlint.core.client.api.common.analysis.AnalysisResults; -import org.sonarsource.sonarlint.core.client.api.common.analysis.Issue; -import org.sonarsource.sonarlint.core.tracking.Trackable; - -import java.util.Collection; -import java.util.Date; -import java.util.function.Function; - -public class JsonReport implements org.sonarlint.cli.report.Reporter { - - final Gson gson; - final Severity minimumSeverity; - final String baseDir; - - public JsonReport(Severity minimumSeverity, String baseDir) { - this.minimumSeverity = minimumSeverity; - this.baseDir = baseDir; - this.gson = new GsonFactory().create(); - } - - @Override - public void execute(String projectName, Date date, Collection trackables, AnalysisResults result, Function ruleDescriptionProducer) { - for (Trackable trackable : trackables) { - Issue issue = trackable.getIssue(); - RuleDetails ruleDetails = ruleDescriptionProducer.apply(issue.getRuleKey()); - - CodeClimateIssue codeClimateIssue = CodeClimateIssue.from(issue, ruleDetails, baseDir); - Severity severity = codeClimateIssue.severity; - if (severity!= null && severity.compareTo(minimumSeverity) >= 0) { - System.out.println(gson.toJson(codeClimateIssue) + "\0"); - } - } - } -} \ No newline at end of file diff --git a/src/main/java/cc/report/ReportFactory.java b/src/main/java/cc/report/ReportFactory.java deleted file mode 100644 index ef52cd9..0000000 --- a/src/main/java/cc/report/ReportFactory.java +++ /dev/null @@ -1,24 +0,0 @@ -package cc.report; - -import cc.models.Severity; -import org.sonarlint.cli.report.Reporter; - -import java.nio.charset.Charset; -import java.nio.file.Path; -import java.util.Arrays; -import java.util.List; - -public class ReportFactory extends org.sonarlint.cli.report.ReportFactory { - - Severity minimumSeverity; - - public ReportFactory(Charset charset, Severity minimumSeverity) { - super(charset); - this.minimumSeverity = minimumSeverity; - } - - @Override - public List createReporters(Path basePath) { - return Arrays.asList(new JsonReport(minimumSeverity, basePath.toString())); - } -} diff --git a/src/main/java/cc/serialization/GsonFactory.java b/src/main/java/cc/serialization/GsonFactory.java deleted file mode 100644 index ad30d57..0000000 --- a/src/main/java/cc/serialization/GsonFactory.java +++ /dev/null @@ -1,13 +0,0 @@ -package cc.serialization; - -import com.google.gson.FieldNamingPolicy; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; - -public class GsonFactory { - public Gson create() { - return new GsonBuilder() - .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) - .create(); - } -} diff --git a/src/main/java/org/sonarlint/cli/EngineWrapper.java b/src/main/java/org/sonarlint/cli/EngineWrapper.java deleted file mode 100644 index 7147190..0000000 --- a/src/main/java/org/sonarlint/cli/EngineWrapper.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * SonarLint CLI - * Copyright (C) 2016-2017 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonarlint.cli; - -import org.sonarlint.cli.analysis.SonarLintFactory; -import org.sonarlint.cli.report.ReportFactory; - -import java.nio.file.Path; - -public class EngineWrapper extends Main { - - public EngineWrapper(Options opts, SonarLintFactory sonarLintFactory, ReportFactory reportFactory, InputFileFinder fileFinder, Path projectHome) { - super(opts, sonarLintFactory, reportFactory, fileFinder, projectHome); - } - - /** - * Required to change method modifier into "public", so we can call it from `cc.App` - */ - @Override - public int run() { - return super.run(); - } -} \ No newline at end of file diff --git a/src/main/java/org/sonarlint/cli/analysis/LogOutputWrapper.java b/src/main/java/org/sonarlint/cli/analysis/LogOutputWrapper.java deleted file mode 100644 index 8886cd2..0000000 --- a/src/main/java/org/sonarlint/cli/analysis/LogOutputWrapper.java +++ /dev/null @@ -1,10 +0,0 @@ -package org.sonarlint.cli.analysis; - -import org.sonarlint.cli.util.Logger; - -public class LogOutputWrapper extends DefaultLogOutput { - - public LogOutputWrapper(boolean verbose) { - super(Logger.get(), verbose); - } -} diff --git a/src/main/java/org/sonarlint/cli/analysis/SonarLintFactoryWrapper.java b/src/main/java/org/sonarlint/cli/analysis/SonarLintFactoryWrapper.java deleted file mode 100644 index 5e680f9..0000000 --- a/src/main/java/org/sonarlint/cli/analysis/SonarLintFactoryWrapper.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.sonarlint.cli.analysis; - -import org.sonarlint.cli.config.ConfigurationReader; - -import java.net.URL; - -public class SonarLintFactoryWrapper extends SonarLintFactory { - public SonarLintFactoryWrapper(ConfigurationReader configurationReader) { - super(configurationReader); - } - - public URL[] plugins() { - try { - return loadPlugins(); - } catch (Exception e) { - throw new IllegalStateException("Error loading plugins", e); - } - } -} diff --git a/src/main/java/org/sonarlint/cli/util/Logger.java b/src/main/java/org/sonarlint/cli/util/Logger.java deleted file mode 100644 index c1d31a8..0000000 --- a/src/main/java/org/sonarlint/cli/util/Logger.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * SonarLint CLI - * Copyright (C) 2016-2017 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonarlint.cli.util; - -import java.io.PrintStream; - -public class Logger { - private static volatile Logger instance; - private boolean debugEnabled = false; - private boolean displayStackTrace = false; - private PrintStream stdOut; - private PrintStream stdErr; - - private Logger() { - this.stdErr = System.err; - this.stdOut = System.out; - } - - public Logger(PrintStream stdOut, PrintStream stdErr) { - this.stdErr = stdErr; - this.stdOut = stdOut; - } - - public static Logger get() { - if (instance == null) { - instance = new Logger(); - } - return instance; - } - - public static void set(PrintStream stdOut, PrintStream stdErr) { - get().stdOut = stdOut; - get().stdErr = stdErr; - } - - public void setDebugEnabled(boolean debugEnabled) { - this.debugEnabled = debugEnabled; - } - - public void setDisplayStackTrace(boolean displayStackTrace) { - this.displayStackTrace = displayStackTrace; - } - - public boolean isDebugEnabled() { - return debugEnabled; - } - - public void debug(String message) { - if (isDebugEnabled()) { - stdErr.println("DEBUG: " + message); - } - } - - public void debug(String message, Throwable t) { - if (isDebugEnabled()) { - stdErr.println("DEBUG: " + message); - if (displayStackTrace) { - t.printStackTrace(stdErr); - } - } - } - - public void info(String message) { - stdErr.println("INFO: " + message); - } - - public void warn(String message) { - stdErr.println("WARN: " + message); - } - - public void error(String message) { - stdErr.println("ERROR: " + message); - } - - public void error(String message, Throwable t) { - stdErr.println("ERROR: " + message); - if (displayStackTrace) { - t.printStackTrace(stdErr); - } - } -} diff --git a/src/test/java/cc/ConfigTest.java b/src/test/java/cc/ConfigTest.java deleted file mode 100644 index e9822d7..0000000 --- a/src/test/java/cc/ConfigTest.java +++ /dev/null @@ -1,73 +0,0 @@ -package cc; - -import cc.models.Severity; -import org.junit.Test; - -import java.nio.charset.Charset; -import java.nio.file.Paths; - -import static org.assertj.core.api.Assertions.assertThat; - -public class ConfigTest { - - @Test - public void include_paths() throws Exception { - Config config = Config.from("fixtures/multiple_paths/config.json"); - assertThat(config.getIncludePaths()).containsOnly("Main.java", "src/included/"); - } - - @Test - public void default_config_path_include_base_dir() throws Exception { - Config config = new Config(); - assertThat(config.getIncludePaths()).containsOnly(""); - } - - @Test - public void fetch_charset() throws Exception { - Config config = Config.gson().fromJson("{\"config\":{\"charset\":\"utf-16\"}}", Config.class); - assertThat(config.getCharset()).isEqualTo(Charset.forName("UTF-16")); - } - - @Test - public void defaults_charset_to_utf8() throws Exception { - Config config = Config.gson().fromJson("{\"config\":{}}", Config.class); - assertThat(config.getCharset()).isEqualTo(Charset.forName("UTF-8")); - } - - @Test - public void fetch_minimum_severity() throws Exception { - Config config = Config.gson().fromJson("{\"config\":{\"minimum_severity\":\"critical\"}}", Config.class); - assertThat(config.getMinimumSeverity()).isEqualTo(Severity.CRITICAL); - } - - @Test - public void defaults_minimum_severity_to_major() throws Exception { - Config config = Config.gson().fromJson("{}", Config.class); - assertThat(config.getMinimumSeverity()).isEqualTo(Severity.MAJOR); - } - - @Test - public void fetch_tests_patterns() throws Exception { - Config config = Config.gson().fromJson("{\"config\":{\"tests_patterns\":[\"src/test/**\",\"src/test2/**\"]}}", Config.class); - assertThat(config.getTestsPatterns()).isEqualTo("{src/test/**,src/test2/**}"); - } - - @Test - public void null_tests_patterns_does_not_cause_error() throws Exception { - Config config = Config.gson().fromJson("{\"config\":{}}", Config.class); - assertThat(config.getTestsPatterns()).isNull(); - } - - @Test - public void has_default_work_dir() throws Exception { - Config config = Config.gson().fromJson("{}", Config.class); - assertThat(config.getSonarlintDir()).isEqualTo(Paths.get("/tmp/sonarlint")); - } - - @Test - public void include_generic_properties() throws Exception { - Config config = Config.gson().fromJson("{\"config\":{\"key\":\"value\",\"charset\":\"utf-16\"}}", Config.class); - assertThat(config.properties().getProperty("key")).isEqualTo("value"); - assertThat(config.getCharset()).isEqualTo(Charset.forName("UTF-16")); - } -} \ No newline at end of file diff --git a/src/test/java/cc/files/FinderTest.java b/src/test/java/cc/files/FinderTest.java deleted file mode 100644 index 654d0af..0000000 --- a/src/test/java/cc/files/FinderTest.java +++ /dev/null @@ -1,83 +0,0 @@ -package cc.files; - -import org.junit.Test; -import org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile; - -import java.nio.charset.Charset; -import java.nio.file.Paths; -import java.util.Arrays; -import java.util.List; -import java.util.stream.Collectors; - -import static org.assertj.core.api.Assertions.assertThat; - - -public class FinderTest { - - @Test - public void find_files_in_directory() throws Exception { - Finder finder = new Finder(Arrays.asList("src/included/"), null, Charset.defaultCharset()); - - List files = finder.collect(Paths.get("fixtures/multiple_paths")); - List paths = files.stream().map(ClientInputFile::getPath).collect(Collectors.toList()); - - assertThat(paths).containsOnly( - "fixtures/multiple_paths/src/included/java/pkg1/HasIssue.java", - "fixtures/multiple_paths/src/included/java/pkg1/HasNoIssue.java" - ); - } - - @Test - public void find_specified_files() throws Exception { - Finder finder = new Finder(Arrays.asList("config.json", "Main.java"), null, Charset.defaultCharset()); - - List files = finder.collect(Paths.get("fixtures/multiple_paths")); - List paths = files.stream().map(ClientInputFile::getPath).collect(Collectors.toList()); - - assertThat(paths).containsOnly( - "fixtures/multiple_paths/Main.java", - "fixtures/multiple_paths/config.json" - ); - } - - @Test - public void find_from_multiple_locations() throws Exception { - Finder finder = new Finder(Arrays.asList("config.json", "src/included/java/"), null, Charset.defaultCharset()); - - List files = finder.collect(Paths.get("fixtures/multiple_paths")); - List paths = files.stream().map(ClientInputFile::getPath).collect(Collectors.toList()); - - assertThat(paths).containsOnly( - "fixtures/multiple_paths/config.json", - "fixtures/multiple_paths/src/included/java/pkg1/HasIssue.java", - "fixtures/multiple_paths/src/included/java/pkg1/HasNoIssue.java" - ); - } - - @Test - public void differentiate_src_and_test() throws Exception { - Finder finder = new Finder(Arrays.asList("src/included/", "src/test/"), "{**/test/**}", Charset.defaultCharset()); - - List files = finder.collect(Paths.get("fixtures/multiple_paths")); - - assertThat(files.stream().filter(f -> !f.isTest()).map(ClientInputFile::getPath).collect(Collectors.toList())).containsOnly( - "fixtures/multiple_paths/src/included/java/pkg1/HasIssue.java", - "fixtures/multiple_paths/src/included/java/pkg1/HasNoIssue.java" - ); - assertThat(files.stream().filter(f -> f.isTest()).map(ClientInputFile::getPath).collect(Collectors.toList())).containsOnly( - "fixtures/multiple_paths/src/test/java/Test.java" - ); - } - - @Test - public void accept_multiple_test_patterns() throws Exception { - Finder finder = new Finder(Arrays.asList("src/"), "{**/test/**,**/test2/**}", Charset.defaultCharset()); - - List files = finder.collect(Paths.get("fixtures/multiple_paths")); - - assertThat(files.stream().filter(f -> f.isTest()).map(ClientInputFile::getPath).collect(Collectors.toList())).containsOnly( - "fixtures/multiple_paths/src/test/java/Test.java", - "fixtures/multiple_paths/src/test2/java/Test.java" - ); - } -} \ No newline at end of file diff --git a/src/test/java/cc/models/CategoriesTest.java b/src/test/java/cc/models/CategoriesTest.java deleted file mode 100644 index 309e958..0000000 --- a/src/test/java/cc/models/CategoriesTest.java +++ /dev/null @@ -1,85 +0,0 @@ -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 deleted file mode 100644 index d222988..0000000 --- a/src/test/java/cc/models/CodeClimateIssueTest.java +++ /dev/null @@ -1,35 +0,0 @@ -package cc.models; - -import cc.serialization.GsonFactory; -import com.google.gson.Gson; -import org.junit.Test; -import support.fakes.FakeRuleDetails; - -import static org.assertj.core.api.Assertions.assertThat; - -public class CodeClimateIssueTest { - - private Gson gson = new GsonFactory().create(); - - @Test - public void down_case_severities_uppon_serialization() throws Exception { - assertThat(gson.toJson(createIssueForSeverity("CRITICAL"))).contains("critical"); - } - - @Test - public void properly_serialize_severity() throws Exception { - assertThat(gson.toJson(createIssueForSeverity("INFO"))).contains("\"severity\":\"info\""); - } - - private CodeClimateIssue createIssueForSeverity(String severity) { - FakeRuleDetails rule = new FakeRuleDetails(severity); - return new CodeClimateIssue( - "check", - Severity.from(rule), - "desc", - new Content(""), - new Location("/tmp", "path", new Lines(0, 1)), - new Categories(rule) - ); - } -} \ No newline at end of file diff --git a/src/test/java/cc/models/LinesTest.java b/src/test/java/cc/models/LinesTest.java deleted file mode 100644 index a35f5ec..0000000 --- a/src/test/java/cc/models/LinesTest.java +++ /dev/null @@ -1,17 +0,0 @@ -package cc.models; - -import org.junit.Test; -import support.fakes.FakeIssue; - -import static org.assertj.core.api.Assertions.assertThat; - -public class LinesTest { - - @Test - public void default_to_line_1() throws Exception { - FakeIssue issue = new FakeIssue("path", null, null); - Lines lines = Lines.from(issue); - assertThat(lines.begin).isEqualTo(1); - assertThat(lines.end).isEqualTo(1); - } -} \ No newline at end of file diff --git a/src/test/java/cc/models/SeverityTest.java b/src/test/java/cc/models/SeverityTest.java deleted file mode 100644 index fa02ad7..0000000 --- a/src/test/java/cc/models/SeverityTest.java +++ /dev/null @@ -1,16 +0,0 @@ -package cc.models; - -import org.junit.Test; - -import static org.assertj.core.api.Assertions.assertThat; - -public class SeverityTest { - - @Test - public void has_order_of_importance() throws Exception { - assertThat(Severity.BLOCKER).isGreaterThan(Severity.CRITICAL); - assertThat(Severity.CRITICAL).isGreaterThan(Severity.MAJOR); - assertThat(Severity.MAJOR).isGreaterThan(Severity.MINOR); - assertThat(Severity.MINOR).isGreaterThan(Severity.INFO); - } -} \ No newline at end of file diff --git a/src/test/java/cc/report/JsonReportTest.java b/src/test/java/cc/report/JsonReportTest.java deleted file mode 100644 index 4dfdff3..0000000 --- a/src/test/java/cc/report/JsonReportTest.java +++ /dev/null @@ -1,74 +0,0 @@ -package cc.report; - -import cc.models.Severity; -import org.junit.Before; -import org.junit.Test; -import org.sonarsource.sonarlint.core.client.api.common.RuleDetails; -import org.sonarsource.sonarlint.core.tracking.Trackable; -import support.OutputHelper; -import support.fakes.FakeIssue; -import support.fakes.FakeRuleDetails; -import support.fakes.FakeTrackable; - -import java.util.Date; -import java.util.List; - -import static java.util.Arrays.asList; -import static org.assertj.core.api.Assertions.assertThat; - -public class JsonReportTest { - - OutputHelper output; - - @Before - public void setUp() throws Exception { - output = OutputHelper.setup(); - } - - @Test - public void serialize_issue() throws Exception { - executeReport("major", new FakeIssue("file.java", 0, 1)); - assertThat(output.stdout.toString()).contains("\"type\":\"issue\""); - } - - @Test - public void serialize_issue_relative_path() throws Exception { - executeReport("major", new FakeIssue("/tmp/dir/file.java", 0, 1)); - assertThat(output.stdout.toString()).contains("\"path\":\"dir/file.java\""); - } - - @Test - public void does_not_include_unknown_path() throws Exception { - executeReport("major", new FakeIssue(null, 1, 0)); - assertThat(output.stdout.toString()).doesNotContain("path"); - } - - @Test - public void does_not_report_issue_below_minimum_severity() throws Exception { - executeReport(Severity.INFO,null, new FakeIssue(null, 1, 0)); - assertThat(output.stdout.toString()).isEmpty(); - - executeReport(Severity.CRITICAL,"major", new FakeIssue(null, 1, 0)); - assertThat(output.stdout.toString()).isEmpty(); - - executeReport(Severity.CRITICAL,"critical", new FakeIssue(null, 1, 0)); - assertThat(output.stdout.toString()).contains("issue"); - } - - @Test - public void unknown_location_defaults_to_first_line() throws Exception { - executeReport("major", new FakeIssue("file.java", null, null)); - assertThat(output.stdout.toString()).contains("\"lines\":{\"begin\":1,\"end\":1}"); - assertThat(output.stderr.toString()).contains("File location was not provided, defaulting to line 1"); - } - - void executeReport(String ruleSeverity, FakeIssue issue) { - executeReport(Severity.MAJOR, ruleSeverity, issue); - } - - void executeReport(Severity minimumSeverity, String ruleSeverity, FakeIssue issue) { - RuleDetails ruleDetails = new FakeRuleDetails(ruleSeverity); - List trackables = asList(new FakeTrackable(issue)); - new JsonReport(minimumSeverity, "/tmp").execute("prj", new Date(0), trackables, null, _ruleKey -> ruleDetails); - } -} \ No newline at end of file diff --git a/src/test/java/integration/ConfigurationOptionsTest.java b/src/test/java/integration/ConfigurationOptionsTest.java deleted file mode 100644 index 2269599..0000000 --- a/src/test/java/integration/ConfigurationOptionsTest.java +++ /dev/null @@ -1,69 +0,0 @@ -package integration; - -import cc.App; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import org.sonarlint.cli.SonarProperties; -import support.SystemHelper; - -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; - -import static org.assertj.core.api.Assertions.assertThat; - -public class ConfigurationOptionsTest { - ByteArrayOutputStream stdout; - ByteArrayOutputStream stderr; - - SystemHelper system; - - @BeforeClass - public static void beforeAll() { - System.setProperty(SonarProperties.SONARLINT_HOME, "build"); - } - - @Before - public void setUp() throws Exception { - system = new SystemHelper(); - stdout = new ByteArrayOutputStream(); - stderr = new ByteArrayOutputStream(); - - System.setOut(new PrintStream(stdout)); - System.setErr(new PrintStream(stderr)); - - system.setProperty(SonarProperties.PROJECT_HOME, "fixtures/multiple_paths"); - } - - @Test - public void limit_path_included_within_analysis() throws Exception { - system.setProperty("config", "fixtures/multiple_paths/config.json"); - - App.execute(system); - - String output = stdout.toString(); - assertThat(output).contains("\"type\":\"issue\"", "src/included/java/pkg1/HasIssue.java"); - assertThat(output).doesNotContain("src/excluded/java/pkg1/HasIssue.java"); - } - - @Test - public void include_all_files_by_default() throws Exception { - App.execute(system); - - assertThat(stdout.toString()).contains( - "\"type\":\"issue\"", - "src/included/java/pkg1/HasIssue.java", - "src/excluded/java/pkg1/HasIssue.java" - ); - } - - @Test - public void capture_exceptions_and_log_to_stderr() throws Exception { - system.removeProperty(SonarProperties.PROJECT_HOME); - - App.execute(system); - - assertThat(system.exitCode).isEqualTo(1); - assertThat(stderr.toString()).contains("Can't find project home. System property not set: project.home"); - } -} \ No newline at end of file diff --git a/src/test/java/support/Factory.java b/src/test/java/support/Factory.java deleted file mode 100644 index e4e28cf..0000000 --- a/src/test/java/support/Factory.java +++ /dev/null @@ -1,19 +0,0 @@ -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/java/support/OutputHelper.java b/src/test/java/support/OutputHelper.java deleted file mode 100644 index ea5e7e3..0000000 --- a/src/test/java/support/OutputHelper.java +++ /dev/null @@ -1,23 +0,0 @@ -package support; - -import java.io.ByteArrayOutputStream; -import java.io.OutputStream; -import java.io.PrintStream; - -public class OutputHelper { - public final OutputStream stdout; - public final OutputStream stderr; - - public OutputHelper(OutputStream out, OutputStream err) { - this.stdout = out; - this.stderr = err; - } - - public static OutputHelper setup() { - OutputStream out = new ByteArrayOutputStream(); - OutputStream err = new ByteArrayOutputStream(); - System.setOut(new PrintStream(out)); - System.setErr(new PrintStream(err)); - return new OutputHelper(out, err); - } -} diff --git a/src/test/java/support/SystemHelper.java b/src/test/java/support/SystemHelper.java deleted file mode 100644 index 47102b6..0000000 --- a/src/test/java/support/SystemHelper.java +++ /dev/null @@ -1,38 +0,0 @@ -package support; - -import org.sonarlint.cli.util.System2; - -import java.util.Properties; - -public class SystemHelper extends System2 { - public int exitCode; - final Properties properties = new Properties(); - - @Override - public void exit(int exitCode) { - this.exitCode = exitCode; - } - - @Override - public Properties properties() { - return properties; - } - - @Override - public String property(String key) { - return properties.getProperty(key); - } - - @Override - public String getProperty(String key) { - return properties.getProperty(key); - } - - public void setProperty(String key, String value) { - properties.setProperty(key, value); - } - - public void removeProperty(String key) { - properties.remove(key); - } -} \ No newline at end of file diff --git a/src/test/java/support/fakes/FakeClientInputFile.java b/src/test/java/support/fakes/FakeClientInputFile.java deleted file mode 100644 index eb068e8..0000000 --- a/src/test/java/support/fakes/FakeClientInputFile.java +++ /dev/null @@ -1,48 +0,0 @@ -package support.fakes; - -import org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile; - -import javax.annotation.CheckForNull; -import java.io.IOException; -import java.io.InputStream; -import java.nio.charset.Charset; - -public class FakeClientInputFile implements ClientInputFile { - String path; - - public FakeClientInputFile(String path) { - this.path = path; - } - - @Override - public String getPath() { - return path; - } - - @CheckForNull - @Override - public boolean isTest() { - return false; - } - - @CheckForNull - @Override - public Charset getCharset() { - return null; - } - - @Override - public G getClientObject() { - return null; - } - - @Override - public InputStream inputStream() throws IOException { - return null; - } - - @Override - public String contents() throws IOException { - return null; - } -} diff --git a/src/test/java/support/fakes/FakeIssue.java b/src/test/java/support/fakes/FakeIssue.java deleted file mode 100644 index 1c25af1..0000000 --- a/src/test/java/support/fakes/FakeIssue.java +++ /dev/null @@ -1,82 +0,0 @@ -package support.fakes; - -import org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile; -import org.sonarsource.sonarlint.core.client.api.common.analysis.Issue; - -import javax.annotation.CheckForNull; -import java.util.List; - -public class FakeIssue implements Issue { - - String path = "/tmp/File.java"; - Integer startLine; - Integer endLine; - - public FakeIssue(String path, Integer startLine, Integer endLine) { - this.path = path; - this.startLine = startLine; - this.endLine = endLine; - } - - @Override - public String getSeverity() { - return null; - } - - @CheckForNull - @Override - public String getType() { - return null; - } - - @CheckForNull - @Override - public String getMessage() { - return null; - } - - @Override - public String getRuleKey() { - return null; - } - - @Override - public String getRuleName() { - return null; - } - - @CheckForNull - @Override - public Integer getStartLine() { - return startLine; - } - - @CheckForNull - @Override - public Integer getStartLineOffset() { - return null; - } - - @CheckForNull - @Override - public Integer getEndLine() { - return endLine; - } - - @CheckForNull - @Override - public Integer getEndLineOffset() { - return null; - } - - @Override - public List flows() { - return null; - } - - @CheckForNull - @Override - public ClientInputFile getInputFile() { - return new FakeClientInputFile(path); - } -} diff --git a/src/test/java/support/fakes/FakeRuleDetails.java b/src/test/java/support/fakes/FakeRuleDetails.java deleted file mode 100644 index 284eecc..0000000 --- a/src/test/java/support/fakes/FakeRuleDetails.java +++ /dev/null @@ -1,54 +0,0 @@ -package support.fakes; - -import org.sonarsource.sonarlint.core.client.api.common.RuleDetails; - -import javax.annotation.CheckForNull; - -public class FakeRuleDetails implements RuleDetails { - private String severity; - - public FakeRuleDetails(String severity) { - this.severity = severity; - } - - @Override - public String getKey() { - return null; - } - - @Override - public String getName() { - return null; - } - - @Override - public String getHtmlDescription() { - return null; - } - - @Override - public String getLanguage() { - return null; - } - - @Override - public String getSeverity() { - return severity; - } - - @CheckForNull - @Override - public String getType() { - return "BUG"; - } - - @Override - public String[] getTags() { - return new String[0]; - } - - @Override - public String getExtendedDescription() { - return null; - } -} diff --git a/src/test/java/support/fakes/FakeTrackable.java b/src/test/java/support/fakes/FakeTrackable.java deleted file mode 100644 index da441b7..0000000 --- a/src/test/java/support/fakes/FakeTrackable.java +++ /dev/null @@ -1,93 +0,0 @@ -package support.fakes; - -import org.sonarsource.sonarlint.core.client.api.common.analysis.Issue; -import org.sonarsource.sonarlint.core.tracking.TextRange; -import org.sonarsource.sonarlint.core.tracking.Trackable; - -import javax.annotation.CheckForNull; - -public class FakeTrackable implements Trackable{ - - private FakeIssue fakeIssue; - - public FakeTrackable(FakeIssue fakeIssue) { - this.fakeIssue = fakeIssue; - } - - @Override - public Issue getIssue() { - return fakeIssue; - } - - @Override - public String getRuleKey() { - return null; - } - - @Override - public String getRuleName() { - return null; - } - - @Override - public String getSeverity() { - return null; - } - - @Override - public String getMessage() { - return null; - } - - @CheckForNull - @Override - public String getType() { - return null; - } - - @CheckForNull - @Override - public Integer getLine() { - return null; - } - - @CheckForNull - @Override - public Integer getLineHash() { - return null; - } - - @CheckForNull - @Override - public TextRange getTextRange() { - return null; - } - - @CheckForNull - @Override - public Integer getTextRangeHash() { - return null; - } - - @CheckForNull - @Override - public Long getCreationDate() { - return null; - } - - @CheckForNull - @Override - public String getServerIssueKey() { - return null; - } - - @Override - public boolean isResolved() { - return false; - } - - @Override - public String getAssignee() { - return null; - } -} From 7b1c59ce97e86e98eeb41d80a4b55d93a3706279 Mon Sep 17 00:00:00 2001 From: Filipe Esperandio Date: Wed, 1 Nov 2017 17:21:01 -0200 Subject: [PATCH 2/3] Use sonar-wrapper lib --- bin/codeclimate-sonar | 7 ++++--- build.gradle | 6 ++---- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/bin/codeclimate-sonar b/bin/codeclimate-sonar index 7db17be..cfcb73c 100755 --- a/bin/codeclimate-sonar +++ b/bin/codeclimate-sonar @@ -1,16 +1,17 @@ #!/usr/bin/env sh BUILD_DIR=$(dirname $0) -APP=$(find ${BUILD_DIR}/libs -name "codeclimate*.jar" | tr "\n" ":") -LIBS=$(find ${BUILD_DIR}/libs -name "*.jar" | tr "\n" ":") +APP=$(find ${BUILD_DIR}/libs -name "codeclimate-sonar-java.jar" | head -n1) +WRAPPER=$(find ${BUILD_DIR}/libs -name "sonar-wrapper*.jar" | head -n1) CORE=$(find ${BUILD_DIR}/libs -name "sonarlint-core*.jar" -or -name "sonarlint-client-api*.jar" | tr "\n" ":") +LIBS=$(find ${BUILD_DIR}/libs -name "*.jar" | tr "\n" ":") CODE_DIR=$1; shift CONFIG_FILE=$1; shift java \ -noverify \ - -cp ${APP}:${CORE}:${LIBS} \ + -cp ${APP}:${WRAPPER}:${CORE}:${LIBS} \ -Djava.awt.headless=true \ -Dsonarlint.home="${BUILD_DIR}" \ -Dproject.home="${CODE_DIR}" \ diff --git a/build.gradle b/build.gradle index 0ae72ea..e3636f3 100644 --- a/build.gradle +++ b/build.gradle @@ -2,6 +2,7 @@ apply plugin: "java" repositories { jcenter() + maven { url 'https://jitpack.io' } } task copyLibs(type: Copy) { @@ -32,10 +33,7 @@ build.dependsOn(infra) test.dependsOn(infra) dependencies { - compile("org.sonarsource.sonarlint.core:sonarlint-core:2.17.0.899") - compile("org.sonarsource.sonarlint.core:sonarlint-client-api:2.17.0.899") - compile("org.sonarsource.sonarlint:sonarlint-cli:2.1.0.566") - compile("com.google.code.gson:gson:2.8.2") + compile("com.github.codeclimate:sonar-wrapper:master-SNAPSHOT") // Plugins compile("org.sonarsource.java:sonar-java-plugin:4.14.0.11784") From 92eeeb7f73d8e5c9afb291cb17f89c8c8204e0b1 Mon Sep 17 00:00:00 2001 From: Filipe Esperandio Date: Thu, 2 Nov 2017 10:47:46 -0200 Subject: [PATCH 3/3] Kick build