From 623efc6198f6d67f38e5338975032edde1178cb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Bielawski?= Date: Wed, 19 Jun 2019 11:11:43 +0100 Subject: [PATCH] Truncate long test names in assets. --- pytest_html/plugin.py | 9 +++++++-- testing/test_pytest_html.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/pytest_html/plugin.py b/pytest_html/plugin.py index 744745a8..3903c1cf 100644 --- a/pytest_html/plugin.py +++ b/pytest_html/plugin.py @@ -150,8 +150,13 @@ def create_asset(self, content, extra_index, str(test_index)]) hash_generator = hashlib.md5() hash_generator.update(hash_key.encode('utf-8')) - asset_file_name = '{0}_{1}.{2}'.format(hash_key, - hash_generator.hexdigest(), + hex_digest = hash_generator.hexdigest() + # 255 is the common max filename length on various filesystems, + # we subtract hash length, file extension length and 2 more + # characters for the underscore and dot + max_length = 255 - len(hex_digest) - len(file_extension) - 2 + asset_file_name = '{0}_{1}.{2}'.format(hash_key[:max_length], + hex_digest, file_extension) 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 eedd44d8..32fc8f6d 100644 --- a/testing/test_pytest_html.py +++ b/testing/test_pytest_html.py @@ -503,6 +503,35 @@ def pytest_runtest_makereport(item, call): assert result.ret == 0 assert ''.format(content) in html + def test_very_long_test_name(self, testdir): + testdir.makeconftest(""" + import pytest + @pytest.mark.hookwrapper + def pytest_runtest_makereport(item, call): + outcome = yield + report = outcome.get_result() + if report.when == 'call': + from pytest_html import extras + report.extra = [extras.image('image.png')] + """) + # This will get truncated + test_name = 'test_{}'.format('a' * 300) + testdir.makepyfile(""" + def {0}(): + assert False + """.format(test_name)) + result, html = run(testdir) + + hash_key = 'test_very_long_test_name.py::{}00'.format(test_name) + hash_generator = hashlib.md5() + hash_generator.update(hash_key.encode('utf-8')) + src = 'assets/{0}_{1}.png'.format(hash_key[:218], + hash_generator.hexdigest()) + link = (''.format(src)) + assert result.ret + assert link in html + assert os.path.exists(src) + def test_no_environment(self, testdir): testdir.makeconftest(""" def pytest_configure(config):