From 249b60c0304cac200d2fc3fa48f6ad0434f3431f Mon Sep 17 00:00:00 2001 From: Ruslan Kuprieiev Date: Fri, 31 Aug 2018 02:18:20 +0300 Subject: [PATCH] remote: properly remove remote section Fixes #1075 Signed-off-by: Ruslan Kuprieiev --- dvc/command/remote.py | 30 +++++++++++++++++++++++++++++- tests/test_remote.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/dvc/command/remote.py b/dvc/command/remote.py index ecda2e4f4..08a5b4334 100644 --- a/dvc/command/remote.py +++ b/dvc/command/remote.py @@ -1,4 +1,6 @@ +import os import re +import configobj from dvc.config import Config from dvc.command.config import CmdConfig @@ -23,9 +25,35 @@ class CmdRemoteAdd(CmdConfig): class CmdRemoteRemove(CmdConfig): + def _remove_default(self, config_file, remote): + path = os.path.join(os.path.dirname(self.config_file), + config_file) + config = configobj.ConfigObj(path) + + core = config.get(Config.SECTION_CORE, None) + if core is None: + return + + default = core.get(Config.SECTION_CORE_REMOTE, None) + if default is None: + return + + if default == remote: + del config[Config.SECTION_CORE][Config.SECTION_CORE_REMOTE] + if len(config[Config.SECTION_CORE]) == 0: + del config[Config.SECTION_CORE] + + config.write() + def run(self): section = Config.SECTION_REMOTE_FMT.format(self.args.name) - return self.unset(section, Config.SECTION_REMOTE_URL) + ret = self.unset(section) + if ret != 0: + return ret + + self._remove_default(Config.CONFIG, self.args.name) + self._remove_default(Config.CONFIG_LOCAL, self.args.name) + return 0 class CmdRemoteModify(CmdConfig): diff --git a/tests/test_remote.py b/tests/test_remote.py index 31884b8ee..87237358a 100644 --- a/tests/test_remote.py +++ b/tests/test_remote.py @@ -1,4 +1,8 @@ +import os +import configobj + from dvc.main import main +from dvc.project import Project from dvc.command.remote import CmdRemoteAdd from dvc.exceptions import UnsupportedRemoteError from dvc.remote import Remote @@ -41,3 +45,30 @@ class TestRemote(TestDvc): def test_unsupported(self): with self.assertRaises(UnsupportedRemoteError): Remote(self.dvc, {Config.SECTION_REMOTE_URL: 'unsupported://url'}) + + +class TestRemoteRemoveDefault(TestDvc): + def test(self): + remote = 'mys3' + self.assertEqual(main(['remote', 'add', '--default', remote, 's3://bucket/name']), 0) + self.assertEqual(main(['remote', 'modify', remote, 'profile', 'default']), 0) + self.assertEqual(main(['config', '--local', 'core.remote', remote]), 0) + + config = configobj.ConfigObj(os.path.join(self.dvc.dvc_dir, Config.CONFIG)) + local_config = configobj.ConfigObj(os.path.join(self.dvc.dvc_dir, Config.CONFIG_LOCAL)) + self.assertEqual(config[Config.SECTION_CORE][Config.SECTION_CORE_REMOTE], remote) + self.assertEqual(local_config[Config.SECTION_CORE][Config.SECTION_CORE_REMOTE], remote) + + self.assertEqual(main(['remote', 'remove', remote]), 0) + config = configobj.ConfigObj(os.path.join(self.dvc.dvc_dir, Config.CONFIG)) + local_config = configobj.ConfigObj(os.path.join(self.dvc.dvc_dir, Config.CONFIG_LOCAL)) + section = Config.SECTION_REMOTE_FMT.format(remote) + self.assertTrue(section not in config.keys()) + + core = config.get(Config.SECTION_CORE, None) + if core is not None: + self.assertTrue(Config.SECTION_CORE_REMOTE not in core.keys()) + + core = local_config.get(Config.SECTION_CORE, None) + if core is not None: + self.assertTrue(Config.SECTION_CORE_REMOTE not in core.keys())