From cf04976cea80c25dbb87820c234122256616d076 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miro=20Hron=C4=8Dok?= Date: Thu, 1 Aug 2019 14:46:01 +0200 Subject: [PATCH] Only use tox_cleanup hook when it is possible --- src/tox_current_env/hooks.py | 31 +++++++++++++++++++------------ tests/test_integration.py | 10 ++++++++++ tox.ini | 1 + 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/src/tox_current_env/hooks.py b/src/tox_current_env/hooks.py index 93c13f3..a44bc77 100644 --- a/src/tox_current_env/hooks.py +++ b/src/tox_current_env/hooks.py @@ -85,10 +85,15 @@ def unsupported_raise(config, venv): return regular = not (config.option.current_env or config.option.print_deps_only) if regular and is_current_env_link(venv): - raise tox.exception.ConfigError( - "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." - ) + if hasattr(tox.hookspecs, "tox_cleanup"): + raise tox.exception.ConfigError( + "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." + ) + else: + raise tox.exception.ConfigError( + "Regular tox run after --current-env or --print-deps-only tox run is not supported without --recreate (-r)." + ) elif config.option.current_env and is_proper_venv(venv): raise tox.exception.ConfigError( "--current-env after regular tox run is not supported without --recreate (-r)." @@ -166,11 +171,13 @@ def tox_runtest(venv, redirect): 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) +if hasattr(tox.hookspecs, "tox_cleanup"): + + @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) diff --git a/tests/test_integration.py b/tests/test_integration.py index 85199fe..5aaf33b 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -6,6 +6,8 @@ import subprocess import sys import textwrap +from packaging import version + import pytest @@ -37,6 +39,8 @@ def is_available(python): return True +TOX_VERSION = version.parse(tox("--version").stdout.split(" ")[0]) + needs_py3678 = pytest.mark.skipif( not is_available("python3.6") or not is_available("python3.7") @@ -44,6 +48,10 @@ needs_py3678 = pytest.mark.skipif( reason="This test needs all of python3.6, python3.7 and python3.8 to be available in $PATH", ) +needs_tox38 = pytest.mark.skipif( + TOX_VERSION < version.parse("3.8"), reason="This test needs at least tox 3.8" +) + def test_native_toxenv_current_env(): result = tox("-e", NATIVE_TOXENV, "--current-env") @@ -195,6 +203,7 @@ def test_regular_run_native_toxenv(): assert len(list(sitelib.glob(f"{pkg}-*.dist-info"))) == 1 +@needs_tox38 def test_regular_after_current_is_supported(): result = tox("-e", NATIVE_TOXENV, "--current-env") assert result.stdout.splitlines()[0] == NATIVE_EXECUTABLE @@ -214,6 +223,7 @@ def test_regular_after_killed_current_is_not_supported(): assert "--recreate" in result.stderr +@needs_tox38 def test_regular_after_first_deps_only_is_supported(): result = tox("-e", NATIVE_TOXENV, "--print-deps-only") assert result.stdout.splitlines()[0] == "six" diff --git a/tox.ini b/tox.ini index 73cd547..693a4da 100644 --- a/tox.ini +++ b/tox.ini @@ -4,6 +4,7 @@ envlist = {py36,py37,py38}-tox{35,release,master} [testenv] deps= pytest + packaging tox35: tox >=3.5,<3.6 toxrelease: tox toxmaster: git+https://github.com/tox-dev/tox.git@master