-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
add debug info for more lines #674
Conversation
9eea56b
to
0a570e7
Compare
0a570e7
to
3f6cddd
Compare
With the proposed patch in the second commit debug_info becomes |
After the third commit the value is NOTE: please review the commits one by one to spot the differences. I'm not convinced this is a proper fix for my issue but being able to properly map between HTML lines in the template the generated Python source will enable proper coverage reporting for Jinja templates. I do have a coverage.py Jinja plugin which appears to be working correctly if not for the fact that Jinja doesn't provide all the line numbers information to properly report on the results. Note2: |
Same as with the other PR #673 I do not quite comprehend why this is considered a bug. Under which circumstance would a yield statement for template data fail? |
To make this more concrete: |
@mitsuhiko correct, no error can be generated here, but I need somehow to be able to map this line of Python code to its corresponding line in the template text. I can jump on IRC or email thread if you guys prefer to discuss at length, just let me know. |
Knowing your usecase for having more detailed debug info would indeed be useful |
I should probably point out that |
@ThiefMaster I am working on coverage.py plugin for Jinja templates. It can be found at The detailed debug info helps me get proper measurements as to which lines were actually covered. Then "fixing" the node line numbers to match the lines in the HTML template will make it very easy to produce coverage reports (e.g. colored HTML or annotated text). If it helps for the
|
I would also like to point out that, the jinja compiler will attempt to merge multiple TemplateData into one statement in the generated code, eg:
Will end up being compiled into something like this:
That line requires multiple entries in debug info, executing it should tell the coverage plugin that 3 different template lines are in fact being covered. This is likely to be more involved as template.get_corresponding_lineno() would actually return more than one line in these cases. There are a few statements like this in jinja, with/endwith for instance, which do not have an obvious line in the compiled output. I wonder if the compiler should start emitting some kind of noop statement just to get better debug info generated. |
@jdahlin can you tell me when does the jinja compiler generate debug info at the moment. By which property does the compiler know to generate debug info ? I may be able to use that in my tool to work around current limitations ? |
FYI https://github.com/MrSenko/coverage-jinja-plugin has been valuable in my testing and it would only be better if the HTML output could be made correct through the fix this patch provides. |
We're actually using Jinja to "templatize" some yaml files and I was after some way to provide coverage information on those files. Anyway, this coverage approach does sound interesting. |
Thanks for looking at this, sorry I took so long to get to it. I've been experimenting with this PR and better line mapping for a few days now, and my conclusion is that this isn't the right direction. It still misses multi-line template data, I recognize that there's interest in a coverage plugin for templates, but it turns out to be non-trivial given the way the lexer and compiler optimizations work. Perhaps we could add start and end lines to nodes, or base coverage on an analysis of the AST rather than the debug_info, but either way someone will have to investigate that more and provide a cleaner solution before I'm willing to add it. |
Using the following template:
jinja yields the following Python source code for it:
Notice that
debug_info
is missing mappings for two moreyield
statements, the one on line 18,yield '\n <ul>\n '
, and the one on line 25,yield '\n </ul>\n'
. This in turn causes theTemplate.get_corresponding_lineno()
method to return incorrect result.