Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SCANCLI-148 Support non Latin characters in scanner properties. #203

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)) {
antoine-vinot-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
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İÖÜ
Loading