minor #33523 Fix lint commands frozen on empty stdin (chalasr)

This PR was merged into the 4.4 branch.

Discussion
----------

Fix lint commands frozen on empty stdin

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Running e.g. `lint:yaml -` with no piped content makes the command hangs currently, this makes it fail instead. Also fixes the command help which we forgot to update

Commits
-------

b60e0c1454 Fix lint commands frozen on empty stdin
This commit is contained in:
Nicolas Grekas 2019-09-18 14:09:11 +02:00
commit 41a450b727
6 changed files with 35 additions and 63 deletions

View File

@ -57,7 +57,7 @@ the first encountered syntax error.
You can validate the syntax of contents passed from STDIN:
<info>cat filename | php %command.full_name%</info>
<info>cat filename | php %command.full_name% -</info>
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.');
}
}

View File

@ -35,29 +35,12 @@ class XliffLintCommandTest extends TestCase
{
$command = new XliffLintCommand();
$expected = <<<EOF
The <info>%command.name%</info> command lints a XLIFF file and outputs to STDOUT
the first encountered syntax error.
You can validates XLIFF contents passed from STDIN:
<info>cat filename | php %command.full_name%</info>
You can also validate the syntax of a file:
<info>php %command.full_name% filename</info>
Or of a whole directory:
<info>php %command.full_name% dirname</info>
<info>php %command.full_name% dirname --format=json</info>
Or find all files in a bundle:
<info>php %command.full_name% @AcmeDemoBundle</info>
EOF;
$this->assertEquals($expected, $command->getHelp());
$this->assertStringContainsString($expected, $command->getHelp());
}
public function testLintFilesFromBundleDirectory()

View File

@ -72,29 +72,12 @@ bar';
{
$command = new YamlLintCommand();
$expected = <<<EOF
The <info>%command.name%</info> command lints a YAML file and outputs to STDOUT
the first encountered syntax error.
You can validates YAML contents passed from STDIN:
<info>cat filename | php %command.full_name%</info>
You can also validate the syntax of a file:
<info>php %command.full_name% filename</info>
Or of a whole directory:
<info>php %command.full_name% dirname</info>
<info>php %command.full_name% dirname --format=json</info>
Or find all files in a bundle:
<info>php %command.full_name% @AcmeDemoBundle</info>
EOF;
$this->assertEquals($expected, $command->getHelp());
$this->assertStringContainsString($expected, $command->getHelp());
}
public function testLintFilesFromBundleDirectory()

View File

@ -61,7 +61,7 @@ the first encountered syntax error.
You can validates XLIFF contents passed from STDIN:
<info>cat filename | php %command.full_name%</info>
<info>cat filename | php %command.full_name% -</info>
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())]);
}

View File

@ -124,7 +124,7 @@ the first encountered syntax error.
You can validates XLIFF contents passed from STDIN:
<info>cat filename | php %command.full_name%</info>
<info>cat filename | php %command.full_name% -</info>
You can also validate the syntax of a file:

View File

@ -63,7 +63,7 @@ the first encountered syntax error.
You can validates YAML contents passed from STDIN:
<info>cat filename | php %command.full_name%</info>
<info>cat filename | php %command.full_name% -</info>
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)) {