]> source.dussan.org Git - sonar-scanner-cli.git/commitdiff
SQSCANNER-24 Always consider project.settings, if set
authorJulien HENRY <julien.henry@sonarsource.com>
Tue, 3 Sep 2019 13:36:13 +0000 (15:36 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Thu, 5 Sep 2019 08:03:19 +0000 (10:03 +0200)
it/projects/override-project-settings-path/conf/sq-project.properties [new file with mode: 0644]
it/projects/override-project-settings-path/sonar-project.properties [new file with mode: 0644]
it/projects/override-project-settings-path/src/basic/Hello.js [new file with mode: 0644]
it/projects/override-project-settings-path/src/basic/World.js [new file with mode: 0644]
it/src/test/java/com/sonarsource/scanner/it/ScannerTest.java
src/main/java/org/sonarsource/scanner/cli/Conf.java
src/test/java/org/sonarsource/scanner/cli/ConfTest.java
src/test/resources/org/sonarsource/scanner/cli/ConfTest/shouldOverrideProjectSettingsPath/conf/sq-project.properties [new file with mode: 0644]
src/test/resources/org/sonarsource/scanner/cli/ConfTest/shouldOverrideProjectSettingsPath/sonar-project.properties [new file with mode: 0644]

diff --git a/it/projects/override-project-settings-path/conf/sq-project.properties b/it/projects/override-project-settings-path/conf/sq-project.properties
new file mode 100644 (file)
index 0000000..a3b76fd
--- /dev/null
@@ -0,0 +1,4 @@
+sonar.projectKey=sample-with-custom-settings-path
+sonar.projectName=Test with custom settings location
+
+sonar.sources=src
diff --git a/it/projects/override-project-settings-path/sonar-project.properties b/it/projects/override-project-settings-path/sonar-project.properties
new file mode 100644 (file)
index 0000000..bd982e4
--- /dev/null
@@ -0,0 +1,4 @@
+sonar.projectKey=sample-should-be-ignored
+sonar.projectName=Should be ignored
+
+sonar.sources=src
diff --git a/it/projects/override-project-settings-path/src/basic/Hello.js b/it/projects/override-project-settings-path/src/basic/Hello.js
new file mode 100644 (file)
index 0000000..fd35455
--- /dev/null
@@ -0,0 +1,2 @@
+function hello() {
+}
diff --git a/it/projects/override-project-settings-path/src/basic/World.js b/it/projects/override-project-settings-path/src/basic/World.js
new file mode 100644 (file)
index 0000000..1ea849a
--- /dev/null
@@ -0,0 +1,2 @@
+function world() {
+}
index f003df28bb8e9ceb9bddd794be96b0f4edf8e259..c0c28754c4b5c4d1d47e0238d8e766e65f80a77e 100644 (file)
@@ -198,4 +198,15 @@ public class ScannerTest extends ScannerTestCase {
     assertThat(logs).containsPattern("Too small (initial|maximum) heap");
   }
 
+  // SQSCANNER-24
+  @Test
+  public void should_override_project_settings_path() {
+    File projectHome = new File("projects/override-project-settings-path");
+    SonarScanner build = newScanner(projectHome)
+      .setProperty("project.settings", 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");
+  }
+
 }
index fd33baf55892f8b51b471bcd02f2151ceaaa0551..0a3da87f9fe5ef05bf36984b8461d221c658b03b 100644 (file)
@@ -30,9 +30,7 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
-
 import javax.annotation.Nullable;
-
 import org.sonarsource.scanner.api.Utils;
 
 class Conf {
@@ -63,7 +61,7 @@ class Conf {
     result.putAll(loadEnvironmentProperties());
     result.putAll(cli.properties());
     result = resolve(result);
-    
+
     // root project base directory must be present and be absolute
     result.setProperty(PROPERTY_PROJECT_BASEDIR, getRootProjectBaseDir(result).toString());
     result.remove(PROJECT_HOME);
@@ -204,8 +202,7 @@ 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)) {
@@ -216,12 +213,12 @@ class Conf {
   }
 
   private static Path locatePropertiesFile(@Nullable Path defaultPath, Properties props, String settingsKey) {
-    Path settingsFile = defaultPath;
-    if (settingsFile == null || !Files.exists(settingsFile)) {
-      String settingsPath = props.getProperty(settingsKey, "");
-      if (!"".equals(settingsPath)) {
-        settingsFile = Paths.get(settingsPath);
-      }
+    Path settingsFile;
+    String settingsPath = props.getProperty(settingsKey, "");
+    if (!"".equals(settingsPath)) {
+      settingsFile = Paths.get(settingsPath);
+    } else {
+      settingsFile = defaultPath;
     }
 
     if (settingsFile != null) {
index 371a6ba4894bcb7a8c1dd07f16644db54ce99905..a966b50023f8c3ed03b7ee3f3d3392c073e1216c 100644 (file)
@@ -301,4 +301,19 @@ public class ConfTest {
       Files.delete(linkProjectHome);
     }
   }
+
+  // SQSCANNER-24
+  @Test
+  public void should_load_project_settings_using_property() 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");
+
+    args.setProperty("project.settings", home.resolve("conf/sq-project.properties").toAbsolutePath().toString());
+
+    properties = conf.properties();
+    assertThat(properties.get("sonar.prop")).isEqualTo("expected");
+  }
 }
diff --git a/src/test/resources/org/sonarsource/scanner/cli/ConfTest/shouldOverrideProjectSettingsPath/conf/sq-project.properties b/src/test/resources/org/sonarsource/scanner/cli/ConfTest/shouldOverrideProjectSettingsPath/conf/sq-project.properties
new file mode 100644 (file)
index 0000000..fa1dbbd
--- /dev/null
@@ -0,0 +1 @@
+sonar.prop=expected
diff --git a/src/test/resources/org/sonarsource/scanner/cli/ConfTest/shouldOverrideProjectSettingsPath/sonar-project.properties b/src/test/resources/org/sonarsource/scanner/cli/ConfTest/shouldOverrideProjectSettingsPath/sonar-project.properties
new file mode 100644 (file)
index 0000000..32f39c9
--- /dev/null
@@ -0,0 +1 @@
+sonar.prop=default