Skip to content

Commit

Permalink
feature #28521 [Yaml] Added support for multiple files or directories…
Browse files Browse the repository at this point in the history
… in LintCommand (yceruto)

This PR was squashed before being merged into the 4.2-dev branch (closes #28521).

Discussion
----------

[Yaml] Added support for multiple files or directories in LintCommand

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #28498
| License       | MIT
| Doc PR        | symfony/symfony-docs#10367

Commits
-------

d0f7950 [Yaml] Added support for multiple files or directories in LintCommand
  • Loading branch information
nicolas-grekas committed Sep 21, 2018
2 parents 4d6fc63 + d0f7950 commit a55853d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
5 changes: 5 additions & 0 deletions src/Symfony/Component/Yaml/CHANGELOG.md
@@ -1,6 +1,11 @@
CHANGELOG
=========

4.2.0
-----

* added support for multiple files or directories in `LintCommand`

4.0.0
-----

Expand Down
21 changes: 12 additions & 9 deletions src/Symfony/Component/Yaml/Command/LintCommand.php
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -53,7 +54,7 @@ protected function configure()
{
$this
->setDescription('Lints a file and outputs encountered errors')
->addArgument('filename', null, 'A file or a directory or STDIN')
->addArgument('filename', InputArgument::IS_ARRAY, '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
Expand Down Expand Up @@ -81,26 +82,28 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);
$filename = $input->getArgument('filename');
$filenames = (array) $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 (0 === \count($filenames)) {
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, $flags)));
}

if (!$this->isReadable($filename)) {
throw new RuntimeException(sprintf('File or directory "%s" is not readable.', $filename));
}

$filesInfo = array();
foreach ($this->getFiles($filename) as $file) {
$filesInfo[] = $this->validate(file_get_contents($file), $flags, $file);
foreach ($filenames as $filename) {
if (!$this->isReadable($filename)) {
throw new RuntimeException(sprintf('File or directory "%s" is not readable.', $filename));
}

foreach ($this->getFiles($filename) as $file) {
$filesInfo[] = $this->validate(file_get_contents($file), $flags, $file);
}
}

return $this->display($io, $filesInfo);
Expand Down
12 changes: 12 additions & 0 deletions src/Symfony/Component/Yaml/Tests/Command/LintCommandTest.php
Expand Up @@ -37,6 +37,18 @@ public function testLintCorrectFile()
$this->assertRegExp('/^\/\/ OK in /', trim($tester->getDisplay()));
}

public function testLintCorrectFiles()
{
$tester = $this->createCommandTester();
$filename1 = $this->createFile('foo: bar');
$filename2 = $this->createFile('bar: baz');

$ret = $tester->execute(array('filename' => array($filename1, $filename2)), array('verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false));

$this->assertEquals(0, $ret, 'Returns 0 in case of success');
$this->assertRegExp('/^\/\/ OK in /', trim($tester->getDisplay()));
}

public function testLintIncorrectFile()
{
$incorrectContent = '
Expand Down

0 comments on commit a55853d

Please sign in to comment.