diff --git a/dvc/command/config.py b/dvc/command/config.py index 079eee5ef9..99a153b46b 100644 --- a/dvc/command/config.py +++ b/dvc/command/config.py @@ -13,7 +13,11 @@ class CmdConfig(CmdBaseNoRepo): def __init__(self, args): super().__init__(args) - self.config = Config(validate=False) + # Verify repository existance when level requires to modify its config + repository_levels = [Config.LEVEL_REPO, Config.LEVEL_LOCAL] + verify_repo = self.args.level in repository_levels + + self.config = Config(validate=False, verify_repo=verify_repo) def run(self): section, opt = self.args.name.lower().strip().split(".", 1) diff --git a/dvc/config.py b/dvc/config.py index 0fd312aa14..e955a93be3 100644 --- a/dvc/config.py +++ b/dvc/config.py @@ -250,7 +250,7 @@ class Config(object): # pylint: disable=too-many-instance-attributes } COMPILED_SCHEMA = Schema(SCHEMA) - def __init__(self, dvc_dir=None, validate=True): + def __init__(self, dvc_dir=None, validate=True, verify_repo=False): self.dvc_dir = dvc_dir self.validate = validate @@ -260,6 +260,8 @@ def __init__(self, dvc_dir=None, validate=True): self.dvc_dir = os.path.join(Repo.find_dvc_dir()) except NotDvcRepoError: + if verify_repo: + raise self.dvc_dir = None else: self.dvc_dir = os.path.abspath(os.path.realpath(dvc_dir)) diff --git a/tests/func/test_config.py b/tests/func/test_config.py index f6cae78d3c..bfeaf0366e 100644 --- a/tests/func/test_config.py +++ b/tests/func/test_config.py @@ -1,5 +1,7 @@ import configobj +import pytest + from dvc.main import main from tests.basic_env import TestDvc @@ -83,3 +85,14 @@ def test_non_existing(self): ret = main(["config", "core.non_existing_field", "-u"]) self.assertEqual(ret, 251) + + +@pytest.mark.parametrize( + "cmd", + [ + ["config", "core.analytics", "false"], + ["config", "--local", "core.analytics", "false"], + ], +) +def test_commands_outside_dvc_repository(tmp_dir, cmd): + assert 253 == main(cmd)