Skip to content

Commit

Permalink
Merge pull request #13 from grasmash/return-code
Browse files Browse the repository at this point in the history
Correctly return exit code.
  • Loading branch information
grasmash authored Mar 2, 2020
2 parents e39bc46 + a3f0ae6 commit 51d8ecf
Show file tree
Hide file tree
Showing 12 changed files with 133 additions and 93 deletions.
9 changes: 7 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
"bin/yaml-cli"
],
"scripts": {
"cs": "phpcs -n --standard=PSR2 src tests/src bin --exclude=Generic.Files.LineLength",
"cbf": "phpcbf -n --standard=PSR2 src tests/src bin --exclude=Generic.Files.LineLength",
"cs": "phpcs -n --standard=PSR2 src tests bin --exclude=Generic.Files.LineLength",
"cbf": "phpcbf -n --standard=PSR2 src tests bin --exclude=Generic.Files.LineLength",
"unit": "phpunit",
"lint": [
"find src -name '*.php' -print0 | xargs -0 -n1 php -l",
Expand All @@ -48,5 +48,10 @@
"squizlabs/php_codesniffer": "^2.7",
"phpunit/phpunit": "^5.5.4",
"satooshi/php-coveralls": "^1.0"
},
"extra": {
"branch-alias": {
"dev-master": "2.x-dev"
}
}
}
73 changes: 36 additions & 37 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/Command/GetValueCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected function configure()
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
*
* @return bool
* @return int 0 if everything went fine, or an exit code
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
Expand All @@ -59,5 +59,6 @@ protected function execute(InputInterface $input, OutputInterface $output)

$value = $data->get($key);
$output->writeln(trim(Yaml::dump($value)));
return 0;
}
}
4 changes: 3 additions & 1 deletion src/Command/LintCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected function configure()
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
*
* @return bool
* @return int 0 if everything went fine, or an exit code
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
Expand All @@ -49,5 +49,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
if (OutputInterface::VERBOSITY_VERBOSE === $output->getVerbosity()) {
$output->writeln("<info>The file $filename contains valid YAML.</info>");
}

return 0;
}
}
6 changes: 5 additions & 1 deletion src/Command/UnsetKeyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected function configure()
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
*
* @return bool
* @return int 0 if everything went fine, or an exit code
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
Expand All @@ -54,13 +54,17 @@ protected function execute(InputInterface $input, OutputInterface $output)

$data = new Data($yaml_parsed);
if (!$this->checkKeyExists($data, $key)) {
$this->output->writeln("<error>The key '$key' does not exist in $filename.</error>");
return 1;
}

$data->remove($key);

if ($this->writeYamlFile($filename, $data)) {
$this->output->writeln("<info>The key '$key' was removed from $filename.</info>");
return 0;
}

return 1;
}
}
6 changes: 5 additions & 1 deletion src/Command/UpdateKeyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected function configure()
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
*
* @return bool
* @return int 0 if everything went fine, or an exit code
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
Expand All @@ -60,6 +60,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

$data = new Data($yaml_parsed);
if (!$this->checkKeyExists($data, $key)) {
$this->output->writeln("<error>The key '$key' does not exist in $filename.</error>");
return 1;
}

Expand All @@ -69,6 +70,9 @@ protected function execute(InputInterface $input, OutputInterface $output)

if ($this->writeYamlFile($filename, $data)) {
$this->output->writeln("<info>The key '$key' was changed to '$new_key' in $filename.</info>");
return 0;
}

return 1;
}
}
9 changes: 6 additions & 3 deletions src/Command/UpdateValueCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected function configure()
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
*
* @return bool
* @return int 0 if everything went fine, or an exit code
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
Expand All @@ -59,18 +59,21 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

$data = new Data($yaml_parsed);

$typed_value = $value;
if (strtolower($value) === 'false') {
$typed_value = false;
} elseif (strtolower($value) === 'true') {
$typed_value = true;
}

$data->set($key, $typed_value);

if ($this->writeYamlFile($filename, $data)) {
$this->output->writeln("<info>The value for key '$key' was set to '$value' in $filename.</info>");
return 0;
}

return 1;
}
}
15 changes: 8 additions & 7 deletions tests/phpunit/GetValueCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class GetValueCommandTest extends TestBase
*
* @dataProvider getValueProvider
*/
public function testGetValue($file, $key, $expected)
public function testGetValue($file, $key, $expected_output, $expected_exit_code)
{
$this->application->add(new GetValueCommand());

Expand All @@ -27,7 +27,8 @@ public function testGetValue($file, $key, $expected)
));

$output = $commandTester->getDisplay();
$this->assertContains($expected, $output);
$this->assertContains($expected_output, $output);
$this->assertEquals($expected_exit_code, $commandTester->getStatusCode());
}

/**
Expand All @@ -42,15 +43,15 @@ public function getValueProvider()
$file = 'tests/resources/good.yml';

return [
[$file, 'not-real', "The key not-real does not exist."],
['missing.yml', 'not-real', "The file missing.yml does not exist."],
[$file, 'deep-array.second.third.fourth', 'hello world'],
[$file, 'not-real', "The key not-real does not exist.", 1],
['missing.yml', 'not-real', "The file missing.yml does not exist.", 1],
[$file, 'deep-array.second.third.fourth', 'hello world', 0],
[$file, 'flat-array', '- one
- two
- three'],
- three', 0],
[$file, 'inline-array', '- one
- two
- three'],
- three', 0],
];
}
}
Loading

0 comments on commit 51d8ecf

Please sign in to comment.