diff --git a/tests/dir_helpers.py b/tests/dir_helpers.py index 30fdfd2953..34e6dbf467 100644 --- a/tests/dir_helpers.py +++ b/tests/dir_helpers.py @@ -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