]> source.dussan.org Git - sonar-scanner-cli.git/commitdiff
SQSCANNER-65 Add a new flag to get the Scanner that invoked the CLI a… (#84)
authormickael-caro-sonarsource <mickael.caro@sonarsource.com>
Mon, 22 Jun 2020 11:27:00 +0000 (13:27 +0200)
committerGitHub <noreply@github.com>
Mon, 22 Jun 2020 11:27:00 +0000 (13:27 +0200)
* SQSCANNER-65 Add a new flag to get the Scanner that invoked the CLI and its version from the invocation commandLine

it/pom.xml
src/main/java/org/sonarsource/scanner/cli/Cli.java
src/main/java/org/sonarsource/scanner/cli/Main.java
src/main/java/org/sonarsource/scanner/cli/ScannerFactory.java
src/test/java/org/sonarsource/scanner/cli/CliTest.java
src/test/java/org/sonarsource/scanner/cli/ConfTest.java
src/test/java/org/sonarsource/scanner/cli/MainTest.java
src/test/java/org/sonarsource/scanner/cli/ScannerFactoryTest.java
src/test/java/org/sonarsource/scanner/cli/StatsTest.java
src/test/java/org/sonarsource/scanner/cli/SystemInfoTest.java

index 3e843d415e403c1cb42655669c17653702797a61..85efafe7af5df5f9055a8e646bbcf785e7e7ddfe 100644 (file)
@@ -36,7 +36,7 @@
     <dependency>
       <groupId>org.sonarsource.orchestrator</groupId>
       <artifactId>sonar-orchestrator</artifactId>
-      <version>3.24.0.1993</version>
+      <version>3.29.0.2543</version>
     </dependency>
     <dependency>
       <groupId>junit</groupId>
       <version>4.12</version>
       <scope>test</scope>
     </dependency>
-    <dependency>
-      <groupId>org.sonarsource.sonarqube</groupId>
-      <artifactId>sonar-ws</artifactId>
-      <version>${sonar.buildVersion}</version>
-      <scope>test</scope>
-    </dependency>
     <dependency>
       <groupId>org.sonarsource.sonarqube</groupId>
       <artifactId>sonar-ws</artifactId>
index d072221cbe638a647405ab9b783ea5724e7d06b5..1c928ad05ed977ccae861584de9e0a2882fd4027 100644 (file)
@@ -29,6 +29,7 @@ class Cli {
   private boolean debugEnabled = false;
   private boolean displayVersionOnly = false;
   private boolean embedded = false;
+  private String invokedFrom = "";
   private final Properties props = new Properties();
   private final Exit exit;
   private final Logs logger;
@@ -50,6 +51,10 @@ class Cli {
     return embedded;
   }
 
+  String getInvokedFrom() {
+    return invokedFrom;
+  }
+
   Properties properties() {
     return props;
   }
@@ -90,7 +95,14 @@ class Cli {
       return processProp(args, pos);
 
     } else if ("--embedded".equals(arg)) {
+      logger.info("Option --embedded is deprecated and will be removed in a future release.");
+      embedded = true;
+
+    } else if (arg.startsWith("--from")) {
       embedded = true;
+      if (arg.length() > "--from=".length()) {
+        invokedFrom = arg.substring("--from=".length());
+      }
 
     } else if (arg.startsWith("-D")) {
       arg = arg.substring(2);
index 37e67483468bc1ac9e3d9ae002ef5d58e2bcb1c2..56f34ac3f171c78810ba766c596411cd2ad20085 100644 (file)
@@ -97,7 +97,7 @@ public class Main {
       exit.exit(Exit.SUCCESS);
     }
 
-    runner = runnerFactory.create(p);
+    runner = runnerFactory.create(p, cli.getInvokedFrom());
   }
 
   private void configureLogging(Properties props) {
index 0a96ae685cb299443b2f16a1f93e1c92cdbee680..820f651d7ddd9056a9bb998adbf635c25ebb0618 100644 (file)
@@ -32,8 +32,15 @@ class ScannerFactory {
     this.logger = logger;
   }
 
-  EmbeddedScanner create(Properties props) {
-    return EmbeddedScanner.create("ScannerCli", ScannerVersion.version(), new DefaultLogOutput())
+  EmbeddedScanner create(Properties props, String isInvokedFrom) {
+    String appName = "ScannerCLI";
+    String appVersion = ScannerVersion.version();
+    if (!isInvokedFrom.equals("") && isInvokedFrom.contains("/")) {
+      appName = isInvokedFrom.split("/")[0];
+      appVersion = isInvokedFrom.split("/")[1];
+    }
+
+    return EmbeddedScanner.create(appName, appVersion, new DefaultLogOutput())
       .addGlobalProperties((Map) props);
   }
 
index f7e2b884b2bbd2af13ca0bcc7d97115c9e5fd894..252fe74631f07ac5377e879a212cefc3a672071c 100644 (file)
@@ -110,6 +110,19 @@ public class CliTest {
     assertThat(cli.properties().get("sonar.verbose")).isNull();
   }
 
+  @Test
+  public void should_parse_from_argument() {
+    cli.parse(new String[] {"--from=ScannerMSBuild/4.8"});
+    assertThat(cli.getInvokedFrom()).isNotEmpty();
+    assertThat(cli.getInvokedFrom()).isEqualTo("ScannerMSBuild/4.8");
+  }
+
+  @Test
+  public void from_argument_is_only_from_let_value_empty() {
+    cli.parse(new String[] {"--from="});
+    assertThat(cli.getInvokedFrom()).isEmpty();
+  }
+
   @Test
   public void should_disable_debug_mode_and_stacktrace_log_by_default() {
     cli.parse(new String[0]);
index bb45d57d96a9cda2df6d7aa088316a6c887210b4..9030d60923405a87f4926f9a19981a77c031cf27 100644 (file)
@@ -48,11 +48,11 @@ public class ConfTest {
   @Rule
   public ExpectedException exception = ExpectedException.none();
 
-  private Map<String, String> env = new HashMap<>();
-  private Properties args = new Properties();
-  private Logs logs = new Logs(System.out, System.err);
-  private Cli cli = mock(Cli.class);
-  private Conf conf = new Conf(cli, logs, env);
+  private final Map<String, String> env = new HashMap<>();
+  private final Properties args = new Properties();
+  private final Logs logs = new Logs(System.out, System.err);
+  private final Cli cli = mock(Cli.class);
+  private final Conf conf = new Conf(cli, logs, env);
 
   @Before
   public void initConf() {
index 35ee089d63fc76498c6df540c2aa9c92461a2b0e..92767c30253555fb9005ace7fa5e36549aa82e11 100644 (file)
@@ -62,17 +62,18 @@ public class MainTest {
   @Before
   public void setUp() {
     MockitoAnnotations.initMocks(this);
-    when(scannerFactory.create(any(Properties.class))).thenReturn(scanner);
+    when(scannerFactory.create(any(Properties.class), any(String.class))).thenReturn(scanner);
     when(conf.properties()).thenReturn(properties);
   }
 
   @Test
   public void should_execute_runner() {
+    when(cli.getInvokedFrom()).thenReturn("");
     Main main = new Main(exit, cli, conf, scannerFactory, logs);
     main.execute();
 
     verify(exit).exit(Exit.SUCCESS);
-    verify(scannerFactory).create(properties);
+    verify(scannerFactory).create(properties, "");
 
     verify(scanner, times(1)).start();
     verify(scanner, times(1)).execute((Map) properties);
@@ -84,7 +85,8 @@ public class MainTest {
     Exception e = new NullPointerException("NPE");
     e = new IllegalStateException("Error", e);
     doThrow(e).when(runner).execute(any());
-    when(scannerFactory.create(any(Properties.class))).thenReturn(runner);
+    when(cli.getInvokedFrom()).thenReturn("");
+    when(scannerFactory.create(any(Properties.class), any(String.class))).thenReturn(runner);
     when(cli.isDebugEnabled()).thenReturn(true);
     Main main = new Main(exit, cli, conf, scannerFactory, logs);
     main.execute();
@@ -99,8 +101,9 @@ public class MainTest {
     Exception e = new NullPointerException("NPE");
     e = new IllegalStateException("Error", e);
     doThrow(e).when(runner).start();
+    when(cli.getInvokedFrom()).thenReturn("");
     when(cli.isDebugEnabled()).thenReturn(true);
-    when(scannerFactory.create(any(Properties.class))).thenReturn(runner);
+    when(scannerFactory.create(any(Properties.class), any(String.class))).thenReturn(runner);
 
     Main main = new Main(exit, cli, conf, scannerFactory, logs);
     main.execute();
@@ -175,10 +178,12 @@ public class MainTest {
   private void testException(Exception e, boolean debugEnabled, boolean isEmbedded, int expectedExitCode) {
     when(cli.isDebugEnabled()).thenReturn(debugEnabled);
     when(cli.isEmbedded()).thenReturn(isEmbedded);
+    when(cli.getInvokedFrom()).thenReturn("");
 
     EmbeddedScanner runner = mock(EmbeddedScanner.class);
     doThrow(e).when(runner).execute(any());
-    when(scannerFactory.create(any(Properties.class))).thenReturn(runner);
+
+    when(scannerFactory.create(any(Properties.class), any(String.class))).thenReturn(runner);
 
     Main main = new Main(exit, cli, conf, scannerFactory, logs);
     main.execute();
@@ -201,6 +206,7 @@ public class MainTest {
   public void should_only_display_version() {
     Properties p = new Properties();
     when(cli.isDisplayVersionOnly()).thenReturn(true);
+    when(cli.getInvokedFrom()).thenReturn("");
     when(conf.properties()).thenReturn(p);
 
     Main main = new Main(exit, cli, conf, scannerFactory, logs);
@@ -209,7 +215,7 @@ public class MainTest {
     InOrder inOrder = Mockito.inOrder(exit, scannerFactory);
 
     inOrder.verify(exit, times(1)).exit(Exit.SUCCESS);
-    inOrder.verify(scannerFactory, times(1)).create(p);
+    inOrder.verify(scannerFactory, times(1)).create(p, "");
     inOrder.verify(exit, times(1)).exit(Exit.SUCCESS);
   }
 
@@ -218,6 +224,7 @@ public class MainTest {
     Properties p = new Properties();
     p.setProperty(ScanProperties.SKIP, "true");
     when(conf.properties()).thenReturn(p);
+    when(cli.getInvokedFrom()).thenReturn("");
 
     Main main = new Main(exit, cli, conf, scannerFactory, logs);
     main.execute();
@@ -226,7 +233,7 @@ public class MainTest {
     InOrder inOrder = Mockito.inOrder(exit, scannerFactory);
 
     inOrder.verify(exit, times(1)).exit(Exit.SUCCESS);
-    inOrder.verify(scannerFactory, times(1)).create(p);
+    inOrder.verify(scannerFactory, times(1)).create(p, "");
     inOrder.verify(exit, times(1)).exit(Exit.SUCCESS);
   }
 
@@ -235,6 +242,7 @@ public class MainTest {
     when(scanner.serverVersion()).thenReturn("5.5");
     Properties p = new Properties();
     when(cli.isDisplayVersionOnly()).thenReturn(true);
+    when(cli.getInvokedFrom()).thenReturn("");
     when(conf.properties()).thenReturn(p);
 
     Main main = new Main(exit, cli, conf, scannerFactory, logs);
@@ -247,6 +255,7 @@ public class MainTest {
     Properties p = new Properties();
     when(conf.properties()).thenReturn(p);
     when(conf.isSonarCloud(null)).thenReturn(true);
+    when(cli.getInvokedFrom()).thenReturn("");
 
     Main main = new Main(exit, cli, conf, scannerFactory, logs);
     main.execute();
@@ -275,6 +284,7 @@ public class MainTest {
     Properties p = new Properties();
     p.put(propKey, propValue);
     when(conf.properties()).thenReturn(p);
+    when(cli.getInvokedFrom()).thenReturn("");
 
     Main main = new Main(exit, cli, conf, scannerFactory, logs);
     main.execute();
index b8aa923aef13a1fd3b3ffd6f47184acc266f0541..1b976ee52fed07938e76e1d23f7db64bda1b70f0 100644 (file)
@@ -33,17 +33,40 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
 
 public class ScannerFactoryTest {
 
-  private Properties props = new Properties();
-  private Logs logs = mock(Logs.class);
+  private final Properties props = new Properties();
+  private final Logs logs = mock(Logs.class);
 
   @Test
   public void should_create_embedded_runner() {
     props.setProperty("foo", "bar");
-    EmbeddedScanner runner = new ScannerFactory(logs).create(props);
+    EmbeddedScanner runner = new ScannerFactory(logs).create(props, "");
 
     assertThat(runner).isInstanceOf(EmbeddedScanner.class);
-    assertThat(runner.globalProperties().get("foo")).isEqualTo("bar");
-    assertThat(runner.app()).isEqualTo("ScannerCli");
+    assertThat(runner.globalProperties()).containsEntry("foo", "bar");
+    assertThat(runner.app()).isEqualTo("ScannerCLI");
+    assertThat(runner.appVersion()).isNotNull();
+  }
+
+  @Test
+  public void should_create_embedded_runner_with_scannername_from_argument() {
+    props.setProperty("foo", "bar");
+    EmbeddedScanner runner = new ScannerFactory(logs).create(props, "ScannerMSBuild/4.8.0");
+
+    assertThat(runner).isInstanceOf(EmbeddedScanner.class);
+    assertThat(runner.globalProperties()).containsEntry("foo", "bar");
+    assertThat(runner.app()).isEqualTo("ScannerMSBuild");
+    assertThat(runner.appVersion()).isEqualTo("4.8.0");
+    assertThat(runner.appVersion()).isNotNull();
+  }
+
+  @Test
+  public void should_create_embedded_runner_from_argument_is_not_regex_compliant_revert_to_default_scanner_name() {
+    props.setProperty("foo", "bar");
+    EmbeddedScanner runner = new ScannerFactory(logs).create(props, "ScannerMSBuild4.8.0");
+
+    assertThat(runner).isInstanceOf(EmbeddedScanner.class);
+    assertThat(runner.globalProperties()).containsEntry("foo", "bar");
+    assertThat(runner.app()).isEqualTo("ScannerCLI");
     assertThat(runner.appVersion()).isNotNull();
   }
 
index b4ef294223898ff1d006bda24577a0535bedafe8..b7fef7de10bfbbe005e25ac366225775dc18c683 100644 (file)
@@ -28,9 +28,9 @@ import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 
 public class StatsTest {
-  private PrintStream stdOut = mock(PrintStream.class);
-  private PrintStream stdErr = mock(PrintStream.class);
-  private Logs logs = new Logs(stdOut, stdErr);
+  private final PrintStream stdOut = mock(PrintStream.class);
+  private final PrintStream stdErr = mock(PrintStream.class);
+  private final Logs logs = new Logs(stdOut, stdErr);
 
   @Test
   public void shouldPrintStats() {
index ec5c5659ba66ee43e2eb1612984149e30610338d..180ceeee30cd95e25b2ba928cc8e7416c8cceb50 100644 (file)
@@ -31,8 +31,8 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
 import static org.mockito.Mockito.when;
 
 public class SystemInfoTest {
-  private System2 mockSystem = mock(System2.class);
-  private Logs logs = mock(Logs.class);
+  private final System2 mockSystem = mock(System2.class);
+  private final Logs logs = mock(Logs.class);
 
   @Before
   public void setUp() {