Browse Source

Merge pull request #5072 from cakephp/issue-5063

Fix bake & other shells exiting incorrectly.
José Lorenzo Rodríguez 11 years ago
parent
commit
493767ea41

+ 8 - 11
src/Console/ShellDispatcher.php

@@ -154,10 +154,17 @@ class ShellDispatcher {
 /**
  * Dispatches a CLI request
  *
+ * Converts a shell command result into an exit code. Null/True
+ * are treated as success. All other return values are an error.
+ *
  * @return int The cli command exit code. 0 is success.
  */
 	public function dispatch() {
-		return $this->_dispatch() === true ? 0 : 1;
+		$result = $this->_dispatch();
+		if ($result === null || $result === true) {
+			return 0;
+		}
+		return 1;
 	}
 
 /**
@@ -328,14 +335,4 @@ class ShellDispatcher {
 		$this->dispatch();
 	}
 
-/**
- * Stop execution of the current script
- *
- * @param int|string $status see http://php.net/exit for values
- * @return void
- */
-	protected function _stop($status = 0) {
-		exit($status);
-	}
-
 }

+ 1 - 0
src/Shell/BakeShell.php

@@ -85,6 +85,7 @@ class BakeShell extends Shell {
 		}
 		$this->out('');
 		$this->out('By using <info>`cake bake [name]`</info> you can invoke a specific bake task.');
+		return false;
 	}
 
 /**

+ 13 - 7
tests/TestCase/Console/ShellDispatcherTest.php

@@ -148,10 +148,11 @@ class ShellDispatcherTest extends TestCase {
 		$dispatcher = $this->getMock('Cake\Console\ShellDispatcher', ['findShell']);
 		$Shell = $this->getMock('Cake\Console\Shell');
 
-		$Shell->expects($this->once())->method('initialize');
-		$Shell->expects($this->once())->method('runCommand')
-			->with([])
+		$Shell->expects($this->exactly(2))->method('initialize');
+		$Shell->expects($this->at(0))->method('runCommand')
 			->will($this->returnValue(true));
+		$Shell->expects($this->at(1))->method('runCommand')
+			->will($this->returnValue(null));
 
 		$dispatcher->expects($this->any())
 			->method('findShell')
@@ -160,7 +161,12 @@ class ShellDispatcherTest extends TestCase {
 
 		$dispatcher->args = array('mock_with_main');
 		$result = $dispatcher->dispatch();
-		$this->assertEquals(0, $result);
+		$this->assertSame(0, $result);
+		$this->assertEquals(array(), $dispatcher->args);
+
+		$dispatcher->args = array('mock_with_main');
+		$result = $dispatcher->dispatch();
+		$this->assertSame(0, $result);
 		$this->assertEquals(array(), $dispatcher->args);
 	}
 
@@ -212,7 +218,7 @@ class ShellDispatcherTest extends TestCase {
 
 		$dispatcher->args = array('example');
 		$result = $dispatcher->dispatch();
-		$this->assertEquals(1, $result);
+		$this->assertEquals(0, $result);
 	}
 
 /**
@@ -239,7 +245,7 @@ class ShellDispatcherTest extends TestCase {
 
 		$dispatcher->args = ['Example'];
 		$result = $dispatcher->dispatch();
-		$this->assertEquals(1, $result);
+		$this->assertEquals(0, $result);
 	}
 
 /**
@@ -266,7 +272,7 @@ class ShellDispatcherTest extends TestCase {
 
 		$dispatcher->args = array('sample');
 		$result = $dispatcher->dispatch();
-		$this->assertEquals(1, $result);
+		$this->assertEquals(0, $result);
 	}
 
 /**