Skip to content
Merged
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
9 changes: 7 additions & 2 deletions pytest_html/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
29 changes: 29 additions & 0 deletions testing/test_pytest_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,35 @@ def pytest_runtest_makereport(item, call):
assert result.ret == 0
assert '<a href="{0}"><img src="{0}"/>'.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 = ('<a class="image" href="{0}" target="_blank">'.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):
Expand Down