diff --git a/CHANGES.rst b/CHANGES.rst index 0b6e24908..5aaa0c4a1 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 526890504..d018d0f01 100644 --- a/coverage/html.py +++ b/coverage/html.py @@ -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: diff --git a/tests/test_html.py b/tests/test_html.py index 51062f732..c8f9c1e61 100644 --- a/tests/test_html.py +++ b/tests/test_html.py @@ -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."""