From: Julien HENRY Date: Fri, 21 Jun 2019 21:36:13 +0000 (+0200) Subject: SQSCANNER-56 Stacktrace are only displayed in verbose mode X-Git-Tag: 3.4.0.1729~1 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=24c7dea34bbec0b53b199fc443d37dd63a01d709;p=sonar-scanner-cli.git SQSCANNER-56 Stacktrace are only displayed in verbose mode --- diff --git a/src/main/java/org/sonarsource/scanner/cli/Main.java b/src/main/java/org/sonarsource/scanner/cli/Main.java index 3e74b17..5764423 100644 --- a/src/main/java/org/sonarsource/scanner/cli/Main.java +++ b/src/main/java/org/sonarsource/scanner/cli/Main.java @@ -122,7 +122,7 @@ public class Main { } private void showError(String message, Throwable e, boolean debug) { - if (debug && !isUserError(e)) { + if (debug || !isUserError(e)) { logger.error(message, e); } else { logger.error(message); diff --git a/src/test/java/org/sonarsource/scanner/cli/MainTest.java b/src/test/java/org/sonarsource/scanner/cli/MainTest.java index 7090196..dc1013b 100644 --- a/src/test/java/org/sonarsource/scanner/cli/MainTest.java +++ b/src/test/java/org/sonarsource/scanner/cli/MainTest.java @@ -34,6 +34,7 @@ import org.sonarsource.scanner.api.ScanProperties; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; @@ -111,46 +112,59 @@ public class MainTest { } @Test - public void show_error_MessageException() { + public void show_stacktrace() { + Exception e = createException(false); + 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."); + } + + @Test + public void dont_show_MessageException_stacktrace() { Exception e = createException(true); testException(e, false, false, Exit.USER_ERROR); + verify(logs, times(5)).error(anyString()); verify(logs).error("Error during SonarQube Scanner execution"); - verify(logs).error("Caused by: NPE"); + verify(logs).error("my message"); + verify(logs).error("Caused by: A functional cause"); + verify(logs).error(""); verify(logs).error("Re-run SonarQube Scanner using the -X switch to enable full debug logging."); } @Test - public void show_error_MessageException_embedded() { + public void dont_show_MessageException_stacktrace_embedded() { Exception e = createException(true); testException(e, false, true, Exit.USER_ERROR); + verify(logs, times(4)).error(anyString()); verify(logs).error("Error during SonarQube Scanner execution"); - verify(logs).error("Caused by: NPE"); + verify(logs).error("my message"); + verify(logs).error("Caused by: A functional cause"); + verify(logs).error(""); } - + @Test - public void show_error_MessageException_debug() { + public void show_MessageException_stacktrace_in_debug() { Exception e = createException(true); testException(e, true, false, Exit.USER_ERROR); - verify(logs).error("Error during SonarQube Scanner execution"); - verify(logs).error("my message"); - verify(logs).error("Caused by: NPE"); + verify(logs, times(1)).error(anyString(), any(Throwable.class)); + verify(logs).error("Error during SonarQube Scanner execution", e); } @Test - public void show_error_MessageException_debug_embedded() { + public void show_MessageException_stacktrace_in_debug_embedded() { Exception e = createException(true); testException(e, true, true, Exit.USER_ERROR); - verify(logs).error("Error during SonarQube Scanner execution"); - verify(logs).error("my message"); - verify(logs).error("Caused by: NPE"); + verify(logs, times(1)).error(anyString(), any(Throwable.class)); + verify(logs).error("Error during SonarQube Scanner execution", e); } @Test - public void show_error_debug() { + public void show_stacktrace_in_debug() { Exception e = createException(false); testException(e, true, false, Exit.INTERNAL_ERROR); @@ -175,7 +189,7 @@ public class MainTest { private Exception createException(boolean messageException) { Exception e; if (messageException) { - e = new MessageException("my message", new NullPointerException("NPE")); + e = new MessageException("my message", new IllegalStateException("A functional cause")); } else { e = new IllegalStateException("Error", new NullPointerException("NPE")); }