@@ -233,6 +233,19 @@ public class JavaTest extends ScannerTestCase { | |||
assertThat(log).contains("(analysis is platform dependent)"); | |||
} | |||
/** | |||
* SONARUNNER-153 | |||
*/ | |||
@Test | |||
public void should_enable_verbose() { | |||
// this line should appear in all versions (LTS-DEV) in debug only | |||
String expectedLog = "Available languages:"; | |||
SonarRunner build = newScanner(new File("projects/java-sample")) | |||
.setProperty("sonar.verbose", "true"); | |||
String logs = orchestrator.executeBuild(build).getLogs(); | |||
assertThat(logs).contains(expectedLog); | |||
} | |||
@Test | |||
public void should_fail_if_unable_to_connect() { | |||
SonarRunner build = newScanner(new File("projects/java-sample")) |
@@ -24,7 +24,7 @@ import org.sonar.runner.api.RunnerProperties; | |||
class Cli { | |||
private boolean debugMode = false; | |||
private boolean debugEnabled = false; | |||
private boolean displayVersionOnly = false; | |||
private boolean displayStackTrace = false; | |||
private boolean interactive = false; | |||
@@ -37,8 +37,8 @@ class Cli { | |||
this.logger = logger; | |||
} | |||
boolean isDebugMode() { | |||
return debugMode; | |||
boolean isDebugEnabled() { | |||
return debugEnabled; | |||
} | |||
boolean isDisplayVersionOnly() { | |||
@@ -77,8 +77,7 @@ class Cli { | |||
} else if ("-X".equals(arg) || "--debug".equals(arg)) { | |||
props.setProperty("sonar.verbose", "true"); | |||
displayStackTrace = true; | |||
debugMode = true; | |||
debugEnabled = true; | |||
logger.setDebugEnabled(true); | |||
logger.setDisplayStackTrace(true); | |||
@@ -113,7 +112,7 @@ class Cli { | |||
private void reset() { | |||
props.clear(); | |||
debugMode = false; | |||
debugEnabled = false; | |||
displayStackTrace = false; | |||
displayVersionOnly = false; | |||
} |
@@ -39,7 +39,7 @@ public class Logs { | |||
public void setDisplayStackTrace(boolean displayStackTrace) { | |||
this.displayStackTrace = displayStackTrace; | |||
} | |||
public boolean isDebugEnabled() { | |||
return debugEnabled; | |||
} |
@@ -68,9 +68,10 @@ public class Main { | |||
void execute() { | |||
Stats stats = new Stats(logger).start(); | |||
try { | |||
Properties p = conf.properties(); | |||
configureLogging(p); | |||
init(p); | |||
runner.start(); | |||
@@ -81,7 +82,7 @@ public class Main { | |||
} | |||
} catch (Exception e) { | |||
displayExecutionResult(stats, "FAILURE"); | |||
showError("Error during SonarQube Scanner execution", e, cli.isDisplayStackTrace()); | |||
showError("Error during SonarQube Scanner execution", e, cli.isDisplayStackTrace() || cli.isDebugEnabled()); | |||
shutdown.exit(Exit.ERROR); | |||
} | |||
@@ -96,7 +97,7 @@ public class Main { | |||
runAnalysis(stats, p); | |||
} catch (Exception e) { | |||
displayExecutionResult(stats, "FAILURE"); | |||
showError("Error during SonarQube Scanner execution", e, cli.isDisplayStackTrace()); | |||
showError("Error during SonarQube Scanner execution", e, cli.isDisplayStackTrace() || cli.isDebugEnabled()); | |||
} | |||
} while (waitForUser()); | |||
} | |||
@@ -114,6 +115,19 @@ public class Main { | |||
runner = runnerFactory.create(p); | |||
} | |||
private void configureLogging(Properties props) throws IOException { | |||
if("true".equals(props.getProperty("sonar.verbose")) | |||
|| "DEBUG".equalsIgnoreCase(props.getProperty("sonar.log.level")) | |||
|| "TRACE".equalsIgnoreCase(props.getProperty("sonar.log.level")) ) { | |||
logger.setDebugEnabled(true); | |||
logger.setDisplayStackTrace(true); | |||
} | |||
if(cli.isDisplayStackTrace()) { | |||
logger.setDisplayStackTrace(true); | |||
} | |||
} | |||
private void runAnalysis(Stats stats, Properties p) { | |||
runner.runAnalysis(p); | |||
displayExecutionResult(stats, "SUCCESS"); | |||
@@ -154,7 +168,7 @@ public class Main { | |||
private void showError(String message, Throwable e, boolean showStackTrace) { | |||
if (showStackTrace) { | |||
logger.error(message, e); | |||
if (!cli.isDebugMode()) { | |||
if (!cli.isDebugEnabled()) { | |||
logger.error(""); | |||
suggestDebugMode(); | |||
} | |||
@@ -172,7 +186,7 @@ public class Main { | |||
} | |||
logger.error(""); | |||
logger.error("To see the full stack trace of the errors, re-run SonarQube Scanner with the -e switch."); | |||
if (!cli.isDebugMode()) { | |||
if (!cli.isDebugEnabled()) { | |||
suggestDebugMode(); | |||
} | |||
} |
@@ -37,7 +37,7 @@ public class CliTest { | |||
public void should_parse_empty_arguments() { | |||
cli.parse(new String[0]); | |||
assertThat(cli.properties()).isNotEmpty(); | |||
assertThat(cli.isDebugMode()).isFalse(); | |||
assertThat(cli.isDebugEnabled()).isFalse(); | |||
assertThat(cli.isDisplayStackTrace()).isFalse(); | |||
assertThat(cli.isDisplayVersionOnly()).isFalse(); | |||
} | |||
@@ -69,15 +69,14 @@ public class CliTest { | |||
@Test | |||
public void should_enable_debug_mode() { | |||
cli.parse(new String[] {"-X"}); | |||
assertThat(cli.isDebugMode()).isTrue(); | |||
assertThat(cli.isDisplayStackTrace()).isTrue(); | |||
assertThat(cli.isDebugEnabled()).isTrue(); | |||
assertThat(cli.properties().get("sonar.verbose")).isEqualTo("true"); | |||
} | |||
@Test | |||
public void should_enable_stacktrace_log() { | |||
cli.parse(new String[] {"-e"}); | |||
assertThat(cli.isDebugMode()).isFalse(); | |||
assertThat(cli.isDebugEnabled()).isFalse(); | |||
assertThat(cli.isDisplayStackTrace()).isTrue(); | |||
assertThat(cli.properties().get("sonar.verbose")).isNull(); | |||
} | |||
@@ -85,7 +84,7 @@ public class CliTest { | |||
@Test | |||
public void should_disable_debug_mode_and_stacktrace_log_by_default() { | |||
cli.parse(new String[0]); | |||
assertThat(cli.isDebugMode()).isFalse(); | |||
assertThat(cli.isDebugEnabled()).isFalse(); | |||
assertThat(cli.isDisplayStackTrace()).isFalse(); | |||
assertThat(cli.properties().get("sonar.verbose")).isNull(); | |||
} |
@@ -27,12 +27,14 @@ import java.nio.charset.StandardCharsets; | |||
import java.util.Properties; | |||
import org.junit.Before; | |||
import org.junit.Test; | |||
import org.mockito.ArgumentCaptor; | |||
import org.mockito.InOrder; | |||
import org.mockito.Mock; | |||
import org.mockito.Mockito; | |||
import org.mockito.MockitoAnnotations; | |||
import org.sonar.runner.api.EmbeddedRunner; | |||
import static org.fest.assertions.Assertions.assertThat; | |||
import static org.mockito.Matchers.any; | |||
import static org.mockito.Mockito.doThrow; | |||
import static org.mockito.Mockito.mock; | |||
@@ -62,7 +64,6 @@ public class MainTest { | |||
MockitoAnnotations.initMocks(this); | |||
when(runnerFactory.create(any(Properties.class))).thenReturn(runner); | |||
when(conf.properties()).thenReturn(properties); | |||
} | |||
@Test | |||
@@ -149,6 +150,46 @@ public class MainTest { | |||
inOrder.verify(shutdown, times(1)).exit(Exit.SUCCESS); | |||
} | |||
@Test | |||
public void should_configure_logging() throws IOException { | |||
Properties p = new Properties(); | |||
p.put("sonar.verbose", "true"); | |||
when(conf.properties()).thenReturn(p); | |||
Main main = new Main(shutdown, cli, conf, runnerFactory, logs); | |||
main.execute(); | |||
// Logger used for callback should have debug enabled | |||
verify(logs).setDebugEnabled(true); | |||
verify(logs).setDisplayStackTrace(true); | |||
ArgumentCaptor<Properties> propertiesCapture = ArgumentCaptor.forClass(Properties.class); | |||
verify(runner).runAnalysis(propertiesCapture.capture()); | |||
Properties analysisProps = propertiesCapture.getValue(); | |||
assertThat(analysisProps.getProperty("sonar.verbose")).isEqualTo("true"); | |||
} | |||
@Test | |||
public void should_configure_logging_trace() throws IOException { | |||
Properties p = new Properties(); | |||
p.put("sonar.log.level", "TRACE"); | |||
when(conf.properties()).thenReturn(p); | |||
Main main = new Main(shutdown, cli, conf, runnerFactory, logs); | |||
main.execute(); | |||
// Logger used for callback should have debug enabled | |||
verify(logs).setDebugEnabled(true); | |||
verify(logs).setDisplayStackTrace(true); | |||
ArgumentCaptor<Properties> propertiesCapture = ArgumentCaptor.forClass(Properties.class); | |||
verify(runner).runAnalysis(propertiesCapture.capture()); | |||
Properties analysisProps = propertiesCapture.getValue(); | |||
assertThat(analysisProps.getProperty("sonar.log.level")).isEqualTo("TRACE"); | |||
} | |||
@Test(timeout = 30000) | |||
public void test_interactive_mode() throws IOException { | |||
String inputStr = "qwe" + System.lineSeparator() + "qwe" + System.lineSeparator(); | |||
@@ -157,7 +198,7 @@ public class MainTest { | |||
input.close(); | |||
when(cli.isInteractive()).thenReturn(true); | |||
when(cli.isDebugMode()).thenReturn(true); | |||
when(cli.isDebugEnabled()).thenReturn(true); | |||
when(cli.isDisplayStackTrace()).thenReturn(true); | |||
Main main = new Main(shutdown, cli, conf, runnerFactory, logs); |