Skip to content

Commit

Permalink
fix: don't overwrite a .gitignore in the html output directory. #1244
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Nov 13, 2021
1 parent bf42d44 commit b471e55
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ Unreleased
- Fix: A colon in a decorator expression would cause an exclusion to end too
early, preventing the exclusion of the decorated function. This is now fixed.

- Fix: The HTML report now will not overwrite a .gitignore file that already
exists in the HTML output directory (follow-on for `issue 1244`_).


.. _changes_612:

Expand Down
6 changes: 4 additions & 2 deletions coverage/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,10 @@ def make_local_static_report_files(self):

# .gitignore can't be copied from the source tree because it would
# prevent the static files from being checked in.
with open(os.path.join(self.directory, ".gitignore"), "w") as fgi:
fgi.write("# Created by coverage.py\n*\n")
gitigore_path = os.path.join(self.directory, ".gitignore")
if not os.path.exists(gitigore_path):
with open(gitigore_path, "w") as fgi:
fgi.write("# Created by coverage.py\n*\n")

# The user may have extra CSS they want copied.
if self.extra_css:
Expand Down
7 changes: 7 additions & 0 deletions tests/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,13 @@ def test_status_format_change(self):
assert "htmlcov/helper2_py.html" in self.files_written
assert "htmlcov/main_file_py.html" in self.files_written

def test_dont_overwrite_gitignore(self):
self.create_initial_files()
self.make_file("htmlcov/.gitignore", "# ignore nothing")
self.run_coverage()
with open("htmlcov/.gitignore") as fgi:
assert fgi.read() == "# ignore nothing"


class HtmlTitleTest(HtmlTestHelpers, CoverageTest):
"""Tests of the HTML title support."""
Expand Down

0 comments on commit b471e55

Please sign in to comment.