Skip to content

Commit

Permalink
Allow for redacting of environment table values (pytest-dev#431)
Browse files Browse the repository at this point in the history
* Add untested change

* Add tests

* Add documentation

* Add the changelog entry

* remove debug code from test

* Change wording of documentation
  • Loading branch information
gnikonorov authored and BeyondEvil committed Apr 3, 2023
1 parent 72cede0 commit c5c2b6e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/changelog.rst
Expand Up @@ -17,6 +17,10 @@ Version History

* Thanks to `@gnikonorov <https://github.com/gnikonorov>`_ for the PR

* Implement :code:`environment_table_redact_list` to allow for redaction of environment table values. (`#233 <https://github.com/pytest-dev/pytest-html/issues/233>`_)

* Thanks to `@fenchu <https://github.com/fenchu>`_ for reporting and `@gnikonorov <https://github.com/gnikonorov>`_ for the PR

3.1.1 (2020-12-13)
~~~~~~~~~~~~~~~~~~

Expand Down
13 changes: 13 additions & 0 deletions src/pytest_html/html_report.py
Expand Up @@ -2,6 +2,7 @@
import datetime
import json
import os
import re
import time
from collections import defaultdict
from collections import OrderedDict
Expand Down Expand Up @@ -226,6 +227,10 @@ def _generate_environment(self, config):

for key in keys:
value = metadata[key]
if self._is_redactable_environment_variable(key, config):
black_box_ascii_value = 0x2593
value = "".join(chr(black_box_ascii_value) for char in str(value))

if isinstance(value, str) and value.startswith("http"):
value = html.a(value, href=value, target="_blank")
elif isinstance(value, (list, tuple, set)):
Expand All @@ -239,6 +244,14 @@ def _generate_environment(self, config):
environment.append(html.table(rows, id="environment"))
return environment

def _is_redactable_environment_variable(self, environment_variable, config):
redactable_regexes = config.getini("environment_table_redact_list")
for redactable_regex in redactable_regexes:
if re.match(redactable_regex, environment_variable):
return True

return False

def _save_report(self, report_content):
dir_name = os.path.dirname(self.logfile)
assets_dir = os.path.join(dir_name, "assets")
Expand Down

0 comments on commit c5c2b6e

Please sign in to comment.