diff --git a/pytest_html/plugin.py b/pytest_html/plugin.py index b92236a9..00eca557 100644 --- a/pytest_html/plugin.py +++ b/pytest_html/plugin.py @@ -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): @@ -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 @@ -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 ) diff --git a/testing/test_pytest_html.py b/testing/test_pytest_html.py index 290ffe98..cf608327 100644 --- a/testing/test_pytest_html.py +++ b/testing/test_pytest_html.py @@ -566,7 +566,8 @@ def pytest_runtest_makereport(item, call): assert result.ret == 0 assert ''.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 @@ -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'' img = f'' diff --git a/tox.ini b/tox.ini index e85d0b3b..d1464b80 100644 --- a/tox.ini +++ b/tox.ini @@ -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