Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
1. add new command `dvc remote rename <old> <new>`.
2. add tests for this new command.
  • Loading branch information
karajan1001 committed May 13, 2020
1 parent d81b569 commit 505b69f
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
39 changes: 39 additions & 0 deletions dvc/command/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,34 @@ def run(self):
return 0


class CmdRemoteRename(CmdRemote):
def run(self):
conf = self.config.load_one(self.args.level)
self._check_exists(conf)

all_config = self.config.load_config_to_level("all")
if self.args.new in all_config.get("remote", {}):
raise ConfigError(
"Rename failed.Remote name {} already exists.".format(
{self.args.new}
)
)

with self.config.edit(self.args.level) as conf:
conf["remote"][self.args.new] = conf["remote"][self.args.name]
del conf["remote"][self.args.name]

for level in reversed(self.config.LEVELS):
with self.config.edit(level) as conf:
if conf["core"].get("remote") == self.args.name:
conf["core"]["remote"] = self.args.new

if level == self.args.level:
break

return 0


def add_parser(subparsers, parent_parser):
from dvc.command.config import parent_config_parser

Expand Down Expand Up @@ -224,3 +252,14 @@ def add_parser(subparsers, parent_parser):
"name", help="Name of the remote to remove."
)
remote_remove_parser.set_defaults(func=CmdRemoteRemove)
REMOTE_RENAME_HELP = "Rename a data remote"
remote_rename_parser = remote_subparsers.add_parser(
"rename",
parents=[parent_config_parser, parent_parser],
description=append_doc_link(REMOTE_RENAME_HELP, "remote/rename"),
help=REMOTE_RENAME_HELP,
formatter_class=argparse.RawDescriptionHelpFormatter,
)
remote_rename_parser.add_argument("name", help="Remote to be renamed")
remote_rename_parser.add_argument("new", help="New name of the remote")
remote_rename_parser.set_defaults(func=CmdRemoteRename)
71 changes: 71 additions & 0 deletions tests/func/test_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,74 @@ def test_remote_modify_default(dvc):

assert repo_config["core"]["remote"] == remote_repo
assert local_config["core"]["remote"] == remote_local


def test_remote_rename(dvc):
remote_name = "drive"
remote_url = "gdrive://test/test"
new_name = "new"
other_name = "other"
# prepare
assert main(["remote", "add", remote_name, remote_url]) == 0
config = dvc.config.load_one("repo")
assert config["remote"][remote_name]["url"] == remote_url
assert new_name not in config.get("remote", {})

# rename failed
assert main(["remote", "rename", remote_name]) == 254
assert main(["remote", "rename", new_name, other_name]) == 251
config = dvc.config.load_one("repo")
assert config["remote"][remote_name]["url"] == remote_url
assert new_name not in config.get("remote", {})

# rename success
assert main(["remote", "rename", remote_name, new_name]) == 0
config = dvc.config.load_one("repo")
assert remote_name not in config.get("remote", {})
assert config["remote"][new_name]["url"] == remote_url


def test_remote_duplicated(dvc):
remote_name = "drive"
remote_url = "gdrive://test/test"
used_name = "overlap"
another_url = "gdrive://test/test1"
# prepare
assert main(["remote", "add", remote_name, remote_url]) == 0
assert main(["remote", "add", "--local", used_name, another_url]) == 0
config = dvc.config.load_one("repo")
assert config["remote"][remote_name]["url"] == remote_url
local_config = dvc.config.load_one("local")
assert local_config["remote"][used_name]["url"] == another_url

# rename duplicated
assert main(["remote", "rename", remote_name, used_name]) == 251
config = dvc.config.load_one("repo")
assert config["remote"][remote_name]["url"] == remote_url
local_config = dvc.config.load_one("local")
assert local_config["remote"][used_name]["url"] == another_url


def test_remote_default(dvc):
remote_name = "drive"
remote_url = "gdrive://test/test"
new_name = "new"
# prepare
assert main(["remote", "add", "-d", remote_name, remote_url]) == 0
assert main(["remote", "default", "--local", remote_name]) == 0
config = dvc.config.load_one("repo")
assert config["core"]["remote"] == remote_name
assert config["remote"][remote_name]["url"] == remote_url
assert new_name not in config.get("remote", {})
local_config = dvc.config.load_one("local")
assert local_config["core"]["remote"] == remote_name

# rename success
assert main(["remote", "rename", remote_name, new_name]) == 0
config = dvc.config.load_one("repo")
assert remote_name not in config.get("remote", {})
assert config["core"]["remote"] == new_name
assert config["remote"][new_name]["url"] == remote_url
assert remote_name not in config.get("remote", {})
local_config = dvc.config.load_one("local")
assert local_config["core"]["remote"] == new_name

0 comments on commit 505b69f

Please sign in to comment.