From 5e70c097f520487e3f1ac6a4a503abc91710c6f9 Mon Sep 17 00:00:00 2001 From: Hendrik Grahl Date: Tue, 7 Apr 2020 08:06:05 +0200 Subject: [PATCH 1/4] Add option to check directories. --- composer.json | 1 + composer.lock | 51 ++++++++++++++++++++++++++++++++++++- src/Command/LintCommand.php | 37 ++++++++++++++++++++++----- 3 files changed, 82 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index 1dec2fd..7a36fbe 100644 --- a/composer.json +++ b/composer.json @@ -7,6 +7,7 @@ "symfony/yaml": "^4 | ^5", "symfony/console": "^4 | ^5", "symfony/filesystem": "^4 | ^5", + "symfony/finder": "^4 | ^5", "dflydev/dot-access-data": "^1.1.0" }, "license": "MIT", diff --git a/composer.lock b/composer.lock index 9a4e676..969c56c 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "1f123a5a1e0ac60f3ff0af3e3bea94d7", + "content-hash": "a95010d0c1a48b4cc020b7c672912a74", "packages": [ { "name": "dflydev/dot-access-data", @@ -240,6 +240,55 @@ "homepage": "https://symfony.com", "time": "2020-01-21T08:40:24+00:00" }, + { + "name": "symfony/finder", + "version": "v5.0.7", + "source": { + "type": "git", + "url": "https://github.com/symfony/finder.git", + "reference": "600a52c29afc0d1caa74acbec8d3095ca7e9910d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/finder/zipball/600a52c29afc0d1caa74acbec8d3095ca7e9910d", + "reference": "600a52c29afc0d1caa74acbec8d3095ca7e9910d", + "shasum": "" + }, + "require": { + "php": "^7.2.5" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "5.0-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\Finder\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony Finder Component", + "homepage": "https://symfony.com", + "time": "2020-03-27T16:56:45+00:00" + }, { "name": "symfony/polyfill-ctype", "version": "v1.14.0", diff --git a/src/Command/LintCommand.php b/src/Command/LintCommand.php index a77136c..93a450a 100644 --- a/src/Command/LintCommand.php +++ b/src/Command/LintCommand.php @@ -6,6 +6,7 @@ use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Finder\Finder; use Symfony\Component\Yaml\Yaml; /** @@ -23,11 +24,11 @@ protected function configure() $this ->setName('lint') ->setDescription('Validates that a given YAML file has valid syntax.') - ->addUsage("path/to/file.yml") + ->addUsage('path/to/file.yml') ->addArgument( - 'filename', + 'path', InputArgument::REQUIRED, - "The filename of the YAML file" + 'The path or directory of the YAML file' ); } @@ -39,15 +40,39 @@ protected function configure() */ protected function execute(InputInterface $input, OutputInterface $output) { - $filename = $input->getArgument('filename'); - $yaml_parsed = $this->loadYamlFile($filename); + $path = $input->getArgument('path'); + $yaml_parsed = $this->loadYamlFile($path); if (!$yaml_parsed) { // Exit with a status of 1. return 1; } + if (is_dir($path)) { + $finder = new Finder(); + $finder->files()->in($path); + foreach ($finder as $file) { + $yaml_parsed = $this->loadYamlFile($file->getRealPath()); + if (!$yaml_parsed) { + // Exit with a status of 1. + return 1; + } + } + } + else { + $yaml_parsed = $this->loadYamlFile($path); + if (!$yaml_parsed) { + // Exit with a status of 1. + return 1; + } + } + if (OutputInterface::VERBOSITY_VERBOSE === $output->getVerbosity()) { - $output->writeln("The file $filename contains valid YAML."); + if (is_dir($path)) { + $output->writeln("The directory $path contains valid YAML."); + } + else { + $output->writeln("The file $path contains valid YAML."); + } } return 0; From 57e0d95f92765679275363713326cf4292732157 Mon Sep 17 00:00:00 2001 From: Hendrik Grahl Date: Tue, 7 Apr 2020 09:43:00 +0200 Subject: [PATCH 2/4] Fix test attribute name. --- tests/phpunit/GetValueCommandTest.php | 2 +- tests/phpunit/LintCommandTest.php | 2 +- tests/phpunit/UnsetKeyCommandTest.php | 2 +- tests/phpunit/UpdateKeyCommandTest.php | 2 +- tests/phpunit/UpdateValueCommandTest.php | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/phpunit/GetValueCommandTest.php b/tests/phpunit/GetValueCommandTest.php index 203beef..72ce4aa 100644 --- a/tests/phpunit/GetValueCommandTest.php +++ b/tests/phpunit/GetValueCommandTest.php @@ -22,7 +22,7 @@ public function testGetValue($file, $key, $expected_output, $expected_exit_code) $commandTester = new CommandTester($command); $commandTester->execute(array( 'command' => $command->getName(), - 'filename' => $file, + 'path' => $file, 'key' => $key )); diff --git a/tests/phpunit/LintCommandTest.php b/tests/phpunit/LintCommandTest.php index 79e7556..057d904 100644 --- a/tests/phpunit/LintCommandTest.php +++ b/tests/phpunit/LintCommandTest.php @@ -24,7 +24,7 @@ public function testLint($file, $expected_output, $expected_exit_code) $commandTester = new CommandTester($command); $commandTester->execute(array( 'command' => $command->getName(), - 'filename' => $file + 'path' => $file ), ['verbosity' => Output::VERBOSITY_VERBOSE]); $output = $commandTester->getDisplay(); diff --git a/tests/phpunit/UnsetKeyCommandTest.php b/tests/phpunit/UnsetKeyCommandTest.php index 60dd980..4210bc2 100644 --- a/tests/phpunit/UnsetKeyCommandTest.php +++ b/tests/phpunit/UnsetKeyCommandTest.php @@ -77,7 +77,7 @@ protected function runCommand($filename, $key) $commandTester = new CommandTester($command); $commandTester->execute(array( 'command' => $command->getName(), - 'filename' => $filename, + 'path' => $filename, 'key' => $key )); diff --git a/tests/phpunit/UpdateKeyCommandTest.php b/tests/phpunit/UpdateKeyCommandTest.php index 8dab9ef..286658a 100644 --- a/tests/phpunit/UpdateKeyCommandTest.php +++ b/tests/phpunit/UpdateKeyCommandTest.php @@ -93,7 +93,7 @@ protected function runCommand($file, $key, $new_key) $commandTester = new CommandTester($command); $commandTester->execute(array( 'command' => $command->getName(), - 'filename' => $file, + 'path' => $file, 'key' => $key, 'new-key' => $new_key, )); diff --git a/tests/phpunit/UpdateValueCommandTest.php b/tests/phpunit/UpdateValueCommandTest.php index 1d5fe0d..5e8680b 100644 --- a/tests/phpunit/UpdateValueCommandTest.php +++ b/tests/phpunit/UpdateValueCommandTest.php @@ -79,7 +79,7 @@ protected function runCommand($file, $key, $value) $commandTester = new CommandTester($command); $commandTester->execute(array( 'command' => $command->getName(), - 'filename' => $file, + 'path' => $file, 'key' => $key, 'value' => $value, )); From 6f80e9821a6e2dcecb33906059d5a18fae7589fe Mon Sep 17 00:00:00 2001 From: Hendrik Grahl Date: Tue, 7 Apr 2020 09:47:29 +0200 Subject: [PATCH 3/4] Fix test attribute name. --- tests/phpunit/GetValueCommandTest.php | 2 +- tests/phpunit/UnsetKeyCommandTest.php | 2 +- tests/phpunit/UpdateKeyCommandTest.php | 2 +- tests/phpunit/UpdateValueCommandTest.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/phpunit/GetValueCommandTest.php b/tests/phpunit/GetValueCommandTest.php index 72ce4aa..203beef 100644 --- a/tests/phpunit/GetValueCommandTest.php +++ b/tests/phpunit/GetValueCommandTest.php @@ -22,7 +22,7 @@ public function testGetValue($file, $key, $expected_output, $expected_exit_code) $commandTester = new CommandTester($command); $commandTester->execute(array( 'command' => $command->getName(), - 'path' => $file, + 'filename' => $file, 'key' => $key )); diff --git a/tests/phpunit/UnsetKeyCommandTest.php b/tests/phpunit/UnsetKeyCommandTest.php index 4210bc2..60dd980 100644 --- a/tests/phpunit/UnsetKeyCommandTest.php +++ b/tests/phpunit/UnsetKeyCommandTest.php @@ -77,7 +77,7 @@ protected function runCommand($filename, $key) $commandTester = new CommandTester($command); $commandTester->execute(array( 'command' => $command->getName(), - 'path' => $filename, + 'filename' => $filename, 'key' => $key )); diff --git a/tests/phpunit/UpdateKeyCommandTest.php b/tests/phpunit/UpdateKeyCommandTest.php index 286658a..8dab9ef 100644 --- a/tests/phpunit/UpdateKeyCommandTest.php +++ b/tests/phpunit/UpdateKeyCommandTest.php @@ -93,7 +93,7 @@ protected function runCommand($file, $key, $new_key) $commandTester = new CommandTester($command); $commandTester->execute(array( 'command' => $command->getName(), - 'path' => $file, + 'filename' => $file, 'key' => $key, 'new-key' => $new_key, )); diff --git a/tests/phpunit/UpdateValueCommandTest.php b/tests/phpunit/UpdateValueCommandTest.php index 5e8680b..1d5fe0d 100644 --- a/tests/phpunit/UpdateValueCommandTest.php +++ b/tests/phpunit/UpdateValueCommandTest.php @@ -79,7 +79,7 @@ protected function runCommand($file, $key, $value) $commandTester = new CommandTester($command); $commandTester->execute(array( 'command' => $command->getName(), - 'path' => $file, + 'filename' => $file, 'key' => $key, 'value' => $value, )); From 741db9a6cefbc7dc3bfd30e9192cfb7f4c3b86e2 Mon Sep 17 00:00:00 2001 From: Hendrik Grahl Date: Tue, 7 Apr 2020 10:19:23 +0200 Subject: [PATCH 4/4] Coding standards. --- src/Command/LintCommand.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Command/LintCommand.php b/src/Command/LintCommand.php index 93a450a..4e1ff4c 100644 --- a/src/Command/LintCommand.php +++ b/src/Command/LintCommand.php @@ -57,8 +57,7 @@ protected function execute(InputInterface $input, OutputInterface $output) return 1; } } - } - else { + } else { $yaml_parsed = $this->loadYamlFile($path); if (!$yaml_parsed) { // Exit with a status of 1. @@ -69,8 +68,7 @@ protected function execute(InputInterface $input, OutputInterface $output) if (OutputInterface::VERBOSITY_VERBOSE === $output->getVerbosity()) { if (is_dir($path)) { $output->writeln("The directory $path contains valid YAML."); - } - else { + } else { $output->writeln("The file $path contains valid YAML."); } }