From 34ec38c33a375755fe97db021175d2008367bb90 Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Thu, 6 Jul 2017 14:50:36 +0200 Subject: SONAR-9198 Support comma in sonar.sources/sonar.tests Add a medium test --- .../sonar/scanner/scan/ProjectReactorBuilder.java | 8 ++++-- .../mediumtest/fs/FileSystemMediumTest.java | 33 +++++++++++++++++++++- .../scanner/scan/ProjectReactorBuilderTest.java | 14 +++++++-- 3 files changed, 49 insertions(+), 6 deletions(-) (limited to 'sonar-scanner-engine') diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectReactorBuilder.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectReactorBuilder.java index f642396ddea..8026c29d579 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectReactorBuilder.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectReactorBuilder.java @@ -35,7 +35,6 @@ import java.util.Map.Entry; import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.apache.commons.lang.ArrayUtils; -import org.apache.commons.lang.ObjectUtils; import org.apache.commons.lang.StringUtils; import org.sonar.api.CoreProperties; import org.sonar.api.batch.bootstrap.ProjectDefinition; @@ -46,6 +45,7 @@ import org.sonar.api.utils.log.Loggers; import org.sonar.api.utils.log.Profiler; import org.sonar.scanner.analysis.AnalysisProperties; import org.sonar.scanner.bootstrap.DroppedPropertyChecker; +import org.sonar.scanner.config.DefaultConfiguration; import org.sonar.scanner.util.ScannerUtils; /** @@ -406,7 +406,11 @@ public class ProjectReactorBuilder { * */ static String[] getListFromProperty(Map properties, String key) { - return (String[]) ObjectUtils.defaultIfNull(StringUtils.stripAll(StringUtils.split(properties.get(key), ',')), new String[0]); + String propValue = properties.get(key); + if (propValue != null) { + return DefaultConfiguration.parseAsCsv(ProjectDefinition.SOURCES_PROPERTY, propValue); + } + return new String[0]; } } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/FileSystemMediumTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/FileSystemMediumTest.java index e369aefeb05..b2d4722cf1c 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/FileSystemMediumTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/FileSystemMediumTest.java @@ -373,7 +373,7 @@ public class FileSystemMediumTest { .put("sonar.sources", "src") .build()) .start(); - + assertThat(logs.getAllAsString()).containsOnlyOnce("- Exclusion pattern 'pattern'"); assertThat(logs.getAllAsString()).containsOnlyOnce("'src/myfile.binary' generating issue exclusions"); } @@ -674,4 +674,35 @@ public class FileSystemMediumTest { assertThat(result.inputDirs()).hasSize(4); } + @Test + public void scanProjectWithCommaInSourcePath() throws IOException { + File srcDir = new File(baseDir, "src"); + srcDir.mkdir(); + + File xooFile = new File(srcDir, "sample,1.xoo"); + FileUtils.write(xooFile, "Sample xoo\ncontent"); + + File xooFile2 = new File(baseDir, "another,2.xoo"); + FileUtils.write(xooFile2, "Sample xoo 2\ncontent"); + + File testDir = new File(baseDir, "test"); + testDir.mkdir(); + + File xooTestFile = new File(testDir, "sampleTest,1.xoo"); + FileUtils.write(xooTestFile, "Sample test xoo\ncontent"); + + File xooTestFile2 = new File(baseDir, "sampleTest,2.xoo"); + FileUtils.write(xooTestFile2, "Sample test xoo 2\ncontent"); + + TaskResult result = tester.newTask() + .properties(builder + .put("sonar.sources", "src,\"another,2.xoo\"") + .put("sonar.tests", "\"test\",\"sampleTest,2.xoo\"") + .build()) + .start(); + + assertThat(result.inputFiles()).hasSize(4); + assertThat(result.inputDirs()).hasSize(3); + } + } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ProjectReactorBuilderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ProjectReactorBuilderTest.java index 1b63100e7d1..bcc57c46c15 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ProjectReactorBuilderTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ProjectReactorBuilderTest.java @@ -535,8 +535,16 @@ public class ProjectReactorBuilderTest { public void shouldGetList() { Map props = new HashMap<>(); - props.put("prop", " foo ,, bar , \n\ntoto,tutu"); - assertThat(ProjectReactorBuilder.getListFromProperty(props, "prop")).containsOnly("foo", "bar", "toto", "tutu"); + props.put("prop", " foo ,, bar , toto,tutu"); + assertThat(ProjectReactorBuilder.getListFromProperty(props, "prop")).containsOnly("foo", "", "bar", "toto", "tutu"); + } + + @Test + public void shouldGetListWithComma() { + Map props = new HashMap<>(); + + props.put("prop", "\"foo,bar\", toto,tutu"); + assertThat(ProjectReactorBuilder.getListFromProperty(props, "prop")).containsOnly("foo,bar", "toto", "tutu"); } @Test @@ -552,7 +560,7 @@ public class ProjectReactorBuilderTest { String filePath = "shouldGetList/foo.properties"; Map props = loadPropsFromFile(filePath); - assertThat(ProjectReactorBuilder.getListFromProperty(props, "prop")).containsOnly("foo", "bar", "toto", "tutu"); + assertThat(ProjectReactorBuilder.getListFromProperty(props, "prop")).containsOnly("foo", "bar", "toto", "tutu", ""); } @Test -- cgit v1.2.3