Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address #319: Give a create_tmp API #320

Merged
merged 6 commits into from
May 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 14 additions & 4 deletions nox/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 9 additions & 2 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import functools
import os

import nox
Expand All @@ -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/"]
Expand All @@ -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"
)
Expand Down Expand Up @@ -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"
Expand Down
19 changes: 19 additions & 0 deletions tests/test_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import logging
import os
import sys
import tempfile
from unittest import mock

import nox.command
Expand Down Expand Up @@ -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()

Expand Down