From a3fddd185169455bd564e67c0d9d0d9cf272c5a9 Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Mon, 29 Jun 2015 17:16:28 +0200 Subject: [PATCH] SONARUNNER-137 Do not stop after error in interactive mode --- .../main/java/org/sonar/runner/cli/Main.java | 29 ++++++++++++++----- .../java/org/sonar/runner/cli/Shutdown.java | 2 -- .../java/org/sonar/runner/cli/MainTest.java | 23 ++++++++++++++- 3 files changed, 44 insertions(+), 10 deletions(-) diff --git a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Main.java b/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Main.java index 3552fbc..6971bc6 100644 --- a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Main.java +++ b/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Main.java @@ -71,13 +71,10 @@ public class Main { init(p); runner.start(); - runAnalysis(stats, p); - if (cli.isInteractive()) { - while (waitForUser()) { - stats = new Stats().start(); - runAnalysis(stats, p); - } + interactiveLoop(p); + } else { + runAnalysis(stats, p); } } catch (Exception e) { displayExecutionResult(stats, "FAILURE"); @@ -89,6 +86,18 @@ public class Main { shutdown.exit(Exit.SUCCESS); } + private void interactiveLoop(Properties p) throws IOException { + do { + Stats stats = new Stats().start(); + try { + runAnalysis(stats, p); + } catch (Exception e) { + displayExecutionResult(stats, "FAILURE"); + showError("Error during Sonar runner execution", e, cli.isDisplayStackTrace()); + } + } while (waitForUser()); + } + private void init(Properties p) throws IOException { SystemInfo.print(); if (cli.isDisplayVersionOnly()) { @@ -118,13 +127,19 @@ public class Main { return false; } - Logs.info(""); + System.out.println(""); + System.out.println(""); String line = inputReader.readLine(); shutdown.signalReady(false); return line != null; } + // Visible for testing + void setInputReader(BufferedReader inputReader) { + this.inputReader = inputReader; + } + private static void displayExecutionResult(Stats stats, String resultMsg) { Logs.info("------------------------------------------------------------------------"); Logs.info("EXECUTION " + resultMsg); diff --git a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Shutdown.java b/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Shutdown.java index 857faac..de5341a 100644 --- a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Shutdown.java +++ b/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Shutdown.java @@ -30,7 +30,6 @@ class Shutdown { private boolean exiting = false; private Object lock = new Object(); private Exit exit; - private Thread t; Shutdown(Exit exit) { this(exit, DEFAULT_MAX_WAIT); @@ -51,7 +50,6 @@ class Shutdown { void signalReady(boolean ready) { synchronized (lock) { - System.out.println("READY: " + ready); this.isReady = ready; lock.notifyAll(); } diff --git a/sonar-runner-cli/src/test/java/org/sonar/runner/cli/MainTest.java b/sonar-runner-cli/src/test/java/org/sonar/runner/cli/MainTest.java index 4ae594d..5b7d8fe 100644 --- a/sonar-runner-cli/src/test/java/org/sonar/runner/cli/MainTest.java +++ b/sonar-runner-cli/src/test/java/org/sonar/runner/cli/MainTest.java @@ -19,6 +19,7 @@ */ package org.sonar.runner.cli; +import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; @@ -76,7 +77,7 @@ public class MainTest { } @Test - public void should_fail_on_error() { + public void should_stop_on_error() { EmbeddedRunner runner = mock(EmbeddedRunner.class); doThrow(new IllegalStateException("Error")).when(runner).runAnalysis(any(Properties.class)); when(runnerFactory.create(any(Properties.class))).thenReturn(runner); @@ -84,9 +85,29 @@ public class MainTest { Main main = new Main(exit, cli, conf, runnerFactory); main.execute(); + verify(runner).stop(); verify(exit).exit(Exit.ERROR); } + @Test + public void should_not_stop_on_error_in_interactive_mode() throws Exception { + EmbeddedRunner runner = mock(EmbeddedRunner.class); + doThrow(new IllegalStateException("Error")).when(runner).runAnalysis(any(Properties.class)); + when(runnerFactory.create(any(Properties.class))).thenReturn(runner); + when(cli.isInteractive()).thenReturn(true); + + Main main = new Main(exit, cli, conf, runnerFactory); + BufferedReader inputReader = mock(BufferedReader.class); + when(inputReader.readLine()).thenReturn(""); + when(exit.shouldExit()).thenReturn(false).thenReturn(true); + main.setInputReader(inputReader); + main.execute(); + + verify(runner, times(2)).runAnalysis(any(Properties.class)); + verify(runner).stop(); + verify(exit).exit(Exit.SUCCESS); + } + @Test public void should_only_display_version() throws IOException { -- 2.39.5