From 9110f3e5a5cd3e5e0e51d972036f43cbd901672a Mon Sep 17 00:00:00 2001 From: Moshe Zadka Date: Fri, 15 May 2020 23:09:38 -0700 Subject: [PATCH] Add seasion.create_tmp (#320) * Address #319: Give a create_tmp API * more stuff * fade to black * woops * calculate tmp directly from envdir * cleanup and fix tests --- nox/sessions.py | 18 ++++++++++++++---- noxfile.py | 11 +++++++++-- tests/test_sessions.py | 19 +++++++++++++++++++ 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/nox/sessions.py b/nox/sessions.py index 59a5cf4b..7fa77eca 100644 --- a/nox/sessions.py +++ b/nox/sessions.py @@ -135,6 +135,13 @@ def bin(self) -> Optional[str]: """The bin directory for the virtualenv.""" return self.virtualenv.bin + def create_tmp(self) -> str: + """Create, and return, a temporary directory.""" + tmpdir = os.path.join(self._runner.envdir, "tmp") + os.makedirs(tmpdir, exist_ok=True) + self.env["TMPDIR"] = tmpdir + return tmpdir + @property def interactive(self) -> bool: """Returns True if Nox is being run in an interactive session or False otherwise.""" @@ -388,33 +395,36 @@ def __str__(self) -> str: def friendly_name(self) -> str: return self.signatures[0] if self.signatures else self.name + @property + def envdir(self) -> str: + return _normalize_path(self.global_config.envdir, self.friendly_name) + def _create_venv(self) -> None: if self.func.python is False: self.venv = ProcessEnv() return - path = _normalize_path(self.global_config.envdir, self.friendly_name) reuse_existing = ( self.func.reuse_venv or self.global_config.reuse_existing_virtualenvs ) if not self.func.venv_backend or self.func.venv_backend == "virtualenv": self.venv = VirtualEnv( - path, + self.envdir, interpreter=self.func.python, # type: ignore reuse_existing=reuse_existing, venv_params=self.func.venv_params, ) elif self.func.venv_backend == "conda": self.venv = CondaEnv( - path, + self.envdir, interpreter=self.func.python, # type: ignore reuse_existing=reuse_existing, venv_params=self.func.venv_params, ) elif self.func.venv_backend == "venv": self.venv = VirtualEnv( - path, + self.envdir, interpreter=self.func.python, # type: ignore reuse_existing=reuse_existing, venv=True, diff --git a/noxfile.py b/noxfile.py index e20e5c1a..5bb786c2 100644 --- a/noxfile.py +++ b/noxfile.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import functools import os import nox @@ -30,6 +31,7 @@ def is_python_version(session, version): @nox.session(python=["3.5", "3.6", "3.7", "3.8"]) def tests(session): """Run test suite with pytest.""" + session.create_tmp() session.install("-r", "requirements-test.txt") session.install("-e", ".[tox_to_nox]") tests = session.posargs or ["tests/"] @@ -45,6 +47,7 @@ def tests(session): @nox.session(python=["3.5", "3.6", "3.7", "3.8"], venv_backend="conda") def conda_tests(session): """Run test suite with pytest.""" + session.create_tmp() session.conda_install( "--file", "requirements-conda-test.txt", "--channel", "conda-forge" ) @@ -93,11 +96,15 @@ def lint(session): @nox.session(python="3.8") def docs(session): """Build the documentation.""" - session.run("rm", "-rf", "docs/_build", external=True) + output_dir = os.path.join(session.create_tmp(), "output") + doctrees, html = map( + functools.partial(os.path.join, output_dir), ["doctrees", "html"] + ) + session.run("rm", "-rf", output_dir, external=True) session.install("-r", "requirements-test.txt") session.install(".") session.cd("docs") - sphinx_args = ["-b", "html", "-W", "-d", "_build/doctrees", ".", "_build/html"] + sphinx_args = ["-b", "html", "-W", "-d", doctrees, ".", html] if not session.interactive: sphinx_cmd = "sphinx-build" diff --git a/tests/test_sessions.py b/tests/test_sessions.py index 8af3f858..f4c457fb 100644 --- a/tests/test_sessions.py +++ b/tests/test_sessions.py @@ -16,6 +16,7 @@ import logging import os import sys +import tempfile from unittest import mock import nox.command @@ -74,6 +75,24 @@ def make_session_and_runner(self): runner.venv.bin = "/no/bin/for/you" return nox.sessions.Session(runner=runner), runner + def test_create_tmp(self): + session, runner = self.make_session_and_runner() + with tempfile.TemporaryDirectory() as root: + runner.global_config.envdir = root + tmpdir = session.create_tmp() + assert session.env["TMPDIR"] == tmpdir + assert tmpdir.startswith(root) + + def test_create_tmp_twice(self): + session, runner = self.make_session_and_runner() + with tempfile.TemporaryDirectory() as root: + runner.global_config.envdir = root + runner.venv.bin = bin + session.create_tmp() + tmpdir = session.create_tmp() + assert session.env["TMPDIR"] == tmpdir + assert tmpdir.startswith(root) + def test_properties(self): session, runner = self.make_session_and_runner()