Skip to content

Commit b471e55

Browse files
committed
fix: don't overwrite a .gitignore in the html output directory. #1244
1 parent bf42d44 commit b471e55

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ Unreleased
2525
- Fix: A colon in a decorator expression would cause an exclusion to end too
2626
early, preventing the exclusion of the decorated function. This is now fixed.
2727

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

2932
.. _changes_612:
3033

coverage/html.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,10 @@ def make_local_static_report_files(self):
226226

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

232234
# The user may have extra CSS they want copied.
233235
if self.extra_css:

tests/test_html.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,13 @@ def test_status_format_change(self):
284284
assert "htmlcov/helper2_py.html" in self.files_written
285285
assert "htmlcov/main_file_py.html" in self.files_written
286286

287+
def test_dont_overwrite_gitignore(self):
288+
self.create_initial_files()
289+
self.make_file("htmlcov/.gitignore", "# ignore nothing")
290+
self.run_coverage()
291+
with open("htmlcov/.gitignore") as fgi:
292+
assert fgi.read() == "# ignore nothing"
293+
287294

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

0 commit comments

Comments
 (0)