diff --git a/nox/virtualenv.py b/nox/virtualenv.py index cc59eebb..13f3fbc3 100644 --- a/nox/virtualenv.py +++ b/nox/virtualenv.py @@ -33,6 +33,7 @@ ["PIP_RESPECT_VIRTUALENV", "PIP_REQUIRE_VIRTUALENV", "__PYVENV_LAUNCHER__"] ) _SYSTEM = platform.system() +_ENABLE_STALENESS_CHECK = "NOX_ENABLE_STALENESS_CHECK" in os.environ class InterpreterNotFound(OSError): @@ -312,6 +313,8 @@ def __init__( def _clean_location(self) -> bool: """Deletes any existing virtual environment""" if os.path.exists(self.location): + if self.reuse_existing and not _ENABLE_STALENESS_CHECK: + return False if ( self.reuse_existing and self._check_reused_environment_type() diff --git a/tests/test_virtualenv.py b/tests/test_virtualenv.py index 5240b524..c85c08bc 100644 --- a/tests/test_virtualenv.py +++ b/tests/test_virtualenv.py @@ -351,6 +351,15 @@ def test_create_reuse_environment(make_one): assert reused +@pytest.fixture +def _enable_staleness_check(monkeypatch): + monkeypatch.setattr("nox.virtualenv._ENABLE_STALENESS_CHECK", True) + + +enable_staleness_check = pytest.mark.usefixtures("_enable_staleness_check") + + +@enable_staleness_check def test_create_reuse_environment_with_different_interpreter(make_one, monkeypatch): venv, location = make_one(reuse_existing=True) venv.create() @@ -367,6 +376,7 @@ def test_create_reuse_environment_with_different_interpreter(make_one, monkeypat assert not location.join("marker").check() +@enable_staleness_check def test_create_reuse_stale_venv_environment(make_one): venv, location = make_one(reuse_existing=True) venv.create() @@ -386,6 +396,7 @@ def test_create_reuse_stale_venv_environment(make_one): assert not reused +@enable_staleness_check def test_create_reuse_stale_virtualenv_environment(make_one): venv, location = make_one(reuse_existing=True, venv=True) venv.create() @@ -410,6 +421,7 @@ def test_create_reuse_stale_virtualenv_environment(make_one): assert not reused +@enable_staleness_check def test_create_reuse_venv_environment(make_one): venv, location = make_one(reuse_existing=True, venv=True) venv.create() @@ -424,6 +436,7 @@ def test_create_reuse_venv_environment(make_one): assert reused +@enable_staleness_check @pytest.mark.skipif(IS_WINDOWS, reason="Avoid 'No pyvenv.cfg file' error on Windows.") def test_create_reuse_oldstyle_virtualenv_environment(make_one): venv, location = make_one(reuse_existing=True) @@ -442,6 +455,7 @@ def test_create_reuse_oldstyle_virtualenv_environment(make_one): assert reused +@enable_staleness_check def test_create_reuse_python2_environment(make_one): venv, location = make_one(reuse_existing=True, interpreter="2.7")