Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config: use wtree for non-repo configs #4341

Merged
merged 1 commit into from
Aug 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions dvc/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,25 +324,32 @@ def load(self, validate=True):
if not self["cache"].get("dir") and self.dvc_dir:
self["cache"]["dir"] = os.path.join(self.dvc_dir, "cache")

def _get_tree(self, level):
# NOTE: this might be a GitTree, which doesn't see things outside of
# the repo.
return self.tree if level == "repo" else self.wtree

def _load_config(self, level):
filename = self.files[level]
tree = self._get_tree(level)

if self.tree.exists(filename, use_dvcignore=False):
with self.tree.open(filename) as fobj:
if tree.exists(filename, use_dvcignore=False):
with tree.open(filename) as fobj:
conf_obj = configobj.ConfigObj(fobj)
else:
conf_obj = configobj.ConfigObj()
return _parse_remotes(_lower_keys(conf_obj.dict()))

def _save_config(self, level, conf_dict):
filename = self.files[level]
tree = self._get_tree(level)

logger.debug(f"Writing '{filename}'.")

self.tree.makedirs(os.path.dirname(filename))
tree.makedirs(os.path.dirname(filename))

config = configobj.ConfigObj(_pack_remotes(conf_dict))
with self.tree.open(filename, "wb") as fobj:
with tree.open(filename, "wb") as fobj:
config.write(fobj)
config.filename = filename

Expand Down
17 changes: 17 additions & 0 deletions tests/unit/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest

from dvc.config import Config
from dvc.tree.local import LocalTree


@pytest.mark.parametrize(
Expand All @@ -16,3 +17,19 @@
)
def test_to_relpath(path, expected):
assert Config._to_relpath(os.path.join(".", "config"), path) == expected


def test_get_tree(tmp_dir, scm):
tmp_dir.scm_gen("foo", "foo", commit="add foo")

tree = scm.get_tree("master")
config = Config(tree=tree)

assert config.tree == tree
assert config.wtree != tree
assert isinstance(config.wtree, LocalTree)

assert config._get_tree("repo") == tree
assert config._get_tree("local") == config.wtree
assert config._get_tree("global") == config.wtree
assert config._get_tree("system") == config.wtree