* SQSCANNER-65 Add a new flag to get the Scanner that invoked the CLI and its version from the invocation commandLinetags/4.4.0.2170
@@ -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> | |||
@@ -44,12 +44,6 @@ | |||
<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> |
@@ -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); |
@@ -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) { |
@@ -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); | |||
} | |||
@@ -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]); |
@@ -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() { |
@@ -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(); |
@@ -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(); | |||
} | |||
@@ -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() { |
@@ -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() { |