]> source.dussan.org Git - sonar-scanner-cli.git/commitdiff
SQSCANNER-101 Do not parse configuration twice
authorJulien HENRY <julien.henry@sonarsource.com>
Mon, 21 Mar 2022 10:56:18 +0000 (11:56 +0100)
committerJulien HENRY <julien.henry@sonarsource.com>
Tue, 22 Mar 2022 08:01:38 +0000 (09:01 +0100)
src/main/java/org/sonarsource/scanner/cli/Conf.java
src/main/java/org/sonarsource/scanner/cli/Main.java
src/test/java/org/sonarsource/scanner/cli/ConfTest.java
src/test/java/org/sonarsource/scanner/cli/MainTest.java

index aa704d53cd1ae34fa8710893853d84cf2aa94657..96e7e96ed8484e8e94f8cd83ff4aa3662e8e2cdf 100644 (file)
@@ -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();
index 60397e159208eba5cee48cf36a58b16ad183b154..63464afc136d86a45252f78886bddaa249643fee 100644 (file)
@@ -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");
   }
 
index ec51b8f3c2a6e2114caaeadd7154df121d7a5e43..57e4cb25af7928ad311b9e352de9434b53fda8b6 100644 (file)
@@ -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();
-  }
 }
index 4c3b54c08ef13331b1ad217992fea8c8f35fa954..80847bea595a95c3a2ac2934a17cdc1a2ec3d54b 100644 (file)
@@ -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");