Fix issues with using 0 as an option for a shell command.

Fixes #2079
This commit is contained in:
mark_story 2011-10-10 09:25:46 -04:00
parent 5d3fdfc9d6
commit bc6de18417
3 changed files with 15 additions and 6 deletions

View file

@ -450,7 +450,7 @@ class ShellDispatcher {
}
$result = trim($result);
if ($default != null && empty($result)) {
if ($default !== null && ($result === '' || $result === null)) {
return $default;
}
return $result;
@ -471,7 +471,6 @@ class ShellDispatcher {
return fwrite($this->stdout, $string);
}
}
/**
* Outputs to the stderr filehandle.
*

View file

@ -347,13 +347,11 @@ class Shell extends Object {
}
}
if (is_array($options)) {
while ($in == '' || ($in && (!in_array(strtolower($in), $options) && !in_array(strtoupper($in), $options)) && !in_array($in, $options))) {
while ($in === '' || ($in !== '' && (!in_array(strtolower($in), $options) && !in_array(strtoupper($in), $options)) && !in_array($in, $options))) {
$in = $this->Dispatch->getInput($prompt, $options, $default);
}
}
if ($in) {
return $in;
}
return $in;
}
/**

View file

@ -208,6 +208,18 @@ class ShellTest extends CakeTestCase {
$result = $this->Shell->in('Just a test?', 'y', 'y');
$this->assertEqual($result, 'y');
$this->Shell->Dispatch->setReturnValueAt(5, 'getInput', 'y');
$this->Shell->Dispatch->expectAt(5, 'getInput', array('Just a test?', array(0, 1, 2), 0));
$result = $this->Shell->in('Just a test?', array(0, 1, 2), 0);
$this->assertEqual($result, 0);
}
/**
* Test in() when not interactive
*
* @return void
*/
function testInNonInteractive() {
$this->Shell->interactive = false;
$result = $this->Shell->in('Just a test?', 'y/n', 'n');