From 6959f93a785a92acf99705c667cc8925a31b21f3 Mon Sep 17 00:00:00 2001 From: Jason Lam Date: Fri, 30 Jun 2023 14:08:43 +0800 Subject: [PATCH] Testing: add git to PATH (#930) * Testing: add git to PATH * Add support for Windows * Add support for Windows * Fix linting * Allow to add more utils in the future * Remove `util_filename` * Update tests/conftest.py Co-authored-by: Tzu-ping Chung --------- Co-authored-by: Tzu-ping Chung --- tests/conftest.py | 32 ++++++++++++++++++++++++++------ tests/test_install.py | 3 +-- tests/test_package_specifier.py | 1 - 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index c643be4569..7eaf8ab375 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. @@ -133,8 +136,23 @@ 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): + tmp_path = tmp_path_factory.mktemp("session_utilstempdir") + utils = ["git"] + for util in utils: + util_path = Path(shutil.which(util)) + try: + (tmp_path / util_path.name).symlink_to(util_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 @@ -143,11 +161,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 @@ -157,4 +177,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 6e8a3566dd..27987ee9d1 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", [