]> source.dussan.org Git - sonar-scanner-cli.git/commitdiff
SONARUNNER-137 Don't prevent aborting process in non interactive mode
authorJulien HENRY <julien.henry@sonarsource.com>
Mon, 28 Sep 2015 16:06:36 +0000 (18:06 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Mon, 28 Sep 2015 16:06:36 +0000 (18:06 +0200)
sonar-runner-cli/src/main/java/org/sonar/runner/cli/Cli.java
sonar-runner-cli/src/main/java/org/sonar/runner/cli/Main.java
sonar-runner-cli/src/main/java/org/sonar/runner/cli/Shutdown.java
sonar-runner-cli/src/test/java/org/sonar/runner/cli/CliTest.java
sonar-runner-cli/src/test/java/org/sonar/runner/cli/MainTest.java
sonar-runner-cli/src/test/java/org/sonar/runner/cli/ShutdownTest.java

index d0a350e7f2ecd0f0d69e8d01d562ed365491f63b..3b627dfde4dcead2b72790f5113c4ec8ab4bcaeb 100644 (file)
@@ -29,11 +29,11 @@ class Cli {
   private boolean displayStackTrace = false;
   private boolean interactive = false;
   private final Properties props = new Properties();
-  private final Shutdown shutdown;
+  private final Exit exit;
   private final Logs logger;
 
-  public Cli(Shutdown shutdown, Logs logger) {
-    this.shutdown = shutdown;
+  public Cli(Exit exit, Logs logger) {
+    this.exit = exit;
     this.logger = logger;
   }
 
@@ -147,6 +147,6 @@ class Cli {
     logger.info(" -v,--version          Display version information");
     logger.info(" -X,--debug            Produce execution debug output");
     logger.info(" -i,--interactive      Run interactively");
-    shutdown.exit(Exit.SUCCESS);
+    exit.exit(Exit.SUCCESS);
   }
 }
index 71488b3cf536c0b4ba703ede520fc115a364142d..457380b90e9750280f211a745c0f556ce0b5b8f7 100644 (file)
@@ -58,10 +58,10 @@ public class Main {
 
   public static void main(String[] args) {
     Exit exit = new Exit();
-    Shutdown shutdown = new Shutdown(exit);
     Logs logs = new Logs();
-    Cli cli = new Cli(shutdown, logs).parse(args);
+    Cli cli = new Cli(exit, logs).parse(args);
     cli.verify();
+    Shutdown shutdown = new Shutdown(exit, cli.isInteractive());
     Main main = new Main(shutdown, cli, new Conf(cli, logs), new RunnerFactory(logs), logs);
     main.execute();
   }
index 3c52bc5ca1c01f0d4bfe9afdf732259d856920e1..7da4f93441d9f1f727fbd76ea77d6f27e14b7581 100644 (file)
@@ -31,14 +31,16 @@ class Shutdown {
   private Object lock = new Object();
   private Exit exit;
 
-  Shutdown(Exit exit) {
-    this(exit, DEFAULT_MAX_WAIT);
+  Shutdown(Exit exit, boolean isInteractive) {
+    this(exit, isInteractive, DEFAULT_MAX_WAIT);
   }
 
-  Shutdown(Exit exit, long maxWait) {
+  Shutdown(Exit exit, boolean isInteractive, long maxWait) {
     this.maxWait = maxWait;
     this.exit = exit;
-    Runtime.getRuntime().addShutdownHook(hook);
+    if (isInteractive) {
+      Runtime.getRuntime().addShutdownHook(hook);
+    }
   }
 
   void exit(int status) {
index b8f6901b38b860dadd48493f290208568ebbfbf0..52f46e0ad29bbd3dae759e3345706face41b0453 100644 (file)
  */
 package org.sonar.runner.cli;
 
-import static org.mockito.Mockito.mock;
-
-import static org.mockito.Mockito.verify;
-
 import org.junit.Test;
-import org.sonar.runner.cli.Cli;
-import org.sonar.runner.cli.Exit;
 
 import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
 
 public class CliTest {
-  Shutdown shutdown = mock(Shutdown.class);
+  Exit exit = mock(Exit.class);
   Logs logs = new Logs();
-  Cli cli = new Cli(shutdown, logs);
+  Cli cli = new Cli(exit, logs);
 
   @Test
   public void should_parse_empty_arguments() {
@@ -55,7 +51,7 @@ public class CliTest {
   public void dont_allow_interactive_fork() {
     cli.parse(new String[] {"-i", "-DsonarRunner.mode=fork"});
     cli.verify();
-    verify(shutdown).exit(Exit.SUCCESS);
+    verify(exit).exit(Exit.SUCCESS);
   }
 
   @Test
index a0dec5c0129765689c7ebd36cc4de0586ea5bb9e..7f1995c37815f6d14696331e0038f7182bb87489 100644 (file)
@@ -25,7 +25,6 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.nio.charset.StandardCharsets;
 import java.util.Properties;
-
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.InOrder;
@@ -33,6 +32,7 @@ import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.mockito.MockitoAnnotations;
 import org.sonar.runner.api.EmbeddedRunner;
+
 import static org.mockito.Matchers.any;
 import static org.mockito.Mockito.doThrow;
 import static org.mockito.Mockito.mock;
@@ -43,7 +43,7 @@ import static org.mockito.Mockito.when;
 public class MainTest {
 
   @Mock
-  private Shutdown exit;
+  private Shutdown shutdown;
   @Mock
   private Cli cli;
   @Mock
@@ -67,10 +67,10 @@ public class MainTest {
 
   @Test
   public void should_execute_runner() {
-    Main main = new Main(exit, cli, conf, runnerFactory, logs);
+    Main main = new Main(shutdown, cli, conf, runnerFactory, logs);
     main.execute();
 
-    verify(exit).exit(Exit.SUCCESS);
+    verify(shutdown).exit(Exit.SUCCESS);
     verify(runnerFactory).create(properties);
 
     verify(runner, times(1)).start();
@@ -86,30 +86,30 @@ public class MainTest {
     doThrow(e).when(runner).runAnalysis(any(Properties.class));
     when(runnerFactory.create(any(Properties.class))).thenReturn(runner);
 
-    Main main = new Main(exit, cli, conf, runnerFactory, logs);
+    Main main = new Main(shutdown, cli, conf, runnerFactory, logs);
     main.execute();
 
     verify(runner).stop();
-    verify(exit).exit(Exit.ERROR);
-    verify(logs).error("Caused by: NPE" );
-    
+    verify(shutdown).exit(Exit.ERROR);
+    verify(logs).error("Caused by: NPE");
+
   }
-  
+
   @Test
   public void show_error_stacktrace() {
     Exception e = new NullPointerException("NPE");
     e = new IllegalStateException("Error", e);
     when(cli.isDisplayStackTrace()).thenReturn(true);
-    
+
     EmbeddedRunner runner = mock(EmbeddedRunner.class);
     doThrow(e).when(runner).runAnalysis(any(Properties.class));
     when(runnerFactory.create(any(Properties.class))).thenReturn(runner);
 
-    Main main = new Main(exit, cli, conf, runnerFactory, logs);
+    Main main = new Main(shutdown, cli, conf, runnerFactory, logs);
     main.execute();
 
     verify(runner).stop();
-    verify(exit).exit(Exit.ERROR);
+    verify(shutdown).exit(Exit.ERROR);
     verify(logs).error("Error during Sonar runner execution", e);
   }
 
@@ -120,16 +120,16 @@ public class MainTest {
     when(runnerFactory.create(any(Properties.class))).thenReturn(runner);
     when(cli.isInteractive()).thenReturn(true);
 
-    Main main = new Main(exit, cli, conf, runnerFactory, logs);
+    Main main = new Main(shutdown, cli, conf, runnerFactory, logs);
     BufferedReader inputReader = mock(BufferedReader.class);
     when(inputReader.readLine()).thenReturn("");
-    when(exit.shouldExit()).thenReturn(false).thenReturn(true);
+    when(shutdown.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);
+    verify(shutdown).exit(Exit.SUCCESS);
   }
 
   @Test
@@ -139,14 +139,14 @@ public class MainTest {
     when(cli.isDisplayVersionOnly()).thenReturn(true);
     when(conf.properties()).thenReturn(p);
 
-    Main main = new Main(exit, cli, conf, runnerFactory, logs);
+    Main main = new Main(shutdown, cli, conf, runnerFactory, logs);
     main.execute();
 
-    InOrder inOrder = Mockito.inOrder(exit, runnerFactory);
+    InOrder inOrder = Mockito.inOrder(shutdown, runnerFactory);
 
-    inOrder.verify(exit, times(1)).exit(Exit.SUCCESS);
+    inOrder.verify(shutdown, times(1)).exit(Exit.SUCCESS);
     inOrder.verify(runnerFactory, times(1)).create(p);
-    inOrder.verify(exit, times(1)).exit(Exit.SUCCESS);
+    inOrder.verify(shutdown, times(1)).exit(Exit.SUCCESS);
   }
 
   @Test(timeout = 30000)
@@ -160,7 +160,7 @@ public class MainTest {
     when(cli.isDebugMode()).thenReturn(true);
     when(cli.isDisplayStackTrace()).thenReturn(true);
 
-    Main main = new Main(exit, cli, conf, runnerFactory, logs);
+    Main main = new Main(shutdown, cli, conf, runnerFactory, logs);
     main.execute();
 
     verify(runner, times(1)).start();
index ec401dbe4489e760e8fe8c38cb719c827060afcf..5f739fc593d3cb810b96defa988788f1e766b98f 100644 (file)
  */
 package org.sonar.runner.cli;
 
-import static org.mockito.Mockito.verify;
-import static org.fest.assertions.Assertions.assertThat;
-import static com.jayway.awaitility.Awaitility.await;
-
+import com.jayway.awaitility.Duration;
 import java.util.concurrent.Callable;
 import java.util.concurrent.TimeUnit;
-
-import com.jayway.awaitility.Duration;
-import org.mockito.MockitoAnnotations;
-import org.sonar.runner.cli.Exit;
-import org.sonar.runner.cli.Shutdown;
-import org.mockito.Mock;
-import org.junit.Test;
 import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import static com.jayway.awaitility.Awaitility.await;
+import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Mockito.verify;
 
 public class ShutdownTest {
   @Mock
@@ -42,7 +39,7 @@ public class ShutdownTest {
   @Before
   public void setUp() {
     MockitoAnnotations.initMocks(this);
-    shutdown = new Shutdown(exit);
+    shutdown = new Shutdown(exit, true);
   }
 
   @Test
@@ -53,7 +50,7 @@ public class ShutdownTest {
 
   @Test(timeout = 60_000)
   public void testWaitReady() throws InterruptedException {
-    shutdown = new Shutdown(exit, 100_000);
+    shutdown = new Shutdown(exit, true, 100_000);
     shutdown.signalReady(false);
     assertThat(shutdown.shouldExit()).isFalse();
 
@@ -75,7 +72,7 @@ public class ShutdownTest {
 
   @Test(timeout = 60_000)
   public void testTimeout() throws InterruptedException {
-    shutdown = new Shutdown(exit, 0);
+    shutdown = new Shutdown(exit, true, 0);
 
     Thread t = new HookCaller();
     t.start();