diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2017-09-07 16:29:42 +0200 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2017-09-15 08:51:08 +0200 |
commit | 2ec01b5375b038baae335785a2622c439de2d399 (patch) | |
tree | 2961841c3edf5988a7416cec923ba82879ca8aee | |
parent | dc5f68488fc62ac6dbaa3f0135ecdb5fa333105e (diff) | |
download | sonarqube-2ec01b5375b038baae335785a2622c439de2d399.tar.gz sonarqube-2ec01b5375b038baae335785a2622c439de2d399.zip |
SONAR-9666 Restore support of multi-valued properties containing line breaks
-rw-r--r-- | sonar-scanner-engine/src/main/java/org/sonar/scanner/config/DefaultConfiguration.java | 47 | ||||
-rw-r--r-- | sonar-scanner-engine/src/test/java/org/sonar/scanner/config/DefaultConfigurationTest.java | 6 |
2 files changed, 51 insertions, 2 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/config/DefaultConfiguration.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/config/DefaultConfiguration.java index 6433faa01f0..3a5e18f2e3f 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/config/DefaultConfiguration.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/config/DefaultConfiguration.java @@ -24,6 +24,7 @@ import java.io.StringReader; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Optional; @@ -116,19 +117,61 @@ public abstract class DefaultConfiguration implements Configuration { List<String> result = new ArrayList<>(); try (CSVParser csvParser = CSVFormat.RFC4180 .withHeader((String) null) - .withIgnoreSurroundingSpaces(true) + .withIgnoreEmptyLines() + .withIgnoreSurroundingSpaces() .parse(new StringReader(value))) { List<CSVRecord> records = csvParser.getRecords(); if (records.isEmpty()) { return ArrayUtils.EMPTY_STRING_ARRAY; } - records.get(0).iterator().forEachRemaining(result::add); + processRecords(result, records); return result.toArray(new String[result.size()]); } catch (IOException e) { throw new IllegalStateException("Property: '" + key + "' doesn't contain a valid CSV value: '" + value + "'", e); } } + /** + * In most cases we expect a single record. <br>Having multiple records means the input value was splitted over multiple lines (this is common in Maven). + * For example: + * <pre> + * <sonar.exclusions> + * src/foo, + * src/bar, + * src/biz + * <sonar.exclusions> + * </pre> + * In this case records will be merged to form a single list of items. Last item of a record is appended to first item of next record. + * <p> + * This is a very curious case, but we try to preserve line break in the middle of an item: + * <pre> + * <sonar.exclusions> + * a + * b, + * c + * <sonar.exclusions> + * </pre> + * will produce ['a\nb', 'c'] + */ + private static void processRecords(List<String> result, List<CSVRecord> records) { + for (CSVRecord csvRecord : records) { + Iterator<String> it = csvRecord.iterator(); + if (!result.isEmpty()) { + String next = it.next(); + if (!next.isEmpty()) { + int lastItemIdx = result.size() - 1; + String previous = result.get(lastItemIdx); + if (previous.isEmpty()) { + result.set(lastItemIdx, next); + } else { + result.set(lastItemIdx, previous + "\n" + next); + } + } + } + it.forEachRemaining(result::add); + } + } + private Optional<String> getInternal(String key) { if (mode.isIssues() && key.endsWith(".secured") && !key.contains(".license")) { throw MessageException.of("Access to the secured property '" + key diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/config/DefaultConfigurationTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/config/DefaultConfigurationTest.java index adf55780f1d..f21b30a8ea5 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/config/DefaultConfigurationTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/config/DefaultConfigurationTest.java @@ -114,6 +114,12 @@ public class DefaultConfigurationTest { assertThat(getStringArray("a , b")).containsExactly("a", "b"); assertThat(getStringArray("\"a \",\" b\"")).containsExactly("a ", " b"); assertThat(getStringArray("\"a,b\",c")).containsExactly("a,b", "c"); + assertThat(getStringArray("\"a\nb\",c")).containsExactly("a\nb", "c"); + assertThat(getStringArray("\"a\",\n b\n")).containsExactly("a", "b"); + assertThat(getStringArray("a\n,b\n")).containsExactly("a", "b"); + assertThat(getStringArray("a\n,,b\n")).containsExactly("a", "", "b"); + assertThat(getStringArray("a,\n\nb,c")).containsExactly("a", "b", "c"); + assertThat(getStringArray("a,b\n\nc,d")).containsExactly("a", "b\nc", "d"); try { getStringArray("\"a ,b"); fail("Expected exception"); |