]> source.dussan.org Git - sonar-scanner-cli.git/commitdiff
SQSCANNER-36 Don't loose error message in case of low level error (like OOM)
authorJulien HENRY <julien.henry@sonarsource.com>
Fri, 13 Jan 2017 10:36:34 +0000 (11:36 +0100)
committerJulien HENRY <henryju@yahoo.fr>
Fri, 13 Jan 2017 10:58:22 +0000 (11:58 +0100)
src/main/java/org/sonarsource/scanner/cli/Main.java
src/test/java/org/sonarsource/scanner/cli/MainTest.java

index 8d030e408ec72dd58064c95d0a1afc102d03bf74..a273381f379116f1b436d3c7d8bed28c5e4cafae 100644 (file)
@@ -65,22 +65,26 @@ public class Main {
     Stats stats = new Stats(logger).start();
 
     int status = Exit.ERROR;
+    boolean started = false;
     try {
       Properties p = conf.properties();
       checkSkip(p);
       configureLogging(p);
       init(p);
       runner.start();
+      started = true;
       logger.info("SonarQube server " + runner.serverVersion());
       runAnalysis(stats, p);
       status = Exit.SUCCESS;
-    } catch (Exception e) {
+    } catch (Throwable e) {
       status = Exit.ERROR;
       displayExecutionResult(stats, "FAILURE");
       showError("Error during SonarQube Scanner execution", e, cli.isDebugEnabled());
     } finally {
       try {
-        runner.stop();
+        if (started) {
+          runner.stop();
+        }
       } catch (Throwable e) {
         status = Exit.ERROR;
         logger.error("Unable to properly stop the scanner", e);
index b410cbdca551952261c87b4f9235971c61fd71eb..9fd5f22fde14e504f8449352e54f6021d5d6f268 100644 (file)
@@ -79,7 +79,7 @@ public class MainTest {
   }
 
   @Test
-  public void should_stop_on_error() {
+  public void should_call_stop_on_error_during_analysis() {
     EmbeddedScanner runner = mock(EmbeddedScanner.class);
     Exception e = new NullPointerException("NPE");
     e = new IllegalStateException("Error", e);
@@ -94,6 +94,24 @@ public class MainTest {
     verify(logs).error("Error during SonarQube Scanner execution", e);
   }
 
+  @Test
+  public void should_not_call_stop_on_error_during_start() {
+    EmbeddedScanner runner = mock(EmbeddedScanner.class);
+    Exception e = new NullPointerException("NPE");
+    e = new IllegalStateException("Error", e);
+    doThrow(e).when(runner).start();
+    when(runnerFactory.create(any(Properties.class))).thenReturn(runner);
+
+    Main main = new Main(exit, cli, conf, runnerFactory, logs);
+    main.execute();
+
+    verify(runner).start();
+    verify(runner, never()).runAnalysis(any());
+    verify(runner, never()).stop();
+    verify(exit).exit(Exit.ERROR);
+    verify(logs).error("Error during SonarQube Scanner execution", e);
+  }
+
   @Test
   public void should_exit_on_error() {
     EmbeddedScanner runner = mock(EmbeddedScanner.class);