Skip to content
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

Fix the reporting of deprecations in twig:lint #36265

Merged
merged 1 commit into from Mar 31, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/Symfony/Bridge/Twig/Command/LintCommand.php
Expand Up @@ -110,7 +110,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$prevErrorHandler = set_error_handler(static function ($level, $message, $file, $line) use (&$prevErrorHandler) {
if (E_USER_DEPRECATED === $level) {
$templateLine = 0;
if (preg_match('/ at line (\d+) /', $message, $matches)) {
if (preg_match('/ at line (\d+)[ .]/', $message, $matches)) {
$templateLine = $matches[1];
}

Expand Down Expand Up @@ -236,6 +236,14 @@ private function renderException(OutputInterface $output, string $template, Erro
$output->text(sprintf('<error> ERROR </error> (line %s)', $line));
}

// If the line is not known (this might happen for deprecations if we fail at detecting the line for instance),
// we render the message without context, to ensure the message is displayed.
if ($line <= 0) {
$output->text(sprintf('<error> >> %s</error> ', $exception->getRawMessage()));

return;
}

foreach ($this->getContext($template, $line) as $lineNumber => $code) {
$output->text(sprintf(
'%s %-6s %s',
Expand Down
36 changes: 35 additions & 1 deletion src/Symfony/Bridge/Twig/Tests/Command/LintCommandTest.php
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\Console\Tester\CommandTester;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
use Twig\TwigFilter;

class LintCommandTest extends TestCase
{
Expand Down Expand Up @@ -66,6 +67,34 @@ public function testLintFileCompileTimeException()
$this->assertRegExp('/ERROR in \S+ \(line /', trim($tester->getDisplay()));
}

/**
* When deprecations are not reported by the command, the testsuite reporter will catch them so we need to mark the test as legacy.
*
* @group legacy
*/
public function testLintFileWithNotReportedDeprecation()
{
$tester = $this->createCommandTester();
$filename = $this->createFile('{{ foo|deprecated_filter }}');

$ret = $tester->execute(['filename' => [$filename]], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false]);

$this->assertEquals(0, $ret, 'Returns 0 in case of success');
$this->assertStringContainsString('OK in', trim($tester->getDisplay()));
}

public function testLintFileWithReportedDeprecation()
{
$tester = $this->createCommandTester();
$filename = $this->createFile('{{ foo|deprecated_filter }}');

$ret = $tester->execute(['filename' => [$filename], '--show-deprecations' => true], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false]);

$this->assertEquals(1, $ret, 'Returns 1 in case of error');
$this->assertRegExp('/ERROR in \S+ \(line 1\)/', trim($tester->getDisplay()));
$this->assertStringContainsString('Filter "deprecated_filter" is deprecated', trim($tester->getDisplay()));
}

/**
* @group tty
*/
Expand All @@ -80,7 +109,12 @@ public function testLintDefaultPaths()

private function createCommandTester(): CommandTester
{
$command = new LintCommand(new Environment(new FilesystemLoader(\dirname(__DIR__).'/Fixtures/templates/')));
$environment = new Environment(new FilesystemLoader(\dirname(__DIR__).'/Fixtures/templates/'));
$environment->addFilter(new TwigFilter('deprecated_filter', function ($v) {
return $v;
}, ['deprecated' => true]));

$command = new LintCommand($environment);

$application = new Application();
$application->add($command);
Expand Down