8000 [Yaml] Add --exclude and negatable --parse-tags option to lint:yaml command by christingruber · Pull Request #39641 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Yaml] Add --exclude and negatable --parse-tags option to lint:yaml command #39641

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Symfony/Component/Yaml/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
---

Expand Down
8000 16 changes: 13 additions & 3 deletions src/Symfony/Component/Yaml/Command/LintCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(<<<EOF
The <info>%command.name%</info> command lints a YAML file and outputs to STDOUT
the first encountered syntax error.
Expand All @@ -76,6 +77,10 @@ protected function configure()
<info>php %command.full_name% dirname</info>
<info>php %command.full_name% dirname --format=json</info>

You can also exclude one or more specific files:

<info>php %command.full_name% dirname --exclude="dirname/foo.yaml" --exclude="dirname/bar.yaml"</info>

EOF
)
;
Expand All @@ -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.');
Expand All @@ -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)]);
Expand All @@ -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);
}
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/Symfony/Component/Yaml/Tests/Command/LintCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Yaml/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
0