From c6cb6e144fe0238bdb181fc2713374a4bab7a9dc Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Tue, 8 Mar 2016 14:23:02 +0100 Subject: [PATCH] SONARUNNER-153 sonar.verbose and sonar.log.level do not work --- .../java/com/sonar/runner/it/JavaTest.java | 13 ++++++ .../java/org/sonarsource/scanner/cli/Cli.java | 11 +++-- .../org/sonarsource/scanner/cli/Logs.java | 2 +- .../org/sonarsource/scanner/cli/Main.java | 24 +++++++--- .../org/sonarsource/scanner/cli/CliTest.java | 9 ++-- .../org/sonarsource/scanner/cli/MainTest.java | 45 ++++++++++++++++++- 6 files changed, 85 insertions(+), 19 deletions(-) diff --git a/it/src/test/java/com/sonar/runner/it/JavaTest.java b/it/src/test/java/com/sonar/runner/it/JavaTest.java index e997a8b..e092ebb 100644 --- a/it/src/test/java/com/sonar/runner/it/JavaTest.java +++ b/it/src/test/java/com/sonar/runner/it/JavaTest.java @@ -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")) diff --git a/src/main/java/org/sonarsource/scanner/cli/Cli.java b/src/main/java/org/sonarsource/scanner/cli/Cli.java index 979cd4a..b660a86 100644 --- a/src/main/java/org/sonarsource/scanner/cli/Cli.java +++ b/src/main/java/org/sonarsource/scanner/cli/Cli.java @@ -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; } diff --git a/src/main/java/org/sonarsource/scanner/cli/Logs.java b/src/main/java/org/sonarsource/scanner/cli/Logs.java index 3d3d191..e4fc31b 100644 --- a/src/main/java/org/sonarsource/scanner/cli/Logs.java +++ b/src/main/java/org/sonarsource/scanner/cli/Logs.java @@ -39,7 +39,7 @@ public class Logs { public void setDisplayStackTrace(boolean displayStackTrace) { this.displayStackTrace = displayStackTrace; } - + public boolean isDebugEnabled() { return debugEnabled; } diff --git a/src/main/java/org/sonarsource/scanner/cli/Main.java b/src/main/java/org/sonarsource/scanner/cli/Main.java index a4b2526..548caa3 100644 --- a/src/main/java/org/sonarsource/scanner/cli/Main.java +++ b/src/main/java/org/sonarsource/scanner/cli/Main.java @@ -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(); } } diff --git a/src/test/java/org/sonarsource/scanner/cli/CliTest.java b/src/test/java/org/sonarsource/scanner/cli/CliTest.java index c1b4a4e..8e9a4ed 100644 --- a/src/test/java/org/sonarsource/scanner/cli/CliTest.java +++ b/src/test/java/org/sonarsource/scanner/cli/CliTest.java @@ -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(); } diff --git a/src/test/java/org/sonarsource/scanner/cli/MainTest.java b/src/test/java/org/sonarsource/scanner/cli/MainTest.java index 8e9cdf3..e2fd69c 100644 --- a/src/test/java/org/sonarsource/scanner/cli/MainTest.java +++ b/src/test/java/org/sonarsource/scanner/cli/MainTest.java @@ -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 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 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); -- 2.39.5