diff --git a/python_files/create_venv.py b/python_files/create_venv.py index 020c119fc1d5..fd1ff9ab1a47 100644 --- a/python_files/create_venv.py +++ b/python_files/create_venv.py @@ -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() @@ -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): diff --git a/python_files/tests/test_create_venv.py b/python_files/tests/test_create_venv.py index 2387f099140f..72fabdaaecac 100644 --- a/python_files/tests/test_create_venv.py +++ b/python_files/tests/test_create_venv.py @@ -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 @@ -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"] @@ -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):