-
Notifications
You must be signed in to change notification settings - Fork 6
Allow configuration overrides #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
e525a4d
0bef539
292ba92
01c8e65
b529d5d
1b6a976
e445863
410439b
5b8246c
d3eeb84
e3b1992
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
public class Test { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,51 @@ | ||
package cc; | ||
|
||
import cc.files.Finder; | ||
import org.sonarlint.cli.CustomMain; | ||
import org.sonarlint.cli.InputFileFinder; | ||
import org.sonarlint.cli.Options; | ||
import org.sonarlint.cli.analysis.SonarLintFactory; | ||
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(args, System2.INSTANCE); | ||
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); | ||
ConfigurationReader reader = new ConfigurationReader(); | ||
SonarLintFactory sonarLintFactory = new SonarLintFactory(reader); | ||
Path projectHome = getProjectHome(system); | ||
|
||
int exitCode = new CustomMain(new Options(), sonarLintFactory, reportFactory, fileFinder, projectHome).run(); | ||
system.exit(exitCode); | ||
} catch (Exception e) { | ||
e.printStackTrace(System.err); | ||
system.exit(ERROR); | ||
} | ||
} | ||
|
||
public static void execute(String[] args, System2 system) { | ||
CustomMain.execute(args, system); | ||
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); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package cc.report; | ||
|
||
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 { | ||
public ReportFactory(Charset charset) { | ||
super(charset); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Think there's any worth adding tests for this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am actually fine with the |
||
|
||
@Override | ||
public List<Reporter> createReporters(Path basePath) { | ||
return Arrays.asList(new JsonReport(basePath.toString())); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,86 +19,19 @@ | |
*/ | ||
package org.sonarlint.cli; | ||
|
||
import cc.Config; | ||
import cc.files.Finder; | ||
import cc.JsonReport; | ||
import org.sonarlint.cli.analysis.SonarLintFactory; | ||
import org.sonarlint.cli.config.ConfigurationReader; | ||
import org.sonarlint.cli.report.ReportFactory; | ||
import org.sonarlint.cli.report.Reporter; | ||
import org.sonarlint.cli.util.Logger; | ||
import org.sonarlint.cli.util.System2; | ||
|
||
import java.nio.charset.Charset; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.text.ParseException; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static org.sonarlint.cli.SonarProperties.PROJECT_HOME; | ||
|
||
|
||
public class CustomMain extends org.sonarlint.cli.Main { | ||
static final int SUCCESS = 0; | ||
static final int ERROR = 1; | ||
private static final Logger LOGGER = Logger.get(); | ||
|
||
public CustomMain(Options opts, SonarLintFactory sonarLintFactory, ReportFactory reportFactory, InputFileFinder fileFinder, Path projectHome) { | ||
super(opts, sonarLintFactory, reportFactory, fileFinder, projectHome); | ||
} | ||
|
||
public static void execute(String[] args, System2 system) { | ||
Options parsedOpts; | ||
try { | ||
parsedOpts = Options.parse(args); | ||
} catch (ParseException e) { | ||
LOGGER.error("Error parsing arguments: " + e.getMessage(), e); | ||
Options.printUsage(); | ||
system.exit(ERROR); | ||
return; | ||
} | ||
|
||
Charset charset; | ||
try { | ||
if (parsedOpts.charset() != null) { | ||
charset = Charset.forName(parsedOpts.charset()); | ||
} else { | ||
charset = Charset.defaultCharset(); | ||
} | ||
} catch (Exception e) { | ||
LOGGER.error("Error creating charset: " + parsedOpts.charset(), e); | ||
system.exit(ERROR); | ||
return; | ||
} | ||
|
||
Config config = Config.from(system.getProperty("config")); | ||
InputFileFinder fileFinder = new Finder(config.includePaths, parsedOpts.tests(), charset); | ||
ReportFactory reportFactory = new CustomReportFactory(charset); | ||
ConfigurationReader reader = new ConfigurationReader(); | ||
SonarLintFactory sonarLintFactory = new SonarLintFactory(reader); | ||
|
||
int ret = new CustomMain(parsedOpts, sonarLintFactory, reportFactory, fileFinder, getProjectHome(system)).run(); | ||
system.exit(ret); | ||
return; | ||
} | ||
|
||
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); | ||
} | ||
|
||
private static class CustomReportFactory extends ReportFactory { | ||
public CustomReportFactory(Charset charset) { | ||
super(charset); | ||
} | ||
|
||
@Override | ||
public List<Reporter> createReporters(Path basePath) { | ||
return Arrays.asList(new JsonReport(basePath.toString())); | ||
} | ||
@Override | ||
public int run() { | ||
return super.run(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it necessary to override this function if we're just delegating to the parent anyways? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needed to change the accessor to be |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,19 +2,45 @@ | |
|
||
import org.junit.Test; | ||
|
||
import java.nio.charset.Charset; | ||
|
||
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.includePaths).containsOnly("Main.java", "src/included/"); | ||
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.includePaths).containsOnly(""); | ||
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_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/**}"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we also add a test case for when |
||
} | ||
|
||
@Test | ||
public void null_tests_patterns_does_not_cause_error() throws Exception { | ||
Config config = Config.gson().fromJson("{\"config\":{}}", Config.class); | ||
assertThat(config.getTestsPatterns()).isNull(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(extra line)