]> source.dussan.org Git - sonar-scanner-cli.git/commitdiff
SONARUNNER-131 Don't fail with empty modules
authorJulien HENRY <julien.henry@sonarsource.com>
Mon, 28 Sep 2015 14:04:44 +0000 (16:04 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Mon, 28 Sep 2015 14:13:48 +0000 (16:13 +0200)
sonar-runner-cli/src/main/java/org/sonar/runner/cli/Conf.java
sonar-runner-cli/src/test/java/org/sonar/runner/cli/ConfTest.java
sonar-runner-cli/src/test/resources/org/sonar/runner/ConfTest/emptyModules/project/sonar-project.properties [new file with mode: 0644]

index ac7248f4caca9b8916ea60928e21ee6c05fade06..be941938526436c230fcf768c40f97de8310d6cd 100644 (file)
@@ -24,6 +24,8 @@ import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.text.MessageFormat;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Properties;
@@ -257,11 +259,18 @@ class Conf {
    *
    */
   static String[] getListFromProperty(Properties properties, String key) {
-    String value = properties.getProperty(key, "");
+    String value = properties.getProperty(key, "").trim();
+    if (value.isEmpty()) {
+      return new String[0];
+    }
     String[] values = value.split(",");
+    List<String> trimmedValues = new ArrayList<>();
     for (int i = 0; i < values.length; i++) {
-      values[i] = values[i].trim();
+      String trimmedValue = values[i].trim();
+      if (!trimmedValue.isEmpty()) {
+        trimmedValues.add(trimmedValue);
+      }
     }
-    return values;
+    return trimmedValues.toArray(new String[trimmedValues.size()]);
   }
 }
index 4c9d77512575575f25c35c8b48fd504fe38e2ad7..50a53e57a6080075791bb6a2d5893c906e8c7ecf 100644 (file)
  */
 package org.sonar.runner.cli;
 
+import java.io.File;
+import java.util.Properties;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.TemporaryFolder;
-import org.sonar.runner.cli.Cli;
-import org.sonar.runner.cli.Conf;
-import java.io.File;
-import java.util.Properties;
 
 import static org.fest.assertions.Assertions.assertThat;
 import static org.mockito.Mockito.mock;
@@ -134,4 +132,21 @@ public class ConfTest {
     assertThat(properties.getProperty("module2.sonar.projectName")).isEqualTo("Module 2");
   }
 
+  @Test
+  public void ignoreEmptyModule() throws Exception {
+    File projectHome = new File(getClass().getResource("/org/sonar/runner/ConfTest/emptyModules/project").toURI());
+    args.setProperty("project.home", temp.newFolder().getCanonicalPath());
+    args.setProperty("sonar.projectBaseDir", projectHome.getCanonicalPath());
+
+    conf.properties();
+  }
+
+  @Test
+  public void shouldGetList() {
+    Properties props = new Properties();
+
+    props.put("prop", "  foo  ,,  bar  , \n\ntoto,tutu");
+    assertThat(Conf.getListFromProperty(props, "prop")).containsOnly("foo", "bar", "toto", "tutu");
+  }
+
 }
diff --git a/sonar-runner-cli/src/test/resources/org/sonar/runner/ConfTest/emptyModules/project/sonar-project.properties b/sonar-runner-cli/src/test/resources/org/sonar/runner/ConfTest/emptyModules/project/sonar-project.properties
new file mode 100644 (file)
index 0000000..708db4c
--- /dev/null
@@ -0,0 +1 @@
+sonar.modules=