diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2022-03-21 11:56:18 +0100 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2022-03-22 09:01:38 +0100 |
commit | 8c73aabed5dee00d5e184750984099b63aaf676a (patch) | |
tree | 01342122ea09c7eabb43eef00fe0cb8b80335ba6 | |
parent | 62768e25da3981a8ffd3778accfcd459d79ede24 (diff) | |
download | sonar-scanner-cli-8c73aabed5dee00d5e184750984099b63aaf676a.tar.gz sonar-scanner-cli-8c73aabed5dee00d5e184750984099b63aaf676a.zip |
SQSCANNER-101 Do not parse configuration twice
4 files changed, 48 insertions, 53 deletions
diff --git a/src/main/java/org/sonarsource/scanner/cli/Conf.java b/src/main/java/org/sonarsource/scanner/cli/Conf.java index aa704d5..96e7e96 100644 --- a/src/main/java/org/sonarsource/scanner/cli/Conf.java +++ b/src/main/java/org/sonarsource/scanner/cli/Conf.java @@ -28,7 +28,6 @@ import java.nio.file.Paths; import java.text.MessageFormat; import java.util.ArrayList; import java.util.List; -import java.util.Locale; import java.util.Map; import java.util.Properties; import javax.annotation.Nullable; @@ -43,7 +42,7 @@ 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"; - private static final String PROPERTY_SONAR_HOST_URL = "sonar.host.url"; + static final String PROPERTY_SONAR_HOST_URL = "sonar.host.url"; private final Cli cli; private final Logs logger; @@ -70,15 +69,6 @@ class Conf { return result; } - boolean isSonarCloud(@Nullable Properties testProperties) { - String hostUrl = testProperties != null ? testProperties.getProperty(PROPERTY_SONAR_HOST_URL) : properties().getProperty(PROPERTY_SONAR_HOST_URL); - if (hostUrl != null) { - return hostUrl.toLowerCase(Locale.getDefault()).contains("sonarcloud"); - } - - return false; - } - private Properties resolve(Properties props) { PropertyResolver resolver = new PropertyResolver(props, env); return resolver.resolve(); diff --git a/src/main/java/org/sonarsource/scanner/cli/Main.java b/src/main/java/org/sonarsource/scanner/cli/Main.java index 60397e1..63464af 100644 --- a/src/main/java/org/sonarsource/scanner/cli/Main.java +++ b/src/main/java/org/sonarsource/scanner/cli/Main.java @@ -19,6 +19,7 @@ */ package org.sonarsource.scanner.cli; +import java.util.Locale; import java.util.Map; import java.util.Properties; import org.sonarsource.scanner.api.EmbeddedScanner; @@ -41,9 +42,9 @@ public class Main { private final Exit exit; private final Cli cli; private final Conf conf; - private EmbeddedScanner runner; - private ScannerFactory runnerFactory; - private Logs logger; + private EmbeddedScanner embeddedScanner; + private final ScannerFactory runnerFactory; + private final Logs logger; Main(Exit exit, Cli cli, Conf conf, ScannerFactory runnerFactory, Logs logger) { this.exit = exit; @@ -70,8 +71,13 @@ public class Main { checkSkip(p); configureLogging(p); init(p); - runner.start(); - logger.info(String.format("Analyzing on %s", conf.isSonarCloud(null) ? "SonarCloud" : ("SonarQube server " + runner.serverVersion()))); + embeddedScanner.start(); + if (isSonarCloud(p)) { + logger.info("Analyzing on SonarCloud"); + } else { + String serverVersion = embeddedScanner.serverVersion(); + logger.info(String.format("Analyzing on SonarQube server %s", serverVersion)); + } execute(stats, p); status = Exit.SUCCESS; } catch (Throwable e) { @@ -81,7 +87,15 @@ public class Main { } finally { exit.exit(status); } + } + + static boolean isSonarCloud(Properties props) { + String hostUrl = props.getProperty(Conf.PROPERTY_SONAR_HOST_URL); + if (hostUrl != null) { + return hostUrl.toLowerCase(Locale.ENGLISH).contains("sonarcloud"); + } + return false; } private void checkSkip(Properties properties) { @@ -97,7 +111,7 @@ public class Main { exit.exit(Exit.SUCCESS); } - runner = runnerFactory.create(p, cli.getInvokedFrom()); + embeddedScanner = runnerFactory.create(p, cli.getInvokedFrom()); } private void configureLogging(Properties props) { @@ -109,7 +123,7 @@ public class Main { } private void execute(Stats stats, Properties p) { - runner.execute((Map) p); + embeddedScanner.execute((Map) p); displayExecutionResult(stats, "SUCCESS"); } diff --git a/src/test/java/org/sonarsource/scanner/cli/ConfTest.java b/src/test/java/org/sonarsource/scanner/cli/ConfTest.java index ec51b8f..57e4cb2 100644 --- a/src/test/java/org/sonarsource/scanner/cli/ConfTest.java +++ b/src/test/java/org/sonarsource/scanner/cli/ConfTest.java @@ -337,38 +337,4 @@ public class ConfTest { assertThat(properties.get("sonar.prop")).isEqualTo("expected"); } - // SQSCANNER-57 - @Test - public void should_return_true_is_sonar_cloud() { - - args.setProperty("sonar.host.url", "https://sonarcloud.io"); - - conf.properties(); - - assertThat(conf.isSonarCloud(null)).isTrue(); - } - - // SQSCANNER-57 - @Test - public void should_return_false_is_sonar_cloud() { - args.setProperty("sonar.host.url", "https://mysonarqube.com:9000/"); - - //Still returns false, sonarcloud not detected in the content of the url - Properties properties = conf.properties(); - - assertThat(properties.getProperty("sonar.host.url")).isEqualTo("https://mysonarqube.com:9000/"); - - assertThat(conf.isSonarCloud(null)).isFalse(); - } - - // SQSCANNER-57 - @Test - public void should_return_false_is_sonar_cloud_host_is_null() { - - Properties emptyProperties = new Properties(); - - assertThat(emptyProperties.getProperty("sonar.host.url")).isNull(); - - assertThat(conf.isSonarCloud(emptyProperties)).isFalse(); - } } diff --git a/src/test/java/org/sonarsource/scanner/cli/MainTest.java b/src/test/java/org/sonarsource/scanner/cli/MainTest.java index 4c3b54c..80847be 100644 --- a/src/test/java/org/sonarsource/scanner/cli/MainTest.java +++ b/src/test/java/org/sonarsource/scanner/cli/MainTest.java @@ -253,8 +253,8 @@ public class MainTest { @Test public void should_log_SonarCloud_server() { Properties p = new Properties(); + p.setProperty("sonar.host.url", "https://sonarcloud.io"); when(conf.properties()).thenReturn(p); - when(conf.isSonarCloud(null)).thenReturn(true); when(cli.getInvokedFrom()).thenReturn(""); Main main = new Main(exit, cli, conf, scannerFactory, logs); @@ -262,6 +262,31 @@ public class MainTest { verify(logs).info("Analyzing on SonarCloud"); } + // SQSCANNER-57 + @Test + public void should_return_true_is_sonar_cloud() { + + Properties properties = new Properties(); + properties.setProperty("sonar.host.url", "https://sonarcloud.io"); + + assertThat(Main.isSonarCloud(properties)).isTrue(); + } + + // SQSCANNER-57 + @Test + public void should_return_false_is_sonar_cloud() { + Properties properties = new Properties(); + properties.setProperty("sonar.host.url", "https://mysonarqube.com:9000/"); + + assertThat(Main.isSonarCloud(properties)).isFalse(); + } + + // SQSCANNER-57 + @Test + public void should_return_false_is_sonar_cloud_host_is_null() { + assertThat(Main.isSonarCloud(new Properties())).isFalse(); + } + @Test public void should_configure_logging() { Properties analysisProps = testLogging("sonar.verbose", "true"); |