From 9477a9135183c347b7ffc957ddf6eb5e1b18f978 Mon Sep 17 00:00:00 2001 From: Chad Smith Date: Sun, 11 Aug 2019 19:00:53 -0700 Subject: [PATCH] Add venv as an option for venv_backend (#231) * 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 --- nox/sessions.py | 9 ++++++++- nox/virtualenv.py | 25 ++++++++++++++++--------- tests/test_sessions.py | 1 + tests/test_virtualenv.py | 6 ++++++ 4 files changed, 31 insertions(+), 10 deletions(-) diff --git a/nox/sessions.py b/nox/sessions.py index 2affcf7e..ccf59935 100644 --- a/nox/sessions.py +++ b/nox/sessions.py @@ -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 ) ) diff --git a/nox/virtualenv.py b/nox/virtualenv.py index 9063b2b5..836b6420 100644 --- a/nox/virtualenv.py +++ b/nox/virtualenv.py @@ -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 @@ -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) diff --git a/tests/test_sessions.py b/tests/test_sessions.py index 0b9559aa..dbe72bb1 100644 --- a/tests/test_sessions.py +++ b/tests/test_sessions.py @@ -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), ], ) diff --git a/tests/test_virtualenv.py b/tests/test_virtualenv.py index 04296e23..61b246d3 100644 --- a/tests/test_virtualenv.py +++ b/tests/test_virtualenv.py @@ -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.") @@ -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")