Skip to content

Commit d8dd91d

Browse files
pierredupfabpot
authored andcommitted
[YAML] Allow to parse custom tags when linting yaml files
1 parent 7bcb90f commit d8dd91d

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

src/Symfony/Component/Yaml/Command/LintCommand.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ protected function configure()
5252
->setDescription('Lints a file and outputs encountered errors')
5353
->addArgument('filename', null, 'A file or a directory or STDIN')
5454
->addOption('format', null, InputOption::VALUE_REQUIRED, 'The output format', 'txt')
55+
->addOption('parse-tags', null, InputOption::VALUE_NONE, 'Parse custom tags')
5556
->setHelp(<<<EOF
5657
The <info>%command.name%</info> command lints a YAML file and outputs to STDOUT
5758
the first encountered syntax error.
@@ -80,13 +81,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
8081
$filename = $input->getArgument('filename');
8182
$this->format = $input->getOption('format');
8283
$this->displayCorrectFiles = $output->isVerbose();
84+
$flags = $input->getOption('parse-tags') ? Yaml::PARSE_CUSTOM_TAGS : 0;
8385

8486
if (!$filename) {
8587
if (!$stdin = $this->getStdin()) {
8688
throw new \RuntimeException('Please provide a filename or pipe file content to STDIN.');
8789
}
8890

89-
return $this->display($io, array($this->validate($stdin)));
91+
return $this->display($io, array($this->validate($stdin, $flags)));
9092
}
9193

9294
if (!$this->isReadable($filename)) {
@@ -95,13 +97,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
9597

9698
$filesInfo = array();
9799
foreach ($this->getFiles($filename) as $file) {
98-
$filesInfo[] = $this->validate(file_get_contents($file), $file);
100+
$filesInfo[] = $this->validate(file_get_contents($file), $flags, $file);
99101
}
100102

101103
return $this->display($io, $filesInfo);
102104
}
103105

104-
private function validate($content, $file = null)
106+
private function validate($content, $flags, $file = null)
105107
{
106108
$prevErrorHandler = set_error_handler(function ($level, $message, $file, $line) use (&$prevErrorHandler) {
107109
if (E_USER_DEPRECATED === $level) {
@@ -112,7 +114,7 @@ private function validate($content, $file = null)
112114
});
113115

114116
try {
115-
$this->getParser()->parse($content, Yaml::PARSE_CONSTANT);
117+
$this->getParser()->parse($content, Yaml::PARSE_CONSTANT | $flags);
116118
} catch (ParseException $e) {
117119
return array('file' => $file, 'valid' => false, 'message' => $e->getMessage());
118120
} finally {

src/Symfony/Component/Yaml/Tests/Command/LintCommandTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,24 @@ public function testConstantAsKey()
6060
$this->assertSame(0, $ret, 'lint:yaml exits with code 0 in case of success');
6161
}
6262

63+
public function testCustomTags()
64+
{
65+
$yaml = <<<YAML
66+
foo: !my_tag {foo: bar}
67+
YAML;
68+
$ret = $this->createCommandTester()->execute(array('filename' => $this->createFile($yaml), '--parse-tags' => true), array('verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false));
69+
$this->assertSame(0, $ret, 'lint:yaml exits with code 0 in case of success');
70+
}
71+
72+
public function testCustomTagsError()
73+
{
74+
$yaml = <<<YAML
75+
foo: !my_tag {foo: bar}
76+
YAML;
77+
$ret = $this->createCommandTester()->execute(array('filename' => $this->createFile($yaml)), array('verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false));
78+
$this->assertSame(1, $ret, 'lint:yaml exits with code 1 in case of error');
79+
}
80+
6381
/**
6482
* @expectedException \RuntimeException
6583
*/

0 commit comments

Comments
 (0)