Skip to content

Commit

Permalink
Added option to append one or more user-provided style sheets to repo…
Browse files Browse the repository at this point in the history
…rt's styling.
  • Loading branch information
i-am-david-fernandez authored and davehunt committed Apr 5, 2018
1 parent ebebda4 commit f8fcd88
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions pytest_html/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ def pytest_addoption(parser):
'that the report may not render or function where CSP '
'restrictions are in place (see '
'https://developer.mozilla.org/docs/Web/Security/CSP)')
group.addoption('--append-css', action='append', dest='appendcss',
metavar='path', default=None,
help='append given CSS file content to report style file.')


def pytest_configure(config):
Expand Down Expand Up @@ -92,6 +95,9 @@ def __init__(self, logfile, config):
self.self_contained = config.getoption('self_contained_html')
self.config = config

self.css_append = config.getoption('appendcss')
self.css_errors = []

class TestResult:

def __init__(self, outcome, report, logfile, config):
Expand Down Expand Up @@ -129,7 +135,7 @@ def __init__(self, outcome, report, logfile, config):
if len(cells) > 0:
self.row_table = html.tr(cells)
self.row_extra = html.tr(html.td(self.additional_html,
class_='extra', colspan=len(cells)))
class_='extra', colspan=len(cells)))

def __lt__(self, other):
order = ('Error', 'Failed', 'Rerun', 'XFailed',
Expand All @@ -139,7 +145,7 @@ def __lt__(self, other):
def create_asset(self, content, extra_index,
test_index, file_extension, mode='w'):
hash_key = ''.join([self.test_id, str(extra_index),
str(test_index)]).encode('utf-8')
str(test_index)]).encode('utf-8')
hash_generator = hashlib.md5()
hash_generator.update(hash_key)
asset_file_name = '{0}.{1}'.format(hash_generator.hexdigest(),
Expand Down Expand Up @@ -321,6 +327,21 @@ def _generate_report(self, session):
ansi_css.extend([str(r) for r in style.get_styles()])
self.style_css += '\n'.join(ansi_css)

# <DF> Add user-provided CSS
if self.css_append:
self.style_css += '\n'
user_css = []
for filename in self.css_append:
try:
with open(filename, 'r') as css_file:
user_css.append('/* Begin CSS from {} */'.format(filename))
user_css.extend(css_file.readlines())
user_css.append('/* End CSS from {} */'.format(filename))
except Exception as e:
self.css_errors.append('Warning: Could not read CSS from {}: {}'.format(filename, e))

self.style_css += '\n'.join(user_css)

css_href = '{0}/{1}'.format('assets', 'style.css')
html_css = html.link(href=css_href, rel='stylesheet',
type='text/css')
Expand Down Expand Up @@ -402,10 +423,10 @@ def generate_summary_item(self):
html.tr(cells),
html.tr([
html.th('No results found. Try to check the filters',
colspan=len(cells))],
colspan=len(cells))],
id='not-found-message', hidden='true'),
id='results-table-head'),
self.test_logs], id='results-table')]
self.test_logs], id='results-table')]

main_js = pkg_resources.resource_string(
__name__, os.path.join('resources', 'main.js'))
Expand Down Expand Up @@ -501,3 +522,5 @@ def pytest_sessionfinish(self, session):
def pytest_terminal_summary(self, terminalreporter):
terminalreporter.write_sep('-', 'generated html file: {0}'.format(
self.logfile))
for css_error in self.css_errors:
terminalreporter.write_line(css_error)

0 comments on commit f8fcd88

Please sign in to comment.