Browse Source

SQSCANNER-61 Consider 'project.settings' if set using SONARQUBE_SCANNER_PARAMS

tags/4.1.0.1829
Julien HENRY 4 years ago
parent
commit
dae4decaa1

+ 13
- 0
it/src/test/java/com/sonarsource/scanner/it/ScannerTest.java View File

@@ -25,6 +25,7 @@ import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.commons.lang.StringEscapeUtils;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
@@ -209,4 +210,16 @@ public class ScannerTest extends ScannerTestCase {
assertThat(getComponent("sample-with-custom-settings-path").getName()).isEqualTo("Test with custom settings location");
}

// SQSCANNER-61
@Test
public void should_override_project_settings_path_using_env_variable() {
File projectHome = new File("projects/override-project-settings-path");
SonarScanner build = newScanner(projectHome)
.setEnvironmentVariable("SONARQUBE_SCANNER_PARAMS", "{"
+ "\"project.settings\" : \"" + StringEscapeUtils.escapeJavaScript(new File(projectHome, "conf/sq-project.properties").getAbsolutePath()) + "\"}");
orchestrator.executeBuild(build);

assertThat(getComponent("sample-with-custom-settings-path").getName()).isEqualTo("Test with custom settings location");
}

}

+ 14
- 7
src/main/java/org/sonarsource/scanner/cli/Conf.java View File

@@ -78,7 +78,13 @@ class Conf {
}

private Properties loadGlobalProperties() {
Path settingsFile = locatePropertiesFile(cli.properties(), SCANNER_HOME, "conf/sonar-scanner.properties",
Properties knownPropsAtThatPoint = new Properties();

knownPropsAtThatPoint.putAll(System.getProperties());
knownPropsAtThatPoint.putAll(loadEnvironmentProperties());
knownPropsAtThatPoint.putAll(cli.properties());

Path settingsFile = locatePropertiesFile(knownPropsAtThatPoint, SCANNER_HOME, "conf/sonar-scanner.properties",
SCANNER_SETTINGS);
if (settingsFile != null && Files.isRegularFile(settingsFile)) {
logger.info("Scanner configuration file: " + settingsFile);
@@ -90,13 +96,14 @@ class Conf {

private Properties loadProjectProperties() {
Properties rootProps = new Properties();
Properties knownProps = new Properties();
Properties knownPropsAtThatPoint = new Properties();

knownProps.putAll(System.getProperties());
knownProps.putAll(cli.properties());
knownPropsAtThatPoint.putAll(System.getProperties());
knownPropsAtThatPoint.putAll(loadEnvironmentProperties());
knownPropsAtThatPoint.putAll(cli.properties());

Path defaultRootSettingsFile = getRootProjectBaseDir(knownProps).resolve(SONAR_PROJECT_PROPERTIES_FILENAME);
Path rootSettingsFile = locatePropertiesFile(defaultRootSettingsFile, knownProps, PROJECT_SETTINGS);
Path defaultRootSettingsFile = getRootProjectBaseDir(knownPropsAtThatPoint).resolve(SONAR_PROJECT_PROPERTIES_FILENAME);
Path rootSettingsFile = locatePropertiesFile(defaultRootSettingsFile, knownPropsAtThatPoint, PROJECT_SETTINGS);
if (rootSettingsFile != null && Files.isRegularFile(rootSettingsFile)) {
logger.info("Project root configuration file: " + rootSettingsFile);
rootProps.putAll(toProperties(rootSettingsFile));
@@ -110,7 +117,7 @@ class Conf {
// root config file
projectProps.putAll(rootProps);

rootProps.putAll(knownProps);
rootProps.putAll(knownPropsAtThatPoint);
rootProps.setProperty(PROPERTY_PROJECT_BASEDIR, getRootProjectBaseDir(rootProps).toString());

// projectProps will be overridden by any properties found in child

+ 15
- 0
src/test/java/org/sonarsource/scanner/cli/ConfTest.java View File

@@ -316,4 +316,19 @@ public class ConfTest {
properties = conf.properties();
assertThat(properties.get("sonar.prop")).isEqualTo("expected");
}

// SQSCANNER-61
@Test
public void should_load_project_settings_using_env() throws Exception {
Path home = Paths.get(getClass().getResource("ConfTest/shouldOverrideProjectSettingsPath/").toURI());
args.setProperty("project.home", home.toAbsolutePath().toString());

Properties properties = conf.properties();
assertThat(properties.get("sonar.prop")).isEqualTo("default");

env.put("SONARQUBE_SCANNER_PARAMS", "{\"project.settings\" : \"" + home.resolve("conf/sq-project.properties").toAbsolutePath().toString() + "\"}");

properties = conf.properties();
assertThat(properties.get("sonar.prop")).isEqualTo("expected");
}
}

Loading…
Cancel
Save