Skip to content

Commit

Permalink
SCANCLI-148 Support non Latin characters in scanner properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
antoine-vinot-sonarsource committed Dec 20, 2024
1 parent ce83f01 commit 2e5f66e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/main/java/org/sonarsource/scanner/cli/Conf.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

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;
Expand All @@ -35,6 +35,8 @@
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);

Expand All @@ -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;
Expand Down Expand Up @@ -92,8 +93,7 @@ private Properties loadGlobalProperties() {
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);
Expand Down Expand Up @@ -217,14 +217,14 @@ protected static Properties extractModuleProperties(String module, Properties pr
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) {
Expand All @@ -244,8 +244,8 @@ private static Path locatePropertiesFile(@Nullable Path defaultPath, Properties

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());
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/org/sonarsource/scanner/cli/ConfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,13 @@ void should_load_project_settings_using_property() throws Exception {
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İÖÜ");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
project.nonlatin=Non Latin ÇŞĞIİÖÜ

0 comments on commit 2e5f66e

Please sign in to comment.