From d0946409d7e5329948f2fb3378d84e0cf70365a8 Mon Sep 17 00:00:00 2001 From: Karthik Nadig Date: Mon, 23 Sep 2024 11:04:55 -0700 Subject: [PATCH 1/3] Fix bug with create venv --- python_files/create_venv.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python_files/create_venv.py b/python_files/create_venv.py index 020c119fc1d5..07c8936c9616 100644 --- a/python_files/create_venv.py +++ b/python_files/create_venv.py @@ -136,7 +136,7 @@ def upgrade_pip(venv_path: str) -> None: def add_gitignore(name: str) -> None: git_ignore = CWD / name / ".gitignore" - if git_ignore.is_file(): + if not git_ignore.is_file(): print("Creating:", os.fspath(git_ignore)) git_ignore.write_text("*") From 9d22442c532f3d3f27a163aac13054be260b225d Mon Sep 17 00:00:00 2001 From: Karthik Nadig Date: Mon, 23 Sep 2024 11:36:35 -0700 Subject: [PATCH 2/3] Add tests --- python_files/create_venv.py | 14 ++++++++--- python_files/tests/test_create_venv.py | 35 ++++++++++++++++++++++---- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/python_files/create_venv.py b/python_files/create_venv.py index 07c8936c9616..3915a5b4ff0f 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)) + git_ignore.write_text("*") + + def add_gitignore(name: str) -> None: git_ignore = CWD / name / ".gitignore" - if not 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..761707cd8ca7 100644 --- a/python_files/tests/test_create_venv.py +++ b/python_files/tests/test_create_venv.py @@ -14,7 +14,9 @@ import create_venv -@pytest.mark.skipif(sys.platform == "win32", reason="Windows does not have micro venv fallback.") +@pytest.mark.skipif( + sys.platform == "win32", reason="Windows does not have micro venv fallback." +) def test_venv_not_installed_unix(): importlib.reload(create_venv) create_venv.is_installed = lambda module: module != "venv" @@ -41,7 +43,9 @@ def run_process(args, error_message): assert run_process_called is True -@pytest.mark.skipif(sys.platform != "win32", reason="Windows does not have microvenv fallback.") +@pytest.mark.skipif( + sys.platform != "win32", reason="Windows does not have microvenv fallback." +) def test_venv_not_installed_windows(): importlib.reload(create_venv) create_venv.is_installed = lambda module: module != "venv" @@ -51,13 +55,16 @@ 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 +91,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"] @@ -102,7 +119,13 @@ def add_gitignore(_name): assert run_process_called == (env_exists == "noEnv") # 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 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"]) @@ -232,7 +255,9 @@ def run_process(args, error_message): if "install" in args and "pip" in args: nonlocal run_process_called run_process_called = True - pip_pyz_path = os.fspath(create_venv.CWD / create_venv.VENV_NAME / "pip.pyz") + pip_pyz_path = os.fspath( + create_venv.CWD / create_venv.VENV_NAME / "pip.pyz" + ) assert args[1:] == [pip_pyz_path, "install", "pip"] assert error_message == "CREATE_VENV.INSTALL_PIP_FAILED" From 6443b4734c808b60d1d3d9f660f05086b12bb101 Mon Sep 17 00:00:00 2001 From: Karthik Nadig Date: Mon, 23 Sep 2024 11:51:07 -0700 Subject: [PATCH 3/3] Fix linting and types --- python_files/create_venv.py | 2 +- python_files/tests/test_create_venv.py | 24 ++++++------------------ 2 files changed, 7 insertions(+), 19 deletions(-) diff --git a/python_files/create_venv.py b/python_files/create_venv.py index 3915a5b4ff0f..fd1ff9ab1a47 100644 --- a/python_files/create_venv.py +++ b/python_files/create_venv.py @@ -140,7 +140,7 @@ def upgrade_pip(venv_path: str) -> None: def create_gitignore(git_ignore: Union[str, pathlib.PurePath]): print("Creating:", os.fspath(git_ignore)) - git_ignore.write_text("*") + pathlib.Path(git_ignore).write_text("*") def add_gitignore(name: str) -> None: diff --git a/python_files/tests/test_create_venv.py b/python_files/tests/test_create_venv.py index 761707cd8ca7..72fabdaaecac 100644 --- a/python_files/tests/test_create_venv.py +++ b/python_files/tests/test_create_venv.py @@ -14,9 +14,7 @@ import create_venv -@pytest.mark.skipif( - sys.platform == "win32", reason="Windows does not have micro venv fallback." -) +@pytest.mark.skipif(sys.platform == "win32", reason="Windows does not have micro venv fallback.") def test_venv_not_installed_unix(): importlib.reload(create_venv) create_venv.is_installed = lambda module: module != "venv" @@ -43,9 +41,7 @@ def run_process(args, error_message): assert run_process_called is True -@pytest.mark.skipif( - sys.platform != "win32", reason="Windows does not have microvenv fallback." -) +@pytest.mark.skipif(sys.platform != "win32", reason="Windows does not have microvenv fallback.") def test_venv_not_installed_windows(): importlib.reload(create_venv) create_venv.is_installed = lambda module: module != "venv" @@ -55,9 +51,7 @@ def test_venv_not_installed_windows(): @pytest.mark.parametrize("env_exists", ["hasEnv", "noEnv"]) -@pytest.mark.parametrize( - "git_ignore", ["useGitIgnore", "skipGitIgnore", "gitIgnoreExists"] -) +@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) @@ -119,13 +113,9 @@ def create_gitignore(_p): assert run_process_called == (env_exists == "noEnv") # 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 add_gitignore_called == ((env_exists == "noEnv") and (git_ignore == "useGitIgnore")) - assert create_gitignore_called == ( - add_gitignore_called and (git_ignore != "gitIgnoreExists") - ) + assert create_gitignore_called == (add_gitignore_called and (git_ignore != "gitIgnoreExists")) @pytest.mark.parametrize("install_type", ["requirements", "pyproject", "both"]) @@ -255,9 +245,7 @@ def run_process(args, error_message): if "install" in args and "pip" in args: nonlocal run_process_called run_process_called = True - pip_pyz_path = os.fspath( - create_venv.CWD / create_venv.VENV_NAME / "pip.pyz" - ) + pip_pyz_path = os.fspath(create_venv.CWD / create_venv.VENV_NAME / "pip.pyz") assert args[1:] == [pip_pyz_path, "install", "pip"] assert error_message == "CREATE_VENV.INSTALL_PIP_FAILED"