From 55704f3d9e8315abfd0b9cbacc144d80a888882c Mon Sep 17 00:00:00 2001 From: Christin Gruber Date: Sat, 7 Aug 2021 18:53:13 +0200 Subject: [PATCH] [Yaml] Add an --exclude option to lint:yaml command --- src/Symfony/Component/Yaml/CHANGELOG.md | 7 +++++++ .../Component/Yaml/Command/LintCommand.php | 16 +++++++++++++--- .../Yaml/Tests/Command/LintCommandTest.php | 11 +++++++++++ src/Symfony/Component/Yaml/composer.json | 4 ++-- 4 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/Yaml/CHANGELOG.md b/src/Symfony/Component/Yaml/CHANGELOG.md index 21a0225e1a0fd..b9561b2af2155 100644 --- a/src/Symfony/Component/Yaml/CHANGELOG.md +++ b/src/Symfony/Component/Yaml/CHANGELOG.md @@ -1,6 +1,13 @@ CHANGELOG ========= +5.4 +--- + + * Add new `lint:yaml dirname --exclude=/dirname/foo.yaml --exclude=/dirname/bar.yaml` + option to exclude one or more specific files from multiple file list + * Allow negatable for the parse tags option with `--no-parse-tags` + 5.3 --- diff --git a/src/Symfony/Component/Yaml/Command/LintCommand.php b/src/Symfony/Component/Yaml/Command/LintCommand.php index 042c43a7af847..92c54d997c133 100644 --- a/src/Symfony/Component/Yaml/Command/LintCommand.php +++ b/src/Symfony/Component/Yaml/Command/LintCommand.php @@ -58,7 +58,8 @@ protected function configure() ->setDescription(self::$defaultDescription) ->addArgument('filename', InputArgument::IS_ARRAY, 'A file, a directory or "-" for reading from STDIN') ->addOption('format', null, InputOption::VALUE_REQUIRED, 'The output format') - ->addOption('parse-tags', null, InputOption::VALUE_NONE, 'Parse custom tags') + ->addOption('exclude', 'e', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Path(s) to exclude') + ->addOption('parse-tags', null, InputOption::VALUE_NEGATABLE, 'Parse custom tags', null) ->setHelp(<<%command.name% command lints a YAML file and outputs to STDOUT the first encountered syntax error. @@ -76,6 +77,10 @@ protected function configure() php %command.full_name% dirname php %command.full_name% dirname --format=json +You can also exclude one or more specific files: + + php %command.full_name% dirname --exclude="dirname/foo.yaml" --exclude="dirname/bar.yaml" + EOF ) ; @@ -85,7 +90,9 @@ protected function execute(InputInterface $input, OutputInterface $output) { $io = new SymfonyStyle($input, $output); $filenames = (array) $input->getArgument('filename'); + $excludes = $input->getOption('exclude'); $this->format = $input->getOption('format'); + $flags = $input->getOption('parse-tags'); if ('github' === $this->format && !class_exists(GithubActionReporter::class)) { throw new \InvalidArgumentException('The "github" format is only available since "symfony/console" >= 5.3.'); @@ -96,8 +103,9 @@ protected function execute(InputInterface $input, OutputInterface $output) $this->format = class_exists(GithubActionReporter::class) && GithubActionReporter::isGithubActionEnvironment() ? 'github' : 'txt'; } + $flags = $flags ? Yaml::PARSE_CUSTOM_TAGS : 0; + $this->displayCorrectFiles = $output->isVerbose(); - $flags = $input->getOption('parse-tags') ? Yaml::PARSE_CUSTOM_TAGS : 0; if (['-'] === $filenames) { return $this->display($io, [$this->validate(file_get_contents('php://stdin'), $flags)]); @@ -114,7 +122,9 @@ protected function execute(InputInterface $input, OutputInterface $output) } foreach ($this->getFiles($filename) as $file) { - $filesInfo[] = $this->validate(file_get_contents($file), $flags, $file); + if (!\in_array($file->getPathname(), $excludes, true)) { + $filesInfo[] = $this->validate(file_get_contents($file), $flags, $file); + } } } diff --git a/src/Symfony/Component/Yaml/Tests/Command/LintCommandTest.php b/src/Symfony/Component/Yaml/Tests/Command/LintCommandTest.php index 6c394f95fd3dc..08c3dd10c410c 100644 --- a/src/Symfony/Component/Yaml/Tests/Command/LintCommandTest.php +++ b/src/Symfony/Component/Yaml/Tests/Command/LintCommandTest.php @@ -142,6 +142,17 @@ public function testCustomTagsError() $this->assertSame(1, $ret, 'lint:yaml exits with code 1 in case of error'); } + public function testLintWithExclude() + { + $tester = $this->createCommandTester(); + $filename1 = $this->createFile('foo: bar'); + $filename2 = $this->createFile('bar: baz'); + + $ret = $tester->execute(['filename' => [$filename1, $filename2], '--exclude' => [$filename1]], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false]); + $this->assertSame(0, $ret, 'lint:yaml exits with code 0 in case of success'); + $this->assertStringContainsString('All 1 YAML files contain valid syntax.', trim($tester->getDisplay())); + } + public function testLintFileNotReadable() { $this->expectException(\RuntimeException::class); diff --git a/src/Symfony/Component/Yaml/composer.json b/src/Symfony/Component/Yaml/composer.json index d374937555122..a3a4bb832f441 100644 --- a/src/Symfony/Component/Yaml/composer.json +++ b/src/Symfony/Component/Yaml/composer.json @@ -23,10 +23,10 @@ "symfony/polyfill-php81": "^1.22" }, "require-dev": { - "symfony/console": "^4.4|^5.0|^6.0" + "symfony/console": "^5.3|^6.0" }, "conflict": { - "symfony/console": "<4.4" + "symfony/console": "<5.3" }, "suggest": { "symfony/console": "For validating YAML files using the lint command"