From e8b1963f22657272105fc99c76c4292393a59958 Mon Sep 17 00:00:00 2001 From: "Mr. Outis" Date: Sat, 18 Jan 2020 22:24:01 -0600 Subject: [PATCH 1/3] remote: verify repository exists when level requires it Close #3191, #2309 --- dvc/command/remote.py | 7 +++++++ tests/func/test_remote.py | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/dvc/command/remote.py b/dvc/command/remote.py index 0655d440c5..3d9c4dc628 100644 --- a/dvc/command/remote.py +++ b/dvc/command/remote.py @@ -1,6 +1,7 @@ import argparse import logging +from dvc.repo import Repo from dvc.command.base import append_doc_link from dvc.command.base import fix_subparsers from dvc.command.config import CmdConfig @@ -14,6 +15,12 @@ def __init__(self, args): super().__init__(args) self.remote_config = RemoteConfig(self.config) + # Verify repository existance when level requires to modify its config + repository_levels = [self.config.LEVEL_REPO, self.config.LEVEL_LOCAL] + + if self.args.level in repository_levels: + Repo.find_dvc_dir() + class CmdRemoteAdd(CmdRemoteConfig): def run(self): diff --git a/tests/func/test_remote.py b/tests/func/test_remote.py index c6d1cffe9e..badbf5726b 100644 --- a/tests/func/test_remote.py +++ b/tests/func/test_remote.py @@ -257,3 +257,22 @@ def test_modify_missing_remote(dvc): with pytest.raises(ConfigError, match=r"unable to find remote section"): remote_config.modify("myremote", "gdrive_client_id", "xxx") + + +@pytest.mark.parametrize( + "cmd", + [ + ["remote", "add", "example", "https://example.com"], + ["remote", "add", "--local", "example", "https://example.com"], + ["remote", "modify", "example", "random", "value"], + ["remote", "modify", "--local", "example", "random", "value"], + ["remote", "remove", "example"], + ["remote", "remove", "--local", "example"], + ["remote", "list"], + ["remote", "list", "--local"], + ["remote", "default", "example"], + ["remote", "default", "--local", "example"], + ], +) +def test_commands_outside_dvc_repository(tmp_dir, cmd): + assert 253 == main(cmd) From e6db2c34e27ceb8c32fe7a63cb73311b1dc54c90 Mon Sep 17 00:00:00 2001 From: "Mr. Outis" Date: Sun, 19 Jan 2020 02:16:35 -0600 Subject: [PATCH 2/3] config: apply repository verification for some config levels --- dvc/command/config.py | 6 +++++- dvc/command/remote.py | 7 ------- dvc/config.py | 4 +++- tests/func/test_config.py | 13 +++++++++++++ 4 files changed, 21 insertions(+), 9 deletions(-) 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/command/remote.py b/dvc/command/remote.py index 3d9c4dc628..0655d440c5 100644 --- a/dvc/command/remote.py +++ b/dvc/command/remote.py @@ -1,7 +1,6 @@ import argparse import logging -from dvc.repo import Repo from dvc.command.base import append_doc_link from dvc.command.base import fix_subparsers from dvc.command.config import CmdConfig @@ -15,12 +14,6 @@ def __init__(self, args): super().__init__(args) self.remote_config = RemoteConfig(self.config) - # Verify repository existance when level requires to modify its config - repository_levels = [self.config.LEVEL_REPO, self.config.LEVEL_LOCAL] - - if self.args.level in repository_levels: - Repo.find_dvc_dir() - class CmdRemoteAdd(CmdRemoteConfig): def run(self): 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) From 7c090ae1e3544d3c22785ee390cf6f65cab2377e Mon Sep 17 00:00:00 2001 From: "Mr. Outis" Date: Mon, 20 Jan 2020 14:12:24 -0600 Subject: [PATCH 3/3] remote: remove left-over tests --- tests/func/test_remote.py | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/tests/func/test_remote.py b/tests/func/test_remote.py index badbf5726b..c6d1cffe9e 100644 --- a/tests/func/test_remote.py +++ b/tests/func/test_remote.py @@ -257,22 +257,3 @@ def test_modify_missing_remote(dvc): with pytest.raises(ConfigError, match=r"unable to find remote section"): remote_config.modify("myremote", "gdrive_client_id", "xxx") - - -@pytest.mark.parametrize( - "cmd", - [ - ["remote", "add", "example", "https://example.com"], - ["remote", "add", "--local", "example", "https://example.com"], - ["remote", "modify", "example", "random", "value"], - ["remote", "modify", "--local", "example", "random", "value"], - ["remote", "remove", "example"], - ["remote", "remove", "--local", "example"], - ["remote", "list"], - ["remote", "list", "--local"], - ["remote", "default", "example"], - ["remote", "default", "--local", "example"], - ], -) -def test_commands_outside_dvc_repository(tmp_dir, cmd): - assert 253 == main(cmd)