From 6cbbc90dcd89d8499191a75133a6e2b29f52270a Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Tue, 11 Jan 2022 18:57:52 -0500 Subject: fix: don't write a .gitignore unless the directory is empty --- coverage/html.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'coverage/html.py') 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. -- cgit v1.2.1