From 38398d7c11f3ef6866d834017e4b2d2c1c6409b7 Mon Sep 17 00:00:00 2001 From: mickael-caro-sonarsource Date: Mon, 22 Jun 2020 13:27:00 +0200 Subject: SQSCANNER-65 Add a new flag to get the Scanner that invoked the CLI a… (#84) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * SQSCANNER-65 Add a new flag to get the Scanner that invoked the CLI and its version from the invocation commandLine --- it/pom.xml | 8 +----- src/main/java/org/sonarsource/scanner/cli/Cli.java | 12 ++++++++ .../java/org/sonarsource/scanner/cli/Main.java | 2 +- .../sonarsource/scanner/cli/ScannerFactory.java | 11 ++++++-- .../java/org/sonarsource/scanner/cli/CliTest.java | 13 +++++++++ .../java/org/sonarsource/scanner/cli/ConfTest.java | 10 +++---- .../java/org/sonarsource/scanner/cli/MainTest.java | 24 +++++++++++----- .../scanner/cli/ScannerFactoryTest.java | 33 ++++++++++++++++++---- .../org/sonarsource/scanner/cli/StatsTest.java | 6 ++-- .../sonarsource/scanner/cli/SystemInfoTest.java | 4 +-- 10 files changed, 91 insertions(+), 32 deletions(-) diff --git a/it/pom.xml b/it/pom.xml index 3e843d4..85efafe 100644 --- a/it/pom.xml +++ b/it/pom.xml @@ -36,7 +36,7 @@ org.sonarsource.orchestrator sonar-orchestrator - 3.24.0.1993 + 3.29.0.2543 junit @@ -44,12 +44,6 @@ 4.12 test - - org.sonarsource.sonarqube - sonar-ws - ${sonar.buildVersion} - test - org.sonarsource.sonarqube sonar-ws diff --git a/src/main/java/org/sonarsource/scanner/cli/Cli.java b/src/main/java/org/sonarsource/scanner/cli/Cli.java index d072221..1c928ad 100644 --- a/src/main/java/org/sonarsource/scanner/cli/Cli.java +++ b/src/main/java/org/sonarsource/scanner/cli/Cli.java @@ -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); diff --git a/src/main/java/org/sonarsource/scanner/cli/Main.java b/src/main/java/org/sonarsource/scanner/cli/Main.java index 37e6748..56f34ac 100644 --- a/src/main/java/org/sonarsource/scanner/cli/Main.java +++ b/src/main/java/org/sonarsource/scanner/cli/Main.java @@ -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) { diff --git a/src/main/java/org/sonarsource/scanner/cli/ScannerFactory.java b/src/main/java/org/sonarsource/scanner/cli/ScannerFactory.java index 0a96ae6..820f651 100644 --- a/src/main/java/org/sonarsource/scanner/cli/ScannerFactory.java +++ b/src/main/java/org/sonarsource/scanner/cli/ScannerFactory.java @@ -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); } diff --git a/src/test/java/org/sonarsource/scanner/cli/CliTest.java b/src/test/java/org/sonarsource/scanner/cli/CliTest.java index f7e2b88..252fe74 100644 --- a/src/test/java/org/sonarsource/scanner/cli/CliTest.java +++ b/src/test/java/org/sonarsource/scanner/cli/CliTest.java @@ -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]); diff --git a/src/test/java/org/sonarsource/scanner/cli/ConfTest.java b/src/test/java/org/sonarsource/scanner/cli/ConfTest.java index bb45d57..9030d60 100644 --- a/src/test/java/org/sonarsource/scanner/cli/ConfTest.java +++ b/src/test/java/org/sonarsource/scanner/cli/ConfTest.java @@ -48,11 +48,11 @@ public class ConfTest { @Rule public ExpectedException exception = ExpectedException.none(); - private Map 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 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() { diff --git a/src/test/java/org/sonarsource/scanner/cli/MainTest.java b/src/test/java/org/sonarsource/scanner/cli/MainTest.java index 35ee089..92767c3 100644 --- a/src/test/java/org/sonarsource/scanner/cli/MainTest.java +++ b/src/test/java/org/sonarsource/scanner/cli/MainTest.java @@ -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(); diff --git a/src/test/java/org/sonarsource/scanner/cli/ScannerFactoryTest.java b/src/test/java/org/sonarsource/scanner/cli/ScannerFactoryTest.java index b8aa923..1b976ee 100644 --- a/src/test/java/org/sonarsource/scanner/cli/ScannerFactoryTest.java +++ b/src/test/java/org/sonarsource/scanner/cli/ScannerFactoryTest.java @@ -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(); } diff --git a/src/test/java/org/sonarsource/scanner/cli/StatsTest.java b/src/test/java/org/sonarsource/scanner/cli/StatsTest.java index b4ef294..b7fef7d 100644 --- a/src/test/java/org/sonarsource/scanner/cli/StatsTest.java +++ b/src/test/java/org/sonarsource/scanner/cli/StatsTest.java @@ -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() { diff --git a/src/test/java/org/sonarsource/scanner/cli/SystemInfoTest.java b/src/test/java/org/sonarsource/scanner/cli/SystemInfoTest.java index ec5c565..180ceee 100644 --- a/src/test/java/org/sonarsource/scanner/cli/SystemInfoTest.java +++ b/src/test/java/org/sonarsource/scanner/cli/SystemInfoTest.java @@ -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() { -- cgit v1.2.3