1
0
mirror of https://github.com/fedora-python/tox-current-env.git synced 2025-01-11 17:06:13 +01:00

Merge PR #10 – Create current environment link when --print-deps-only has no env

https://github.com/fedora-python/tox-current-env/pull/10
This commit is contained in:
Petr Viktorin 2019-07-30 13:54:18 +02:00 committed by GitHub
commit 6b2fe8ea56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 97 additions and 35 deletions

View File

@ -158,7 +158,8 @@ Don't mix current-env and regular tox runs
Tox caches the virtualenvs it creates, and doesn't distinguish between
regular virtualenvs and ``--current-env``.
Don't mix ``tox --current-env`` runs and regular ``tox`` runs (without the flag).
Don't mix ``tox --current-env`` or ``tox --print-deps-only`` runs
and regular ``tox`` runs (without the flag).
If you ever need to do this, use tox's ``--recreate/-r`` flag to clear the cache.
The plugin should abort with a meaningful error message if this is detected,

View File

@ -37,39 +37,6 @@ class InterpreterMismatch(tox.exception.InterpreterNotFound):
"""Interpreter version in current env does not match requested version"""
@tox.hookimpl
def tox_testenv_create(venv, action):
"""We create a fake virtualenv with just the symbolic link"""
config = venv.envconfig.config
if config.option.print_deps_only:
# We don't need anything
return True
if config.option.current_env:
version_info = venv.envconfig.python_info.version_info
if version_info is None:
raise tox.exception.InterpreterNotFound(venv.envconfig.basepython)
if version_info[:2] != sys.version_info[:2]:
raise InterpreterMismatch(
f"tox_current_env: interpreter versions do not match:\n"
+ f" in current env: {tuple(sys.version_info)}\n"
+ f" requested: {version_info}"
)
# 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.
link = venv.envconfig.get_envpython()
target = sys.executable
shutil.rmtree(os.path.dirname(os.path.dirname(link)), ignore_errors=True)
os.makedirs(os.path.dirname(link))
os.symlink(target, link)
return True
else:
link = venv.envconfig.get_envpython()
shutil.rmtree(os.path.dirname(os.path.dirname(link)), ignore_errors=True)
return None # let tox handle the rest
def _python_activate_exists(venv):
python = venv.envconfig.get_envpython()
bindir = os.path.dirname(python)
@ -87,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
@ -101,6 +73,55 @@ def unsupported_raise(config, venv):
)
@tox.hookimpl
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:
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)
if version_info[:2] != sys.version_info[:2]:
raise InterpreterMismatch(
f"tox_current_env: interpreter versions do not match:\n"
+ 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.
link = venv.envconfig.get_envpython()
target = sys.executable
shutil.rmtree(os.path.dirname(os.path.dirname(link)), ignore_errors=True)
os.makedirs(os.path.dirname(link))
os.symlink(target, link)
return True
else:
link = venv.envconfig.get_envpython()
shutil.rmtree(os.path.dirname(os.path.dirname(link)), ignore_errors=True)
return None # let tox handle the rest
@tox.hookimpl
def tox_package(session, venv):
"""Fail early when unsupported"""
config = venv.envconfig.config
unsupported_raise(config, venv)
@tox.hookimpl
def tox_testenv_install_deps(venv, action):
"""We don't install anything"""

View File

@ -149,6 +149,22 @@ def test_regular_after_current_is_not_supported():
assert "not supported" in result.stderr
def test_regular_after_first_deps_only_is_not_supported():
result = tox("-e", NATIVE_TOXENV, "--print-deps-only")
assert result.stdout.splitlines()[0] == "six"
result = tox("-e", NATIVE_TOXENV, prune=False, check=False)
assert result.returncode > 0
assert "not supported" in result.stderr
# check that "test" was not installed to current environment
pip_freeze = subprocess.run(
(sys.executable, "-m", "pip", "freeze"),
encoding="utf-8",
stdout=subprocess.PIPE,
).stdout.splitlines()
assert "test==0.0.0" not in pip_freeze
def test_regular_recreate_after_current():
result = tox("-e", NATIVE_TOXENV, "--current-env")
assert result.stdout.splitlines()[0] == NATIVE_EXECUTABLE
@ -184,14 +200,38 @@ def test_current_after_deps_only():
assert result.stdout.splitlines()[0] == NATIVE_EXECUTABLE
def test_regular_after_deps_only():
def test_regular_recreate_after_deps_only():
result = tox("-e", NATIVE_TOXENV, "--print-deps-only")
assert "bin/python" not in result.stdout
assert "six" in result.stdout
result = tox("-re", NATIVE_TOXENV, prune=False)
assert result.stdout.splitlines()[0] != NATIVE_EXECUTABLE
sitelib = DOT_TOX / f"{NATIVE_TOXENV}/lib/python3.{NATIVE_TOXENV[-1]}/site-packages"
assert sitelib.is_dir()
assert len(list(sitelib.glob("test-*.dist-info"))) == 1
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