Skip to content

Instantly share code, notes, and snippets.

@ilaif
Created April 16, 2019 21:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ilaif/d99c8a3d79afb01e2409a740d2db2d37 to your computer and use it in GitHub Desktop.
Save ilaif/d99c8a3d79afb01e2409a740d2db2d37 to your computer and use it in GitHub Desktop.
instrument-everything-blog-generate-html-report
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
def generate_html_report(coverage_object):
"""
Given a coverage object returns an html report
:param dict coverage_object:
:return list(string), string: html files, css file
"""
lexer = get_lexer_by_name('ruby', stripall=True)
formatter = HtmlFormatter(linenos=True, cssclass='source')
source_code_template = fs_util.read_file(path.join(CUR_DIR, '..', 'assets', 'instrumentation', 'code_cover.jinja2'))
html_files = {}
file_names = coverage_object['files'].keys()
for file_name, file_coverage in coverage_object['files'].items():
source = highlight(file_coverage['original_content'], lexer, formatter)
source_lines = source.split('\n')
# find the start of the code
indices = [i for (i, line) in enumerate(source_lines) if '<td class="code"><div class="source"><pre>' in line]
if len(indices) != 1:
raise Exception('No suitable pre code entries found')
uncovered_line_numbers_offset = [indices[0] + i - 1 for i in file_coverage['uncovered_line_numbers']]
for i in uncovered_line_numbers_offset:
source_lines[i] = '<span class="uncovered">{}</span>'.format(source_lines[i])
covered_line_numbers_offset = [indices[0] + i - 1 for i in file_coverage['covered_line_numbers']]
for i in covered_line_numbers_offset:
source_lines[i] = '<span class="covered">{}</span>'.format(source_lines[i])
source = '\n'.join(source_lines)
result = string_util.render_template_with_variables(source_code_template, {
'title': '{}.vcl'.format(file_name),
'file_names': file_names,
'file': file_coverage,
'source': source
})
html_files[file_name] = result
return html_files
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment