Skip to content

Commit

Permalink
Make the maximum asset filename length configurable. (#313)
Browse files Browse the repository at this point in the history
* Make the maximum asset filename length configurable.
  • Loading branch information
D3X authored Jul 27, 2020
1 parent 1924c82 commit 4556076
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
12 changes: 10 additions & 2 deletions pytest_html/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ def pytest_addoption(parser):
default=False,
help="Open the report with all rows collapsed. Useful for very large reports",
)
parser.addini(
"max_asset_filename_length",
default=255,
help="set the maximum filename length for assets "
"attached to the html report.",
)


def pytest_configure(config):
Expand Down Expand Up @@ -145,6 +151,9 @@ def __init__(self, outcome, report, logfile, config):
self.additional_html = []
self.links_html = []
self.self_contained = config.getoption("self_contained_html")
self.max_asset_filename_length = int(
config.getini("max_asset_filename_length")
)
self.logfile = logfile
self.config = config
self.row_table = self.row_extra = None
Expand Down Expand Up @@ -194,13 +203,12 @@ def __lt__(self, other):
def create_asset(
self, content, extra_index, test_index, file_extension, mode="w"
):
# 255 is the common max filename length on various filesystems
asset_file_name = "{}_{}_{}.{}".format(
re.sub(r"[^\w\.]", "_", self.test_id),
str(extra_index),
str(test_index),
file_extension,
)[-255:]
)[-self.max_asset_filename_length :]
asset_path = os.path.join(
os.path.dirname(self.logfile), "assets", asset_file_name
)
Expand Down
15 changes: 12 additions & 3 deletions testing/test_pytest_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,8 @@ def pytest_runtest_makereport(item, call):
assert result.ret == 0
assert '<a href="{0}"><img src="{0}"/>'.format(content) in html

def test_very_long_test_name(self, testdir):
@pytest.mark.parametrize("max_asset_filename_length", [10, 100])
def test_very_long_test_name(self, testdir, max_asset_filename_length):
testdir.makeconftest(
"""
import pytest
Expand All @@ -587,8 +588,16 @@ def {test_name}():
assert False
"""
)
result, html = run(testdir)
file_name = f"test_very_long_test_name.py__{test_name}_0_0.png"[-255:]
testdir.makeini(
f"""
[pytest]
max_asset_filename_length = {max_asset_filename_length}
"""
)
result, html = run(testdir, "report.html")
file_name = f"test_very_long_test_name.py__{test_name}_0_0.png"[
-max_asset_filename_length:
]
src = "assets/" + file_name
link = f'<a class="image" href="{src}" target="_blank">'
img = f'<img src="{src}"/>'
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ commands = pre-commit run --all-files --show-diff-on-failure
[flake8]
max-line-length = 88
exclude = .eggs,.tox
ignore = E203

[pytest]
testpaths = testing
Expand Down

0 comments on commit 4556076

Please sign in to comment.