Skip to content

Commit

Permalink
bug #24870 [YAML] Allow to parse custom tags when linting yaml files …
Browse files Browse the repository at this point in the history
…(pierredup)

This PR was squashed before being merged into the 3.3 branch (closes #24870).

Discussion
----------

[YAML] Allow to parse custom tags when linting yaml files

| Q             | A
| ------------- | ---
| Branch?       | 3.3
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | N/A
| License       | MIT
| Doc PR        | N/A

When using custom tags in yaml, currently the `lint:yaml` command fails since the flag `Yaml::PARSE_CUSTOM_TAGS` is not passed through

Commits
-------

d8dd91d [YAML] Allow to parse custom tags when linting yaml files
  • Loading branch information
fabpot committed Nov 10, 2017
2 parents d4d8d3a + d8dd91d commit 35f300d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/Symfony/Component/Yaml/Command/LintCommand.php
Expand Up @@ -52,6 +52,7 @@ protected function configure()
->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(<<<EOF
The <info>%command.name%</info> command lints a YAML file and outputs to STDOUT
the first encountered syntax error.
Expand Down Expand Up @@ -80,13 +81,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
$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)) {
Expand All @@ -95,13 +97,13 @@ protected function execute(InputInterface $input, OutputInterface $output)

$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) {
Expand All @@ -112,7 +114,7 @@ private function validate($content, $file = null)
});

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 {
Expand Down
18 changes: 18 additions & 0 deletions src/Symfony/Component/Yaml/Tests/Command/LintCommandTest.php
Expand Up @@ -60,6 +60,24 @@ public function testConstantAsKey()
$this->assertSame(0, $ret, 'lint:yaml exits with code 0 in case of success');
}

public function testCustomTags()
{
$yaml = <<<YAML
foo: !my_tag {foo: bar}
YAML;
$ret = $this->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 = <<<YAML
foo: !my_tag {foo: bar}
YAML;
$ret = $this->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
*/
Expand Down

0 comments on commit 35f300d

Please sign in to comment.