diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2016-08-03 13:53:23 +0200 |
---|---|---|
committer | Duarte Meneses <duarte.meneses@sonarsource.com> | 2016-08-03 13:53:23 +0200 |
commit | d1537d139b0034ac151969f221c98b2e57701e95 (patch) | |
tree | 72a1f611edc6cb9b40f1d237edc9820ae11479e0 | |
parent | cc72c54ef3fd4b63751e65862fad250795accc04 (diff) | |
download | sonar-scanner-cli-d1537d139b0034ac151969f221c98b2e57701e95.tar.gz sonar-scanner-cli-d1537d139b0034ac151969f221c98b2e57701e95.zip |
SQSCANNER-26 Support SONARQUBE_SCANNER_PARAMS and sonar.scanner.skip
9 files changed, 127 insertions, 24 deletions
diff --git a/it/projects/java-sample-no-properties/.sonar/.sonar_lock b/it/projects/java-sample-no-properties/.sonar/.sonar_lock new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/it/projects/java-sample-no-properties/.sonar/.sonar_lock diff --git a/it/projects/java-sample-no-properties/.sonar/report-task.txt b/it/projects/java-sample-no-properties/.sonar/report-task.txt new file mode 100644 index 0000000..c8e06cf --- /dev/null +++ b/it/projects/java-sample-no-properties/.sonar/report-task.txt @@ -0,0 +1,5 @@ +projectKey=java:sample +serverUrl=http://localhost:33151 +dashboardUrl=http://localhost:33151/dashboard/index/java:sample +ceTaskId=AVZQO0KqHsi6TBOtG5xl +ceTaskUrl=http://localhost:33151/api/ce/task?id=AVZQO0KqHsi6TBOtG5xl diff --git a/it/projects/java-sample-no-properties/src/basic/Hello.java b/it/projects/java-sample-no-properties/src/basic/Hello.java new file mode 100644 index 0000000..b9db5a0 --- /dev/null +++ b/it/projects/java-sample-no-properties/src/basic/Hello.java @@ -0,0 +1,9 @@ +package basic; + +public class Hello { + + public void hello() { + int i=356; + if (true) i=5658; + } +} diff --git a/it/projects/java-sample-no-properties/src/basic/World.java b/it/projects/java-sample-no-properties/src/basic/World.java new file mode 100644 index 0000000..c65d91c --- /dev/null +++ b/it/projects/java-sample-no-properties/src/basic/World.java @@ -0,0 +1,8 @@ +package basic; + +public final class World { + + public void world() { + System.out.println("hello world"); + } +} diff --git a/it/src/test/java/com/sonar/runner/it/JavaTest.java b/it/src/test/java/com/sonar/runner/it/JavaTest.java index 7209ea9..c93e3cc 100644 --- a/it/src/test/java/com/sonar/runner/it/JavaTest.java +++ b/it/src/test/java/com/sonar/runner/it/JavaTest.java @@ -212,6 +212,20 @@ public class JavaTest extends ScannerTestCase { } @Test + public void should_use_environment_props() { + SonarScanner build = newScanner(new File("projects/java-sample-no-properties")) + .setEnvironmentVariable("SONARQUBE_SCANNER_PARAMS", "{" + + "\"sonar.projectKey\" : \"java:sample\"," + + "\"sonar.projectName\" : \"Java Sample, with comma\"," + + "\"sonar.projectDescription\" : \"This is a Java sample\"," + + "\"sonar.projectVersion\" : \"1.2.3\"," + + "\"sonar.sources\" : \"src\" }"); + String logs = orchestrator.executeBuild(build).getLogs(); + System.out.println(logs); + + } + + @Test public void should_fail_if_unable_to_connect() { SonarScanner build = newScanner(new File("projects/java-sample")) .setProperty("sonar.host.url", "http://foo"); @@ -3,7 +3,7 @@ <parent> <groupId>org.sonarsource.parent</groupId> <artifactId>parent</artifactId> - <version>31</version> + <version>36</version> </parent> <groupId>org.sonarsource.scanner.cli</groupId> @@ -50,7 +50,12 @@ <dependency> <groupId>org.sonarsource.scanner.api</groupId> <artifactId>sonar-scanner-api</artifactId> - <version>2.6</version> + <version>2.7-build634</version> + </dependency> + <dependency> + <groupId>com.eclipsesource.minimal-json</groupId> + <artifactId>minimal-json</artifactId> + <version>0.9.4</version> </dependency> <dependency> <groupId>com.google.code.findbugs</groupId> @@ -164,8 +169,8 @@ <configuration> <rules> <requireFilesSize> - <minsize>500000</minsize> - <maxsize>510000</maxsize> + <minsize>510000</minsize> + <maxsize>530000</maxsize> <files> <file>${project.build.directory}/sonar-scanner-${project.version}.zip</file> </files> diff --git a/src/main/java/org/sonarsource/scanner/cli/Conf.java b/src/main/java/org/sonarsource/scanner/cli/Conf.java index cf2b37f..7907030 100644 --- a/src/main/java/org/sonarsource/scanner/cli/Conf.java +++ b/src/main/java/org/sonarsource/scanner/cli/Conf.java @@ -27,10 +27,16 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.text.MessageFormat; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Properties; +import com.eclipsesource.json.Json; +import com.eclipsesource.json.JsonObject; +import com.eclipsesource.json.JsonObject.Member; +import com.eclipsesource.json.JsonValue; + class Conf { private static final String SCANNER_HOME = "scanner.home"; private static final String SCANNER_SETTINGS = "scanner.settings"; @@ -40,13 +46,16 @@ class Conf { private static final String PROPERTY_PROJECT_BASEDIR = "sonar.projectBaseDir"; private static final String PROPERTY_PROJECT_CONFIG_FILE = "sonar.projectConfigFile"; private static final String SONAR_PROJECT_PROPERTIES_FILENAME = "sonar-project.properties"; + private static final String SONARQUBE_SCANNER_PARAMS = "SONARQUBE_SCANNER_PARAMS"; private final Cli cli; private final Logs logger; + private final Map<String, String> env; - Conf(Cli cli, Logs logger) { + Conf(Cli cli, Logs logger, Map<String, String> env) { this.cli = cli; this.logger = logger; + this.env = env; } Properties properties() throws IOException { @@ -54,6 +63,7 @@ class Conf { result.putAll(loadGlobalProperties()); result.putAll(loadProjectProperties()); result.putAll(System.getProperties()); + result.putAll(loadEnvironmentProperties()); result.putAll(cli.properties()); // root project base directory must be present and be absolute result.setProperty(PROPERTY_PROJECT_BASEDIR, getRootProjectBaseDir(result).toString()); @@ -61,8 +71,33 @@ class Conf { return result; } + private Properties loadEnvironmentProperties() { + Properties props = new Properties(); + + String scannerParams = env.get(SONARQUBE_SCANNER_PARAMS); + if (scannerParams != null) { + try { + + JsonValue jsonValue = Json.parse(scannerParams); + JsonObject jsonObject = jsonValue.asObject(); + Iterator<Member> it = jsonObject.iterator(); + + while (it.hasNext()) { + Member member = it.next(); + String key = member.getName(); + String value = member.getValue().asString(); + props.put(key, value); + } + } catch (Exception e) { + throw new IllegalStateException("Failed to parse JSON in SONARQUBE_SCANNER_PARAMS environment variable", e); + } + } + return props; + } + private Properties loadGlobalProperties() throws IOException { - Path settingsFile = locatePropertiesFile(cli.properties(), SCANNER_HOME, "conf/sonar-scanner.properties", SCANNER_SETTINGS); + Path settingsFile = locatePropertiesFile(cli.properties(), SCANNER_HOME, "conf/sonar-scanner.properties", + SCANNER_SETTINGS); if (settingsFile != null && Files.isRegularFile(settingsFile)) { logger.info("Scanner configuration file: " + settingsFile); return toProperties(settingsFile); @@ -89,13 +124,15 @@ class Conf { Properties projectProps = new Properties(); - // include already root base directory and eventually props loaded from root config file + // include already root base directory and eventually props loaded from + // root config file projectProps.putAll(rootProps); rootProps.putAll(knownProps); rootProps.setProperty(PROPERTY_PROJECT_BASEDIR, getRootProjectBaseDir(rootProps).toString()); - // projectProps will be overridden by any properties found in child project settings + // projectProps will be overridden by any properties found in child + // project settings loadModulesProperties(rootProps, projectProps, ""); return projectProps; } @@ -164,7 +201,8 @@ class Conf { private static void setModuleBaseDir(Path absoluteBaseDir, Properties childProps, String moduleId) { if (!Files.isDirectory(absoluteBaseDir)) { - throw new IllegalStateException(MessageFormat.format("The base directory of the module ''{0}'' does not exist: {1}", moduleId, absoluteBaseDir)); + throw new IllegalStateException(MessageFormat + .format("The base directory of the module ''{0}'' does not exist: {1}", moduleId, absoluteBaseDir)); } childProps.put(PROPERTY_PROJECT_BASEDIR, absoluteBaseDir.toString()); } @@ -182,7 +220,8 @@ class Conf { return moduleProps; } - private static Path locatePropertiesFile(Properties props, String homeKey, String relativePathFromHome, String settingsKey) { + private static Path locatePropertiesFile(Properties props, String homeKey, String relativePathFromHome, + String settingsKey) { Path settingsFile = null; String scannerHome = props.getProperty(homeKey, ""); if (!"".equals(scannerHome)) { @@ -222,18 +261,21 @@ class Conf { } protected void loadModulePropsFile(Path parentAbsoluteBaseDir, Properties moduleProps, String moduleId) { - Path propertyFile = getAbsolutePath(moduleProps.getProperty(PROPERTY_PROJECT_CONFIG_FILE), parentAbsoluteBaseDir); + Path propertyFile = getAbsolutePath(moduleProps.getProperty(PROPERTY_PROJECT_CONFIG_FILE), + parentAbsoluteBaseDir); if (Files.isRegularFile(propertyFile)) { moduleProps.putAll(toProperties(propertyFile)); Path absoluteBaseDir; if (moduleProps.containsKey(PROPERTY_PROJECT_BASEDIR)) { - absoluteBaseDir = getAbsolutePath(moduleProps.getProperty(PROPERTY_PROJECT_BASEDIR), propertyFile.getParent()); + absoluteBaseDir = getAbsolutePath(moduleProps.getProperty(PROPERTY_PROJECT_BASEDIR), + propertyFile.getParent()); } else { absoluteBaseDir = propertyFile.getParent(); } setModuleBaseDir(absoluteBaseDir, moduleProps, moduleId); } else { - throw new IllegalStateException("The properties file of the module '" + moduleId + "' does not exist: " + propertyFile); + throw new IllegalStateException( + "The properties file of the module '" + moduleId + "' does not exist: " + propertyFile); } } @@ -245,13 +287,15 @@ class Conf { moduleProps.putAll(toProperties(propertyFile)); if (moduleProps.containsKey(PROPERTY_PROJECT_BASEDIR)) { - Path overwrittenBaseDir = getAbsolutePath(moduleProps.getProperty(PROPERTY_PROJECT_BASEDIR), propertyFile.getParent()); + Path overwrittenBaseDir = getAbsolutePath(moduleProps.getProperty(PROPERTY_PROJECT_BASEDIR), + propertyFile.getParent()); setModuleBaseDir(overwrittenBaseDir, moduleProps, moduleId); } } /** - * Returns the file denoted by the given path, may this path be relative to "baseDir" or absolute. + * Returns the file denoted by the given path, may this path be relative to + * "baseDir" or absolute. */ protected static Path getAbsolutePath(String path, Path baseDir) { Path propertyFile = Paths.get(path.trim()); @@ -262,9 +306,11 @@ class Conf { } /** - * Transforms a comma-separated list String property in to a array of trimmed strings. + * Transforms a comma-separated list String property in to a array of + * trimmed strings. * - * This works even if they are separated by whitespace characters (space char, EOL, ...) + * This works even if they are separated by whitespace characters (space + * char, EOL, ...) * */ static String[] getListFromProperty(Properties properties, String key) { diff --git a/src/main/java/org/sonarsource/scanner/cli/Main.java b/src/main/java/org/sonarsource/scanner/cli/Main.java index f0b76bf..19b953c 100644 --- a/src/main/java/org/sonarsource/scanner/cli/Main.java +++ b/src/main/java/org/sonarsource/scanner/cli/Main.java @@ -56,7 +56,7 @@ public class Main { Logs logs = new Logs(System.out, System.err); Exit exit = new Exit(); Cli cli = new Cli(exit, logs).parse(args); - Main main = new Main(exit, cli, new Conf(cli, logs), new ScannerFactory(logs), logs); + Main main = new Main(exit, cli, new Conf(cli, logs, System.getenv()), new ScannerFactory(logs), logs); main.execute(); } diff --git a/src/test/java/org/sonarsource/scanner/cli/ConfTest.java b/src/test/java/org/sonarsource/scanner/cli/ConfTest.java index 3450e88..506e01d 100644 --- a/src/test/java/org/sonarsource/scanner/cli/ConfTest.java +++ b/src/test/java/org/sonarsource/scanner/cli/ConfTest.java @@ -19,35 +19,40 @@ */ package org.sonarsource.scanner.cli; +import static org.fest.assertions.Assertions.assertThat; +import static org.junit.Assume.assumeTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + import java.io.IOException; import java.net.URISyntaxException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.HashMap; +import java.util.Map; import java.util.Properties; + import org.apache.commons.lang.SystemUtils; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; -import static org.fest.assertions.Assertions.assertThat; -import static org.junit.Assume.assumeTrue; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - public class ConfTest { @Rule public TemporaryFolder temp = new TemporaryFolder(); + Map<String, String> env = new HashMap<>(); Properties args = new Properties(); Logs logs = new Logs(System.out, System.err); Cli cli = mock(Cli.class); - Conf conf = new Conf(cli, logs); + Conf conf = new Conf(cli, logs, env); @Before public void initConf() { + env.clear(); when(cli.properties()).thenReturn(args); } @@ -116,6 +121,17 @@ public class ConfTest { } @Test + public void shouldLoadEnvironmentProperties() throws IOException { + env.put("SONARQUBE_SCANNER_PARAMS", "{\"sonar.key1\" : \"v1\", \"sonar.key2\" : \"v2\"}"); + args.put("sonar.key2", "v3"); + + Properties props = conf.properties(); + + assertThat(props.getProperty("sonar.key1")).isEqualTo("v1"); + assertThat(props.getProperty("sonar.key2")).isEqualTo("v3"); + } + + @Test public void shouldSupportDeepModuleConfigurationInRoot() throws Exception { Path projectHome = Paths.get(getClass().getResource("ConfTest/shouldSupportDeepModuleConfigurationInRoot/project").toURI()); args.setProperty("project.home", projectHome.toAbsolutePath().toString()); |