From c329ca7e014ab2583f1a69531f99d243e7125038 Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Mon, 30 Mar 2020 14:56:31 +0200 Subject: [PATCH] Fix the reporting of deprecations in twig:lint - ensure that the message is rendered when the line detection fails and we end up with 0 as line number (the implementation also deals with -1 which is sometimes used by Twig for errors when it does not know the line, even though this should not happen for compile-time errors). - fix the detection of the line number when the number is at the end of the sentence, which happens for the deprecation of filters for instance. --- .../Bridge/Twig/Command/LintCommand.php | 10 +++++- .../Twig/Tests/Command/LintCommandTest.php | 36 ++++++++++++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Bridge/Twig/Command/LintCommand.php b/src/Symfony/Bridge/Twig/Command/LintCommand.php index fa12718818815..16f960e9f74c0 100644 --- a/src/Symfony/Bridge/Twig/Command/LintCommand.php +++ b/src/Symfony/Bridge/Twig/Command/LintCommand.php @@ -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]; } @@ -236,6 +236,14 @@ private function renderException(OutputInterface $output, string $template, Erro $output->text(sprintf(' 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(' >> %s ', $exception->getRawMessage())); + + return; + } + foreach ($this->getContext($template, $line) as $lineNumber => $code) { $output->text(sprintf( '%s %-6s %s', diff --git a/src/Symfony/Bridge/Twig/Tests/Command/LintCommandTest.php b/src/Symfony/Bridge/Twig/Tests/Command/LintCommandTest.php index 46c0883722a20..8970bb497764e 100644 --- a/src/Symfony/Bridge/Twig/Tests/Command/LintCommandTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Command/LintCommandTest.php @@ -18,6 +18,7 @@ use Symfony\Component\Console\Tester\CommandTester; use Twig\Environment; use Twig\Loader\FilesystemLoader; +use Twig\TwigFilter; class LintCommandTest extends TestCase { @@ -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 */ @@ -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); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy