Skip to content

Commit

Permalink
Add a "shared cache" directory (#476)
Browse files Browse the repository at this point in the history
* 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 <cjolowicz@gmail.com>

* Update sessions.py

Use ".cache" instead of ".shared".

* Update test_sessions.py

Use ".cache" instead of ".shared".

Co-authored-by: Tom Fleet <tomfleet2018@gmail.com>
Co-authored-by: Claudio Jolowicz <cjolowicz@gmail.com>
  • Loading branch information
3 people committed Sep 15, 2021
1 parent 74f8793 commit 9ebae0a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
8 changes: 8 additions & 0 deletions nox/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import enum
import hashlib
import os
import pathlib
import re
import sys
import unicodedata
Expand Down Expand Up @@ -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."""
Expand Down
21 changes: 13 additions & 8 deletions tests/test_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 9ebae0a

Please sign in to comment.