From b636c5328c77a7ae3f83caeecb7e8e1f194ee306 Mon Sep 17 00:00:00 2001 From: meowmeow Date: Sun, 22 Jan 2023 12:21:17 +0000 Subject: [PATCH 1/7] Testing: add git to PATH --- tests/conftest.py | 30 ++++++++++++++++++++++++------ tests/test_install.py | 3 +-- tests/test_package_specifier.py | 1 - 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index cc32319a3c..bb79373471 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,4 +1,5 @@ import os +import shutil import subprocess import sys from pathlib import Path @@ -40,7 +41,9 @@ def pytest_configure(config): config.option.markexpr = new_markexpr -def pipx_temp_env_helper(pipx_shared_dir, tmp_path, monkeypatch, request): +def pipx_temp_env_helper( + pipx_shared_dir, tmp_path, monkeypatch, request, utils_temp_dir +): home_dir = Path(tmp_path) / "subdir" / "pipxhome" bin_dir = Path(tmp_path) / "otherdir" / "pipxbindir" @@ -59,7 +62,7 @@ def pipx_temp_env_helper(pipx_shared_dir, tmp_path, monkeypatch, request): # which make tests fail (e.g. on Github ansible apps exist in /usr/bin) monkeypatch.setenv("PATH_ORIG", str(bin_dir) + os.pathsep + os.getenv("PATH")) monkeypatch.setenv("PATH_TEST", str(bin_dir)) - monkeypatch.setenv("PATH", str(bin_dir)) + monkeypatch.setenv("PATH", str(bin_dir) + os.pathsep + str(utils_temp_dir)) # On Windows, monkeypatch pipx.commands.common._can_symlink_cache to # indicate that constants.LOCAL_BIN_DIR cannot use symlinks, even if # we're running as administrator and symlinks are actually possible. @@ -134,8 +137,21 @@ def pipx_session_shared_dir(tmp_path_factory): return tmp_path_factory.mktemp("session_shareddir") +@pytest.fixture(scope="session") +def utils_temp_dir(tmp_path_factory): + git_path = shutil.which("git") + tmp_path = tmp_path_factory.mktemp("session_utilstempdir") + try: + Path(tmp_path / "git").symlink_to(Path(git_path)) + except FileExistsError: + pass + return tmp_path + + @pytest.fixture -def pipx_temp_env(tmp_path, monkeypatch, pipx_session_shared_dir, request): +def pipx_temp_env( + tmp_path, monkeypatch, pipx_session_shared_dir, request, utils_temp_dir +): """Sets up temporary paths for pipx to install into. Shared libs are setup once per session, all other pipx dirs, constants are @@ -144,11 +160,13 @@ def pipx_temp_env(tmp_path, monkeypatch, pipx_session_shared_dir, request): Also adds environment variables as necessary to make pip installations seamless. """ - pipx_temp_env_helper(pipx_session_shared_dir, tmp_path, monkeypatch, request) + pipx_temp_env_helper( + pipx_session_shared_dir, tmp_path, monkeypatch, request, utils_temp_dir + ) @pytest.fixture -def pipx_ultra_temp_env(tmp_path, monkeypatch, request): +def pipx_ultra_temp_env(tmp_path, monkeypatch, request, utils_temp_dir): """Sets up temporary paths for pipx to install into. Fully temporary environment, every test function starts as if pipx has @@ -158,4 +176,4 @@ def pipx_ultra_temp_env(tmp_path, monkeypatch, request): seamless. """ shared_dir = Path(tmp_path) / "shareddir" - pipx_temp_env_helper(shared_dir, tmp_path, monkeypatch, request) + pipx_temp_env_helper(shared_dir, tmp_path, monkeypatch, request, utils_temp_dir) diff --git a/tests/test_install.py b/tests/test_install.py index a135b86101..fb2737af86 100644 --- a/tests/test_install.py +++ b/tests/test_install.py @@ -71,11 +71,10 @@ def test_install_tricky_packages( install_package(capsys, pipx_temp_env, caplog, package_spec, package_name) -# TODO: Add git+... spec when git is in binpath of tests (Issue #303) @pytest.mark.parametrize( "package_name, package_spec", [ - # ("nox", "git+https://github.com/cs01/nox.git@5ea70723e9e6"), + ("pycowsay", "git+https://github.com/cs01/pycowsay.git@master"), ("pylint", PKG["pylint"]["spec"]), ("nox", "https://github.com/wntrblm/nox/archive/2022.1.7.zip"), ], diff --git a/tests/test_package_specifier.py b/tests/test_package_specifier.py index 154416ee0a..458ff7c5da 100644 --- a/tests/test_package_specifier.py +++ b/tests/test_package_specifier.py @@ -52,7 +52,6 @@ def test_fix_package_name(package_spec_in, package_name, package_spec_out): assert fix_package_name(package_spec_in, package_name) == package_spec_out -# TODO: Make sure git+ works with tests, correct in test_install as well @pytest.mark.parametrize( "package_spec_in,package_or_url_correct,valid_spec", [ From 3999247009a503297e1993a2ac8dedfb3ce41c86 Mon Sep 17 00:00:00 2001 From: meowmeow Date: Sun, 22 Jan 2023 12:55:08 +0000 Subject: [PATCH 2/7] Add support for Windows --- tests/conftest.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/conftest.py b/tests/conftest.py index bb79373471..2e48f64b44 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,3 +1,4 @@ +import ctypes import os import shutil import subprocess @@ -142,7 +143,12 @@ def utils_temp_dir(tmp_path_factory): git_path = shutil.which("git") tmp_path = tmp_path_factory.mktemp("session_utilstempdir") try: - Path(tmp_path / "git").symlink_to(Path(git_path)) + if WIN: + ctypes.windll.kernel32.CreateSymbolicLinkA( + os.path.join(tmp_path, "git"), git_path, 0 + ) + else: + Path(tmp_path / "git").symlink_to(Path(git_path)) except FileExistsError: pass return tmp_path From b05025798886fcfbb486a0b5495c1b6ca457d614 Mon Sep 17 00:00:00 2001 From: meowmeow Date: Sun, 22 Jan 2023 13:49:12 +0000 Subject: [PATCH 3/7] Add support for Windows --- tests/conftest.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 2e48f64b44..a8cde758ff 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -140,15 +140,10 @@ def pipx_session_shared_dir(tmp_path_factory): @pytest.fixture(scope="session") def utils_temp_dir(tmp_path_factory): - git_path = shutil.which("git") + git_path = shutil.which("git.exe" if WIN else "git") tmp_path = tmp_path_factory.mktemp("session_utilstempdir") - try: - if WIN: - ctypes.windll.kernel32.CreateSymbolicLinkA( - os.path.join(tmp_path, "git"), git_path, 0 - ) - else: - Path(tmp_path / "git").symlink_to(Path(git_path)) + try: + Path(tmp_path / "git.exe" if WIN else "git").symlink_to(Path(git_path)) except FileExistsError: pass return tmp_path From cdc8285d73edf398cbb1c15832f87cf1d34e52fe Mon Sep 17 00:00:00 2001 From: meowmeow Date: Sun, 22 Jan 2023 14:00:36 +0000 Subject: [PATCH 4/7] Fix linting --- tests/conftest.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index a8cde758ff..b10cb51ecb 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,4 +1,3 @@ -import ctypes import os import shutil import subprocess @@ -142,8 +141,10 @@ def pipx_session_shared_dir(tmp_path_factory): def utils_temp_dir(tmp_path_factory): git_path = shutil.which("git.exe" if WIN else "git") tmp_path = tmp_path_factory.mktemp("session_utilstempdir") - try: - Path(tmp_path / "git.exe" if WIN else "git").symlink_to(Path(git_path)) + try: + Path(tmp_path / "git.exe" if WIN else tmp_path / "git").symlink_to( + Path(git_path) + ) except FileExistsError: pass return tmp_path From 4db4f2b2e4be7108f3bab511de20066629a1d5f5 Mon Sep 17 00:00:00 2001 From: Jason Lam Date: Thu, 22 Jun 2023 10:28:14 +0000 Subject: [PATCH 5/7] Allow to add more utils in the future --- tests/conftest.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index b10cb51ecb..646a990ec3 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -139,14 +139,15 @@ def pipx_session_shared_dir(tmp_path_factory): @pytest.fixture(scope="session") def utils_temp_dir(tmp_path_factory): - git_path = shutil.which("git.exe" if WIN else "git") tmp_path = tmp_path_factory.mktemp("session_utilstempdir") - try: - Path(tmp_path / "git.exe" if WIN else tmp_path / "git").symlink_to( - Path(git_path) - ) - except FileExistsError: - pass + utils = ["git"] + for util in utils: + util_filename = f"{util}.exe" if WIN else util + util_path = shutil.which(util_filename) + try: + Path(tmp_path / util_filename).symlink_to(Path(util_path)) + except FileExistsError: + pass return tmp_path From d859de2c386486906ec02eef5bf929f7e5f5c580 Mon Sep 17 00:00:00 2001 From: meowmeow Date: Wed, 28 Jun 2023 09:32:24 +0000 Subject: [PATCH 6/7] Remove `util_filename` --- tests/conftest.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 646a990ec3..e324f3cf5a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -142,10 +142,9 @@ def utils_temp_dir(tmp_path_factory): tmp_path = tmp_path_factory.mktemp("session_utilstempdir") utils = ["git"] for util in utils: - util_filename = f"{util}.exe" if WIN else util - util_path = shutil.which(util_filename) + util_path = Path(shutil.which(util)) try: - Path(tmp_path / util_filename).symlink_to(Path(util_path)) + Path(tmp_path / util_path.name).symlink_to(util_path) except FileExistsError: pass return tmp_path From 90415140565fffaded6cb5552afa35a4f3992afe Mon Sep 17 00:00:00 2001 From: Jason Lam Date: Wed, 28 Jun 2023 18:53:31 +0800 Subject: [PATCH 7/7] Update tests/conftest.py Co-authored-by: Tzu-ping Chung --- tests/conftest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/conftest.py b/tests/conftest.py index e324f3cf5a..e33d7aea3b 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -144,7 +144,7 @@ def utils_temp_dir(tmp_path_factory): for util in utils: util_path = Path(shutil.which(util)) try: - Path(tmp_path / util_path.name).symlink_to(util_path) + (tmp_path / util_path.name).symlink_to(util_path) except FileExistsError: pass return tmp_path