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 | 20 ++++++++++--------- 6 files changed, 33 insertions(+), 61 deletions(-) diff --git a/src/Symfony/Bridge/Twig/Command/LintCommand.php b/src/Symfony/Bridge/Twig/Command/LintCommand.php index 8828bab1d1885..f25574c1e2dca 100644 --- a/src/Symfony/Bridge/Twig/Command/LintCommand.php +++ b/src/Symfony/Bridge/Twig/Command/LintCommand.php @@ -57,7 +57,7 @@ protected function configure() 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 @@ protected function execute(InputInterface $input, OutputInterface $output) { $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 @@ protected function execute(InputInterface $input, OutputInterface $output) $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 ed8dd52d88852..7d6783d9352c0 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/XliffLintCommandTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/XliffLintCommandTest.php @@ -35,29 +35,12 @@ public function testGetHelp() { $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 43e0b7f071fe8..af81f335e3cdb 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/YamlLintCommandTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/YamlLintCommandTest.php @@ -72,29 +72,12 @@ public function testGetHelp() { $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 e7575155bd0f3..f078b55bbf5ce 100644 --- a/src/Symfony/Component/Translation/Command/XliffLintCommand.php +++ b/src/Symfony/Component/Translation/Command/XliffLintCommand.php @@ -61,7 +61,7 @@ protected function configure() 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 @@ protected function execute(InputInterface $input, OutputInterface $output) $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 39c673924d7e5..67ad952b89224 100644 --- a/src/Symfony/Component/Translation/Tests/Command/XliffLintCommandTest.php +++ b/src/Symfony/Component/Translation/Tests/Command/XliffLintCommandTest.php @@ -124,7 +124,7 @@ public function testGetHelp() 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 d2a216b919eed..368cc2311dfc2 100644 --- a/src/Symfony/Component/Yaml/Command/LintCommand.php +++ b/src/Symfony/Component/Yaml/Command/LintCommand.php @@ -63,7 +63,7 @@ protected function configure() 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,18 +86,20 @@ protected function execute(InputInterface $input, OutputInterface $output) $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 (['-'] === $filenames) { + return $this->display($io, [$this->validate($this->getStdin(), $flags)]); + } - 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); + // @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)]); } - return $this->display($io, [$this->validate($this->getStdin(), $flags)]); + throw new RuntimeException('Please provide a filename or pipe file content to STDIN.'); } $filesInfo = [];