mirror of
https://github.com/deadc0de6/dotdrop.git
synced 2026-02-11 12:34:16 +00:00
Adding tests for overrided settings in imported_configs
This commit is contained in:
@@ -445,12 +445,14 @@ class Cfg:
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
def _merge_cfg(self, config_path):
|
def _merge_cfg(self, config_path):
|
||||||
|
# Parsing external config file
|
||||||
try:
|
try:
|
||||||
ext_config = Cfg(self._abs_path(config_path))
|
ext_config = Cfg(self._abs_path(config_path))
|
||||||
except ValueError:
|
except ValueError:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
'external config file not found: {}'.format(config_path))
|
'external config file not found: {}'.format(config_path))
|
||||||
|
|
||||||
|
# Merging lists in config settings
|
||||||
list_settings = (
|
list_settings = (
|
||||||
(k, v)
|
(k, v)
|
||||||
for k, v in ext_config.lnk_settings.items()
|
for k, v in ext_config.lnk_settings.items()
|
||||||
@@ -459,6 +461,7 @@ class Cfg:
|
|||||||
for k, v in list_settings:
|
for k, v in list_settings:
|
||||||
self.lnk_settings[k] += v
|
self.lnk_settings[k] += v
|
||||||
|
|
||||||
|
# Merging dictionaries
|
||||||
ext_members = (
|
ext_members = (
|
||||||
(name, member)
|
(name, member)
|
||||||
for name, member in inspect.getmembers(ext_config, is_dict)
|
for name, member in inspect.getmembers(ext_config, is_dict)
|
||||||
@@ -469,6 +472,16 @@ class Cfg:
|
|||||||
ext_member.update(self_member)
|
ext_member.update(self_member)
|
||||||
setattr(self, name, ext_member)
|
setattr(self, name, ext_member)
|
||||||
|
|
||||||
|
# Merging variables
|
||||||
|
self_content, ext_content = self.content, ext_config.content
|
||||||
|
(ext_content[self.key_variables]
|
||||||
|
.update(self_content[self.key_variables]))
|
||||||
|
self.content[self.key_variables] = ext_content[self.key_variables]
|
||||||
|
(ext_content[self.key_dynvariables]
|
||||||
|
.update(self_content[self.key_dynvariables]))
|
||||||
|
self.content[self.key_dynvariables] = \
|
||||||
|
ext_content[self.key_dynvariables]
|
||||||
|
|
||||||
def _load_ext_variables(self, paths, profile=None):
|
def _load_ext_variables(self, paths, profile=None):
|
||||||
"""load external variables"""
|
"""load external variables"""
|
||||||
variables = {}
|
variables = {}
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ class SubsetTestCase(TestCase):
|
|||||||
supValue = sup[subKey]
|
supValue = sup[subKey]
|
||||||
|
|
||||||
if isinstance(subValue, str):
|
if isinstance(subValue, str):
|
||||||
self.assertEquals(subValue, supValue)
|
self.assertEqual(subValue, supValue)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if isinstance(subValue, dict):
|
if isinstance(subValue, dict):
|
||||||
@@ -41,7 +41,7 @@ class SubsetTestCase(TestCase):
|
|||||||
for subItem in subValue
|
for subItem in subValue
|
||||||
))
|
))
|
||||||
except TypeError:
|
except TypeError:
|
||||||
self.assertEquals(subValue, supValue)
|
self.assertEqual(subValue, supValue)
|
||||||
|
|
||||||
|
|
||||||
def clean(path):
|
def clean(path):
|
||||||
|
|||||||
@@ -337,8 +337,7 @@ profiles:
|
|||||||
importing_cfg.lnk_profiles)
|
importing_cfg.lnk_profiles)
|
||||||
|
|
||||||
# test dotfiles
|
# test dotfiles
|
||||||
self.assertIsSubset(imported_cfg.dotfiles,
|
self.assertIsSubset(imported_cfg.dotfiles, importing_cfg.dotfiles)
|
||||||
importing_cfg.dotfiles)
|
|
||||||
|
|
||||||
# test actions
|
# test actions
|
||||||
self.assertIsSubset(imported_cfg.actions['pre'],
|
self.assertIsSubset(imported_cfg.actions['pre'],
|
||||||
@@ -347,44 +346,84 @@ profiles:
|
|||||||
importing_cfg.actions['post'])
|
importing_cfg.actions['post'])
|
||||||
|
|
||||||
# test transactions
|
# test transactions
|
||||||
self.assertIsSubset(imported_cfg.trans_r,
|
self.assertIsSubset(imported_cfg.trans_r, importing_cfg.trans_r)
|
||||||
importing_cfg.trans_r)
|
self.assertIsSubset(imported_cfg.trans_w, importing_cfg.trans_w)
|
||||||
self.assertIsSubset(imported_cfg.trans_w,
|
|
||||||
importing_cfg.trans_w)
|
|
||||||
|
|
||||||
# test transactions
|
# test variables
|
||||||
self.assertIsSubset(imported_cfg.trans_r,
|
imported_vars = {
|
||||||
importing_cfg.trans_r)
|
k: v
|
||||||
self.assertIsSubset(imported_cfg.trans_w,
|
for k, v in imported_cfg.get_variables(None).items()
|
||||||
importing_cfg.trans_w)
|
if not k.startswith('_')
|
||||||
|
}
|
||||||
|
importing_vars = {
|
||||||
|
k: v
|
||||||
|
for k, v in importing_cfg.get_variables(None).items()
|
||||||
|
if not k.startswith('_')
|
||||||
|
}
|
||||||
|
self.assertIsSubset(imported_vars, importing_vars)
|
||||||
|
|
||||||
# test prodots
|
# test prodots
|
||||||
self.assertIsSubset(imported_cfg.prodots,
|
self.assertIsSubset(imported_cfg.prodots, importing_cfg.prodots)
|
||||||
importing_cfg.prodots)
|
|
||||||
|
|
||||||
# test ext_variables
|
# test ext_variables (reduntant, but still)
|
||||||
self.assertIsSubset(imported_cfg.ext_variables,
|
self.assertIsSubset(imported_cfg.ext_variables,
|
||||||
importing_cfg.ext_variables)
|
importing_cfg.ext_variables)
|
||||||
|
|
||||||
# test ext_dynvariables
|
# test ext_dynvariables (reduntant, but still)
|
||||||
self.assertIsSubset(imported_cfg.ext_dynvariables,
|
self.assertIsSubset(imported_cfg.ext_dynvariables,
|
||||||
importing_cfg.ext_dynvariables)
|
importing_cfg.ext_dynvariables)
|
||||||
|
|
||||||
def test_import_configs_override(self):
|
def test_import_configs_override(self):
|
||||||
|
"""Test import_configs when some config keys overlap."""
|
||||||
|
tmp = get_tempdir()
|
||||||
|
self.assertTrue(os.path.exists(tmp))
|
||||||
|
self.addCleanup(clean, tmp)
|
||||||
|
|
||||||
|
vars_ed = {
|
||||||
|
'variables': {
|
||||||
|
'a_var': '33',
|
||||||
|
},
|
||||||
|
'dynvariables': {
|
||||||
|
'a_dynvar': 'echo 33',
|
||||||
|
},
|
||||||
|
}
|
||||||
|
vars_ing = {
|
||||||
|
'variables': {
|
||||||
|
'a_var': 'dd',
|
||||||
|
},
|
||||||
|
'dynvariables': {
|
||||||
|
'a_dynvar': 'echo dd',
|
||||||
|
},
|
||||||
|
}
|
||||||
|
vars_ed_file = create_yaml_keyval(vars_ed, tmp)
|
||||||
|
vars_ing_file = create_yaml_keyval(vars_ing, tmp)
|
||||||
|
|
||||||
imported = {
|
imported = {
|
||||||
'config': {
|
'config': {
|
||||||
'dotpath': 'imported',
|
'dotpath': 'imported',
|
||||||
'backup': False,
|
'backup': False,
|
||||||
|
'import_variables': [vars_ed_file],
|
||||||
},
|
},
|
||||||
'dotfiles': {
|
'dotfiles': {
|
||||||
'f_vimrc': {'dst': '~/.vimrc', 'src': 'vimrc'},
|
'f_vimrc': {'dst': '~/.vimrc', 'src': 'vimrc'},
|
||||||
|
'f_xinitrc': {'dst': '~/.xinitrc', 'src': 'xinitrc',
|
||||||
|
'link': 'link'},
|
||||||
},
|
},
|
||||||
'profiles': {
|
'profiles': {
|
||||||
'host1': {
|
'host1': {
|
||||||
'dotfiles': ['f_vimrc'],
|
'dotfiles': ['f_vimrc'],
|
||||||
},
|
},
|
||||||
|
'host2': {
|
||||||
|
'dotfiles': ['f_xinitrc'],
|
||||||
|
},
|
||||||
},
|
},
|
||||||
'actions': {
|
'actions': {
|
||||||
|
'pre': {
|
||||||
|
'a_pre_log': 'echo pre 2',
|
||||||
|
},
|
||||||
|
'post': {
|
||||||
|
'a_post_log': 'echo post 2',
|
||||||
|
},
|
||||||
'a_log': 'echo 2',
|
'a_log': 'echo 2',
|
||||||
},
|
},
|
||||||
'trans': {
|
'trans': {
|
||||||
@@ -404,6 +443,7 @@ profiles:
|
|||||||
'config': {
|
'config': {
|
||||||
'dotpath': 'importing',
|
'dotpath': 'importing',
|
||||||
'backup': True,
|
'backup': True,
|
||||||
|
'import_variables': [vars_ing_file],
|
||||||
},
|
},
|
||||||
'dotfiles': {
|
'dotfiles': {
|
||||||
'f_xinitrc': {'dst': '~/.xinitrc', 'src': 'xinitrc'},
|
'f_xinitrc': {'dst': '~/.xinitrc', 'src': 'xinitrc'},
|
||||||
@@ -415,6 +455,12 @@ profiles:
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
'actions': {
|
'actions': {
|
||||||
|
'pre': {
|
||||||
|
'a_pre_log': 'echo pre a',
|
||||||
|
},
|
||||||
|
'post': {
|
||||||
|
'a_post_log': 'echo post a',
|
||||||
|
},
|
||||||
'a_log': 'echo a',
|
'a_log': 'echo a',
|
||||||
},
|
},
|
||||||
'trans': {
|
'trans': {
|
||||||
@@ -431,6 +477,103 @@ profiles:
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# create the imported base config file
|
||||||
|
imported_path = create_fake_config(tmp,
|
||||||
|
configname=self.CONFIG_NAME_2,
|
||||||
|
**imported['config'])
|
||||||
|
# create the importing base config file
|
||||||
|
importing_path = create_fake_config(tmp,
|
||||||
|
configname=self.CONFIG_NAME,
|
||||||
|
import_configs=(imported_path,),
|
||||||
|
**importing['config'])
|
||||||
|
|
||||||
|
# edit the imported config
|
||||||
|
populate_fake_config(imported_path, **{
|
||||||
|
k: v
|
||||||
|
for k, v in imported.items()
|
||||||
|
if k != 'config'
|
||||||
|
})
|
||||||
|
|
||||||
|
# edit the importing config
|
||||||
|
populate_fake_config(importing_path, **{
|
||||||
|
k: v
|
||||||
|
for k, v in importing.items()
|
||||||
|
if k != 'config'
|
||||||
|
})
|
||||||
|
|
||||||
|
# do the tests
|
||||||
|
importing_cfg = Cfg(importing_path)
|
||||||
|
imported_cfg = Cfg(imported_path)
|
||||||
|
self.assertIsNotNone(importing_cfg)
|
||||||
|
self.assertIsNotNone(imported_cfg)
|
||||||
|
|
||||||
|
# test settings
|
||||||
|
self.assertTrue(importing_cfg.lnk_settings['dotpath']
|
||||||
|
.endswith(importing['config']['dotpath']))
|
||||||
|
self.assertEqual(importing_cfg.lnk_settings['backup'],
|
||||||
|
importing['config']['backup'])
|
||||||
|
|
||||||
|
# test profiles
|
||||||
|
self.assertIsSubset(imported_cfg.lnk_profiles,
|
||||||
|
importing_cfg.lnk_profiles)
|
||||||
|
|
||||||
|
# test dotfiles
|
||||||
|
self.assertEqual(importing_cfg.dotfiles['f_vimrc'],
|
||||||
|
imported_cfg.dotfiles['f_vimrc'])
|
||||||
|
self.assertNotEqual(importing_cfg.dotfiles['f_xinitrc'],
|
||||||
|
imported_cfg.dotfiles['f_xinitrc'])
|
||||||
|
|
||||||
|
# test actions
|
||||||
|
self.assertFalse(any(
|
||||||
|
(imported_cfg.actions['pre'][key]
|
||||||
|
== importing_cfg.actions['pre'][key])
|
||||||
|
for key in imported_cfg.actions['pre']
|
||||||
|
))
|
||||||
|
self.assertFalse(any(
|
||||||
|
(imported_cfg.actions['post'][key]
|
||||||
|
== importing_cfg.actions['post'][key])
|
||||||
|
for key in imported_cfg.actions['post']
|
||||||
|
))
|
||||||
|
|
||||||
|
# test transactions
|
||||||
|
self.assertFalse(any(
|
||||||
|
imported_cfg.trans_r[key] == importing_cfg.trans_r[key]
|
||||||
|
for key in imported_cfg.trans_r
|
||||||
|
))
|
||||||
|
self.assertFalse(any(
|
||||||
|
imported_cfg.trans_w[key] == importing_cfg.trans_w[key]
|
||||||
|
for key in imported_cfg.trans_w
|
||||||
|
))
|
||||||
|
|
||||||
|
# test variables
|
||||||
|
imported_vars = imported_cfg.get_variables(None)
|
||||||
|
self.assertFalse(any(
|
||||||
|
imported_vars[k] == v
|
||||||
|
for k, v in importing_cfg.get_variables(None).items()
|
||||||
|
if not k.startswith('_')
|
||||||
|
))
|
||||||
|
|
||||||
|
# test prodots
|
||||||
|
self.assertEqual(imported_cfg.prodots['host1'],
|
||||||
|
importing_cfg.prodots['host1'])
|
||||||
|
self.assertNotEqual(imported_cfg.prodots['host2'],
|
||||||
|
importing_cfg.prodots['host2'])
|
||||||
|
self.assertTrue(set(imported_cfg.prodots['host1'])
|
||||||
|
< set(importing_cfg.prodots['host2']))
|
||||||
|
|
||||||
|
# test ext_variables (reduntant, but still)
|
||||||
|
self.assertFalse(any(
|
||||||
|
imported_cfg.ext_variables[key] == importing_cfg.ext_variables[key]
|
||||||
|
for key in imported_cfg.ext_variables
|
||||||
|
))
|
||||||
|
|
||||||
|
# test ext_dynvariables (reduntant, but still)
|
||||||
|
self.assertFalse(any(
|
||||||
|
(imported_cfg.ext_dynvariables[key]
|
||||||
|
== importing_cfg.ext_dynvariables[key])
|
||||||
|
for key in imported_cfg.ext_dynvariables
|
||||||
|
))
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|||||||
Reference in New Issue
Block a user