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");
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()) {
return false;
}
- Logs.info("<Press enter to restart analysis>");
+ System.out.println("");
+ System.out.println("<Press enter to restart analysis or Ctrl+C to exit the interactive mode>");
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);
*/
package org.sonar.runner.cli;
+import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
}
@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);
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 {