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

remote: verify repository exists when level requires it #3194

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
6 changes: 5 additions & 1 deletion dvc/command/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion dvc/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Copy link
Author

@ghost ghost Jan 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a new parameter because I didn't want to mess up with any previous logic dealing with having an empty dvc_dir.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MrOutis This looks like a hack rather than a proper solution 🙂

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@efiop, could you explain? Not sure how to proceed with such observation 😅

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are hacking your way through the bug instead of solving the bug. This verify_repo shouldn't be an option at all. Config should always raise if you try to modify in-repo config when there isn't one.

self.dvc_dir = dvc_dir
self.validate = validate

Expand All @@ -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))
Expand Down
13 changes: 13 additions & 0 deletions tests/func/test_config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import configobj

import pytest

from dvc.main import main
from tests.basic_env import TestDvc

Expand Down Expand Up @@ -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)
19 changes: 19 additions & 0 deletions tests/func/test_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Solution is for config in general, but tests are for remote specifically, not nice 🙂Also not clear why are you testing CLI again and not API?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, that's a left over. Those tests are not relevant since they were replaced for the ones in test_config.py

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer testing the CLI in this case, it is more complete, but if you want, I use the API.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MrOutis CLI is a dumb wrapper for API. We always test API, so let's do the same here. If you want to check that CLI returns proper codes on corresponding exceptions, just add a unit test to tests/unit/command/.

"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)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

253 is returned when NotDvcRepoError is raised.