10
10
import pkg_resources
11
11
import random
12
12
import re
13
- import hashlib
14
13
15
14
import pytest
16
15
@@ -468,13 +467,8 @@ def pytest_runtest_makereport(item, call):
468
467
)
469
468
testdir .makepyfile ("def test_pass(): pass" )
470
469
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" ))
474
470
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"
478
472
link = '<a class="text" href="{0}" target="_blank">' .format (src )
479
473
assert link in html
480
474
assert os .path .exists (src )
@@ -501,13 +495,9 @@ def pytest_runtest_makereport(item, call):
501
495
)
502
496
testdir .makepyfile ("def test_pass(): pass" )
503
497
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" ))
507
498
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
511
501
)
512
502
link = '<a class="image" href="{0}" target="_blank">' .format (src )
513
503
assert link in html
@@ -543,12 +533,8 @@ def test_fail():
543
533
result , html = run (testdir )
544
534
545
535
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 )
552
538
link = '<a class="image" href="{0}" target="_blank">' .format (src )
553
539
assert result .ret
554
540
assert link in html
@@ -602,16 +588,36 @@ def {0}():
602
588
)
603
589
)
604
590
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
610
593
link = '<a class="image" href="{0}" target="_blank">' .format (src )
611
594
assert result .ret
612
595
assert link in html
613
596
assert os .path .exists (src )
614
597
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
+
615
621
def test_no_environment (self , testdir ):
616
622
testdir .makeconftest (
617
623
"""
0 commit comments