summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2021-11-12 19:06:14 -0500
committerNed Batchelder <ned@nedbatchelder.com>2021-11-12 19:06:14 -0500
commitb471e55a92718fa4711c040edc88308d5101fc86 (patch)
treeb85b5eef38b5a9034fa67bf6efbc54ad7aaee279
parentbf42d44403351cd35d67af5bc1bb9b284ad1a08b (diff)
downloadpython-coveragepy-git-nedbat/1244-again.tar.gz
fix: don't overwrite a .gitignore in the html output directory. #1244nedbat/1244-again
-rw-r--r--CHANGES.rst3
-rw-r--r--coverage/html.py6
-rw-r--r--tests/test_html.py7
3 files changed, 14 insertions, 2 deletions
diff --git a/CHANGES.rst b/CHANGES.rst
index 0b6e2490..5aaa0c4a 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -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:
diff --git a/coverage/html.py b/coverage/html.py
index 52689050..d018d0f0 100644
--- a/coverage/html.py
+++ b/coverage/html.py
@@ -226,8 +226,10 @@ class HtmlReporter:
# .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:
diff --git a/tests/test_html.py b/tests/test_html.py
index 51062f73..c8f9c1e6 100644
--- a/tests/test_html.py
+++ b/tests/test_html.py
@@ -284,6 +284,13 @@ class HtmlDeltaTest(HtmlTestHelpers, CoverageTest):
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."""