From 45bac2d3ee1790744772fe46ee1902681c71c7ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miro=20Hron=C4=8Dok?= Date: Fri, 26 Jul 2019 13:49:54 +0200 Subject: [PATCH] Create current environemnt link when --print-deps-only has no env Fixes https://github.com/fedora-python/tox-current-env/issues/9 --- src/tox_current_env/hooks.py | 22 ++++++++++++++++++---- tests/test_integration.py | 21 +++++++++++++++++++++ 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/tox_current_env/hooks.py b/src/tox_current_env/hooks.py index e23040e..e52a438 100644 --- a/src/tox_current_env/hooks.py +++ b/src/tox_current_env/hooks.py @@ -54,6 +54,11 @@ def is_proper_venv(venv): return python and activate +def is_any_env(venv): + python, activate = _python_activate_exists(venv) + return python + + def unsupported_raise(config, venv): if config.option.recreate: return @@ -72,10 +77,19 @@ def unsupported_raise(config, venv): def tox_testenv_create(venv, action): """We create a fake virtualenv with just the symbolic link""" config = venv.envconfig.config + create_fake_env = check_version = config.option.current_env if config.option.print_deps_only: - # We don't need anything - return True - if config.option.current_env: + if is_any_env(venv): + # We don't need anything + return True + else: + # We need at least some kind of environment, + # or tox fails without a python command + # We fallback to --current-env behavior, + # because it's cheaper, faster and won't install stuff + create_fake_env = True + if check_version: + # With real --current-env, we check this, but not with --print-deps-only only version_info = venv.envconfig.python_info.version_info if version_info is None: raise tox.exception.InterpreterNotFound(venv.envconfig.basepython) @@ -85,7 +99,7 @@ def tox_testenv_create(venv, action): + f" in current env: {tuple(sys.version_info)}\n" + f" requested: {version_info}" ) - + if create_fake_env: # Make sure the `python` command on path is sys.executable. # (We might have e.g. /usr/bin/python3, not `python`.) # Remove the rest of the virtualenv. diff --git a/tests/test_integration.py b/tests/test_integration.py index 94329fb..7588923 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -195,3 +195,24 @@ def test_regular_after_deps_only(): result = tox("-e", NATIVE_TOXENV, "--print-deps-only", prune=False) assert "bin/python" not in result.stdout assert "six" in result.stdout + + +def test_print_deps_without_python_command(tmp_path): + bin = tmp_path / "bin" + bin.mkdir() + tox_link = bin / "tox" + tox_path = shutil.which("tox") + tox_link.symlink_to(tox_path) + env = {**os.environ, "PATH": str(bin)} + + result = tox("-e", NATIVE_TOXENV, "--print-deps-only", env=env) + expected = textwrap.dedent( + f""" + six + py + ___________________________________ summary ____________________________________ + {NATIVE_TOXENV}: commands succeeded + congratulations :) + """ + ).lstrip() + assert result.stdout == expected