1010import pkg_resources
1111import random
1212import re
13- import hashlib
1413
1514import pytest
1615
@@ -468,13 +467,8 @@ def pytest_runtest_makereport(item, call):
468467 )
469468 testdir .makepyfile ("def test_pass(): pass" )
470469 result , html = run (testdir )
471- hash_key = "test_extra_text_separated.py::" "test_pass00"
472- hash_generator = hashlib .md5 ()
473- hash_generator .update (hash_key .encode ("utf-8" ))
474470 assert result .ret == 0
475- src = "{0}/{1}" .format (
476- "assets" , "{0}_{1}.txt" .format (hash_key , hash_generator .hexdigest ())
477- )
471+ src = "assets/test_extra_text_separated.py__test_pass_0_0.txt"
478472 link = '<a class="text" href="{0}" target="_blank">' .format (src )
479473 assert link in html
480474 assert os .path .exists (src )
@@ -501,13 +495,9 @@ def pytest_runtest_makereport(item, call):
501495 )
502496 testdir .makepyfile ("def test_pass(): pass" )
503497 result , html = run (testdir )
504- hash_key = "test_extra_image_separated.py::test_pass00"
505- hash_generator = hashlib .md5 ()
506- hash_generator .update (hash_key .encode ("utf-8" ))
507498 assert result .ret == 0
508- src = "{0}/{1}" .format (
509- "assets" ,
510- "{0}_{1}.{2}" .format (hash_key , hash_generator .hexdigest (), file_extension ),
499+ src = "assets/test_extra_image_separated.py__test_pass_0_0.{}" .format (
500+ file_extension
511501 )
512502 link = '<a class="image" href="{0}" target="_blank">' .format (src )
513503 assert link in html
@@ -543,12 +533,8 @@ def test_fail():
543533 result , html = run (testdir )
544534
545535 for i in range (1 , 4 ):
546- hash_key = "test_extra_image_separated_rerun.py::" "test_fail0{0}" .format (i )
547- hash_generator = hashlib .md5 ()
548- hash_generator .update (hash_key .encode ("utf-8" ))
549- src = "assets/{0}_{1}.{2}" .format (
550- hash_key , hash_generator .hexdigest (), file_extension
551- )
536+ asset_name = "test_extra_image_separated_rerun.py__test_fail"
537+ src = "assets/{}_0_{}.{}" .format (asset_name , i , file_extension )
552538 link = '<a class="image" href="{0}" target="_blank">' .format (src )
553539 assert result .ret
554540 assert link in html
@@ -602,16 +588,36 @@ def {0}():
602588 )
603589 )
604590 result , html = run (testdir )
605-
606- hash_key = "test_very_long_test_name.py::{}00" .format (test_name )
607- hash_generator = hashlib .md5 ()
608- hash_generator .update (hash_key .encode ("utf-8" ))
609- src = "assets/{0}_{1}.png" .format (hash_key [:218 ], hash_generator .hexdigest ())
591+ file_name = "test_very_long_test_name.py__{}_0_0.png" .format (test_name )[- 255 :]
592+ src = "assets/" + file_name
610593 link = '<a class="image" href="{0}" target="_blank">' .format (src )
611594 assert result .ret
612595 assert link in html
613596 assert os .path .exists (src )
614597
598+ def test_no_invalid_characters_in_filename (self , testdir ):
599+ testdir .makeconftest (
600+ """
601+ import pytest
602+ @pytest.hookimpl(hookwrapper=True)
603+ def pytest_runtest_makereport(item, call):
604+ outcome = yield
605+ report = outcome.get_result()
606+ if report.when == 'call':
607+ from pytest_html import extras
608+ report.extra = [extras.image('image.png')]
609+ """
610+ )
611+ testdir .makepyfile (
612+ """
613+ def test_fail():
614+ assert False
615+ """
616+ )
617+ run (testdir )
618+ for filename in os .listdir ("assets" ):
619+ assert re .search (r'[:\\<>\*\?\|"}{}~]' , filename ) is None
620+
615621 def test_no_environment (self , testdir ):
616622 testdir .makeconftest (
617623 """
0 commit comments