Skip to content

Commit e5125d5

Browse files
authored
Merge pull request #210 from D3X/truncate-long-test-names
Truncate long test names in assets.
2 parents 6a41052 + 623efc6 commit e5125d5

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

pytest_html/plugin.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,13 @@ def create_asset(self, content, extra_index,
150150
str(test_index)])
151151
hash_generator = hashlib.md5()
152152
hash_generator.update(hash_key.encode('utf-8'))
153-
asset_file_name = '{0}_{1}.{2}'.format(hash_key,
154-
hash_generator.hexdigest(),
153+
hex_digest = hash_generator.hexdigest()
154+
# 255 is the common max filename length on various filesystems,
155+
# we subtract hash length, file extension length and 2 more
156+
# characters for the underscore and dot
157+
max_length = 255 - len(hex_digest) - len(file_extension) - 2
158+
asset_file_name = '{0}_{1}.{2}'.format(hash_key[:max_length],
159+
hex_digest,
155160
file_extension)
156161
asset_path = os.path.join(os.path.dirname(self.logfile),
157162
'assets', asset_file_name)

testing/test_pytest_html.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,35 @@ def pytest_runtest_makereport(item, call):
503503
assert result.ret == 0
504504
assert '<a href="{0}"><img src="{0}"/>'.format(content) in html
505505

506+
def test_very_long_test_name(self, testdir):
507+
testdir.makeconftest("""
508+
import pytest
509+
@pytest.mark.hookwrapper
510+
def pytest_runtest_makereport(item, call):
511+
outcome = yield
512+
report = outcome.get_result()
513+
if report.when == 'call':
514+
from pytest_html import extras
515+
report.extra = [extras.image('image.png')]
516+
""")
517+
# This will get truncated
518+
test_name = 'test_{}'.format('a' * 300)
519+
testdir.makepyfile("""
520+
def {0}():
521+
assert False
522+
""".format(test_name))
523+
result, html = run(testdir)
524+
525+
hash_key = 'test_very_long_test_name.py::{}00'.format(test_name)
526+
hash_generator = hashlib.md5()
527+
hash_generator.update(hash_key.encode('utf-8'))
528+
src = 'assets/{0}_{1}.png'.format(hash_key[:218],
529+
hash_generator.hexdigest())
530+
link = ('<a class="image" href="{0}" target="_blank">'.format(src))
531+
assert result.ret
532+
assert link in html
533+
assert os.path.exists(src)
534+
506535
def test_no_environment(self, testdir):
507536
testdir.makeconftest("""
508537
def pytest_configure(config):

0 commit comments

Comments
 (0)