diff options
author | mickael-caro-sonarsource <mickael.caro@sonarsource.com> | 2020-06-22 13:27:00 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-22 13:27:00 +0200 |
commit | 38398d7c11f3ef6866d834017e4b2d2c1c6409b7 (patch) | |
tree | e2ce87af64650d888233bec4130fd2214a8f4192 /src/test | |
parent | 90a36e384448c3f3b419ddfbab3071e00392d267 (diff) | |
download | sonar-scanner-cli-38398d7c11f3ef6866d834017e4b2d2c1c6409b7.tar.gz sonar-scanner-cli-38398d7c11f3ef6866d834017e4b2d2c1c6409b7.zip |
SQSCANNER-65 Add a new flag to get the Scanner that invoked the CLI a… (#84)
* SQSCANNER-65 Add a new flag to get the Scanner that invoked the CLI and its version from the invocation commandLine
Diffstat (limited to 'src/test')
6 files changed, 68 insertions, 22 deletions
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 @@ -111,6 +111,19 @@ public class CliTest { } @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]); assertThat(cli.isDebugEnabled()).isFalse(); 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<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() { 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() { |