Skip to content

Commit df134b2

Browse files
committed
fix: generate flat file names differently
Fixes a few unusual issues with reports: - #580: HTML report generation fails on too long path - #584: File collisions in coverage report html - #1167: Remove leading underscore in coverage html
1 parent 0ff5a1c commit df134b2

File tree

3 files changed

+27
-18
lines changed

3 files changed

+27
-18
lines changed

CHANGES.rst

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,20 @@ Unreleased
4646

4747
- TOML parsing now uses the `tomli`_ library.
4848

49-
- Use a modern hash algorithm when fingerprinting to speed HTML reports
50-
(`issue 1189`_).
49+
- Some minor changes to usually invisible details of the HTML report:
50+
51+
- Use a modern hash algorithm when fingerprinting, for high-security
52+
environments (`issue 1189`_).
53+
54+
- Change how report file names are generated, to avoid leading underscores
55+
(`issue 1167`_), to avoid rare file name collisions (`issue 584`_), and to
56+
avoid file names becoming too long (`issue 580`_).
5157

5258
.. _Django coverage plugin: https://pypi.org/project/django-coverage-plugin/
59+
.. _issue 580: https://github.com/nedbat/coveragepy/issues/580
60+
.. _issue 584: https://github.com/nedbat/coveragepy/issues/584
5361
.. _issue 1150: https://github.com/nedbat/coveragepy/issues/1150
62+
.. _issue 1167: https://github.com/nedbat/coveragepy/issues/1167
5463
.. _issue 1168: https://github.com/nedbat/coveragepy/issues/1168
5564
.. _issue 1189: https://github.com/nedbat/coveragepy/issues/1189
5665
.. _tomli: https://pypi.org/project/tomli/

coverage/files.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def canonical_filename(filename):
7777
return CANONICAL_FILENAME_CACHE[filename]
7878

7979

80-
MAX_FLAT = 200
80+
MAX_FLAT = 100
8181

8282
@contract(filename='unicode', returns='unicode')
8383
def flat_rootname(filename):
@@ -90,11 +90,9 @@ def flat_rootname(filename):
9090
For example, the file a/b/c.py will return 'a_b_c_py'
9191
9292
"""
93-
name = ntpath.splitdrive(filename)[1]
94-
name = re.sub(r"[\\/.:]", "_", name)
95-
if len(name) > MAX_FLAT:
96-
h = hashlib.sha1(name.encode('UTF-8')).hexdigest()
97-
name = name[-(MAX_FLAT-len(h)-1):] + '_' + h
93+
hash = hashlib.new("sha3_256", filename.encode("UTF-8")).hexdigest()
94+
name = re.sub(r"[\\/.:]", "_", filename)
95+
name = hash[:16] + "_" + name[-(MAX_FLAT-17):]
9896
return name
9997

10098

tests/test_files.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,20 @@ def test_canonical_filename_ensure_cache_hit(self):
6969

7070

7171
@pytest.mark.parametrize("original, flat", [
72-
("a/b/c.py", "a_b_c_py"),
73-
(r"c:\foo\bar.html", "_foo_bar_html"),
74-
("Montréal/☺/conf.py", "Montréal_☺_conf_py"),
72+
("a/b/c.py", "9740bf4b0a33df83_a_b_c_py"),
73+
("/a/b/c.py", "055993932a538df4__a_b_c_py"),
74+
(r"c:\foo\bar.html", "e402c17adf14f7d1_c__foo_bar_html"),
75+
(r"d:\foo\bar.html", "85b26652b26cac26_d__foo_bar_html"),
76+
("Montréal/☺/conf.py", "1c7e19adc3b6fe6b_Montréal_☺_conf_py"),
7577
( # original:
76-
r"c:\lorem\ipsum\quia\dolor\sit\amet\consectetur\adipisci\velit\sed\quia\non"
77-
r"\numquam\eius\modi\tempora\incidunt\ut\labore\et\dolore\magnam\aliquam"
78-
r"\quaerat\voluptatem\ut\enim\ad\minima\veniam\quis\nostrum\exercitationem"
79-
r"\ullam\corporis\suscipit\laboriosam\Montréal\☺\my_program.py",
78+
r"c:\lorem\ipsum\quia\dolor\sit\amet\consectetur\adipisci\velit\sed" +
79+
r"\quia\non\numquam\eius\modi\tempora\incidunt\ut\labore\et\dolore" +
80+
r"\magnam\aliquam\quaerat\voluptatem\ut\enim\ad\minima\veniam\quis" +
81+
r"\nostrum\exercitationem\ullam\corporis\suscipit\laboriosam" +
82+
r"\Montréal\☺\my_program.py",
8083
# flat:
81-
"re_et_dolore_magnam_aliquam_quaerat_voluptatem_ut_enim_ad_minima_veniam_quis_"
82-
"nostrum_exercitationem_ullam_corporis_suscipit_laboriosam_Montréal_☺_my_program_py_"
83-
"97eaca41b860faaa1a21349b1f3009bb061cf0a8"
84+
"0e99262918baa54c__nostrum_exercitationem_ullam_corporis_suscipit_" +
85+
"laboriosam_Montréal_☺_my_program_py"
8486
),
8587
])
8688
def test_flat_rootname(original, flat):

0 commit comments

Comments
 (0)