From 8865c467f7686cc52938b361fcd738a48cab654b Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Mon, 28 Sep 2015 16:04:44 +0200 Subject: [PATCH] SONARUNNER-131 Don't fail with empty modules --- .../main/java/org/sonar/runner/cli/Conf.java | 15 +++++++++--- .../java/org/sonar/runner/cli/ConfTest.java | 23 +++++++++++++++---- .../project/sonar-project.properties | 1 + 3 files changed, 32 insertions(+), 7 deletions(-) create mode 100644 sonar-runner-cli/src/test/resources/org/sonar/runner/ConfTest/emptyModules/project/sonar-project.properties diff --git a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Conf.java b/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Conf.java index ac7248f..be94193 100644 --- a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Conf.java +++ b/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Conf.java @@ -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 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()]); } } diff --git a/sonar-runner-cli/src/test/java/org/sonar/runner/cli/ConfTest.java b/sonar-runner-cli/src/test/java/org/sonar/runner/cli/ConfTest.java index 4c9d775..50a53e5 100644 --- a/sonar-runner-cli/src/test/java/org/sonar/runner/cli/ConfTest.java +++ b/sonar-runner-cli/src/test/java/org/sonar/runner/cli/ConfTest.java @@ -19,14 +19,12 @@ */ 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 index 0000000..708db4c --- /dev/null +++ b/sonar-runner-cli/src/test/resources/org/sonar/runner/ConfTest/emptyModules/project/sonar-project.properties @@ -0,0 +1 @@ +sonar.modules= -- 2.39.5