From d8dd91d919464e95c11240bed7ea3996093987a7 Mon Sep 17 00:00:00 2001 From: Pierre du Plessis Date: Wed, 8 Nov 2017 15:32:34 +0200 Subject: [PATCH] [YAML] Allow to parse custom tags when linting yaml files --- .../Component/Yaml/Command/LintCommand.php | 10 ++++++---- .../Yaml/Tests/Command/LintCommandTest.php | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/Yaml/Command/LintCommand.php b/src/Symfony/Component/Yaml/Command/LintCommand.php index c6bc267d3f..1797020c97 100644 --- a/src/Symfony/Component/Yaml/Command/LintCommand.php +++ b/src/Symfony/Component/Yaml/Command/LintCommand.php @@ -52,6 +52,7 @@ class LintCommand extends Command ->setDescription('Lints a file and outputs encountered errors') ->addArgument('filename', null, 'A file or a directory or STDIN') ->addOption('format', null, InputOption::VALUE_REQUIRED, 'The output format', 'txt') + ->addOption('parse-tags', null, InputOption::VALUE_NONE, 'Parse custom tags') ->setHelp(<<%command.name% command lints a YAML file and outputs to STDOUT the first encountered syntax error. @@ -80,13 +81,14 @@ EOF $filename = $input->getArgument('filename'); $this->format = $input->getOption('format'); $this->displayCorrectFiles = $output->isVerbose(); + $flags = $input->getOption('parse-tags') ? Yaml::PARSE_CUSTOM_TAGS : 0; if (!$filename) { if (!$stdin = $this->getStdin()) { throw new \RuntimeException('Please provide a filename or pipe file content to STDIN.'); } - return $this->display($io, array($this->validate($stdin))); + return $this->display($io, array($this->validate($stdin, $flags))); } if (!$this->isReadable($filename)) { @@ -95,13 +97,13 @@ EOF $filesInfo = array(); foreach ($this->getFiles($filename) as $file) { - $filesInfo[] = $this->validate(file_get_contents($file), $file); + $filesInfo[] = $this->validate(file_get_contents($file), $flags, $file); } return $this->display($io, $filesInfo); } - private function validate($content, $file = null) + private function validate($content, $flags, $file = null) { $prevErrorHandler = set_error_handler(function ($level, $message, $file, $line) use (&$prevErrorHandler) { if (E_USER_DEPRECATED === $level) { @@ -112,7 +114,7 @@ EOF }); try { - $this->getParser()->parse($content, Yaml::PARSE_CONSTANT); + $this->getParser()->parse($content, Yaml::PARSE_CONSTANT | $flags); } catch (ParseException $e) { return array('file' => $file, 'valid' => false, 'message' => $e->getMessage()); } finally { diff --git a/src/Symfony/Component/Yaml/Tests/Command/LintCommandTest.php b/src/Symfony/Component/Yaml/Tests/Command/LintCommandTest.php index 75aa067f22..dfde169083 100644 --- a/src/Symfony/Component/Yaml/Tests/Command/LintCommandTest.php +++ b/src/Symfony/Component/Yaml/Tests/Command/LintCommandTest.php @@ -60,6 +60,24 @@ YAML; $this->assertSame(0, $ret, 'lint:yaml exits with code 0 in case of success'); } + public function testCustomTags() + { + $yaml = <<createCommandTester()->execute(array('filename' => $this->createFile($yaml), '--parse-tags' => true), array('verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false)); + $this->assertSame(0, $ret, 'lint:yaml exits with code 0 in case of success'); + } + + public function testCustomTagsError() + { + $yaml = <<createCommandTester()->execute(array('filename' => $this->createFile($yaml)), array('verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false)); + $this->assertSame(1, $ret, 'lint:yaml exits with code 1 in case of error'); + } + /** * @expectedException \RuntimeException */