aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorantoine.vinot <antoine.vinot@sonarsource.com>2024-12-19 11:42:22 +0100
committerAntoine Vinot <antoine.vinot@sonarsource.com>2024-12-20 11:56:58 +0100
commit2e5f66e99ca00cab5d689a0daebd2f5a7f44f371 (patch)
tree96f1d191ec7e100c05e866cf81081c45b56812e8
parentce83f01c38147147728f426b543e4359f20d5f5e (diff)
downloadsonar-scanner-cli-master.tar.gz
sonar-scanner-cli-master.zip
SCANCLI-148 Support non Latin characters in scanner properties.HEADmaster
-rw-r--r--src/main/java/org/sonarsource/scanner/cli/Conf.java20
-rw-r--r--src/test/java/org/sonarsource/scanner/cli/ConfTest.java9
-rw-r--r--src/test/resources/org/sonarsource/scanner/cli/ConfTest/shouldHandleNonLatinChars/project/sonar-project.properties1
3 files changed, 20 insertions, 10 deletions
diff --git a/src/main/java/org/sonarsource/scanner/cli/Conf.java b/src/main/java/org/sonarsource/scanner/cli/Conf.java
index 47f431f..d71d82c 100644
--- a/src/main/java/org/sonarsource/scanner/cli/Conf.java
+++ b/src/main/java/org/sonarsource/scanner/cli/Conf.java
@@ -21,7 +21,7 @@ package org.sonarsource.scanner.cli;
import java.io.FileInputStream;
import java.io.IOException;
-import java.io.InputStream;
+import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@@ -35,6 +35,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonarsource.scanner.lib.EnvironmentConfig;
+import static java.nio.charset.StandardCharsets.UTF_8;
+
class Conf {
private static final Logger LOG = LoggerFactory.getLogger(Conf.class);
@@ -46,7 +48,6 @@ class Conf {
private static final String PROPERTY_PROJECT_BASEDIR = "sonar.projectBaseDir";
private static final String PROPERTY_PROJECT_CONFIG_FILE = "sonar.projectConfigFile";
private static final String SONAR_PROJECT_PROPERTIES_FILENAME = "sonar-project.properties";
- static final String PROPERTY_SONAR_HOST_URL = "sonar.host.url";
private static final String BOOTSTRAP_START_TIME = "sonar.scanner.bootstrapStartTime";
private final Cli cli;
@@ -92,8 +93,7 @@ class Conf {
knownPropsAtThatPoint.putAll(loadEnvironmentProperties());
knownPropsAtThatPoint.putAll(cli.properties());
- Path settingsFile = locatePropertiesFile(knownPropsAtThatPoint, SCANNER_HOME, "conf/sonar-scanner.properties",
- SCANNER_SETTINGS);
+ Path settingsFile = locatePropertiesFile(knownPropsAtThatPoint);
if (settingsFile != null && Files.isRegularFile(settingsFile)) {
LOG.info("Scanner configuration file: {}", settingsFile);
return toProperties(settingsFile);
@@ -217,14 +217,14 @@ class Conf {
return moduleProps;
}
- private static Path locatePropertiesFile(Properties props, String homeKey, String relativePathFromHome, String settingsKey) {
+ private static Path locatePropertiesFile(Properties props) {
Path settingsFile = null;
- String scannerHome = props.getProperty(homeKey, "");
+ String scannerHome = props.getProperty(Conf.SCANNER_HOME, "");
if (!"".equals(scannerHome)) {
- settingsFile = Paths.get(scannerHome, relativePathFromHome);
+ settingsFile = Paths.get(scannerHome, "conf/sonar-scanner.properties");
}
- return locatePropertiesFile(settingsFile, props, settingsKey);
+ return locatePropertiesFile(settingsFile, props, Conf.SCANNER_SETTINGS);
}
private static Path locatePropertiesFile(@Nullable Path defaultPath, Properties props, String settingsKey) {
@@ -244,8 +244,8 @@ class Conf {
private static Properties toProperties(Path file) {
Properties properties = new Properties();
- try (InputStream in = new FileInputStream(file.toFile())) {
- properties.load(in);
+ try (InputStreamReader reader = new InputStreamReader(new FileInputStream(file.toFile()), UTF_8)) {
+ properties.load(reader);
// Trim properties
for (String propKey : properties.stringPropertyNames()) {
properties.setProperty(propKey, properties.getProperty(propKey).trim());
diff --git a/src/test/java/org/sonarsource/scanner/cli/ConfTest.java b/src/test/java/org/sonarsource/scanner/cli/ConfTest.java
index 82bd955..280c7b7 100644
--- a/src/test/java/org/sonarsource/scanner/cli/ConfTest.java
+++ b/src/test/java/org/sonarsource/scanner/cli/ConfTest.java
@@ -289,4 +289,13 @@ class ConfTest {
assertThat(properties).containsEntry("sonar.prop", "expected");
}
+ @Test
+ void should_handle_non_latin_characters() throws Exception {
+ Path home = Paths.get(getClass().getResource("ConfTest/shouldHandleNonLatinChars/project").toURI());
+ args.setProperty("project.home", home.toAbsolutePath().toString());
+
+ Properties properties = conf.properties();
+ assertThat(properties).containsEntry("project.nonlatin", "Non Latin ÇŞĞIİÖÜ");
+ }
+
}
diff --git a/src/test/resources/org/sonarsource/scanner/cli/ConfTest/shouldHandleNonLatinChars/project/sonar-project.properties b/src/test/resources/org/sonarsource/scanner/cli/ConfTest/shouldHandleNonLatinChars/project/sonar-project.properties
new file mode 100644
index 0000000..24ced59
--- /dev/null
+++ b/src/test/resources/org/sonarsource/scanner/cli/ConfTest/shouldHandleNonLatinChars/project/sonar-project.properties
@@ -0,0 +1 @@
+project.nonlatin=Non Latin ÇŞĞIİÖÜ