From b60e0c14544c5a57970f4d3ae3d52bcf741823f9 Mon Sep 17 00:00:00 2001 From: Robin Chalas Date: Mon, 9 Sep 2019 08:13:16 +0200 Subject: [PATCH] Fix lint commands frozen on empty stdin --- .../Bridge/Twig/Command/LintCommand.php | 18 +++++++------- .../Tests/Command/XliffLintCommandTest.php | 19 +-------------- .../Tests/Command/YamlLintCommandTest.php | 19 +-------------- .../Translation/Command/XliffLintCommand.php | 16 +++++++------ .../Tests/Command/XliffLintCommandTest.php | 2 +- .../Component/Yaml/Command/LintCommand.php | 24 ++++++++++--------- 6 files changed, 35 insertions(+), 63 deletions(-) diff --git a/src/Symfony/Bridge/Twig/Command/LintCommand.php b/src/Symfony/Bridge/Twig/Command/LintCommand.php index 8828bab1d1..f25574c1e2 100644 --- a/src/Symfony/Bridge/Twig/Command/LintCommand.php +++ b/src/Symfony/Bridge/Twig/Command/LintCommand.php @@ -57,7 +57,7 @@ the first encountered syntax error. You can validate the syntax of contents passed from STDIN: - cat filename | php %command.full_name% + cat filename | php %command.full_name% - Or the syntax of a file: @@ -77,13 +77,15 @@ EOF { $io = new SymfonyStyle($input, $output); $filenames = $input->getArgument('filename'); - $hasStdin = ['-'] === $filenames; - if ($hasStdin || !$filenames) { - if ($hasStdin || 0 === ftell(STDIN)) { // remove 0 === ftell(STDIN) check in 5.0 - if (!$hasStdin) { - @trigger_error('Calling to the "lint:twig" command providing pipe file content to STDIN without passing the dash symbol "-" explicitly is deprecated since Symfony 4.4.', E_USER_DEPRECATED); - } + if (['-'] === $filenames) { + return $this->display($input, $output, $io, [$this->validate($this->getStdin(), uniqid('sf_', true))]); + } + + if (!$filenames) { + // @deprecated to be removed in 5.0 + if (0 === ftell(STDIN)) { + @trigger_error('Piping content from STDIN to the "lint:twig" command without passing the dash symbol "-" as argument is deprecated since Symfony 4.4.', E_USER_DEPRECATED); return $this->display($input, $output, $io, [$this->validate($this->getStdin(), uniqid('sf_', true))]); } @@ -97,7 +99,7 @@ EOF $filenames = array_merge(...$paths); } - if (0 === \count($filenames)) { + if (!$filenames) { throw new RuntimeException('Please provide a filename or pipe template content to STDIN.'); } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/XliffLintCommandTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/XliffLintCommandTest.php index ed8dd52d88..7d6783d935 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/XliffLintCommandTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/XliffLintCommandTest.php @@ -35,29 +35,12 @@ class XliffLintCommandTest extends TestCase { $command = new XliffLintCommand(); $expected = <<%command.name% command lints a XLIFF file and outputs to STDOUT -the first encountered syntax error. - -You can validates XLIFF contents passed from STDIN: - - cat filename | php %command.full_name% - -You can also validate the syntax of a file: - - php %command.full_name% filename - -Or of a whole directory: - - php %command.full_name% dirname - php %command.full_name% dirname --format=json - Or find all files in a bundle: php %command.full_name% @AcmeDemoBundle - EOF; - $this->assertEquals($expected, $command->getHelp()); + $this->assertStringContainsString($expected, $command->getHelp()); } public function testLintFilesFromBundleDirectory() diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/YamlLintCommandTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/YamlLintCommandTest.php index 43e0b7f071..af81f335e3 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/YamlLintCommandTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/YamlLintCommandTest.php @@ -72,29 +72,12 @@ bar'; { $command = new YamlLintCommand(); $expected = <<%command.name% command lints a YAML file and outputs to STDOUT -the first encountered syntax error. - -You can validates YAML contents passed from STDIN: - - cat filename | php %command.full_name% - -You can also validate the syntax of a file: - - php %command.full_name% filename - -Or of a whole directory: - - php %command.full_name% dirname - php %command.full_name% dirname --format=json - Or find all files in a bundle: php %command.full_name% @AcmeDemoBundle - EOF; - $this->assertEquals($expected, $command->getHelp()); + $this->assertStringContainsString($expected, $command->getHelp()); } public function testLintFilesFromBundleDirectory() diff --git a/src/Symfony/Component/Translation/Command/XliffLintCommand.php b/src/Symfony/Component/Translation/Command/XliffLintCommand.php index e7575155bd..f078b55bbf 100644 --- a/src/Symfony/Component/Translation/Command/XliffLintCommand.php +++ b/src/Symfony/Component/Translation/Command/XliffLintCommand.php @@ -61,7 +61,7 @@ the first encountered syntax error. You can validates XLIFF contents passed from STDIN: - cat filename | php %command.full_name% + cat filename | php %command.full_name% - You can also validate the syntax of a file: @@ -83,16 +83,18 @@ EOF $filenames = (array) $input->getArgument('filename'); $this->format = $input->getOption('format'); $this->displayCorrectFiles = $output->isVerbose(); - $hasStdin = ['-'] === $filenames; - if ($hasStdin || !$filenames) { - if (!$hasStdin && 0 !== ftell(STDIN)) { // remove 0 !== ftell(STDIN) check in 5.0 + if (['-'] === $filenames) { + return $this->display($io, [$this->validate($this->getStdin())]); + } + + // @deprecated to be removed in 5.0 + if (!$filenames) { + if (0 !== ftell(STDIN)) { throw new RuntimeException('Please provide a filename or pipe file content to STDIN.'); } - if (!$hasStdin) { - @trigger_error('Calling to the "lint:xliff" command providing pipe file content to STDIN without passing the dash symbol "-" explicitly is deprecated since Symfony 4.4.', E_USER_DEPRECATED); - } + @trigger_error('Piping content from STDIN to the "lint:xliff" command without passing the dash symbol "-" as argument is deprecated since Symfony 4.4.', E_USER_DEPRECATED); return $this->display($io, [$this->validate($this->getStdin())]); } diff --git a/src/Symfony/Component/Translation/Tests/Command/XliffLintCommandTest.php b/src/Symfony/Component/Translation/Tests/Command/XliffLintCommandTest.php index 39c673924d..67ad952b89 100644 --- a/src/Symfony/Component/Translation/Tests/Command/XliffLintCommandTest.php +++ b/src/Symfony/Component/Translation/Tests/Command/XliffLintCommandTest.php @@ -124,7 +124,7 @@ the first encountered syntax error. You can validates XLIFF contents passed from STDIN: - cat filename | php %command.full_name% + cat filename | php %command.full_name% - You can also validate the syntax of a file: diff --git a/src/Symfony/Component/Yaml/Command/LintCommand.php b/src/Symfony/Component/Yaml/Command/LintCommand.php index d2a216b919..368cc2311d 100644 --- a/src/Symfony/Component/Yaml/Command/LintCommand.php +++ b/src/Symfony/Component/Yaml/Command/LintCommand.php @@ -63,7 +63,7 @@ the first encountered syntax error. You can validates YAML contents passed from STDIN: - cat filename | php %command.full_name% + cat filename | php %command.full_name% - You can also validate the syntax of a file: @@ -86,20 +86,22 @@ EOF $this->format = $input->getOption('format'); $this->displayCorrectFiles = $output->isVerbose(); $flags = $input->getOption('parse-tags') ? Yaml::PARSE_CUSTOM_TAGS : 0; - $hasStdin = ['-'] === $filenames; - - if ($hasStdin || !$filenames) { - if (!$hasStdin && 0 !== ftell(STDIN)) { // remove 0 !== ftell(STDIN) check in 5.0 - throw new RuntimeException('Please provide a filename or pipe file content to STDIN.'); - } - - if (!$hasStdin) { - @trigger_error('Calling to the "lint:yaml" command providing pipe file content to STDIN without passing the dash symbol "-" explicitly is deprecated since Symfony 4.4.', E_USER_DEPRECATED); - } + if (['-'] === $filenames) { return $this->display($io, [$this->validate($this->getStdin(), $flags)]); } + // @deprecated to be removed in 5.0 + if (!$filenames) { + if (0 === ftell(STDIN)) { + @trigger_error('Piping content from STDIN to the "lint:yaml" command without passing the dash symbol "-" as argument is deprecated since Symfony 4.4.', E_USER_DEPRECATED); + + return $this->display($io, [$this->validate($this->getStdin(), $flags)]); + } + + throw new RuntimeException('Please provide a filename or pipe file content to STDIN.'); + } + $filesInfo = []; foreach ($filenames as $filename) { if (!$this->isReadable($filename)) {