* SQSCANNER-57 - Rename scanner SonarScannertags/4.3.0.2102
"{ \"sonar.scanner.skip\":\"true\" }"); | "{ \"sonar.scanner.skip\":\"true\" }"); | ||||
BuildResult result = orchestrator.executeBuild(build); | BuildResult result = orchestrator.executeBuild(build); | ||||
assertThat(result.getLogs()).contains("SonarQube Scanner analysis skipped"); | |||||
assertThat(result.getLogs()).contains("SonarScanner analysis skipped"); | |||||
} | } | ||||
@Test | @Test |
--- | --- | ||||
[[info]] | [[info]] | ||||
| **Download SonarScanner 4.0** - Compatible with SonarQube 6.7+ (LTS) | |||||
| **Download SonarScanner 4.0** - Compatible with SonarQube 6.7+ (LTS) and SonarCloud | |||||
| By [SonarSource](https://www.sonarsource.com/) – GNU LGPL 3 – [Issue Tracker](https://jira.sonarsource.com/browse/SQSCANNER) – [Source](https://github.com/Sonarsource/sonar-scanner-cli) | | By [SonarSource](https://www.sonarsource.com/) – GNU LGPL 3 – [Issue Tracker](https://jira.sonarsource.com/browse/SQSCANNER) – [Source](https://github.com/Sonarsource/sonar-scanner-cli) | ||||
| | | | ||||
| [Linux 64-bit](https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.0.0.1744-linux.zip) | | | [Linux 64-bit](https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.0.0.1744-linux.zip) | |
#!/bin/sh | #!/bin/sh | ||||
# | # | ||||
# SonarQube Scanner Startup Script for Unix | |||||
# SonarScanner Startup Script for Unix | |||||
# | # | ||||
# Optional ENV vars: | # Optional ENV vars: | ||||
# SONAR_SCANNER_OPTS - Parameters passed to the Java VM when running the SonarQube Scanner | |||||
# SONAR_SCANNER_OPTS - Parameters passed to the Java VM when running the SonarScanner | |||||
# SONAR_SCANNER_DEBUG_OPTS - Extra parameters passed to the Java VM for debugging | # SONAR_SCANNER_DEBUG_OPTS - Extra parameters passed to the Java VM for debugging | ||||
# JAVA_HOME - Location of Java's installation | # JAVA_HOME - Location of Java's installation | ||||
#!/bin/sh | #!/bin/sh | ||||
# | # | ||||
# SonarQube Scanner Startup Script for Unix | |||||
# SonarScanner Startup Script for Unix | |||||
# | # | ||||
# Optional ENV vars: | # Optional ENV vars: | ||||
# SONAR_SCANNER_OPTS - parameters passed to the Java VM when running the SonarQube Scanner | |||||
# SONAR_SCANNER_OPTS - parameters passed to the Java VM when running the SonarScanner | |||||
# JAVA_HOME - Location of Java's installation | # JAVA_HOME - Location of Java's installation | ||||
SONAR_SCANNER_DEBUG_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000" | SONAR_SCANNER_DEBUG_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000" | ||||
echo "Executing SonarQube Scanner in Debug Mode" | |||||
echo "Executing SonarScanner in Debug Mode" | |||||
echo "SONAR_SCANNER_DEBUG_OPTS=\"-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000\"" | echo "SONAR_SCANNER_DEBUG_OPTS=\"-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000\"" | ||||
env SONAR_SCANNER_OPTS="$SONAR_SCANNER_OPTS" SONAR_SCANNER_DEBUG_OPTS="$SONAR_SCANNER_DEBUG_OPTS" "$(dirname "$0")"/sonar-scanner "$@" | env SONAR_SCANNER_OPTS="$SONAR_SCANNER_OPTS" SONAR_SCANNER_DEBUG_OPTS="$SONAR_SCANNER_DEBUG_OPTS" "$(dirname "$0")"/sonar-scanner "$@" |
@REM SonarQube Scanner Startup Script for Windows | |||||
@REM SonarScanner Startup Script for Windows | |||||
@REM | @REM | ||||
@REM Required ENV vars: | @REM Required ENV vars: | ||||
@REM JAVA_HOME - location of a JDK home dir | @REM JAVA_HOME - location of a JDK home dir | ||||
@REM | @REM | ||||
@REM Optional ENV vars: | @REM Optional ENV vars: | ||||
@REM SONAR_SCANNER_OPTS - parameters passed to the Java VM when running the SonarQube Scanner | |||||
@REM SONAR_SCANNER_OPTS - parameters passed to the Java VM when running the SonarScanner | |||||
@setlocal | @setlocal | ||||
@set SONAR_SCANNER_DEBUG_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 | @set SONAR_SCANNER_DEBUG_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 | ||||
echo "Executing SonarQube Scanner in Debug Mode" | |||||
echo "Executing SonarScanner in Debug Mode" | |||||
echo "SONAR_SCANNER_DEBUG_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000" | echo "SONAR_SCANNER_DEBUG_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000" | ||||
@call "%~dp0"sonar-scanner.bat %* | @call "%~dp0"sonar-scanner.bat %* |
@REM SonarQube Scanner Startup Script for Windows | |||||
@REM SonarScanner Startup Script for Windows | |||||
@REM | @REM | ||||
@REM Required ENV vars: | @REM Required ENV vars: | ||||
@REM JAVA_HOME - location of a JDK home dir | @REM JAVA_HOME - location of a JDK home dir | ||||
@REM | @REM | ||||
@REM Optional ENV vars: | @REM Optional ENV vars: | ||||
@REM SONAR_SCANNER_OPTS - parameters passed to the Java VM when running the SonarQube Scanner | |||||
@REM SONAR_SCANNER_OPTS - parameters passed to the Java VM when running the SonarScanner | |||||
@echo off | @echo off | ||||
import java.text.MessageFormat; | import java.text.MessageFormat; | ||||
import java.util.ArrayList; | import java.util.ArrayList; | ||||
import java.util.List; | import java.util.List; | ||||
import java.util.Locale; | |||||
import java.util.Map; | import java.util.Map; | ||||
import java.util.Properties; | import java.util.Properties; | ||||
import javax.annotation.Nullable; | import javax.annotation.Nullable; | ||||
private static final String PROPERTY_PROJECT_BASEDIR = "sonar.projectBaseDir"; | private static final String PROPERTY_PROJECT_BASEDIR = "sonar.projectBaseDir"; | ||||
private static final String PROPERTY_PROJECT_CONFIG_FILE = "sonar.projectConfigFile"; | 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 SONAR_PROJECT_PROPERTIES_FILENAME = "sonar-project.properties"; | ||||
private static final String PROPERTY_SONAR_HOST_URL = "sonar.host.url"; | |||||
private final Cli cli; | private final Cli cli; | ||||
private final Logs logger; | private final Logs logger; | ||||
return result; | 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) { | private Properties resolve(Properties props) { | ||||
PropertyResolver resolver = new PropertyResolver(props, env); | PropertyResolver resolver = new PropertyResolver(props, env); | ||||
return resolver.resolve(); | return resolver.resolve(); |
configureLogging(p); | configureLogging(p); | ||||
init(p); | init(p); | ||||
runner.start(); | runner.start(); | ||||
logger.info("SonarQube server " + runner.serverVersion()); | |||||
logger.info(String.format("Analyzing on %s", conf.isSonarCloud(null) ? "SonarCloud" : ("SonarQube server " + runner.serverVersion()))); | |||||
execute(stats, p); | execute(stats, p); | ||||
status = Exit.SUCCESS; | status = Exit.SUCCESS; | ||||
} catch (Throwable e) { | } catch (Throwable e) { | ||||
displayExecutionResult(stats, "FAILURE"); | displayExecutionResult(stats, "FAILURE"); | ||||
showError("Error during SonarQube Scanner execution", e, cli.isDebugEnabled()); | |||||
showError("Error during SonarScanner execution", e, cli.isDebugEnabled()); | |||||
status = isUserError(e) ? Exit.USER_ERROR : Exit.INTERNAL_ERROR; | status = isUserError(e) ? Exit.USER_ERROR : Exit.INTERNAL_ERROR; | ||||
} finally { | } finally { | ||||
exit.exit(status); | exit.exit(status); | ||||
private void checkSkip(Properties properties) { | private void checkSkip(Properties properties) { | ||||
if ("true".equalsIgnoreCase(properties.getProperty(ScanProperties.SKIP))) { | if ("true".equalsIgnoreCase(properties.getProperty(ScanProperties.SKIP))) { | ||||
logger.info("SonarQube Scanner analysis skipped"); | |||||
logger.info("SonarScanner analysis skipped"); | |||||
exit.exit(Exit.SUCCESS); | exit.exit(Exit.SUCCESS); | ||||
} | } | ||||
} | } | ||||
private void suggestDebugMode() { | private void suggestDebugMode() { | ||||
if (!cli.isEmbedded()) { | if (!cli.isEmbedded()) { | ||||
logger.error("Re-run SonarQube Scanner using the -X switch to enable full debug logging."); | |||||
logger.error("Re-run SonarScanner using the -X switch to enable full debug logging."); | |||||
} | } | ||||
} | } | ||||
} | } | ||||
static void print(Logs logger) { | static void print(Logs logger) { | ||||
logger.info("SonarQube Scanner " + ScannerVersion.version()); | |||||
logger.info("SonarScanner " + ScannerVersion.version()); | |||||
logger.info(java()); | logger.info(java()); | ||||
logger.info(os()); | logger.info(os()); | ||||
String scannerOpts = system.getenv("SONAR_SCANNER_OPTS"); | String scannerOpts = system.getenv("SONAR_SCANNER_OPTS"); |
properties = conf.properties(); | properties = conf.properties(); | ||||
assertThat(properties.get("sonar.prop")).isEqualTo("expected"); | 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(); | |||||
} | |||||
} | } |
main.execute(); | main.execute(); | ||||
verify(exit).exit(Exit.INTERNAL_ERROR); | verify(exit).exit(Exit.INTERNAL_ERROR); | ||||
verify(logs).error("Error during SonarQube Scanner execution", e); | |||||
verify(logs).error("Error during SonarScanner execution", e); | |||||
} | } | ||||
@Test | @Test | ||||
verify(runner).start(); | verify(runner).start(); | ||||
verify(runner, never()).execute(any()); | verify(runner, never()).execute(any()); | ||||
verify(exit).exit(Exit.INTERNAL_ERROR); | verify(exit).exit(Exit.INTERNAL_ERROR); | ||||
verify(logs).error("Error during SonarQube Scanner execution", e); | |||||
verify(logs).error("Error during SonarScanner execution", e); | |||||
} | } | ||||
@Test | @Test | ||||
Exception e = createException(false); | Exception e = createException(false); | ||||
testException(e, false, false, Exit.INTERNAL_ERROR); | testException(e, false, false, Exit.INTERNAL_ERROR); | ||||
verify(logs).error("Error during SonarQube Scanner execution", e); | |||||
verify(logs).error("Re-run SonarQube Scanner using the -X switch to enable full debug logging."); | |||||
verify(logs).error("Error during SonarScanner execution", e); | |||||
verify(logs).error("Re-run SonarScanner using the -X switch to enable full debug logging."); | |||||
} | } | ||||
@Test | @Test | ||||
testException(e, false, false, Exit.USER_ERROR); | testException(e, false, false, Exit.USER_ERROR); | ||||
verify(logs, times(5)).error(anyString()); | verify(logs, times(5)).error(anyString()); | ||||
verify(logs).error("Error during SonarQube Scanner execution"); | |||||
verify(logs).error("Error during SonarScanner execution"); | |||||
verify(logs).error("my message"); | verify(logs).error("my message"); | ||||
verify(logs).error("Caused by: A functional cause"); | verify(logs).error("Caused by: A functional cause"); | ||||
verify(logs).error(""); | verify(logs).error(""); | ||||
verify(logs).error("Re-run SonarQube Scanner using the -X switch to enable full debug logging."); | |||||
verify(logs).error("Re-run SonarScanner using the -X switch to enable full debug logging."); | |||||
} | } | ||||
@Test | @Test | ||||
testException(e, false, true, Exit.USER_ERROR); | testException(e, false, true, Exit.USER_ERROR); | ||||
verify(logs, times(4)).error(anyString()); | verify(logs, times(4)).error(anyString()); | ||||
verify(logs).error("Error during SonarQube Scanner execution"); | |||||
verify(logs).error("Error during SonarScanner execution"); | |||||
verify(logs).error("my message"); | verify(logs).error("my message"); | ||||
verify(logs).error("Caused by: A functional cause"); | verify(logs).error("Caused by: A functional cause"); | ||||
verify(logs).error(""); | verify(logs).error(""); | ||||
testException(e, true, false, Exit.USER_ERROR); | testException(e, true, false, Exit.USER_ERROR); | ||||
verify(logs, times(1)).error(anyString(), any(Throwable.class)); | verify(logs, times(1)).error(anyString(), any(Throwable.class)); | ||||
verify(logs).error("Error during SonarQube Scanner execution", e); | |||||
verify(logs).error("Error during SonarScanner execution", e); | |||||
} | } | ||||
@Test | @Test | ||||
testException(e, true, true, Exit.USER_ERROR); | testException(e, true, true, Exit.USER_ERROR); | ||||
verify(logs, times(1)).error(anyString(), any(Throwable.class)); | verify(logs, times(1)).error(anyString(), any(Throwable.class)); | ||||
verify(logs).error("Error during SonarQube Scanner execution", e); | |||||
verify(logs).error("Error during SonarScanner execution", e); | |||||
} | } | ||||
@Test | @Test | ||||
Exception e = createException(false); | Exception e = createException(false); | ||||
testException(e, true, false, Exit.INTERNAL_ERROR); | testException(e, true, false, Exit.INTERNAL_ERROR); | ||||
verify(logs).error("Error during SonarQube Scanner execution", e); | |||||
verify(logs, never()).error("Re-run SonarQube Scanner using the -X switch to enable full debug logging."); | |||||
verify(logs).error("Error during SonarScanner execution", e); | |||||
verify(logs, never()).error("Re-run SonarScanner using the -X switch to enable full debug logging."); | |||||
} | } | ||||
private void testException(Exception e, boolean debugEnabled, boolean isEmbedded, int expectedExitCode) { | private void testException(Exception e, boolean debugEnabled, boolean isEmbedded, int expectedExitCode) { | ||||
Main main = new Main(exit, cli, conf, scannerFactory, logs); | Main main = new Main(exit, cli, conf, scannerFactory, logs); | ||||
main.execute(); | main.execute(); | ||||
verify(logs).info("SonarQube Scanner analysis skipped"); | |||||
verify(logs).info("SonarScanner analysis skipped"); | |||||
InOrder inOrder = Mockito.inOrder(exit, scannerFactory); | InOrder inOrder = Mockito.inOrder(exit, scannerFactory); | ||||
inOrder.verify(exit, times(1)).exit(Exit.SUCCESS); | inOrder.verify(exit, times(1)).exit(Exit.SUCCESS); | ||||
Main main = new Main(exit, cli, conf, scannerFactory, logs); | Main main = new Main(exit, cli, conf, scannerFactory, logs); | ||||
main.execute(); | main.execute(); | ||||
verify(logs).info("SonarQube server 5.5"); | |||||
verify(logs).info("Analyzing on SonarQube server 5.5"); | |||||
} | |||||
@Test | |||||
public void should_log_SonarCloud_server() { | |||||
Properties p = new Properties(); | |||||
when(conf.properties()).thenReturn(p); | |||||
when(conf.isSonarCloud(null)).thenReturn(true); | |||||
Main main = new Main(exit, cli, conf, scannerFactory, logs); | |||||
main.execute(); | |||||
verify(logs).info("Analyzing on SonarCloud"); | |||||
} | } | ||||
@Test | @Test |
verify(mockSystem).getProperty("os.version"); | verify(mockSystem).getProperty("os.version"); | ||||
verify(mockSystem).getenv("SONAR_SCANNER_OPTS"); | verify(mockSystem).getenv("SONAR_SCANNER_OPTS"); | ||||
verify(logs, never()).info("SonarQube Scanner null"); | |||||
verify(logs).info("SonarQube Scanner " + ScannerVersion.version()); | |||||
verify(logs, never()).info("SonarScanner null"); | |||||
verify(logs).info("SonarScanner " + ScannerVersion.version()); | |||||
verify(logs).info("Java 1.9 oracle (64-bit)"); | verify(logs).info("Java 1.9 oracle (64-bit)"); | ||||
verify(logs).info("linux 2.5 x64"); | verify(logs).info("linux 2.5 x64"); | ||||
verify(logs).info("SONAR_SCANNER_OPTS=arg"); | verify(logs).info("SONAR_SCANNER_OPTS=arg"); |