mirror of
https://github.com/deadc0de6/dotdrop.git
synced 2026-02-08 10:49:17 +00:00
refactor the parsing
This commit is contained in:
@@ -171,8 +171,9 @@ def get_dotfile_from_yaml(dic, path):
|
||||
"""Return the dotfile from the yaml dictionary"""
|
||||
# path is not the file in dotpath but on the FS
|
||||
dotfiles = dic['dotfiles']
|
||||
src = get_path_strip_version(path)
|
||||
return [d for d in dotfiles.values() if d['src'] == src][0]
|
||||
# src = get_path_strip_version(path)
|
||||
dotfile = [d for d in dotfiles.values() if d['dst'] == path][0]
|
||||
return dotfile
|
||||
|
||||
|
||||
def yaml_dashed_list(items, indent=0):
|
||||
@@ -256,10 +257,10 @@ def file_in_yaml(yaml_file, path, link=False):
|
||||
|
||||
dotfiles = yaml_conf['dotfiles'].values()
|
||||
|
||||
in_src = strip in (x['src'] for x in dotfiles)
|
||||
in_src = any([x['src'].endswith(strip) for x in dotfiles])
|
||||
in_dst = path in (os.path.expanduser(x['dst']) for x in dotfiles)
|
||||
|
||||
if link:
|
||||
has_link = get_dotfile_from_yaml(yaml_conf, path)['link']
|
||||
has_link = 'link' in get_dotfile_from_yaml(yaml_conf, path)
|
||||
return in_src and in_dst and has_link
|
||||
return in_src and in_dst
|
||||
|
||||
@@ -33,7 +33,7 @@ class TestImport(unittest.TestCase):
|
||||
self.assertTrue(os.path.exists(path))
|
||||
content = ''
|
||||
with open(path, 'r') as f:
|
||||
content = yaml.load(f)
|
||||
content = yaml.safe_load(f)
|
||||
return content
|
||||
|
||||
def assert_file(self, path, o, profile):
|
||||
@@ -45,7 +45,7 @@ class TestImport(unittest.TestCase):
|
||||
|
||||
def assert_in_yaml(self, path, dic, link=False):
|
||||
"""Make sure "path" is in the "dic" representing the yaml file"""
|
||||
self.assertTrue(file_in_yaml(dic, path, link))
|
||||
self.assertTrue(file_in_yaml(dic, path, link=link))
|
||||
|
||||
def test_import(self):
|
||||
"""Test the import function"""
|
||||
@@ -117,7 +117,7 @@ class TestImport(unittest.TestCase):
|
||||
o = load_options(confpath, profile)
|
||||
|
||||
# test dotfiles in config class
|
||||
self.assertTrue(profile in o.profiles)
|
||||
self.assertTrue(profile in [p.key for p in o.profiles])
|
||||
self.assert_file(dotfile1, o, profile)
|
||||
self.assert_file(dotfile2, o, profile)
|
||||
self.assert_file(dotfile3, o, profile)
|
||||
@@ -218,9 +218,10 @@ class TestImport(unittest.TestCase):
|
||||
self.assertTrue(os.path.exists(dotdrop_home))
|
||||
self.addCleanup(clean, dotdrop_home)
|
||||
|
||||
dotpath_ed = 'imported'
|
||||
imported = {
|
||||
'config': {
|
||||
'dotpath': 'imported',
|
||||
'dotpath': dotpath_ed,
|
||||
},
|
||||
'dotfiles': {},
|
||||
'profiles': {
|
||||
@@ -250,9 +251,10 @@ class TestImport(unittest.TestCase):
|
||||
'dv_log_ed': 'echo 5',
|
||||
},
|
||||
}
|
||||
dotpath_ing = 'importing'
|
||||
importing = {
|
||||
'config': {
|
||||
'dotpath': 'importing',
|
||||
'dotpath': dotpath_ing,
|
||||
},
|
||||
'dotfiles': {},
|
||||
'profiles': {
|
||||
@@ -293,7 +295,7 @@ class TestImport(unittest.TestCase):
|
||||
# create the importing base config file
|
||||
importing_path = create_fake_config(dotdrop_home,
|
||||
configname='config.yaml',
|
||||
import_configs=('config-*.yaml',),
|
||||
import_configs=['config-2.yaml'],
|
||||
**importing['config'])
|
||||
|
||||
# edit the imported config
|
||||
@@ -326,8 +328,10 @@ class TestImport(unittest.TestCase):
|
||||
y = self.load_yaml(imported_path)
|
||||
|
||||
# testing dotfiles
|
||||
self.assertTrue(all(file_in_yaml(y, df) for df in dotfiles_ed))
|
||||
self.assertFalse(any(file_in_yaml(y, df) for df in dotfiles_ing))
|
||||
self.assertTrue(all(file_in_yaml(y, df)
|
||||
for df in dotfiles_ed))
|
||||
self.assertFalse(any(file_in_yaml(y, df)
|
||||
for df in dotfiles_ing))
|
||||
|
||||
# testing profiles
|
||||
profiles = y['profiles'].keys()
|
||||
@@ -355,7 +359,7 @@ class TestImport(unittest.TestCase):
|
||||
self.assertFalse(any(t.endswith('ing') for t in transformations))
|
||||
|
||||
# testing variables
|
||||
variables = y['variables'].keys()
|
||||
variables = self._remove_priv_vars(y['variables'].keys())
|
||||
self.assertTrue(all(v.endswith('ed') for v in variables))
|
||||
self.assertFalse(any(v.endswith('ing') for v in variables))
|
||||
dyn_variables = y['dynvariables'].keys()
|
||||
@@ -366,8 +370,10 @@ class TestImport(unittest.TestCase):
|
||||
y = self.load_yaml(importing_path)
|
||||
|
||||
# testing dotfiles
|
||||
self.assertTrue(all(file_in_yaml(y, df) for df in dotfiles_ing))
|
||||
self.assertFalse(any(file_in_yaml(y, df) for df in dotfiles_ed))
|
||||
self.assertTrue(all(file_in_yaml(y, df)
|
||||
for df in dotfiles_ing))
|
||||
self.assertFalse(any(file_in_yaml(y, df)
|
||||
for df in dotfiles_ed))
|
||||
|
||||
# testing profiles
|
||||
profiles = y['profiles'].keys()
|
||||
@@ -395,13 +401,19 @@ class TestImport(unittest.TestCase):
|
||||
self.assertFalse(any(t.endswith('ed') for t in transformations))
|
||||
|
||||
# testing variables
|
||||
variables = y['variables'].keys()
|
||||
variables = self._remove_priv_vars(y['variables'].keys())
|
||||
self.assertTrue(all(v.endswith('ing') for v in variables))
|
||||
self.assertFalse(any(v.endswith('ed') for v in variables))
|
||||
dyn_variables = y['dynvariables'].keys()
|
||||
self.assertTrue(all(dv.endswith('ing') for dv in dyn_variables))
|
||||
self.assertFalse(any(dv.endswith('ed') for dv in dyn_variables))
|
||||
|
||||
def _remove_priv_vars(self, variables_keys):
|
||||
variables = [v for v in variables_keys if not v.startswith('_')]
|
||||
if 'profile' in variables:
|
||||
variables.remove('profile')
|
||||
return variables
|
||||
|
||||
|
||||
def main():
|
||||
unittest.main()
|
||||
|
||||
@@ -9,7 +9,7 @@ import unittest
|
||||
from unittest.mock import MagicMock, patch
|
||||
import filecmp
|
||||
|
||||
from dotdrop.config import Cfg
|
||||
from dotdrop.cfg_aggregator import CfgAggregator as Cfg
|
||||
from tests.helpers import (clean, create_dir, create_fake_config,
|
||||
create_random_file, get_string, get_tempdir,
|
||||
load_options, populate_fake_config)
|
||||
@@ -89,7 +89,7 @@ exec bspwm
|
||||
f1, c1 = create_random_file(tmp)
|
||||
dst1 = os.path.join(dst, get_string(6))
|
||||
d1 = Dotfile(get_string(5), dst1, os.path.basename(f1))
|
||||
# fake a print
|
||||
# fake a __str__
|
||||
self.assertTrue(str(d1) != '')
|
||||
f2, c2 = create_random_file(tmp)
|
||||
dst2 = os.path.join(dst, get_string(6))
|
||||
@@ -178,7 +178,7 @@ exec bspwm
|
||||
dotfiles = [d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, ddot]
|
||||
self.fake_config(confpath, dotfiles,
|
||||
profile, tmp, [act1], [tr])
|
||||
conf = Cfg(confpath)
|
||||
conf = Cfg(confpath, profile)
|
||||
self.assertTrue(conf is not None)
|
||||
|
||||
# install them
|
||||
@@ -305,7 +305,7 @@ exec bspwm
|
||||
# create the importing base config file
|
||||
importing_path = create_fake_config(tmp,
|
||||
configname='config.yaml',
|
||||
import_configs=('config-*.yaml',),
|
||||
import_configs=['config-2.yaml'],
|
||||
**importing['config'])
|
||||
|
||||
# edit the imported config
|
||||
|
||||
@@ -117,7 +117,7 @@ class TestUpdate(unittest.TestCase):
|
||||
# retrieve the path of the sub in the dotpath
|
||||
d1indotpath = os.path.join(o.dotpath, dotfile.src)
|
||||
d1indotpath = os.path.expanduser(d1indotpath)
|
||||
dotfile.trans_w = trans
|
||||
dotfile.trans_w = [trans]
|
||||
|
||||
# update template
|
||||
o.update_path = [d3t]
|
||||
|
||||
@@ -10,7 +10,7 @@ from unittest.mock import patch
|
||||
import os
|
||||
import yaml
|
||||
|
||||
from dotdrop.config import Cfg
|
||||
from dotdrop.cfg_yaml import CfgYaml as Cfg
|
||||
from dotdrop.options import Options
|
||||
from dotdrop.linktypes import LinkTypes
|
||||
from tests.helpers import (SubsetTestCase, _fake_args, clean,
|
||||
@@ -41,14 +41,12 @@ class TestConfig(SubsetTestCase):
|
||||
conf = Cfg(confpath)
|
||||
self.assertTrue(conf is not None)
|
||||
|
||||
opts = conf.get_settings()
|
||||
opts = conf.settings
|
||||
self.assertTrue(opts is not None)
|
||||
self.assertTrue(opts != {})
|
||||
self.assertTrue(opts['backup'] == self.CONFIG_BACKUP)
|
||||
self.assertTrue(opts['create'] == self.CONFIG_CREATE)
|
||||
dotpath = os.path.join(tmp, self.CONFIG_DOTPATH)
|
||||
self.assertTrue(opts['dotpath'] == dotpath)
|
||||
self.assertTrue(conf._is_valid())
|
||||
self.assertTrue(opts['dotpath'] == self.CONFIG_DOTPATH)
|
||||
self.assertTrue(conf.dump() != '')
|
||||
|
||||
def test_def_link(self):
|
||||
@@ -68,8 +66,8 @@ class TestConfig(SubsetTestCase):
|
||||
'link_children')
|
||||
self._test_link_import_fail('whatever')
|
||||
|
||||
@patch('dotdrop.config.open', create=True)
|
||||
@patch('dotdrop.config.os.path.exists', create=True)
|
||||
@patch('dotdrop.cfg_yaml.open', create=True)
|
||||
@patch('dotdrop.cfg_yaml.os.path.exists', create=True)
|
||||
def _test_link_import(self, cfgstring, expected,
|
||||
cliargs, mock_exists, mock_open):
|
||||
data = '''
|
||||
@@ -99,8 +97,8 @@ profiles:
|
||||
|
||||
self.assertTrue(o.import_link == expected)
|
||||
|
||||
@patch('dotdrop.config.open', create=True)
|
||||
@patch('dotdrop.config.os.path.exists', create=True)
|
||||
@patch('dotdrop.cfg_yaml.open', create=True)
|
||||
@patch('dotdrop.cfg_yaml.os.path.exists', create=True)
|
||||
def _test_link_import_fail(self, value, mock_exists, mock_open):
|
||||
data = '''
|
||||
config:
|
||||
@@ -125,7 +123,7 @@ profiles:
|
||||
args['--profile'] = 'p1'
|
||||
args['--cfg'] = 'mocked'
|
||||
|
||||
with self.assertRaisesRegex(ValueError, 'config is not valid'):
|
||||
with self.assertRaises(ValueError):
|
||||
o = Options(args=args)
|
||||
print(o.import_link)
|
||||
|
||||
@@ -143,7 +141,7 @@ profiles:
|
||||
|
||||
# edit the config
|
||||
with open(confpath, 'r') as f:
|
||||
content = yaml.load(f)
|
||||
content = yaml.safe_load(f)
|
||||
|
||||
# adding dotfiles
|
||||
df1key = 'f_vimrc'
|
||||
@@ -171,22 +169,22 @@ profiles:
|
||||
self.assertTrue(conf is not None)
|
||||
|
||||
# test profile
|
||||
profiles = conf.get_profiles()
|
||||
profiles = conf.profiles
|
||||
self.assertTrue(pf1key in profiles)
|
||||
self.assertTrue(pf2key in profiles)
|
||||
|
||||
# test dotfiles
|
||||
dotfiles = conf._get_dotfiles(pf1key)
|
||||
self.assertTrue(df1key in [x.key for x in dotfiles])
|
||||
self.assertTrue(df2key in [x.key for x in dotfiles])
|
||||
dotfiles = conf._get_dotfiles(pf2key)
|
||||
self.assertTrue(df1key in [x.key for x in dotfiles])
|
||||
self.assertFalse(df2key in [x.key for x in dotfiles])
|
||||
dotfiles = conf.profiles[pf1key]['dotfiles']
|
||||
self.assertTrue(df1key in dotfiles)
|
||||
self.assertTrue(df2key in dotfiles)
|
||||
dotfiles = conf.profiles[pf2key]['dotfiles']
|
||||
self.assertTrue(df1key in dotfiles)
|
||||
self.assertFalse(df2key in dotfiles)
|
||||
|
||||
# test not existing included profile
|
||||
# edit the config
|
||||
with open(confpath, 'r') as f:
|
||||
content = yaml.load(f)
|
||||
content = yaml.safe_load(f)
|
||||
content['profiles'] = {
|
||||
pf1key: {'dotfiles': [df2key], 'include': ['host2']},
|
||||
pf2key: {'dotfiles': [df1key], 'include': ['host3']}
|
||||
@@ -227,22 +225,26 @@ profiles:
|
||||
vars_ing_file = create_yaml_keyval(vars_ing, tmp)
|
||||
|
||||
actions_ed = {
|
||||
'pre': {
|
||||
'a_pre_action_ed': 'echo pre 22',
|
||||
},
|
||||
'post': {
|
||||
'a_post_action_ed': 'echo post 22',
|
||||
},
|
||||
'a_action_ed': 'echo 22',
|
||||
'actions': {
|
||||
'pre': {
|
||||
'a_pre_action_ed': 'echo pre 22',
|
||||
},
|
||||
'post': {
|
||||
'a_post_action_ed': 'echo post 22',
|
||||
},
|
||||
'a_action_ed': 'echo 22',
|
||||
}
|
||||
}
|
||||
actions_ing = {
|
||||
'pre': {
|
||||
'a_pre_action_ing': 'echo pre aa',
|
||||
},
|
||||
'post': {
|
||||
'a_post_action_ing': 'echo post aa',
|
||||
},
|
||||
'a_action_ing': 'echo aa',
|
||||
'actions': {
|
||||
'pre': {
|
||||
'a_pre_action_ing': 'echo pre aa',
|
||||
},
|
||||
'post': {
|
||||
'a_post_action_ing': 'echo post aa',
|
||||
},
|
||||
'a_action_ing': 'echo aa',
|
||||
}
|
||||
}
|
||||
actions_ed_file = create_yaml_keyval(actions_ed, tmp)
|
||||
actions_ing_file = create_yaml_keyval(actions_ing, tmp)
|
||||
@@ -328,7 +330,9 @@ profiles:
|
||||
# create the importing base config file
|
||||
importing_path = create_fake_config(tmp,
|
||||
configname=self.CONFIG_NAME,
|
||||
import_configs=('config-*.yaml',),
|
||||
import_configs=[
|
||||
self.CONFIG_NAME_2
|
||||
],
|
||||
**importing['config'])
|
||||
|
||||
# edit the imported config
|
||||
@@ -352,17 +356,28 @@ profiles:
|
||||
self.assertIsNotNone(imported_cfg)
|
||||
|
||||
# test profiles
|
||||
self.assertIsSubset(imported_cfg.lnk_profiles,
|
||||
importing_cfg.lnk_profiles)
|
||||
self.assertIsSubset(imported_cfg.profiles,
|
||||
importing_cfg.profiles)
|
||||
|
||||
# test dotfiles
|
||||
self.assertIsSubset(imported_cfg.dotfiles, importing_cfg.dotfiles)
|
||||
|
||||
# test actions
|
||||
self.assertIsSubset(imported_cfg.actions['pre'],
|
||||
importing_cfg.actions['pre'])
|
||||
self.assertIsSubset(imported_cfg.actions['post'],
|
||||
importing_cfg.actions['post'])
|
||||
pre_ed = post_ed = pre_ing = post_ing = {}
|
||||
for k, v in imported_cfg.actions.items():
|
||||
kind, _ = v
|
||||
if kind == 'pre':
|
||||
pre_ed[k] = v
|
||||
elif kind == 'post':
|
||||
post_ed[k] = v
|
||||
for k, v in importing_cfg.actions.items():
|
||||
kind, _ = v
|
||||
if kind == 'pre':
|
||||
pre_ing[k] = v
|
||||
elif kind == 'post':
|
||||
post_ing[k] = v
|
||||
self.assertIsSubset(pre_ed, pre_ing)
|
||||
self.assertIsSubset(post_ed, post_ing)
|
||||
|
||||
# test transactions
|
||||
self.assertIsSubset(imported_cfg.trans_r, importing_cfg.trans_r)
|
||||
@@ -371,18 +386,18 @@ profiles:
|
||||
# test variables
|
||||
imported_vars = {
|
||||
k: v
|
||||
for k, v in imported_cfg.get_variables(None).items()
|
||||
for k, v in imported_cfg.variables.items()
|
||||
if not k.startswith('_')
|
||||
}
|
||||
importing_vars = {
|
||||
k: v
|
||||
for k, v in importing_cfg.get_variables(None).items()
|
||||
for k, v in importing_cfg.variables.items()
|
||||
if not k.startswith('_')
|
||||
}
|
||||
self.assertIsSubset(imported_vars, importing_vars)
|
||||
|
||||
# test prodots
|
||||
self.assertIsSubset(imported_cfg.prodots, importing_cfg.prodots)
|
||||
self.assertIsSubset(imported_cfg.profiles, importing_cfg.profiles)
|
||||
|
||||
def test_import_configs_override(self):
|
||||
"""Test import_configs when some config keys overlap."""
|
||||
@@ -410,22 +425,26 @@ profiles:
|
||||
vars_ing_file = create_yaml_keyval(vars_ing, tmp)
|
||||
|
||||
actions_ed = {
|
||||
'pre': {
|
||||
'a_pre_action': 'echo pre 22',
|
||||
},
|
||||
'post': {
|
||||
'a_post_action': 'echo post 22',
|
||||
},
|
||||
'a_action': 'echo 22',
|
||||
'actions': {
|
||||
'pre': {
|
||||
'a_pre_action': 'echo pre 22',
|
||||
},
|
||||
'post': {
|
||||
'a_post_action': 'echo post 22',
|
||||
},
|
||||
'a_action': 'echo 22',
|
||||
}
|
||||
}
|
||||
actions_ing = {
|
||||
'pre': {
|
||||
'a_pre_action': 'echo pre aa',
|
||||
},
|
||||
'post': {
|
||||
'a_post_action': 'echo post aa',
|
||||
},
|
||||
'a_action': 'echo aa',
|
||||
'actions': {
|
||||
'pre': {
|
||||
'a_pre_action': 'echo pre aa',
|
||||
},
|
||||
'post': {
|
||||
'a_post_action': 'echo post aa',
|
||||
},
|
||||
'a_action': 'echo aa',
|
||||
}
|
||||
}
|
||||
actions_ed_file = create_yaml_keyval(actions_ed, tmp)
|
||||
actions_ing_file = create_yaml_keyval(actions_ing, tmp)
|
||||
@@ -542,8 +561,8 @@ profiles:
|
||||
self.assertIsNotNone(imported_cfg)
|
||||
|
||||
# test profiles
|
||||
self.assertIsSubset(imported_cfg.lnk_profiles,
|
||||
importing_cfg.lnk_profiles)
|
||||
self.assertIsSubset(imported_cfg.profiles,
|
||||
importing_cfg.profiles)
|
||||
|
||||
# test dotfiles
|
||||
self.assertEqual(importing_cfg.dotfiles['f_vimrc'],
|
||||
@@ -553,14 +572,9 @@ profiles:
|
||||
|
||||
# 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']
|
||||
(imported_cfg.actions[key]
|
||||
== importing_cfg.actions[key])
|
||||
for key in imported_cfg.actions
|
||||
))
|
||||
|
||||
# test transactions
|
||||
@@ -574,20 +588,20 @@ profiles:
|
||||
))
|
||||
|
||||
# test variables
|
||||
imported_vars = imported_cfg.get_variables(None)
|
||||
imported_vars = imported_cfg.variables
|
||||
self.assertFalse(any(
|
||||
imported_vars[k] == v
|
||||
for k, v in importing_cfg.get_variables(None).items()
|
||||
for k, v in importing_cfg.variables.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 profiles dotfiles
|
||||
self.assertEqual(imported_cfg.profiles['host1']['dotfiles'],
|
||||
importing_cfg.profiles['host1']['dotfiles'])
|
||||
self.assertNotEqual(imported_cfg.profiles['host2']['dotfiles'],
|
||||
importing_cfg.profiles['host2']['dotfiles'])
|
||||
self.assertTrue(set(imported_cfg.profiles['host1']['dotfiles'])
|
||||
< set(importing_cfg.profiles['host2']['dotfiles']))
|
||||
|
||||
|
||||
def main():
|
||||
Reference in New Issue
Block a user