Skip to content

Commit

Permalink
cache created test dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
efiop committed Sep 23, 2020
1 parent 2ac65f7 commit d19929d
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions tests/dir_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,33 @@ class PosixTmpDir(TmpDir, pathlib.PurePosixPath):
pass


CACHE = {}


@pytest.fixture(scope="session")
def make_tmp_dir(tmp_path_factory, request):
def make(name, *, scm=False, dvc=False, **kwargs):
def make_tmp_dir(tmp_path_factory, request, worker_id):
def make(name, *, scm=False, dvc=False, subdir=False):
from dvc.repo import Repo
from dvc.scm.git import Git
from dvc.utils.fs import fs_copy

cache = CACHE.get((scm, dvc, subdir))
if not cache:
cache = tmp_path_factory.mktemp("dvc-test-cache" + worker_id)
TmpDir(cache).init(scm=scm, dvc=dvc, subdir=subdir)
CACHE[(scm, dvc, subdir)] = os.fspath(cache)
path = tmp_path_factory.mktemp(name) if isinstance(name, str) else name
for entry in os.listdir(cache):
# shutil.copytree's dirs_exist_ok is only available in >=3.8
fs_copy(os.path.join(cache, entry), os.path.join(path, entry))
new_dir = TmpDir(path)
new_dir.init(scm=scm, dvc=dvc, **kwargs)
str_path = os.fspath(new_dir)
if dvc:
new_dir.dvc = Repo(str_path)
if scm:
new_dir.scm = (
new_dir.dvc.scm if hasattr(new_dir, "dvc") else Git(str_path)
)
request.addfinalizer(new_dir.close)
return new_dir

Expand Down

0 comments on commit d19929d

Please sign in to comment.