8000 Accept overrides by console argument · symfony/symfony@45d803a · GitHub
[go: up one dir, main page]

Skip to content

Commit 45d803a

Browse files
Accept overrides by console argument
1 parent c223f3e commit 45d803a

File tree

2 files changed

+33
-22
lines changed

2 files changed

+33
-22
lines changed

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

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ class LintCommand extends Command
3939
private $displayCorrectFiles;
4040
private $directoryIteratorProvider;
4141
private $isReadableProvider;
42-
private $config;
4342

4443
public function __construct(string $name = null, callable $directoryIteratorProvider = null, callable $isReadableProvider = null)
4544
{
@@ -95,9 +94,31 @@ protected function execute(InputInterface $input, OutputInterface $output)
9594
{
9695
$io = new SymfonyStyle($input, $output);
9796
$filenames = (array) $input->getArgument('filename');
97+
$config = $input->getOption('config');
9898
$excludes = $input->getOption('exclude');
9999
$this->format = $input->getOption('format');
100-
$this->config = $input->getOption('config');
100+
$flags = $input->getOption('parse-tags');
101+
102+
if ($config) {
103+
if (!file_exists($config)) {
104+
throw new RuntimeException(sprintf('Lint config "%s" not found.', $config));
105+
}
106+
107+
$lintConfig = $this->parseLintConfig($config);
108+
109+
if ($this->format === null && isset($lintConfig['format'])) {
110+
$this->format = $lintConfig['format'];
111+
}
112+
if ($flags === false && isset($lintConfig['parse-tags'])) {
113+
$flags = $lintConfig['parse-tags'];
114+
}
115+
if (!count($filenames) && isset($lintConfig['includes'])) {
116+
$filenames = $lintConfig['includes'];
117+
}
118+
if (!count($excludes) && isset($lintConfig['excludes'])) {
119+
$excludes = $lintConfig['excludes'];
120+
}
121+
}
101122

102123
if ('github' === $this->format && !class_exists(GithubActionReporter::class)) {
103124
throw new \InvalidArgumentException('The "github" format is only available since "symfony/console" >= 5.3.');
@@ -108,21 +129,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
108129
$this->format = class_exists(GithubActionReporter::class) && GithubActionReporter::isGithubActionEnvironment() ? 'github' : 'txt';
109130
}
110131

111-
$this->displayCorrectFiles = $output->isVerbose();
112-
$flags = $input->getOption('parse-tags') ? Yaml::PARSE_CUSTOM_TAGS : 0;
113-
114-
if ($this->config) {
115-
if (!file_exists($this->config)) {
116-
throw new RuntimeException(sprintf('Lint config "%s" not found.', $this->config));
117-
}
132+
$flags = $flags ? Yaml::PARSE_CUSTOM_TAGS : 0;
118133

119-
$lintConfig = $this->parseLintConfig($this->config);
120-
121-
$this->format = $lintConfig['format'] ?? $this->format;
122-
$flags = $lintConfig['parse-tags'] ?? $flags;
123-
$filenames = $lintConfig['includes'] ?? [];
124-
$excludes = $lintConfig['excludes'] ?? $excludes;
125-
}
134+
$this->displayCorrectFiles = $output->isVerbose();
126135

127136
if (['-'] === $filenames) {
128137
return $this->display($io, [$this->validate(file_get_contents('php://stdin'), $flags)]);

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,20 +183,22 @@ public function testCorrectLintConfig()
183183
$this->assertStringContainsString('All 1 YAML files contain valid syntax.', trim($tester->getDisplay()));
184184
}
185185

186-
public function testMixedLintConfig()
186+
public function testOverrideLintConfig()
187187
{
188188
$tester = $this->createCommandTester();
189-
$filename = $this->createFile('foo: bar');
189+
$filename1 = $this->createFile('foo: bar');
190+
$filename2 = $this->createFile('foo: bar');
190191

191192
$yaml = <<<YAML
192193
yaml-lint:
193-
format: txt
194+
format: invalid
194195
includes:
195-
- $filename
196-
excludes: []
196+
- invalid
197+
excludes:
198+
- invalid
197199
YAML;
198200

199-
$ret = $tester->execute(['filename' => $this->createFile('invalid'), '--format' => 'invalid', '--exclude' => [$filename], '--config' => $this->createFile($yaml)], ['decorated' => false]);
201+
$ret = $tester->execute(['filename' => [$filename1, $filename2], '--format' => 'txt', '--exclude' => [$filename2], '--config' => $this->createFile($yaml)], ['decorated' => false]);
200202
$this->assertSame(0, $ret, 'lint:yaml exits with code 0 in case of success');
201203
$this->assertStringContainsString('All 1 YAML files contain valid syntax.', trim($tester->getDisplay()));
202204
}

0 commit comments

Comments
 (0)
0