Skip to content

Commit

Permalink
Check whether interpreter changed and type (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
Diego committed Apr 10, 2021
1 parent 3240441 commit 3f94d57
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
30 changes: 25 additions & 5 deletions nox/virtualenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,12 +313,19 @@ def _clean_location(self) -> bool:
"""Deletes any existing virtual environment"""
if os.path.exists(self.location):
if self.reuse_existing:
self._check_reused_environment()
return False
else:
shutil.rmtree(self.location)

return True

def _check_reused_environment(self) -> None:
"""Check if reused environment type is the same."""
with open(os.path.join(self.location, "pyvenv.cfg")) as fp:
old_env = "virtualenv" if "virtualenv" in fp.read() else "venv"
assert old_env == self.venv_or_virtualenv

@property
def _resolved_interpreter(self) -> str:
"""Return the interpreter, appropriately resolved for the platform.
Expand Down Expand Up @@ -394,12 +401,25 @@ def bin_paths(self) -> List[str]:
def create(self) -> bool:
"""Create the virtualenv or venv."""
if not self._clean_location():
logger.debug(
"Re-using existing virtual environment at {}.".format(
self.location_name
)
original = nox.command.run(
[self._resolved_interpreter, "-c", "import sys; print(sys.prefix)"],
silent=True,
)
return False
created = nox.command.run(
[
os.path.join(self.location, "bin", "python"),
"-c",
"import sys; print(sys.real_prefix)",
],
silent=True,
)
if original == created:
logger.debug(
"Re-using existing virtual environment at {}.".format(
self.location_name
)
)
return False

if self.venv_or_virtualenv == "virtualenv":
cmd = [sys.executable, "-m", "virtualenv", self.location]
Expand Down
22 changes: 21 additions & 1 deletion tests/test_virtualenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ def test__clean_location(monkeypatch, make_one):

# Don't re-use existing, but doesn't currently exist.
# Should return True indicating that the venv needs to be created.
monkeypatch.setattr(
nox.virtualenv.VirtualEnv, "_check_reused_environment", mock.MagicMock()
)
monkeypatch.delattr(nox.virtualenv.shutil, "rmtree")
assert not dir_.check()
assert venv._clean_location()
Expand Down Expand Up @@ -289,7 +292,7 @@ def test_bin_windows(make_one):
assert dir_.join("Scripts").strpath == venv.bin


def test_create(make_one):
def test_create(monkeypatch, make_one):
venv, dir_ = make_one()
venv.create()

Expand All @@ -312,10 +315,27 @@ def test_create(make_one):
dir_.ensure("test.txt")
assert dir_.join("test.txt").check()
venv.reuse_existing = True
monkeypatch.setattr(nox.virtualenv.nox.command, "run", mock.MagicMock())
venv.create()
assert dir_.join("test.txt").check()


def test_create_check_interpreter(make_one, monkeypatch, tmpdir):
cmd_mock = mock.MagicMock(
side_effect=["python-1", "python-1", "python-2", "python-3", "python-4"]
)
monkeypatch.setattr(nox.virtualenv.nox.command, "run", cmd_mock)
test_dir = tmpdir.mkdir("pytest")
fp = test_dir.join("pyvenv.cfg")
fp.write("virtualenv")
venv, dir_ = make_one()
venv_or_virtualenv = "venv"
venv.reuse_existing = True
venv.location = test_dir.strpath
assert not venv.create()
assert venv.create()


def test_create_venv_backend(make_one):
venv, dir_ = make_one(venv=True)
venv.create()
Expand Down

0 comments on commit 3f94d57

Please sign in to comment.