Browse Source

SQSCANNER-26 Support SONARQUBE_SCANNER_PARAMS and sonar.scanner.skip

tags/2.7
Duarte Meneses 7 years ago
parent
commit
d1537d139b

+ 0
- 0
it/projects/java-sample-no-properties/.sonar/.sonar_lock View File


+ 5
- 0
it/projects/java-sample-no-properties/.sonar/report-task.txt View File

@@ -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

+ 9
- 0
it/projects/java-sample-no-properties/src/basic/Hello.java View File

@@ -0,0 +1,9 @@
package basic;

public class Hello {

public void hello() {
int i=356;
if (true) i=5658;
}
}

+ 8
- 0
it/projects/java-sample-no-properties/src/basic/World.java View File

@@ -0,0 +1,8 @@
package basic;

public final class World {

public void world() {
System.out.println("hello world");
}
}

+ 14
- 0
it/src/test/java/com/sonar/runner/it/JavaTest.java View File

@@ -211,6 +211,20 @@ public class JavaTest extends ScannerTestCase {
assertThat(logs).contains(expectedLog);
}

@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"))

+ 9
- 4
pom.xml View File

@@ -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>

+ 59
- 13
src/main/java/org/sonarsource/scanner/cli/Conf.java View File

@@ -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) {

+ 1
- 1
src/main/java/org/sonarsource/scanner/cli/Main.java View File

@@ -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();
}


+ 22
- 6
src/test/java/org/sonarsource/scanner/cli/ConfTest.java View File

@@ -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);
}

@@ -115,6 +120,17 @@ public class ConfTest {
assertThat(properties.getProperty("sonar.projectBaseDir")).isEqualTo(projectHome.toString());
}

@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());

Loading…
Cancel
Save