浏览代码

Fix shell command output, fixes #1295

Jordi Boggiano 12 年之前
父节点
当前提交
7d7eb3b2e8
共有 2 个文件被更改,包括 50 次插入2 次删除
  1. 2 2
      src/Composer/Script/EventDispatcher.php
  2. 48 0
      tests/Composer/Test/Script/EventDispatcherTest.php

+ 2 - 2
src/Composer/Script/EventDispatcher.php

@@ -105,8 +105,8 @@ class EventDispatcher
                     throw $e;
                 }
             } else {
-                if (0 !== $this->process->execute($callable, $callback)) {
-                    $event->getIO()->write(sprintf('<error>Script %s handling the %s event returned with an error: %s</script>', $callable, $event->getName(), $this->process->getErrorOutput()));
+                if (0 !== $this->process->execute($callable)) {
+                    $event->getIO()->write(sprintf('<error>Script %s handling the %s event returned with an error: %s</error>', $callable, $event->getName(), $this->process->getErrorOutput()));
                 }
             }
         }

+ 48 - 0
tests/Composer/Test/Script/EventDispatcherTest.php

@@ -15,6 +15,7 @@ namespace Composer\Test\Script;
 use Composer\Test\TestCase;
 use Composer\Script\Event;
 use Composer\Script\EventDispatcher;
+use Composer\Util\ProcessExecutor;
 
 class EventDispatcherTest extends TestCase
 {
@@ -124,6 +125,53 @@ class EventDispatcherTest extends TestCase
         );
     }
 
+    public function testDispatcherOutputsCommands()
+    {
+        $dispatcher = $this->getMockBuilder('Composer\Script\EventDispatcher')
+            ->setConstructorArgs(array(
+                $this->getMock('Composer\Composer'),
+                $this->getMock('Composer\IO\IOInterface'),
+                new ProcessExecutor,
+            ))
+            ->setMethods(array('getListeners'))
+            ->getMock();
+
+        $listener = array('echo foo');
+        $dispatcher->expects($this->atLeastOnce())
+            ->method('getListeners')
+            ->will($this->returnValue($listener));
+
+        ob_start();
+        $dispatcher->dispatchCommandEvent("post-install-cmd", false);
+        $this->assertEquals('foo', trim(ob_get_clean()));
+    }
+
+    public function testDispatcherOutputsErrorOnFailedCommand()
+    {
+        $dispatcher = $this->getMockBuilder('Composer\Script\EventDispatcher')
+            ->setConstructorArgs(array(
+                $this->getMock('Composer\Composer'),
+                $io = $this->getMock('Composer\IO\IOInterface'),
+                new ProcessExecutor,
+            ))
+            ->setMethods(array('getListeners'))
+            ->getMock();
+
+        $code = sprintf('echo bar>&2 %s exit 1', defined('PHP_WINDOWS_VERSION_BUILD') ? '&' : ';');
+        $listener = array($code);
+        $dispatcher->expects($this->atLeastOnce())
+            ->method('getListeners')
+            ->will($this->returnValue($listener));
+
+        $io->expects($this->once())
+            ->method('write')
+            ->with($this->equalTo('<error>Script '.$code.' handling the post-install-cmd event returned with an error: bar '.PHP_EOL.'</error>'));
+
+        ob_start();
+        $dispatcher->dispatchCommandEvent("post-install-cmd", false);
+        $this->assertEquals('bar', trim(ob_get_clean()));
+    }
+
     public static function call()
     {
         throw new \RuntimeException();