From ab691a47095d0c93d5027b4466b520d06cfd5e0b Mon Sep 17 00:00:00 2001 From: Holger Frey Date: Thu, 17 Aug 2023 12:16:16 +0200 Subject: [PATCH] added repo caching to noxfile --- noxfile.py | 45 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/noxfile.py b/noxfile.py index e472bc0..6a5f3bd 100644 --- a/noxfile.py +++ b/noxfile.py @@ -1,9 +1,42 @@ import pathlib import tempfile +import typing import nox +class RepoCache: + """class for caching checkouts of downstream repos + + By caching the checkouts for downstream test runs, + the nox invokation is faster and there is less load + on the git server and network. + """ + + _checkouts: typing.ClassVar = {} + + @classmethod + def clone(cls, session: nox.Session, repo: str) -> str: + """clones a git repo only once + + Arguments: + session: the nox session in use + repo: a git repo url + + Returns: + path to a temporary directory containing the + cloned git repo + """ + tmpdir = cls._checkouts.get(repo, None) + + if tmpdir is None: + tmpdir = tempfile.TemporaryDirectory() + session.run("git", "clone", repo, tmpdir.name, external=True) + cls._checkouts[repo] = tmpdir + + return tmpdir.name + + @nox.session(python=["3.9", "3.10", "3.11"]) def tests(session): session.install(".[test]") @@ -19,12 +52,10 @@ def tests(session): ], ) def downstream(session, repo, test_dir): - session.install(".") - - with tempfile.TemporaryDirectory() as tmpdir: - session.run("git", "clone", repo, tmpdir, external=True) + tmpdir = RepoCache.clone(session, repo) - session.install(f"{tmpdir}[test]") + session.install(".") + session.install(f"{tmpdir}[test]") - test_path = pathlib.Path(tmpdir) / test_dir - session.run("pytest", test_path) + test_path = pathlib.Path(tmpdir) / test_dir + session.run("pytest", test_path)