Skip to content

Commit 4556076

Browse files
authored
Make the maximum asset filename length configurable. (#313)
* Make the maximum asset filename length configurable.
1 parent 1924c82 commit 4556076

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

pytest_html/plugin.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ def pytest_addoption(parser):
6969
default=False,
7070
help="Open the report with all rows collapsed. Useful for very large reports",
7171
)
72+
parser.addini(
73+
"max_asset_filename_length",
74+
default=255,
75+
help="set the maximum filename length for assets "
76+
"attached to the html report.",
77+
)
7278

7379

7480
def pytest_configure(config):
@@ -145,6 +151,9 @@ def __init__(self, outcome, report, logfile, config):
145151
self.additional_html = []
146152
self.links_html = []
147153
self.self_contained = config.getoption("self_contained_html")
154+
self.max_asset_filename_length = int(
155+
config.getini("max_asset_filename_length")
156+
)
148157
self.logfile = logfile
149158
self.config = config
150159
self.row_table = self.row_extra = None
@@ -194,13 +203,12 @@ def __lt__(self, other):
194203
def create_asset(
195204
self, content, extra_index, test_index, file_extension, mode="w"
196205
):
197-
# 255 is the common max filename length on various filesystems
198206
asset_file_name = "{}_{}_{}.{}".format(
199207
re.sub(r"[^\w\.]", "_", self.test_id),
200208
str(extra_index),
201209
str(test_index),
202210
file_extension,
203-
)[-255:]
211+
)[-self.max_asset_filename_length :]
204212
asset_path = os.path.join(
205213
os.path.dirname(self.logfile), "assets", asset_file_name
206214
)

testing/test_pytest_html.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,8 @@ def pytest_runtest_makereport(item, call):
566566
assert result.ret == 0
567567
assert '<a href="{0}"><img src="{0}"/>'.format(content) in html
568568

569-
def test_very_long_test_name(self, testdir):
569+
@pytest.mark.parametrize("max_asset_filename_length", [10, 100])
570+
def test_very_long_test_name(self, testdir, max_asset_filename_length):
570571
testdir.makeconftest(
571572
"""
572573
import pytest
@@ -587,8 +588,16 @@ def {test_name}():
587588
assert False
588589
"""
589590
)
590-
result, html = run(testdir)
591-
file_name = f"test_very_long_test_name.py__{test_name}_0_0.png"[-255:]
591+
testdir.makeini(
592+
f"""
593+
[pytest]
594+
max_asset_filename_length = {max_asset_filename_length}
595+
"""
596+
)
597+
result, html = run(testdir, "report.html")
598+
file_name = f"test_very_long_test_name.py__{test_name}_0_0.png"[
599+
-max_asset_filename_length:
600+
]
592601
src = "assets/" + file_name
593602
link = f'<a class="image" href="{src}" target="_blank">'
594603
img = f'<img src="{src}"/>'

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ commands = pre-commit run --all-files --show-diff-on-failure
2424
[flake8]
2525
max-line-length = 88
2626
exclude = .eggs,.tox
27+
ignore = E203
2728

2829
[pytest]
2930
testpaths = testing

0 commit comments

Comments
 (0)