Skip to content

Commit

Permalink
Fix issue with creating .gitignore with venvs (#24155)
Browse files Browse the repository at this point in the history
Fixes #24151
  • Loading branch information
karthiknadig authored Sep 23, 2024
1 parent 8cfd2d0 commit c314bab
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
14 changes: 11 additions & 3 deletions python_files/create_venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ def file_exists(path: Union[str, pathlib.PurePath]) -> bool:
return pathlib.Path(path).exists()


def is_file(path: Union[str, pathlib.PurePath]) -> bool:
return pathlib.Path(path).is_file()


def venv_exists(name: str) -> bool:
return (
(CWD / name).exists()
Expand Down Expand Up @@ -134,11 +138,15 @@ def upgrade_pip(venv_path: str) -> None:
print("CREATE_VENV.UPGRADED_PIP")


def create_gitignore(git_ignore: Union[str, pathlib.PurePath]):
print("Creating:", os.fspath(git_ignore))
pathlib.Path(git_ignore).write_text("*")


def add_gitignore(name: str) -> None:
git_ignore = CWD / name / ".gitignore"
if git_ignore.is_file():
print("Creating:", os.fspath(git_ignore))
git_ignore.write_text("*")
if not is_file(git_ignore):
create_gitignore(git_ignore)


def download_pip_pyz(name: str):
Expand Down
15 changes: 14 additions & 1 deletion python_files/tests/test_create_venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,14 @@ def test_venv_not_installed_windows():


@pytest.mark.parametrize("env_exists", ["hasEnv", "noEnv"])
@pytest.mark.parametrize("git_ignore", ["useGitIgnore", "skipGitIgnore"])
@pytest.mark.parametrize("git_ignore", ["useGitIgnore", "skipGitIgnore", "gitIgnoreExists"])
@pytest.mark.parametrize("install", ["requirements", "toml", "skipInstall"])
def test_create_env(env_exists, git_ignore, install):
importlib.reload(create_venv)
create_venv.is_installed = lambda _x: True
create_venv.venv_exists = lambda _n: env_exists == "hasEnv"
create_venv.upgrade_pip = lambda _x: None
create_venv.is_file = lambda _x: git_ignore == "gitIgnoreExists"

install_packages_called = False

Expand All @@ -84,9 +85,19 @@ def run_process(args, error_message):
def add_gitignore(_name):
nonlocal add_gitignore_called
add_gitignore_called = True
if not create_venv.is_file(_name):
create_venv.create_gitignore(_name)

create_venv.add_gitignore = add_gitignore

create_gitignore_called = False

def create_gitignore(_p):
nonlocal create_gitignore_called
create_gitignore_called = True

create_venv.create_gitignore = create_gitignore

args = []
if git_ignore == "useGitIgnore":
args += ["--git-ignore"]
Expand All @@ -104,6 +115,8 @@ def add_gitignore(_name):
# add_gitignore is called when new venv is created and git_ignore is True
assert add_gitignore_called == ((env_exists == "noEnv") and (git_ignore == "useGitIgnore"))

assert create_gitignore_called == (add_gitignore_called and (git_ignore != "gitIgnoreExists"))


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

0 comments on commit c314bab

Please sign in to comment.