Skip to content

Commit c4abc31

Browse files
committed
Append to .gitignore instead of overwriting it
Help ensure that the `.gitignore` file is not destroyed, if an environment is created in a directory that already exists.
1 parent 9b35f7c commit c4abc31

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

Lib/test/test_venv.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,20 @@ def test_scm_ignore_files_git(self):
787787
file_lines = self.get_text_file_contents('.gitignore').splitlines()
788788
self.assertIn('*', file_lines)
789789

790+
@requireVenvCreate
791+
def test_scm_ignore_files_git_appends_to_existing_file(self):
792+
"""Test that an existing .gitignore file is appended to."""
793+
gitignore_path = self.get_env_file('.gitignore')
794+
with open(gitignore_path, 'w', encoding='utf-8') as fp:
795+
fp.write("# Existing comment\n")
796+
797+
self.run_with_capture(venv.create, self.env_dir,
798+
scm_ignore_files={'git'})
799+
800+
file_lines = self.get_text_file_contents('.gitignore').splitlines()
801+
self.assertIn('# Existing comment', file_lines)
802+
self.assertIn('*', file_lines)
803+
790804
@requireVenvCreate
791805
def test_create_scm_ignore_files_multiple(self):
792806
"""

Lib/venv/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,8 @@ def create_git_ignore_file(self, context):
293293
ignored by git.
294294
"""
295295
gitignore_path = os.path.join(context.env_dir, '.gitignore')
296-
with open(gitignore_path, 'w', encoding='utf-8') as file:
297-
file.write('# Created by venv; '
296+
with open(gitignore_path, 'a', encoding='utf-8') as file:
297+
file.write('# Added by venv; '
298298
'see https://docs.python.org/3/library/venv.html\n')
299299
file.write('*\n')
300300

0 commit comments

Comments
 (0)