From cce14166758f1e5772194f72ae46c3c7a8c03c68 Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Sat, 19 Jul 2008 22:07:19 -0400 Subject: [PATCH] Modify __call() to pass array to execute(). Signed-off-by: Edward Z. Yang --- library/Git.php | 49 ++++++++++++++++++++++--------------------------- tests/GitTest.php | 23 +++++++++++++---------- 2 files changed, 35 insertions(+), 37 deletions(-) diff --git a/library/Git.php b/library/Git.php index a776342..eb440bd 100644 --- a/library/Git.php +++ b/library/Git.php @@ -57,24 +57,24 @@ class Git * @param $command Command argument list to handle. * @param $istream Stdin string passed to subprocess. * @param $options Lookup array of options. These options are: - * 'keepCwd' => Whether to use current working directory from + * 'withKeepCwd' => Whether to use current working directory from * getcwd() or the Git directory in $this->dir - * 'extendedOutput' => Whether to return array(status, stdout, stderr) - * 'exceptions' => Whether to raise an exception if Git returns + * 'withExtendedOutput' => Whether to return array(status, stdout, stderr) + * 'withExceptions' => Whether to raise an exception if Git returns * a non-zero exit status - * 'rawOutput' => Whether to avoid stripping off trailing whitespace + * 'withRawOutput' => Whether to avoid stripping off trailing whitespace * @return String stdout output when withExtendedOutput is false, see above * if true. */ public function execute($command, $istream = null, $options = array()) { if (is_array($command)) $command = implode(' ', $command); $options = array_merge(array( - 'keepCwd' => false, - 'extendedOutput' => false, - 'exceptions' => true, - 'rawOutput' => false, + 'withKeepCwd' => false, + 'withExtendedOutput' => false, + 'withExceptions' => true, + 'withRawOutput' => false, ), $options); - if ($options['keepCwd'] || is_null($this->dir)) { + if ($options['withKeepCwd'] || is_null($this->dir)) { $cwd = getcwd(); } else { $cwd = $this->dir; @@ -128,16 +128,16 @@ class Git if (empty($code)) $status = $close; } // We want $stderr, $stdout and $status - if (!$options['rawOutput']) { + if (!$options['withRawOutput']) { // This feels buggy to me, especially with git cat-file $stdout = rtrim($stdout); $stderr = rtrim($stderr); } - if ($options['exceptions'] && $status !== 0) { + if ($options['withExceptions'] && $status !== 0) { throw new Git_Exception_Command($command, $status, $stderr); } // Trace code omitted - if ($options['extendedOutput']) { + if ($options['withExtendedOutput']) { return array($status, $stdout, $stderr); } else { return $stdout; @@ -188,14 +188,18 @@ class Git $args[] = $kwargs[$i]; unset($kwargs[$i]); } + // Grab "special" arguments prior to transformation + $executeKwargs = array(); + foreach ($kwargs as $k => $v) { + if (isset(self::$executeKwargs[$k])) { + $executeKwargs[$k] = $v; + unset($kwargs[$k]); + } + } // $args and $kwargs are interesting $opt_args = $this->transformArgs($kwargs); // parse through $args again to determine which ones are actually kwargs - $ext_args = array(); - foreach ($args as $v) { - if ($v == '--') $ext_args[] = $v; - else $ext_args[] = $this->escape($v); - } + $ext_args = $args; // Full arguments $args = array_merge($opt_args, $ext_args); // Convert methodName to method-name (our equivalent of dashify). @@ -205,18 +209,9 @@ class Git $c = $method[$i]; $command .= ctype_upper($c) ? '-' . strtolower($c) : $c; } - $call = self::$git . " $command " . implode(' ', $args); - // var_dump($call); + $call = array_merge(array('git', $command), $args); $result = $this->execute($call); return $result; } - /** - * Escape argument for shell. I don't think this actually works properly - * on Windows. - */ - public function escape($v) { - return str_replace("'", "\\\\'", $v); - } - } diff --git a/tests/GitTest.php b/tests/GitTest.php index bacda4f..40da099 100644 --- a/tests/GitTest.php +++ b/tests/GitTest.php @@ -7,9 +7,14 @@ Mock::generatePartial( class GitTest extends UnitTestCase { + protected $git; + + public function setUp() { + $this->git = new GitPartialMock(); + } + function testTransformArgs() { - $git = new Git('..'); - $result = $git->transformArgs(array( + $result = $this->git->transformArgs(array( 'v' => true, 's' => 'ours', 'no-commit' => true, @@ -21,20 +26,18 @@ class GitTest extends UnitTestCase $this->assertIdentical($result, $expect); } - function testEscape() { - $git = new Git('..'); - $result = $git->escape("foo's"); - $expect = "foo\\\\'s"; - $this->assertIdentical($result, $expect); - } - function testCall() { $git = new GitPartialMock(); $git->__construct('dir'); - $git->expectOnce('execute', array('git cherry-pick -s f533ebca --no-commit')); + $git->expectOnce('execute', array(array('git', 'cherry-pick', '-s', 'f533ebca', '--no-commit'))); $git->setReturnValue('execute', $expect = 'Result'); $result = $git->cherryPick('f533ebca', array('--no-commit', 's' => true)); $this->assertIdentical($result, $expect); } + // now, for the ported tests! + function testCallProcessCallsExecute() { + //$git = n + } + } -- 2.11.4.GIT