Skip to content

Commit

Permalink
fix: generate flat file names differently
Browse files Browse the repository at this point in the history
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
  • Loading branch information
nedbat committed Jul 14, 2021
1 parent 0ff5a1c commit df134b2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
13 changes: 11 additions & 2 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,20 @@ Unreleased

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

- Use a modern hash algorithm when fingerprinting to speed HTML reports
(`issue 1189`_).
- Some minor changes to usually invisible details of the HTML report:

- Use a modern hash algorithm when fingerprinting, for high-security
environments (`issue 1189`_).

- Change how report file names are generated, to avoid leading underscores
(`issue 1167`_), to avoid rare file name collisions (`issue 584`_), and to
avoid file names becoming too long (`issue 580`_).

.. _Django coverage plugin: https://pypi.org/project/django-coverage-plugin/
.. _issue 580: https://github.com/nedbat/coveragepy/issues/580
.. _issue 584: https://github.com/nedbat/coveragepy/issues/584
.. _issue 1150: https://github.com/nedbat/coveragepy/issues/1150
.. _issue 1167: https://github.com/nedbat/coveragepy/issues/1167
.. _issue 1168: https://github.com/nedbat/coveragepy/issues/1168
.. _issue 1189: https://github.com/nedbat/coveragepy/issues/1189
.. _tomli: https://pypi.org/project/tomli/
Expand Down
10 changes: 4 additions & 6 deletions coverage/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def canonical_filename(filename):
return CANONICAL_FILENAME_CACHE[filename]


MAX_FLAT = 200
MAX_FLAT = 100

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


Expand Down
22 changes: 12 additions & 10 deletions tests/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,20 @@ def test_canonical_filename_ensure_cache_hit(self):


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

0 comments on commit df134b2

Please sign in to comment.