diff --git a/dvc/repo/__init__.py b/dvc/repo/__init__.py index c857a13c7c..8078a6d811 100644 --- a/dvc/repo/__init__.py +++ b/dvc/repo/__init__.py @@ -146,7 +146,6 @@ def __init__( # noqa: PLR0915 scm: Optional[Union["Git", "NoSCM"]] = None, ): from dvc.cachemgr import CacheManager - from dvc.config import Config from dvc.data_cloud import DataCloud from dvc.fs import GitFileSystem, LocalFileSystem, localfs from dvc.lock import LockNoop, make_lock @@ -163,6 +162,7 @@ def __init__( # noqa: PLR0915 self._fs_conf = {"repo_factory": repo_factory} self._fs = fs or localfs self._scm = scm + self._config = config self._data_index = None if rev and not fs: @@ -182,7 +182,6 @@ def __init__( # noqa: PLR0915 scm=scm, ) - self.config: Config = Config(self.dvc_dir, fs=self.fs, config=config) self._uninitialized = uninitialized # used by DVCFileSystem to determine if it should traverse subrepos @@ -234,6 +233,12 @@ def __init__( # noqa: PLR0915 def __str__(self): return self.url or self.root_dir + @cached_property + def config(self): + from dvc.config import Config + + return Config(self.dvc_dir, fs=self.fs, config=self._config) + @cached_property def local_dvc_dir(self): from dvc.fs import GitFileSystem, LocalFileSystem @@ -629,6 +634,7 @@ def _reset(self): self.__dict__.pop("dvcignore", None) self.__dict__.pop("dvcfs", None) self.__dict__.pop("datafs", None) + self.__dict__.pop("config", None) def __enter__(self): return self diff --git a/dvc/repo/brancher.py b/dvc/repo/brancher.py index 93768edfd1..75a8283fae 100644 --- a/dvc/repo/brancher.py +++ b/dvc/repo/brancher.py @@ -67,6 +67,7 @@ def brancher( # noqa: E302 saved_fs = self.fs saved_root = self.root_dir + saved_dvc_dir = self.dvc_dir scm = self.scm @@ -101,6 +102,8 @@ def brancher( # noqa: E302 finally: self.fs = saved_fs self.root_dir = saved_root + self.dvc_dir = saved_dvc_dir + self._reset() # pylint: disable=protected-access def _switch_fs( @@ -125,6 +128,8 @@ def _switch_fs( repo.fs = fs repo.root_dir = root_dir + repo.dvc_dir = fs.path.join(root_dir, repo.DVC_DIR) + repo._reset() # pylint: disable=protected-access if cwd_parts: cwd = repo.fs.path.join("/", *cwd_parts) @@ -149,9 +154,12 @@ def switch(repo: "Repo", rev: str) -> Iterator[str]: saved_fs = repo.fs saved_root = repo.root_dir + saved_dvc_dir = repo.dvc_dir try: _switch_fs(repo, rev, repo_root_parts, cwd_parts) yield rev finally: repo.fs = saved_fs repo.root_dir = saved_root + repo.dvc_dir = saved_dvc_dir + repo._reset() # pylint: disable=protected-access diff --git a/tests/func/test_data_cloud.py b/tests/func/test_data_cloud.py index 644d7bc75c..000d6ab20e 100644 --- a/tests/func/test_data_cloud.py +++ b/tests/func/test_data_cloud.py @@ -203,7 +203,8 @@ def test_verify_hashes(tmp_dir, scm, dvc, mocker, tmp_path_factory, local_remote # Removing cache will invalidate existing state entries dvc.cache.local.clear() - dvc.config["remote"]["upstream"]["verify"] = True + with dvc.config.edit() as conf: + conf["remote"]["upstream"]["verify"] = True dvc.pull() assert hash_spy.call_count == 10 diff --git a/tests/func/test_gc.py b/tests/func/test_gc.py index c281599d20..a7e96b91d9 100644 --- a/tests/func/test_gc.py +++ b/tests/func/test_gc.py @@ -196,11 +196,7 @@ def test_gc_cloud_positive(tmp_dir, dvc, tmp_path_factory, local_remote): assert main(["gc", "-vf", flag]) == 0 -def test_gc_cloud_remove_order(tmp_dir, scm, dvc, tmp_path_factory, mocker): - storage = os.fspath(tmp_path_factory.mktemp("test_remote_base")) - dvc.config["remote"]["local_remote"] = {"url": storage} - dvc.config["core"]["remote"] = "local_remote" - +def test_gc_cloud_remove_order(tmp_dir, scm, dvc, mocker, local_remote): (standalone, dir1, dir2) = tmp_dir.dvc_gen( { "file1": "standalone", @@ -321,11 +317,7 @@ def test_date(tmp_dir, scm, dvc): ) # "modified, again" -def test_gc_not_in_remote(tmp_dir, scm, dvc, tmp_path_factory, mocker): - storage = os.fspath(tmp_path_factory.mktemp("test_remote_base")) - dvc.config["remote"]["local_remote"] = {"url": storage} - dvc.config["core"]["remote"] = "local_remote" - +def test_gc_not_in_remote(tmp_dir, scm, dvc, mocker, local_remote): (standalone, dir1, dir2) = tmp_dir.dvc_gen( { "file1": "standalone", @@ -358,12 +350,9 @@ def test_gc_not_in_remote(tmp_dir, scm, dvc, tmp_path_factory, mocker): ) -def test_gc_not_in_remote_remote_arg(tmp_dir, scm, dvc, tmp_path_factory, mocker): - storage = os.fspath(tmp_path_factory.mktemp("test_remote_base")) - dvc.config["remote"]["local_remote"] = {"url": storage} - dvc.config["core"]["remote"] = "local_remote" - other_storage = os.fspath(tmp_path_factory.mktemp("test_remote_other")) - dvc.config["remote"]["other_remote"] = {"url": other_storage} +def test_gc_not_in_remote_remote_arg(tmp_dir, scm, dvc, mocker, make_remote): + make_remote("local_remote", typ="local") + make_remote("other_remote", typ="local", default=False) tmp_dir.dvc_gen( { @@ -385,15 +374,9 @@ def test_gc_not_in_remote_remote_arg(tmp_dir, scm, dvc, tmp_path_factory, mocker assert len(mocked_remove.mock_calls) == 3 -def test_gc_not_in_remote_with_remote_field( - tmp_dir, scm, dvc, tmp_path_factory, mocker -): - storage = os.fspath(tmp_path_factory.mktemp("test_remote_base")) - dvc.config["remote"]["local_remote"] = {"url": storage} - dvc.config["core"]["remote"] = "local_remote" - - other_storage = os.fspath(tmp_path_factory.mktemp("test_remote_other")) - dvc.config["remote"]["other_remote"] = {"url": other_storage} +def test_gc_not_in_remote_with_remote_field(tmp_dir, scm, dvc, mocker, make_remote): + make_remote("local_remote", typ="local") + make_remote("other_remote", typ="local", default=False) text = textwrap.dedent( """\ @@ -420,12 +403,9 @@ def test_gc_not_in_remote_cloud(tmp_dir, scm, dvc): dvc.gc(workspace=True, not_in_remote=True, cloud=True) -def test_gc_cloud_remote_field(tmp_dir, scm, dvc, tmp_path_factory, mocker): - storage = os.fspath(tmp_path_factory.mktemp("test_remote_base")) - dvc.config["remote"]["local_remote"] = {"url": storage} - dvc.config["core"]["remote"] = "local_remote" - other_storage = os.fspath(tmp_path_factory.mktemp("test_remote_other")) - dvc.config["remote"]["other_remote"] = {"url": other_storage} +def test_gc_cloud_remote_field(tmp_dir, scm, dvc, mocker, make_remote): + make_remote("local_remote", typ="local") + make_remote("other_remote", typ="local", default=False) text = textwrap.dedent( """\