Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/pytest_html/basereport.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,20 @@ def css(self):
return

def _asset_filename(self, test_id, extra_index, test_index, file_extension):
return "{}_{}_{}.{}".format(
filename = "{}_{}_{}.{}".format(
re.sub(r"[^\w.]", "_", test_id),
str(extra_index),
str(test_index),
file_extension,
)[-self._max_asset_filename_length :]
)
max_length = self._max_asset_filename_length
assets_dir = getattr(self, "_assets_path", None)
if assets_dir is not None:
# Files are written under this directory (see Report._write_content).
# Reserve the directory name plus separator so the relative path
# stays within max_asset_filename_length on path-limited systems.
max_length -= len(assets_dir.name) + 1
return filename[-max(1, max_length) :]

def _generate_report(self, self_contained=False):
generated = datetime.datetime.now()
Expand Down
3 changes: 2 additions & 1 deletion testing/legacy_test_pytest_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,8 +681,9 @@ def {test_name}():
"""
)
result, html = run(testdir, "report.html")
truncated_length = max(1, max_asset_filename_length - len("assets/"))
file_name = f"test_very_long_test_name.py__{test_name}_0_0.png"[
-max_asset_filename_length:
-truncated_length:
]
src = "assets/" + file_name
link = f'<a class="image" href="{src}" target="_blank">'
Expand Down
23 changes: 23 additions & 0 deletions testing/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import pytest
from assertpy import assert_that

from pytest_html.basereport import BaseReport

pytest_plugins = ("pytester",)


Expand Down Expand Up @@ -146,3 +148,24 @@ def test_custom_css_selfcontained(pytester, css_file_path, expandvar):
with open(pytester.path / "report.html") as f:
html = f.read()
assert_that(html).contains("* " + str(css_file_path)).contains("* two.css")


def test_asset_filename_accounts_for_assets_directory_prefix():
"""Relative asset paths must stay within max_asset_filename_length (#906)."""

class _Report:
_max_asset_filename_length = 255
_assets_path = Path("report_dir") / "assets"

filename = BaseReport._asset_filename(
_Report(),
"test_" + "a" * 300,
extra_index=0,
test_index=0,
file_extension="png",
)
relative_path = str(Path("assets") / filename).replace("\\", "/")
assert_that(len(filename)).is_less_than_or_equal_to(255 - len("assets/"))
assert_that(len(relative_path)).is_less_than_or_equal_to(255)
assert_that(relative_path).starts_with("assets/")
assert_that(filename).ends_with(".png")