summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2022-01-11 18:57:52 -0500
committerNed Batchelder <ned@nedbatchelder.com>2022-01-11 16:20:58 -0800
commit6cbbc90dcd89d8499191a75133a6e2b29f52270a (patch)
tree9e26d0cdc30120d5a3031cf3a6e7b1326c268453
parentd004b18a1ad59ec89b89c96c03a789a55cc51693 (diff)
downloadpython-coveragepy-git-6cbbc90dcd89d8499191a75133a6e2b29f52270a.tar.gz
fix: don't write a .gitignore unless the directory is empty
-rw-r--r--CHANGES.rst4
-rw-r--r--coverage/html.py9
-rw-r--r--tests/test_html.py7
3 files changed, 17 insertions, 3 deletions
diff --git a/CHANGES.rst b/CHANGES.rst
index 8ac4997b..670f3363 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -24,6 +24,10 @@ Unreleased
- Dropped support for Python 3.6, which ended support on 2021-12-23.
+- Fix: a .gitignore file will only be written into the HTML report output
+ directory if the directory is empty. This should prevent certain unfortunate
+ accidents of writing the file where it is not wanted.
+
- Releases now have MacOS arm64 wheels for Apple Silicon (fixes `issue 1288`_).
.. _issue 1288: https://github.com/nedbat/coveragepy/issues/1288
diff --git a/coverage/html.py b/coverage/html.py
index ae1d041f..342d2ad1 100644
--- a/coverage/html.py
+++ b/coverage/html.py
@@ -164,6 +164,7 @@ class HtmlReporter:
self.incr = IncrementalChecker(self.directory)
self.datagen = HtmlDataGeneration(self.coverage)
self.totals = Numbers(precision=self.config.precision)
+ self.directory_was_empty = False
self.template_globals = {
# Functions available in the templates.
@@ -224,11 +225,11 @@ class HtmlReporter:
for static in self.STATIC_FILES:
shutil.copyfile(data_filename(static), os.path.join(self.directory, static))
+ # Only write the .gitignore file if the directory was originally empty.
# .gitignore can't be copied from the source tree because it would
# prevent the static files from being checked in.
- gitigore_path = os.path.join(self.directory, ".gitignore")
- if not os.path.exists(gitigore_path):
- with open(gitigore_path, "w") as fgi:
+ if self.directory_was_empty:
+ with open(os.path.join(self.directory, ".gitignore"), "w") as fgi:
fgi.write("# Created by coverage.py\n*\n")
# The user may have extra CSS they want copied.
@@ -240,6 +241,8 @@ class HtmlReporter:
rootname = flat_rootname(fr.relative_filename())
html_filename = rootname + ".html"
ensure_dir(self.directory)
+ if not os.listdir(self.directory):
+ self.directory_was_empty = True
html_path = os.path.join(self.directory, html_filename)
# Get the numbers for this file.
diff --git a/tests/test_html.py b/tests/test_html.py
index cb222270..797fee1d 100644
--- a/tests/test_html.py
+++ b/tests/test_html.py
@@ -291,6 +291,13 @@ class HtmlDeltaTest(HtmlTestHelpers, CoverageTest):
with open("htmlcov/.gitignore") as fgi:
assert fgi.read() == "# ignore nothing"
+ def test_dont_write_gitignore_into_existing_directory(self):
+ self.create_initial_files()
+ self.make_file("htmlcov/README", "My files: don't touch!")
+ self.run_coverage()
+ self.assert_doesnt_exist("htmlcov/.gitignore")
+ self.assert_exists("htmlcov/index.html")
+
class HtmlTitleTest(HtmlTestHelpers, CoverageTest):
"""Tests of the HTML title support."""