From 9ebae0ac400a3542a8e4e5ac1a6746e9f08419a8 Mon Sep 17 00:00:00 2001 From: Diego Ramirez Date: Wed, 15 Sep 2021 08:50:44 -0500 Subject: [PATCH] Add a "shared cache" directory (#476) * Add a "shared cache" directory Add an "{envdir}/.shared" directory, and create an API for accesing to it. * Don't fail if the ".shared" directory exists Don't raise an exception. * Update sessions.py Move the "shared cache" API inside of nox.sessions.Session * Update sessions.py Convert the "shared cache" path into a property. Now, it returns a "pathlib.Path". * Update test_sessions.py Make a test for the recent changes. * Update sessions.py Use the "pathlib.Path" methods to reduce the variable usage. * Update test_sessions.py Make some modifications to the tests. * Update test_sessions.py Fix an import error. * Update sessions.py Use the parent directory to create the cache dir. * Update test_sessions.py Use a tempfile to test the session properties. * Fix test indent * Update nox/sessions.py This avoids some unnecessary path manipulations, and using the parent directory of a virtualenv which does not necessarily exist (`PassthroughEnv` does not create a virtualenv). Co-authored-by: Claudio Jolowicz * Update sessions.py Use ".cache" instead of ".shared". * Update test_sessions.py Use ".cache" instead of ".shared". Co-authored-by: Tom Fleet Co-authored-by: Claudio Jolowicz --- nox/sessions.py | 8 ++++++++ tests/test_sessions.py | 21 +++++++++++++-------- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/nox/sessions.py b/nox/sessions.py index c97c7eb2..5d1b35c0 100644 --- a/nox/sessions.py +++ b/nox/sessions.py @@ -16,6 +16,7 @@ import enum import hashlib import os +import pathlib import re import sys import unicodedata @@ -184,6 +185,13 @@ def create_tmp(self) -> str: self.env["TMPDIR"] = tmpdir return tmpdir + @property + def cache_dir(self) -> pathlib.Path: + """Create and return a 'shared cache' directory to be used across sessions.""" + path = pathlib.Path(self._runner.global_config.envdir).joinpath(".cache") + path.mkdir(exist_ok=True) + return path + @property def interactive(self) -> bool: """Returns True if Nox is being run in an interactive session or False otherwise.""" diff --git a/tests/test_sessions.py b/tests/test_sessions.py index f12f02c7..3c949e64 100644 --- a/tests/test_sessions.py +++ b/tests/test_sessions.py @@ -99,15 +99,20 @@ def test_create_tmp_twice(self): def test_properties(self): session, runner = self.make_session_and_runner() + with tempfile.TemporaryDirectory() as root: + runner.global_config.envdir = root - assert session.name is runner.friendly_name - assert session.env is runner.venv.env - assert session.posargs == runner.global_config.posargs - assert session.virtualenv is runner.venv - assert session.bin_paths is runner.venv.bin_paths - assert session.bin is runner.venv.bin_paths[0] - assert session.python is runner.func.python - assert session.invoked_from is runner.global_config.invoked_from + assert session.name is runner.friendly_name + assert session.env is runner.venv.env + assert session.posargs == runner.global_config.posargs + assert session.virtualenv is runner.venv + assert session.bin_paths is runner.venv.bin_paths + assert session.bin is runner.venv.bin_paths[0] + assert session.python is runner.func.python + assert session.invoked_from is runner.global_config.invoked_from + assert session.cache_dir == Path(runner.global_config.envdir).joinpath( + ".cache" + ) def test_no_bin_paths(self): session, runner = self.make_session_and_runner()