Skip to content

Add suggestions for the option 'format' of lints commands: twig, yaml and xliff #43680

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Symfony/Bridge/Twig/Command/LintCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

use Symfony\Component\Console\CI\GithubActionReporter;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Completion\CompletionInput;
use Symfony\Component\Console\Completion\CompletionSuggestions;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Input\InputArgument;
Expand Down Expand Up @@ -284,4 +286,11 @@ private function getContext(string $template, int $line, int $context = 3)

return $result;
}

public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
{
if ($input->mustSuggestOptionValuesFor('format')) {
$suggestions->suggestValues(['txt', 'json', 'github']);
}
}
}
25 changes: 23 additions & 2 deletions src/Symfony/Bridge/Twig/Tests/Command/LintCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Twig\Command\LintCommand;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Tester\CommandCompletionTester;
use Symfony\Component\Console\Tester\CommandTester;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
Expand Down Expand Up @@ -134,7 +136,27 @@ public function testLintAutodetectsGithubActionEnvironment()
}
}

/**
* @dataProvider provideCompletionSuggestions
*/
public function testComplete(array $input, array $expectedSuggestions)
{
$tester = new CommandCompletionTester($this->createCommand());

$this->assertSame($expectedSuggestions, $tester->complete($input));
}

public function provideCompletionSuggestions()
{
yield 'option' => [['--format', ''], ['txt', 'json', 'github']];
}

private function createCommandTester(): CommandTester
{
return new CommandTester($this->createCommand());
}

private function createCommand(): Command
{
$environment = new Environment(new FilesystemLoader(\dirname(__DIR__).'/Fixtures/templates/'));
$environment->addFilter(new TwigFilter('deprecated_filter', function ($v) {
Expand All @@ -145,9 +167,8 @@ private function createCommandTester(): CommandTester

$application = new Application();
$application->add($command);
$command = $application->find('lint:twig');

return new CommandTester($command);
return $application->find('lint:twig');
}

private function createFile($content): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

use Symfony\Component\Console\CI\GithubActionReporter;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Completion\CompletionInput;
use Symfony\Component\Console\Completion\CompletionSuggestions;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -274,4 +276,11 @@ private function getTargetLanguageFromFile(\DOMDocument $xliffContents): ?string

return null;
}

public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
{
if ($input->mustSuggestOptionValuesFor('format')) {
$suggestions->suggestValues(['txt', 'json', 'github']);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Tester\CommandCompletionTester;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Translation\Command\XliffLintCommand;

Expand Down Expand Up @@ -201,7 +203,7 @@ private function createFile($sourceContent = 'note', $targetLanguage = 'en', $fi
return $filename;
}

private function createCommandTester($requireStrictFileNames = true, $application = null): CommandTester
private function createCommand($requireStrictFileNames = true, $application = null): Command
{
if (!$application) {
$application = new Application();
Expand All @@ -214,7 +216,12 @@ private function createCommandTester($requireStrictFileNames = true, $applicatio
$command->setApplication($application);
}

return new CommandTester($command);
return $command;
}

private function createCommandTester($requireStrictFileNames = true, $application = null): CommandTester
{
return new CommandTester($this->createCommand($requireStrictFileNames, $application));
}

protected function setUp(): void
Expand Down Expand Up @@ -244,4 +251,19 @@ public function provideStrictFilenames()
yield [true, '%locale%.messages.xlf', 'en', true];
yield [true, '%locale%.messages.xlf', 'es', true];
}

/**
* @dataProvider provideCompletionSuggestions
*/
public function testComplete(array $input, array $expectedSuggestions)
{
$tester = new CommandCompletionTester($this->createCommand());

$this->assertSame($expectedSuggestions, $tester->complete($input));
}

public function provideCompletionSuggestions()
{
yield 'option' => [['--format', ''], ['txt', 'json', 'github']];
}
}
9 changes: 9 additions & 0 deletions src/Symfony/Component/Yaml/Command/LintCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

use Symfony\Component\Console\CI\GithubActionReporter;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Completion\CompletionInput;
use Symfony\Component\Console\Completion\CompletionSuggestions;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Input\InputArgument;
Expand Down Expand Up @@ -277,4 +279,11 @@ private function isReadable(string $fileOrDirectory): bool

return $default($fileOrDirectory);
}

public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
{
if ($input->mustSuggestOptionValuesFor('format')) {
$suggestions->suggestValues(['txt', 'json', 'github']);
}
}
}
27 changes: 24 additions & 3 deletions src/Symfony/Component/Yaml/Tests/Command/LintCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\CI\GithubActionReporter;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Tester\CommandCompletionTester;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Yaml\Command\LintCommand;

Expand Down Expand Up @@ -163,6 +165,21 @@ public function testLintFileNotReadable()
$tester->execute(['filename' => $filename], ['decorated' => false]);
}

/**
* @dataProvider provideCompletionSuggestions
*/
public function testComplete(array $input, array $expectedSuggestions)
{
$tester = new CommandCompletionTester($this->createCommand());

$this->assertSame($expectedSuggestions, $tester->complete($input));
}

public function provideCompletionSuggestions()
{
yield 'option' => [['--format', ''], ['txt', 'json', 'github']];
}

private function createFile($content): string
{
$filename = tempnam(sys_get_temp_dir().'/framework-yml-lint-test', 'sf-');
Expand All @@ -173,13 +190,17 @@ private function createFile($content): string
return $filename;
}

protected function createCommandTester(): CommandTester
protected function createCommand(): Command
{
$application = new Application();
$application->add(new LintCommand());
$command = $application->find('lint:yaml');

return new CommandTester($command);
return $application->find('lint:yaml');
}

protected function createCommandTester(): CommandTester
{
return new CommandTester($this->createCommand());
}

protected function setUp(): void
Expand Down