Skip to content

Commit

Permalink
feature #3695 Add @codeCoverageIgnore to untestable compiled method…
Browse files Browse the repository at this point in the history
…s (markhuot)

This PR was merged into the 3.x branch.

Discussion
----------

Add `@codeCoverageIgnore` to untestable compiled methods

I've been experimenting with adding my compiled templates to my code coverage reports and it works largely as expected. Templates that get executed return percentages accurate to the number of lines the tests actually cover. The only exception are the "meta" methods on the compiled template that aren't necessarily called by the tests.

This PR adds ``@codeCoverageIgnore`` comments to the compiled template for all non-display methods so the coverage report only lists lines from `doDisplay`.

I'm assuming that I will need to update some of the Twig tests to account for these new comments, but am curious if you'd be open to this change.

An example screenshot below. The last line shows line 46 out of the compiled template is never executed. The compiled template's `getDebugInfo` even correctly informs me that line 46 maps to line 4 in my `.twig` so I'll work on updating the line numbering next.

![image](https://user-images.githubusercontent.com/48975/166098890-fbaf99ad-9aa4-4430-9f82-050f482a6777.png)

Commits
-------

1cf610b Add `@codeCoverageIgnore` to untestable compiled methods
  • Loading branch information
fabpot committed Oct 27, 2023
2 parents 065634f + 1cf610b commit 9a602c7
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Node/ModuleNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,9 @@ protected function compileMacros(Compiler $compiler)
protected function compileGetTemplateName(Compiler $compiler)
{
$compiler
->write("/**")
->write(" * @codeCoverageIgnore")
->write(" */")
->write("public function getTemplateName()\n", "{\n")
->indent()
->write('return ')
Expand Down Expand Up @@ -409,6 +412,9 @@ protected function compileIsTraitable(Compiler $compiler)
}

$compiler
->write("/**")
->write(" * @codeCoverageIgnore")
->write(" */")
->write("public function isTraitable()\n", "{\n")
->indent()
->write(sprintf("return %s;\n", $traitable ? 'true' : 'false'))
Expand All @@ -420,6 +426,9 @@ protected function compileIsTraitable(Compiler $compiler)
protected function compileDebugInfo(Compiler $compiler)
{
$compiler
->write("/**")
->write(" * @codeCoverageIgnore")
->write(" */")
->write("public function getDebugInfo()\n", "{\n")
->indent()
->write(sprintf("return %s;\n", str_replace("\n", '', var_export(array_reverse($compiler->getDebugInfo(), true), true))))
Expand Down

0 comments on commit 9a602c7

Please sign in to comment.