8000 Add tests for --exclude option and .yamllint.yml · symfony/symfony@68f8708 · GitHub
[go: up one dir, main page]

Skip to content

Commit 68f8708

Browse files
Add tests for --exclude option and .yamllint.yml
1 parent e01d9dd commit 68f8708

File tree

5 files changed

+51
-17
lines changed

5 files changed

+51
-17
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
yamllinter:
1+
yaml-lint:
22
format: txt
33
includes:
44
- src/Resources/config/foo.yml
55
- src/Resources/config/bar.yml
66
- src/Resources/config/path/
77
excludes:
8-
- src/Resources/config/path/exclude.yml
8+
- src/Resources/config/path/exclude.yml

src/Symfony/Component/Yaml/CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ CHANGELOG
66

77
* Added `github` format support & autodetection to render errors as annotations
88
when running the YAML linter command in a Github Action environment.
9-
* Added support for optional linter config from file `lint:yaml .yamllinter.yml`.
9+
* Added support for optional lint config from file `lint:yaml .yamllint.yml`.
1010

1111
```yml
12-
yamllinter:
12+
yaml-lint:
1313
format: txt
1414
includes:
1515
- src/Resources/config/foo.yml

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ class LintCommand extends Command
3939
private $displayCorrectFiles;
4040
private $directoryIteratorProvider;
4141
private $isReadableProvider;
42-
private $linterConfig;
42+
private $lintConfig;
4343

44-
public function __construct(string $name = null, callable $directoryIteratorProvider = null, callable $isReadableProvider = null, string $linterConfig = '.yamllinter.yml')
44+
public function __construct(string $name = null, callable $directoryIteratorProvider = null, callable $isReadableProvider = null, string $lintConfig = '.yamllint.yml')
4545
{
4646
parent::__construct($name);
4747

4848
$this->directoryIteratorProvider = $directoryIteratorProvider;
4949
$this->isReadableProvider = $isReadableProvider;
50-
$this->linterConfig = $linterConfig;
50+
$this->lintConfig = $lintConfig;
5151
}
5252

5353
/**
@@ -57,7 +57,7 @@ protected function configure()
5757
{
5858
$this
5959
->setDescription('Lints a file and outputs encountered errors')
60-
->addArgument('filename', InputArgument::IS_ARRAY, 'A file, a '.$this->linterConfig.' config, a directory or "-" for reading from STDIN')
60+
->addArgument('filename', InputArgument::IS_ARRAY, 'A file, a '.$this->lintConfig.' config, a directory or "-" for reading from STDIN')
6161
->addOption('format', null, InputOption::VALUE_REQUIRED, 'The output format')
6262
->addOption('exclude', null, InputOption::VALUE_REQUIRED, 'Files to exclude')
6363
->addOption('parse-tags', null, InputOption::VALUE_NONE, 'Parse custom tags')
@@ -71,7 +71,7 @@ protected function configure()
7171
7272
You can validate YAML contents passed from config file:
7373
74-
<info>php %command.full_name% .yamllinter.yml</info>
74+
<info>php %command.full_name% .yamllint.yml</info>
7575
7676
You can also validate the syntax of a file:
7777
@@ -107,12 +107,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
107107
$this->displayCorrectFiles = $output->isVerbose();
108108
$flags = $input->getOption('parse-tags') ? Yaml::PARSE_CUSTOM_TAGS : 0;
109109

110-
if ([$this->linterConfig] === $filenames) {
110+
if ([$this->lintConfig] === $filenames) {
111111
if (!file_exists($filenames[0])) {
112-
throw new RuntimeException(sprintf('Linter configuration file "%s" not found', $filenames[0]));
112+
throw new RuntimeException(sprintf('Lint config "%s" not found', $filenames[0]));
113113
}
114114

115-
$config = $this->parseLinterConfig($filenames[0]);
115+
$config = $this->parseLintConfig($filenames[0]);
116116

117117
$this->format = $config['format'] ?? $this->format;
118118
$flags = $config['parse-tags'] ?? $flags;
@@ -293,15 +293,15 @@ private function isReadable(string $fileOrDirectory): bool
293293
return $default($fileOrDirectory);
294294
}
295295

296-
private function parseLinterConfig(string $config): array
296+
private function parseLintConfig(string $config): array
297297
{
298298
$result = (new Parser())
299299
->parseFile($config);
300300

301-
if (!isset($result['yamllinter'])) {
302-
throw new RuntimeException(sprintf('Invalid YAML linter config "%s".', $config));
301+
if (!isset($result['yaml-lint'])) {
302+
throw new RuntimeException(sprintf('Invalid YAML lint config "%s".', $config));
303303
}
304304

305-
return $result['yamllinter'];
305+
return $result['yaml-lint'];
306306
}
307307
}

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,35 @@ public function testCustomTagsError()
142142
$this->assertSame(1, $ret, 'lint:yaml exits with code 1 in case of error');
143143
}
144144

145+
public function testLintWithExclude()
146+
{
147+
$tester = $this->createCommandTester();
148+
$filename = $this->createFile('foo: bar');
149+
150+
$ret = $tester->execute(['filename' => $filename, '--exclude' => $filename], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false]);
151+
$this->assertSame(0, $ret, 'lint:yaml exits with code 0 in case of success');
152+
$this->assertStringContainsString('All 0 YAML files contain valid syntax.', trim($tester->getDisplay()));
153+
}
154+
155+
public function testIncorrectLintConfig()
156+
{
157+
$this->expectException('RuntimeException');
158+
$this->createCommandTester()->execute(['filename' => '.yamllint.yml'], ['decorated' => false]);
159+
}
160+
161+
public function testCorrectLintConfig()
162+
{
163+
$tester = $this->createCommandTester();
164+
165+
copy(__DIR__.'/../Fixtures/.yamllint.yml', './.yamllint.yml');
166+
167+
$ret = $tester->execute(['filename' => '.yamllint.yml'], ['decorated' => false]);
168+
169+
$this->assertSame(0, $ret, 'lint:yaml exits with code 0 in case of success');
170+
$this->assertStringContainsString('All 1 YAML files contain valid syntax.', trim($tester->getDisplay()));
171+
unlink('./.yamllint.yml');
172+
}
173+
145174
public function testLintFileNotReadable()
146175
{
147176
$this->expectException('RuntimeException');
@@ -166,7 +195,7 @@ protected function createCommandTester(): CommandTester
166195
{
167196
$application = new Application();
168197
$application->add(new LintCommand());
169-
$command = $application->find('lint:yaml');
198+
$command = $application->find(LintCommand::getDefaultName());
170199

171200
return new CommandTester($command);
172201
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
yaml-lint:
2+
format: txt
3+
includes:
4+
- src/Symfony/Component/Yaml/Tests/Fixtures/index.yml
5+
excludes: []

0 commit comments

Comments
 (0)
0