Skip to content

Commit

Permalink
Add venv as an option for venv_backend (#231)
Browse files Browse the repository at this point in the history
* add venv as an option for venv_backend

* remove python resolution changes

* update tests

* run black

* use different interpreters for venv vs virtualenv

* update test
  • Loading branch information
Chad Smith authored and theacodes committed Aug 12, 2019
1 parent 69fb536 commit 9477a91
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
9 changes: 8 additions & 1 deletion nox/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,9 +375,16 @@ def _create_venv(self):
self.venv = CondaEnv(
path, interpreter=self.func.python, reuse_existing=reuse_existing
)
elif self.func.venv_backend == "venv":
self.venv = VirtualEnv(
path,
interpreter=self.func.python,
reuse_existing=reuse_existing,
venv=True,
)
else:
raise ValueError(
"Expected venv_backend one of ('virtualenv', 'conda'), but got '{}'.".format(
"Expected venv_backend one of ('virtualenv', 'conda', 'venv'), but got '{}'.".format(
self.func.venv_backend
)
)
Expand Down
25 changes: 16 additions & 9 deletions nox/virtualenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,13 @@ class VirtualEnv(ProcessEnv):

is_sandboxed = True

def __init__(self, location, interpreter=None, reuse_existing=False):
def __init__(self, location, interpreter=None, reuse_existing=False, *, venv=False):
self.location_name = location
self.location = os.path.abspath(location)
self.interpreter = interpreter
self._resolved = None
self.reuse_existing = reuse_existing
self.venv_or_virtualenv = "venv" if venv else "virtualenv"
super(VirtualEnv, self).__init__()

_clean_location = _clean_location
Expand Down Expand Up @@ -275,21 +276,27 @@ def bin(self):
return os.path.join(self.location, "bin")

def create(self):
"""Create the virtualenv."""
"""Create the virtualenv or venv."""
if not self._clean_location():
logger.debug(
"Re-using existing virtualenv at {}.".format(self.location_name)
"Re-using existing virtual environment at {}.".format(
self.location_name
)
)
return False

cmd = [sys.executable, "-m", "virtualenv", self.location]

if self.interpreter:
cmd.extend(["-p", self._resolved_interpreter])
if self.venv_or_virtualenv == "virtualenv":
cmd = [sys.executable, "-m", "virtualenv", self.location]
if self.interpreter:
cmd.extend(["-p", self._resolved_interpreter])
else:
cmd = [self._resolved_interpreter, "-m", "venv", self.location]

logger.info(
"Creating virtualenv using {} in {}".format(
os.path.basename(self._resolved_interpreter), self.location_name
"Creating virtual environment ({}) using {} in {}".format(
self.venv_or_virtualenv,
os.path.basename(self._resolved_interpreter),
self.location_name,
)
)
nox.command.run(cmd, silent=True, log=False)
Expand Down
1 change: 1 addition & 0 deletions tests/test_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,7 @@ def test__create_venv(self, create):
"virtualenv",
nox.virtualenv.VirtualEnv,
),
("nox.virtualenv.VirtualEnv.create", "venv", nox.virtualenv.VirtualEnv),
("nox.virtualenv.CondaEnv.create", "conda", nox.virtualenv.CondaEnv),
],
)
Expand Down
6 changes: 6 additions & 0 deletions tests/test_virtualenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def test_constructor_defaults(make_one):
assert venv.location
assert venv.interpreter is None
assert venv.reuse_existing is False
assert venv.venv_or_virtualenv == "virtualenv"


@pytest.mark.skipif(IS_WINDOWS, reason="Not testing multiple interpreters on Windows.")
Expand Down Expand Up @@ -213,6 +214,11 @@ def test_create(make_one):
assert dir_.join("test.txt").check()


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


@pytest.mark.skipif(IS_WINDOWS, reason="Not testing multiple interpreters on Windows.")
def test_create_interpreter(make_one):
venv, dir_ = make_one(interpreter="python3")
Expand Down

0 comments on commit 9477a91

Please sign in to comment.