1
0
mirror of https://github.com/fedora-python/tox-current-env.git synced 2024-12-25 01:26:13 +01:00

Clean fake venvs after --current-env

This makes it easier to run regular tox after tox --current-env,
however not the other way around.

The documented caveat remains the same: Don't mix those without -r.

Partially fixes https://github.com/fedora-python/tox-current-env/issues/14
This commit is contained in:
Miro Hrončok 2019-08-01 13:24:41 +02:00
parent 56d71f94dc
commit a645fe2175
3 changed files with 40 additions and 11 deletions

View File

@ -164,8 +164,9 @@ and regular ``tox`` runs (without the flag).
If you ever need to do this, use tox's ``--recreate/-r`` flag to clear the cache. 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, The plugin should abort with a meaningful error message if this is detected,
but in some cases (such as running ``tox --current-env``, uninstalling the but in some corner cases (such as running ``tox --current-env``,
plugin, and running ``tox``), you will get undefined results forcefully killing it before it finished, uninstalling the plugin,
and running ``tox``), you will get undefined results
(such as installing packages from PyPI into your current environment). (such as installing packages from PyPI into your current environment).
Environment variables are not passed by default Environment variables are not passed by default

View File

@ -75,13 +75,19 @@ def is_any_env(venv):
return python return python
def rm_venv(venv):
link = venv.envconfig.get_envpython()
shutil.rmtree(os.path.dirname(os.path.dirname(link)), ignore_errors=True)
def unsupported_raise(config, venv): def unsupported_raise(config, venv):
if config.option.recreate: if config.option.recreate:
return return
regular = not (config.option.current_env or config.option.print_deps_only) regular = not (config.option.current_env or config.option.print_deps_only)
if regular and is_current_env_link(venv): if regular and is_current_env_link(venv):
raise tox.exception.ConfigError( raise tox.exception.ConfigError(
"Regular tox run after --current-env or --print-deps-only tox run is not supported without --recreate (-r)." "Looks like previous --current-env or --print-deps-only tox run didn't finish the cleanup. "
"Run tox run with --recreate (-r) or manually remove the environment in .tox."
) )
elif config.option.current_env and is_proper_venv(venv): elif config.option.current_env and is_proper_venv(venv):
raise tox.exception.ConfigError( raise tox.exception.ConfigError(
@ -126,8 +132,7 @@ def tox_testenv_create(venv, action):
os.symlink(target, link) os.symlink(target, link)
return True return True
else: else:
link = venv.envconfig.get_envpython() rm_venv(venv)
shutil.rmtree(os.path.dirname(os.path.dirname(link)), ignore_errors=True)
return None # let tox handle the rest return None # let tox handle the rest
@ -159,3 +164,13 @@ def tox_runtest(venv, redirect):
if config.option.print_deps_only: if config.option.print_deps_only:
print(*venv.get_resolved_dependencies(), sep="\n") print(*venv.get_resolved_dependencies(), sep="\n")
return True return True
@tox.hookimpl
def tox_cleanup(session):
"""Remove the fake virtualenv not to collide with regular tox
Collisions can happen anyway (when tox is killed forcefully before this happens)
Note that we don't remove real venvs, as recreating them is expensive"""
for venv in session.venv_dict.values():
if is_current_env_link(venv):
rm_venv(venv)

View File

@ -195,20 +195,32 @@ def test_regular_run_native_toxenv():
assert len(list(sitelib.glob(f"{pkg}-*.dist-info"))) == 1 assert len(list(sitelib.glob(f"{pkg}-*.dist-info"))) == 1
def test_regular_after_current_is_not_supported(): def test_regular_after_current_is_supported():
result = tox("-e", NATIVE_TOXENV, "--current-env") result = tox("-e", NATIVE_TOXENV, "--current-env")
assert result.stdout.splitlines()[0] == NATIVE_EXECUTABLE assert result.stdout.splitlines()[0] == NATIVE_EXECUTABLE
result = tox("-e", NATIVE_TOXENV, prune=False)
assert f"/.tox/{NATIVE_TOXENV}/bin/python" in result.stdout
assert "--recreate" not in result.stderr
def test_regular_after_killed_current_is_not_supported():
# fake broken tox run
shutil.rmtree(DOT_TOX, ignore_errors=True)
(DOT_TOX / NATIVE_TOXENV / "bin").mkdir(parents=True)
(DOT_TOX / NATIVE_TOXENV / "bin" / "python").symlink_to(NATIVE_EXECUTABLE)
result = tox("-e", NATIVE_TOXENV, prune=False, check=False) result = tox("-e", NATIVE_TOXENV, prune=False, check=False)
assert result.returncode > 0 assert result.returncode > 0
assert "not supported" in result.stderr assert "--recreate" in result.stderr
def test_regular_after_first_deps_only_is_not_supported(): def test_regular_after_first_deps_only_is_supported():
result = tox("-e", NATIVE_TOXENV, "--print-deps-only") result = tox("-e", NATIVE_TOXENV, "--print-deps-only")
assert result.stdout.splitlines()[0] == "six" assert result.stdout.splitlines()[0] == "six"
result = tox("-e", NATIVE_TOXENV, prune=False, check=False) result = tox("-e", NATIVE_TOXENV, prune=False)
assert result.returncode > 0 lines = sorted(result.stdout.splitlines()[:1])
assert "not supported" in result.stderr assert "--recreate" not in result.stderr
assert f"/.tox/{NATIVE_TOXENV}/bin/python" in lines[0]
# check that "test" was not installed to current environment # check that "test" was not installed to current environment
pip_freeze = subprocess.run( pip_freeze = subprocess.run(
@ -225,6 +237,7 @@ def test_regular_recreate_after_current():
result = tox("-re", NATIVE_TOXENV, prune=False) result = tox("-re", NATIVE_TOXENV, prune=False)
assert f"/.tox/{NATIVE_TOXENV}/bin/python" in result.stdout assert f"/.tox/{NATIVE_TOXENV}/bin/python" in result.stdout
assert "not supported" not in result.stderr assert "not supported" not in result.stderr
assert "--recreate" not in result.stderr
def test_current_after_regular_is_not_supported(): def test_current_after_regular_is_not_supported():