Skip to content

Commit c314bab

Browse files
authored
Fix issue with creating .gitignore with venvs (#24155)
Fixes #24151
1 parent 8cfd2d0 commit c314bab

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

python_files/create_venv.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ def file_exists(path: Union[str, pathlib.PurePath]) -> bool:
7878
return pathlib.Path(path).exists()
7979

8080

81+
def is_file(path: Union[str, pathlib.PurePath]) -> bool:
82+
return pathlib.Path(path).is_file()
83+
84+
8185
def venv_exists(name: str) -> bool:
8286
return (
8387
(CWD / name).exists()
@@ -134,11 +138,15 @@ def upgrade_pip(venv_path: str) -> None:
134138
print("CREATE_VENV.UPGRADED_PIP")
135139

136140

141+
def create_gitignore(git_ignore: Union[str, pathlib.PurePath]):
142+
print("Creating:", os.fspath(git_ignore))
143+
pathlib.Path(git_ignore).write_text("*")
144+
145+
137146
def add_gitignore(name: str) -> None:
138147
git_ignore = CWD / name / ".gitignore"
139-
if git_ignore.is_file():
140-
print("Creating:", os.fspath(git_ignore))
141-
git_ignore.write_text("*")
148+
if not is_file(git_ignore):
149+
create_gitignore(git_ignore)
142150

143151

144152
def download_pip_pyz(name: str):

python_files/tests/test_create_venv.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,14 @@ def test_venv_not_installed_windows():
5151

5252

5353
@pytest.mark.parametrize("env_exists", ["hasEnv", "noEnv"])
54-
@pytest.mark.parametrize("git_ignore", ["useGitIgnore", "skipGitIgnore"])
54+
@pytest.mark.parametrize("git_ignore", ["useGitIgnore", "skipGitIgnore", "gitIgnoreExists"])
5555
@pytest.mark.parametrize("install", ["requirements", "toml", "skipInstall"])
5656
def test_create_env(env_exists, git_ignore, install):
5757
importlib.reload(create_venv)
5858
create_venv.is_installed = lambda _x: True
5959
create_venv.venv_exists = lambda _n: env_exists == "hasEnv"
6060
create_venv.upgrade_pip = lambda _x: None
61+
create_venv.is_file = lambda _x: git_ignore == "gitIgnoreExists"
6162

6263
install_packages_called = False
6364

@@ -84,9 +85,19 @@ def run_process(args, error_message):
8485
def add_gitignore(_name):
8586
nonlocal add_gitignore_called
8687
add_gitignore_called = True
88+
if not create_venv.is_file(_name):
89+
create_venv.create_gitignore(_name)
8790

8891
create_venv.add_gitignore = add_gitignore
8992

93+
create_gitignore_called = False
94+
95+
def create_gitignore(_p):
96+
nonlocal create_gitignore_called
97+
create_gitignore_called = True
98+
99+
create_venv.create_gitignore = create_gitignore
100+
90101
args = []
91102
if git_ignore == "useGitIgnore":
92103
args += ["--git-ignore"]
@@ -104,6 +115,8 @@ def add_gitignore(_name):
104115
# add_gitignore is called when new venv is created and git_ignore is True
105116
assert add_gitignore_called == ((env_exists == "noEnv") and (git_ignore == "useGitIgnore"))
106117

118+
assert create_gitignore_called == (add_gitignore_called and (git_ignore != "gitIgnoreExists"))
119+
107120

108121
@pytest.mark.parametrize("install_type", ["requirements", "pyproject", "both"])
109122
def test_install_packages(install_type):

0 commit comments

Comments
 (0)