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

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

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

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

assertThat(logs).contains(expectedLog); 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 @Test
public void should_fail_if_unable_to_connect() { public void should_fail_if_unable_to_connect() {
SonarScanner build = newScanner(new File("projects/java-sample")) SonarScanner build = newScanner(new File("projects/java-sample"))

+ 9
- 4
pom.xml View File

<parent> <parent>
<groupId>org.sonarsource.parent</groupId> <groupId>org.sonarsource.parent</groupId>
<artifactId>parent</artifactId> <artifactId>parent</artifactId>
<version>31</version>
<version>36</version>
</parent> </parent>


<groupId>org.sonarsource.scanner.cli</groupId> <groupId>org.sonarsource.scanner.cli</groupId>
<dependency> <dependency>
<groupId>org.sonarsource.scanner.api</groupId> <groupId>org.sonarsource.scanner.api</groupId>
<artifactId>sonar-scanner-api</artifactId> <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>
<dependency> <dependency>
<groupId>com.google.code.findbugs</groupId> <groupId>com.google.code.findbugs</groupId>
<configuration> <configuration>
<rules> <rules>
<requireFilesSize> <requireFilesSize>
<minsize>500000</minsize>
<maxsize>510000</maxsize>
<minsize>510000</minsize>
<maxsize>530000</maxsize>
<files> <files>
<file>${project.build.directory}/sonar-scanner-${project.version}.zip</file> <file>${project.build.directory}/sonar-scanner-${project.version}.zip</file>
</files> </files>

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

import java.nio.file.Paths; import java.nio.file.Paths;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Properties; 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 { class Conf {
private static final String SCANNER_HOME = "scanner.home"; private static final String SCANNER_HOME = "scanner.home";
private static final String SCANNER_SETTINGS = "scanner.settings"; private static final String SCANNER_SETTINGS = "scanner.settings";
private static final String PROPERTY_PROJECT_BASEDIR = "sonar.projectBaseDir"; private static final String PROPERTY_PROJECT_BASEDIR = "sonar.projectBaseDir";
private static final String PROPERTY_PROJECT_CONFIG_FILE = "sonar.projectConfigFile"; 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 SONAR_PROJECT_PROPERTIES_FILENAME = "sonar-project.properties";
private static final String SONARQUBE_SCANNER_PARAMS = "SONARQUBE_SCANNER_PARAMS";


private final Cli cli; private final Cli cli;
private final Logs logger; 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.cli = cli;
this.logger = logger; this.logger = logger;
this.env = env;
} }


Properties properties() throws IOException { Properties properties() throws IOException {
result.putAll(loadGlobalProperties()); result.putAll(loadGlobalProperties());
result.putAll(loadProjectProperties()); result.putAll(loadProjectProperties());
result.putAll(System.getProperties()); result.putAll(System.getProperties());
result.putAll(loadEnvironmentProperties());
result.putAll(cli.properties()); result.putAll(cli.properties());
// root project base directory must be present and be absolute // root project base directory must be present and be absolute
result.setProperty(PROPERTY_PROJECT_BASEDIR, getRootProjectBaseDir(result).toString()); result.setProperty(PROPERTY_PROJECT_BASEDIR, getRootProjectBaseDir(result).toString());
return result; 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 { 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)) { if (settingsFile != null && Files.isRegularFile(settingsFile)) {
logger.info("Scanner configuration file: " + settingsFile); logger.info("Scanner configuration file: " + settingsFile);
return toProperties(settingsFile); return toProperties(settingsFile);


Properties projectProps = new Properties(); 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); projectProps.putAll(rootProps);


rootProps.putAll(knownProps); rootProps.putAll(knownProps);
rootProps.setProperty(PROPERTY_PROJECT_BASEDIR, getRootProjectBaseDir(rootProps).toString()); 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, ""); loadModulesProperties(rootProps, projectProps, "");
return projectProps; return projectProps;
} }


private static void setModuleBaseDir(Path absoluteBaseDir, Properties childProps, String moduleId) { private static void setModuleBaseDir(Path absoluteBaseDir, Properties childProps, String moduleId) {
if (!Files.isDirectory(absoluteBaseDir)) { 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()); childProps.put(PROPERTY_PROJECT_BASEDIR, absoluteBaseDir.toString());
} }
return moduleProps; 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; Path settingsFile = null;
String scannerHome = props.getProperty(homeKey, ""); String scannerHome = props.getProperty(homeKey, "");
if (!"".equals(scannerHome)) { if (!"".equals(scannerHome)) {
} }


protected void loadModulePropsFile(Path parentAbsoluteBaseDir, Properties moduleProps, String moduleId) { 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)) { if (Files.isRegularFile(propertyFile)) {
moduleProps.putAll(toProperties(propertyFile)); moduleProps.putAll(toProperties(propertyFile));
Path absoluteBaseDir; Path absoluteBaseDir;
if (moduleProps.containsKey(PROPERTY_PROJECT_BASEDIR)) { 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 { } else {
absoluteBaseDir = propertyFile.getParent(); absoluteBaseDir = propertyFile.getParent();
} }
setModuleBaseDir(absoluteBaseDir, moduleProps, moduleId); setModuleBaseDir(absoluteBaseDir, moduleProps, moduleId);
} else { } 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);
} }
} }




moduleProps.putAll(toProperties(propertyFile)); moduleProps.putAll(toProperties(propertyFile));
if (moduleProps.containsKey(PROPERTY_PROJECT_BASEDIR)) { 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); 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) { protected static Path getAbsolutePath(String path, Path baseDir) {
Path propertyFile = Paths.get(path.trim()); Path propertyFile = Paths.get(path.trim());
} }


/** /**
* 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) { static String[] getListFromProperty(Properties properties, String key) {

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

Logs logs = new Logs(System.out, System.err); Logs logs = new Logs(System.out, System.err);
Exit exit = new Exit(); Exit exit = new Exit();
Cli cli = new Cli(exit, logs).parse(args); 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(); main.execute();
} }



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

*/ */
package org.sonarsource.scanner.cli; 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.io.IOException;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties; import java.util.Properties;

import org.apache.commons.lang.SystemUtils; import org.apache.commons.lang.SystemUtils;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.TemporaryFolder; 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 { public class ConfTest {


@Rule @Rule
public TemporaryFolder temp = new TemporaryFolder(); public TemporaryFolder temp = new TemporaryFolder();


Map<String, String> env = new HashMap<>();
Properties args = new Properties(); Properties args = new Properties();
Logs logs = new Logs(System.out, System.err); Logs logs = new Logs(System.out, System.err);
Cli cli = mock(Cli.class); Cli cli = mock(Cli.class);
Conf conf = new Conf(cli, logs);
Conf conf = new Conf(cli, logs, env);


@Before @Before
public void initConf() { public void initConf() {
env.clear();
when(cli.properties()).thenReturn(args); when(cli.properties()).thenReturn(args);
} }


assertThat(properties.getProperty("sonar.projectBaseDir")).isEqualTo(projectHome.toString()); 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 @Test
public void shouldSupportDeepModuleConfigurationInRoot() throws Exception { public void shouldSupportDeepModuleConfigurationInRoot() throws Exception {
Path projectHome = Paths.get(getClass().getResource("ConfTest/shouldSupportDeepModuleConfigurationInRoot/project").toURI()); Path projectHome = Paths.get(getClass().getResource("ConfTest/shouldSupportDeepModuleConfigurationInRoot/project").toURI());

Loading…
Cancel
Save