mirror of
https://github.com/deadc0de6/dotdrop.git
synced 2026-02-09 04:49:17 +00:00
Revert "Import config"
This commit is contained in:
105
tests/helpers.py
105
tests/helpers.py
@@ -5,13 +5,10 @@ helpers for the unittests
|
||||
"""
|
||||
|
||||
import os
|
||||
import random
|
||||
import shutil
|
||||
import string
|
||||
import random
|
||||
import tempfile
|
||||
from unittest import TestCase
|
||||
|
||||
import yaml
|
||||
|
||||
from dotdrop.options import Options, ENV_NODEBUG
|
||||
from dotdrop.linktypes import LinkTypes
|
||||
@@ -20,30 +17,6 @@ from dotdrop.utils import strip_home
|
||||
TMPSUFFIX = '-dotdrop-tests'
|
||||
|
||||
|
||||
class SubsetTestCase(TestCase):
|
||||
def assertIsSubset(self, sub, sup):
|
||||
for subKey, subValue in sub.items():
|
||||
self.assertIn(subKey, sup)
|
||||
supValue = sup[subKey]
|
||||
|
||||
if isinstance(subValue, str):
|
||||
self.assertEqual(subValue, supValue)
|
||||
continue
|
||||
|
||||
if isinstance(subValue, dict):
|
||||
self.assertIsSubset(subValue, supValue)
|
||||
continue
|
||||
|
||||
try:
|
||||
iter(subValue)
|
||||
self.assertTrue(all(
|
||||
subItem in supValue
|
||||
for subItem in subValue
|
||||
))
|
||||
except TypeError:
|
||||
self.assertEqual(subValue, supValue)
|
||||
|
||||
|
||||
def clean(path):
|
||||
"""Delete file or directory"""
|
||||
if not os.path.exists(path):
|
||||
@@ -148,6 +121,7 @@ def load_options(confpath, profile):
|
||||
o = Options(args=args)
|
||||
o.profile = profile
|
||||
o.dry = False
|
||||
o.profile = profile
|
||||
o.safe = True
|
||||
o.install_diff = True
|
||||
o.import_link = LinkTypes.NOLINK
|
||||
@@ -175,15 +149,8 @@ def get_dotfile_from_yaml(dic, path):
|
||||
return [d for d in dotfiles.values() if d['src'] == src][0]
|
||||
|
||||
|
||||
def yaml_dashed_list(items, indent=0):
|
||||
return ('\n'.join('{}- {}'.format(' ' * indent, item) for item in items)
|
||||
+ '\n')
|
||||
|
||||
|
||||
def create_fake_config(directory, configname='config.yaml',
|
||||
dotpath='dotfiles', backup=True, create=True,
|
||||
import_configs=(), import_actions=(),
|
||||
import_variables=()):
|
||||
dotpath='dotfiles', backup=True, create=True):
|
||||
"""Create a fake config file"""
|
||||
path = os.path.join(directory, configname)
|
||||
workdir = os.path.join(directory, 'workdir')
|
||||
@@ -193,73 +160,7 @@ def create_fake_config(directory, configname='config.yaml',
|
||||
f.write(' create: {}\n'.format(str(create)))
|
||||
f.write(' dotpath: {}\n'.format(dotpath))
|
||||
f.write(' workdir: {}\n'.format(workdir))
|
||||
if import_actions:
|
||||
f.write(' import_actions:\n')
|
||||
f.write(yaml_dashed_list(import_actions, 4))
|
||||
if import_configs:
|
||||
f.write(' import_configs:\n')
|
||||
f.write(yaml_dashed_list(import_configs, 4))
|
||||
if import_variables:
|
||||
f.write(' import_variables:\n')
|
||||
f.write(yaml_dashed_list(import_variables, 4))
|
||||
f.write('dotfiles:\n')
|
||||
f.write('profiles:\n')
|
||||
f.write('actions:\n')
|
||||
return path
|
||||
|
||||
|
||||
def create_yaml_keyval(pairs, parent_dir=None, top_key=None):
|
||||
if top_key:
|
||||
pairs = {top_key: pairs}
|
||||
if not parent_dir:
|
||||
parent_dir = get_tempdir()
|
||||
|
||||
fd, file_name = tempfile.mkstemp(dir=parent_dir, suffix='.yaml', text=True)
|
||||
with os.fdopen(fd, 'w') as f:
|
||||
yaml.safe_dump(pairs, f)
|
||||
return file_name
|
||||
|
||||
|
||||
def populate_fake_config(config, dotfiles={}, profiles={}, actions={},
|
||||
trans={}, trans_write={}, variables={},
|
||||
dynvariables={}):
|
||||
"""Adds some juicy content to config files"""
|
||||
is_path = isinstance(config, str)
|
||||
if is_path:
|
||||
config_path = config
|
||||
with open(config_path) as config_file:
|
||||
config = yaml.safe_load(config_file)
|
||||
|
||||
config['dotfiles'] = dotfiles
|
||||
config['profiles'] = profiles
|
||||
config['actions'] = actions
|
||||
config['trans'] = trans
|
||||
config['trans_write'] = trans_write
|
||||
config['variables'] = variables
|
||||
config['dynvariables'] = dynvariables
|
||||
|
||||
if is_path:
|
||||
with open(config_path, 'w') as config_file:
|
||||
yaml.safe_dump(config, config_file, default_flow_style=False,
|
||||
indent=2)
|
||||
|
||||
|
||||
def file_in_yaml(yaml_file, path, link=False):
|
||||
"""Return whether path is in the given yaml file as a dotfile."""
|
||||
strip = get_path_strip_version(path)
|
||||
|
||||
if isinstance(yaml_file, str):
|
||||
with open(yaml_file) as f:
|
||||
yaml_conf = yaml.safe_load(f)
|
||||
else:
|
||||
yaml_conf = yaml_file
|
||||
|
||||
dotfiles = yaml_conf['dotfiles'].values()
|
||||
|
||||
in_src = strip in (x['src'] 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']
|
||||
return in_src and in_dst and has_link
|
||||
return in_src and in_dst
|
||||
|
||||
@@ -13,19 +13,17 @@ import yaml
|
||||
from dotdrop.config import Cfg
|
||||
from dotdrop.options import Options
|
||||
from dotdrop.linktypes import LinkTypes
|
||||
from tests.helpers import (SubsetTestCase, _fake_args, clean,
|
||||
create_fake_config, create_yaml_keyval, get_tempdir,
|
||||
populate_fake_config)
|
||||
from tests.helpers import get_tempdir, clean, \
|
||||
create_fake_config, _fake_args
|
||||
|
||||
|
||||
class TestConfig(SubsetTestCase):
|
||||
class TestConfig(unittest.TestCase):
|
||||
|
||||
CONFIG_BACKUP = False
|
||||
CONFIG_CREATE = True
|
||||
CONFIG_DOTPATH = 'dotfiles'
|
||||
TMPSUFFIX = '.dotdrop'
|
||||
CONFIG_NAME = 'config.yaml'
|
||||
CONFIG_NAME_2 = 'config-2.yaml'
|
||||
|
||||
def test_config(self):
|
||||
"""Test the config class"""
|
||||
@@ -201,394 +199,6 @@ profiles:
|
||||
conf = Cfg(confpath)
|
||||
self.assertTrue(conf is not None)
|
||||
|
||||
def test_import_configs_merge(self):
|
||||
"""Test import_configs when all config keys merge."""
|
||||
tmp = get_tempdir()
|
||||
self.assertTrue(os.path.exists(tmp))
|
||||
self.addCleanup(clean, tmp)
|
||||
|
||||
vars_ed = {
|
||||
'variables': {
|
||||
'a_var_ed': '33',
|
||||
},
|
||||
'dynvariables': {
|
||||
'a_dynvar_ed': 'echo 33',
|
||||
},
|
||||
}
|
||||
vars_ing = {
|
||||
'variables': {
|
||||
'a_var_ing': 'dd',
|
||||
},
|
||||
'dynvariables': {
|
||||
'a_dynvar_ing': 'echo dd',
|
||||
},
|
||||
}
|
||||
vars_ed_file = create_yaml_keyval(vars_ed, tmp)
|
||||
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_ing = {
|
||||
'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)
|
||||
|
||||
imported = {
|
||||
'config': {
|
||||
'dotpath': 'importing',
|
||||
'import_variables': [vars_ed_file],
|
||||
'import_actions': [actions_ed_file],
|
||||
},
|
||||
'dotfiles': {
|
||||
'f_vimrc': {'dst': '~/.vimrc', 'src': 'vimrc'},
|
||||
},
|
||||
'profiles': {
|
||||
'host1': {
|
||||
'dotfiles': ['f_vimrc'],
|
||||
},
|
||||
},
|
||||
'actions': {
|
||||
'pre': {
|
||||
'a_pre_log_ed': 'echo pre 2',
|
||||
},
|
||||
'post': {
|
||||
'a_post_log_ed': 'echo post 2',
|
||||
},
|
||||
'a_log_ed': 'echo 2',
|
||||
},
|
||||
'trans': {
|
||||
't_log_ed': 'echo 3',
|
||||
},
|
||||
'trans_write': {
|
||||
'tw_log_ed': 'echo 4',
|
||||
},
|
||||
'variables': {
|
||||
'v_log_ed': '42',
|
||||
},
|
||||
'dynvariables': {
|
||||
'dv_log_ed': 'echo 5',
|
||||
},
|
||||
}
|
||||
importing = {
|
||||
'config': {
|
||||
'dotpath': 'importing',
|
||||
'import_variables': [vars_ing_file],
|
||||
'import_actions': [actions_ing_file],
|
||||
},
|
||||
'dotfiles': {
|
||||
'f_xinitrc': {'dst': '~/.xinitrc', 'src': 'xinitrc'},
|
||||
},
|
||||
'profiles': {
|
||||
'host2': {
|
||||
'dotfiles': ['f_xinitrc'],
|
||||
'include': ['host1'],
|
||||
},
|
||||
},
|
||||
'actions': {
|
||||
'pre': {
|
||||
'a_pre_log_ing': 'echo pre a',
|
||||
},
|
||||
'post': {
|
||||
'a_post_log_ing': 'echo post a',
|
||||
},
|
||||
'a_log_ing': 'echo a',
|
||||
},
|
||||
'trans': {
|
||||
't_log_ing': 'echo b',
|
||||
},
|
||||
'trans_write': {
|
||||
'tw_log_ing': 'echo c',
|
||||
},
|
||||
'variables': {
|
||||
'v_log_ing': 'd',
|
||||
},
|
||||
'dynvariables': {
|
||||
'dv_log_ing': 'echo e',
|
||||
},
|
||||
}
|
||||
|
||||
# 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=('config-*.yaml',),
|
||||
**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 profiles
|
||||
self.assertIsSubset(imported_cfg.lnk_profiles,
|
||||
importing_cfg.lnk_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'])
|
||||
|
||||
# test transactions
|
||||
self.assertIsSubset(imported_cfg.trans_r, importing_cfg.trans_r)
|
||||
self.assertIsSubset(imported_cfg.trans_w, importing_cfg.trans_w)
|
||||
|
||||
# test variables
|
||||
imported_vars = {
|
||||
k: v
|
||||
for k, v in imported_cfg.get_variables(None).items()
|
||||
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
|
||||
self.assertIsSubset(imported_cfg.prodots, importing_cfg.prodots)
|
||||
|
||||
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)
|
||||
|
||||
actions_ed = {
|
||||
'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_ed_file = create_yaml_keyval(actions_ed, tmp)
|
||||
actions_ing_file = create_yaml_keyval(actions_ing, tmp)
|
||||
|
||||
imported = {
|
||||
'config': {
|
||||
'dotpath': 'imported',
|
||||
'backup': False,
|
||||
'import_variables': [vars_ed_file],
|
||||
'import_actions': [actions_ed_file],
|
||||
},
|
||||
'dotfiles': {
|
||||
'f_vimrc': {'dst': '~/.vimrc', 'src': 'vimrc'},
|
||||
'f_xinitrc': {'dst': '~/.xinitrc', 'src': 'xinitrc',
|
||||
'link': 'link'},
|
||||
},
|
||||
'profiles': {
|
||||
'host1': {
|
||||
'dotfiles': ['f_vimrc'],
|
||||
},
|
||||
'host2': {
|
||||
'dotfiles': ['f_xinitrc'],
|
||||
},
|
||||
},
|
||||
'actions': {
|
||||
'pre': {
|
||||
'a_pre_log': 'echo pre 2',
|
||||
},
|
||||
'post': {
|
||||
'a_post_log': 'echo post 2',
|
||||
},
|
||||
'a_log': 'echo 2',
|
||||
},
|
||||
'trans': {
|
||||
't_log': 'echo 3',
|
||||
},
|
||||
'trans_write': {
|
||||
'tw_log': 'echo 4',
|
||||
},
|
||||
'variables': {
|
||||
'v_log': '42',
|
||||
},
|
||||
'dynvariables': {
|
||||
'dv_log': 'echo 5',
|
||||
},
|
||||
}
|
||||
importing = {
|
||||
'config': {
|
||||
'dotpath': 'importing',
|
||||
'backup': True,
|
||||
'import_variables': [vars_ing_file],
|
||||
'import_actions': [actions_ing_file],
|
||||
},
|
||||
'dotfiles': {
|
||||
'f_xinitrc': {'dst': '~/.xinitrc', 'src': 'xinitrc'},
|
||||
},
|
||||
'profiles': {
|
||||
'host2': {
|
||||
'dotfiles': ['f_xinitrc'],
|
||||
'include': ['host1'],
|
||||
},
|
||||
},
|
||||
'actions': {
|
||||
'pre': {
|
||||
'a_pre_log': 'echo pre a',
|
||||
},
|
||||
'post': {
|
||||
'a_post_log': 'echo post a',
|
||||
},
|
||||
'a_log': 'echo a',
|
||||
},
|
||||
'trans': {
|
||||
't_log': 'echo b',
|
||||
},
|
||||
'trans_write': {
|
||||
'tw_log': 'echo c',
|
||||
},
|
||||
'variables': {
|
||||
'v_log': 'd',
|
||||
},
|
||||
'dynvariables': {
|
||||
'dv_log': 'echo e',
|
||||
},
|
||||
}
|
||||
|
||||
# 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 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']))
|
||||
|
||||
|
||||
def main():
|
||||
unittest.main()
|
||||
|
||||
@@ -15,10 +15,10 @@ from dotdrop.dotdrop import cmd_list_files
|
||||
from dotdrop.dotdrop import cmd_update
|
||||
from dotdrop.linktypes import LinkTypes
|
||||
|
||||
from tests.helpers import (clean, create_dir, create_fake_config,
|
||||
create_random_file, edit_content, file_in_yaml,
|
||||
get_path_strip_version, get_string, get_tempdir,
|
||||
load_options, populate_fake_config)
|
||||
from tests.helpers import get_path_strip_version, edit_content, \
|
||||
load_options, create_random_file, \
|
||||
clean, get_string, get_dotfile_from_yaml, \
|
||||
get_tempdir, create_fake_config, create_dir
|
||||
|
||||
|
||||
class TestImport(unittest.TestCase):
|
||||
@@ -39,13 +39,18 @@ class TestImport(unittest.TestCase):
|
||||
def assert_file(self, path, o, profile):
|
||||
"""Make sure path has been inserted in conf for profile"""
|
||||
strip = get_path_strip_version(path)
|
||||
self.assertTrue(any(x.src.endswith(strip) for x in o.dotfiles))
|
||||
dsts = (os.path.expanduser(x.dst) for x in o.dotfiles)
|
||||
self.assertTrue(strip in [x.src for x in o.dotfiles])
|
||||
dsts = [os.path.expanduser(x.dst) for x in o.dotfiles]
|
||||
self.assertTrue(path in dsts)
|
||||
|
||||
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))
|
||||
strip = get_path_strip_version(path)
|
||||
self.assertTrue(strip in [x['src'] for x in dic['dotfiles'].values()])
|
||||
dsts = [os.path.expanduser(x['dst']) for x in dic['dotfiles'].values()]
|
||||
if link:
|
||||
self.assertTrue(get_dotfile_from_yaml(dic, path)['link'])
|
||||
self.assertTrue(path in dsts)
|
||||
|
||||
def test_import(self):
|
||||
"""Test the import function"""
|
||||
@@ -198,210 +203,6 @@ class TestImport(unittest.TestCase):
|
||||
c2 = open(indt1, 'r').read()
|
||||
self.assertTrue(editcontent == c2)
|
||||
|
||||
def test_ext_config_yaml_not_mix(self):
|
||||
"""Test whether the import_configs mixes yaml files upon importing."""
|
||||
# dotfiles on filesystem
|
||||
src = get_tempdir()
|
||||
self.assertTrue(os.path.exists(src))
|
||||
self.addCleanup(clean, src)
|
||||
|
||||
# create some random dotfiles
|
||||
dotfiles = []
|
||||
for _ in range(3):
|
||||
dotfile, _ = create_random_file(src)
|
||||
dotfiles.append(dotfile)
|
||||
self.addCleanup(clean, dotfile)
|
||||
self.assertTrue(all(map(os.path.exists, dotfiles)))
|
||||
|
||||
# create dotdrop home
|
||||
dotdrop_home = get_tempdir()
|
||||
self.assertTrue(os.path.exists(dotdrop_home))
|
||||
self.addCleanup(clean, dotdrop_home)
|
||||
|
||||
imported = {
|
||||
'config': {
|
||||
'dotpath': 'imported',
|
||||
},
|
||||
'dotfiles': {},
|
||||
'profiles': {
|
||||
'host1': {
|
||||
'dotfiles': [],
|
||||
},
|
||||
},
|
||||
'actions': {
|
||||
'pre': {
|
||||
'a_pre_log_ed': 'echo pre 2',
|
||||
},
|
||||
'post': {
|
||||
'a_post_log_ed': 'echo post 2',
|
||||
},
|
||||
'a_log_ed': 'echo 2',
|
||||
},
|
||||
'trans': {
|
||||
't_log_ed': 'echo 3',
|
||||
},
|
||||
'trans_write': {
|
||||
'tw_log_ed': 'echo 4',
|
||||
},
|
||||
'variables': {
|
||||
'v_log_ed': '42',
|
||||
},
|
||||
'dynvariables': {
|
||||
'dv_log_ed': 'echo 5',
|
||||
},
|
||||
}
|
||||
importing = {
|
||||
'config': {
|
||||
'dotpath': 'importing',
|
||||
},
|
||||
'dotfiles': {},
|
||||
'profiles': {
|
||||
'host2': {
|
||||
'dotfiles': [],
|
||||
'include': ['host1'],
|
||||
},
|
||||
},
|
||||
'actions': {
|
||||
'pre': {
|
||||
'a_pre_log_ing': 'echo pre a',
|
||||
},
|
||||
'post': {
|
||||
'a_post_log_ing': 'echo post a',
|
||||
},
|
||||
'a_log_ing': 'echo a',
|
||||
},
|
||||
'trans': {
|
||||
't_log_ing': 'echo b',
|
||||
},
|
||||
'trans_write': {
|
||||
'tw_log_ing': 'echo c',
|
||||
},
|
||||
'variables': {
|
||||
'v_log_ing': 'd',
|
||||
},
|
||||
'dynvariables': {
|
||||
'dv_log_ing': 'echo e',
|
||||
},
|
||||
}
|
||||
|
||||
dotfiles_ing, dotfiles_ed = dotfiles[:-1], dotfiles[-1:]
|
||||
|
||||
# create the imported base config file
|
||||
imported_path = create_fake_config(dotdrop_home,
|
||||
configname='config-2.yaml',
|
||||
**imported['config'])
|
||||
# create the importing base config file
|
||||
importing_path = create_fake_config(dotdrop_home,
|
||||
configname='config.yaml',
|
||||
import_configs=('config-*.yaml',),
|
||||
**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'
|
||||
})
|
||||
|
||||
# import the dotfiles
|
||||
o = load_options(imported_path, 'host1')
|
||||
o.import_path = dotfiles_ed
|
||||
cmd_importer(o)
|
||||
|
||||
o = load_options(importing_path, 'host2')
|
||||
o.import_path = dotfiles_ing
|
||||
cmd_importer(o)
|
||||
|
||||
# reload the config
|
||||
o = load_options(importing_path, 'host2')
|
||||
|
||||
# test imported config
|
||||
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))
|
||||
|
||||
# testing profiles
|
||||
profiles = y['profiles'].keys()
|
||||
self.assertTrue('host1' in profiles)
|
||||
self.assertFalse('host2' in profiles)
|
||||
|
||||
# testing actions
|
||||
actions = y['actions']['pre']
|
||||
actions.update(y['actions']['post'])
|
||||
actions.update({
|
||||
k: v
|
||||
for k, v in y['actions'].items()
|
||||
if k not in ('pre', 'post')
|
||||
})
|
||||
actions = actions.keys()
|
||||
self.assertTrue(all(a.endswith('ed') for a in actions))
|
||||
self.assertFalse(any(a.endswith('ing') for a in actions))
|
||||
|
||||
# testing transformations
|
||||
transformations = y['trans'].keys()
|
||||
self.assertTrue(all(t.endswith('ed') for t in transformations))
|
||||
self.assertFalse(any(t.endswith('ing') for t in transformations))
|
||||
transformations = y['trans_write'].keys()
|
||||
self.assertTrue(all(t.endswith('ed') for t in transformations))
|
||||
self.assertFalse(any(t.endswith('ing') for t in transformations))
|
||||
|
||||
# testing variables
|
||||
variables = 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()
|
||||
self.assertTrue(all(dv.endswith('ed') for dv in dyn_variables))
|
||||
self.assertFalse(any(dv.endswith('ing') for dv in dyn_variables))
|
||||
|
||||
# test importing config
|
||||
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))
|
||||
|
||||
# testing profiles
|
||||
profiles = y['profiles'].keys()
|
||||
self.assertTrue('host2' in profiles)
|
||||
self.assertFalse('host1' in profiles)
|
||||
|
||||
# testing actions
|
||||
actions = y['actions']['pre']
|
||||
actions.update(y['actions']['post'])
|
||||
actions.update({
|
||||
k: v
|
||||
for k, v in y['actions'].items()
|
||||
if k not in ('pre', 'post')
|
||||
})
|
||||
actions = actions.keys()
|
||||
self.assertTrue(all(action.endswith('ing') for action in actions))
|
||||
self.assertFalse(any(action.endswith('ed') for action in actions))
|
||||
|
||||
# testing transformations
|
||||
transformations = y['trans'].keys()
|
||||
self.assertTrue(all(t.endswith('ing') for t in transformations))
|
||||
self.assertFalse(any(t.endswith('ed') for t in transformations))
|
||||
transformations = y['trans_write'].keys()
|
||||
self.assertTrue(all(t.endswith('ing') for t in transformations))
|
||||
self.assertFalse(any(t.endswith('ed') for t in transformations))
|
||||
|
||||
# testing variables
|
||||
variables = 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 main():
|
||||
unittest.main()
|
||||
|
||||
@@ -10,9 +10,8 @@ from unittest.mock import MagicMock, patch
|
||||
import filecmp
|
||||
|
||||
from dotdrop.config import Cfg
|
||||
from tests.helpers import (clean, create_dir, create_fake_config,
|
||||
create_random_file, get_string, get_tempdir,
|
||||
load_options, populate_fake_config)
|
||||
from tests.helpers import create_dir, get_string, get_tempdir, clean, \
|
||||
create_random_file, load_options
|
||||
from dotdrop.dotfile import Dotfile
|
||||
from dotdrop.installer import Installer
|
||||
from dotdrop.action import Action
|
||||
@@ -232,107 +231,6 @@ exec bspwm
|
||||
tempcontent = open(dst10, 'r').read().rstrip()
|
||||
self.assertTrue(tempcontent == header())
|
||||
|
||||
def test_install_import_configs(self):
|
||||
"""Test the install function with imported configs"""
|
||||
# dotpath location
|
||||
tmp = get_tempdir()
|
||||
self.assertTrue(os.path.exists(tmp))
|
||||
self.addCleanup(clean, tmp)
|
||||
|
||||
os.mkdir(os.path.join(tmp, 'importing'))
|
||||
os.mkdir(os.path.join(tmp, 'imported'))
|
||||
|
||||
# where dotfiles will be installed
|
||||
dst = get_tempdir()
|
||||
self.assertTrue(os.path.exists(dst))
|
||||
self.addCleanup(clean, dst)
|
||||
|
||||
# creating random dotfiles
|
||||
imported_dotfile, _ = create_random_file(os.path.join(tmp, 'imported'))
|
||||
imported_dotfile = {
|
||||
'dst': os.path.join(dst, imported_dotfile),
|
||||
'key': 'f_{}'.format(imported_dotfile),
|
||||
'name': imported_dotfile,
|
||||
'src': os.path.join(tmp, 'imported', imported_dotfile),
|
||||
}
|
||||
importing_dotfile, _ = \
|
||||
create_random_file(os.path.join(tmp, 'importing'))
|
||||
importing_dotfile = {
|
||||
'dst': os.path.join(dst, importing_dotfile),
|
||||
'key': 'f_{}'.format(importing_dotfile),
|
||||
'name': importing_dotfile,
|
||||
'src': os.path.join(tmp, 'imported', importing_dotfile),
|
||||
}
|
||||
|
||||
imported = {
|
||||
'config': {
|
||||
'dotpath': 'imported',
|
||||
},
|
||||
'dotfiles': {
|
||||
imported_dotfile['key']: {
|
||||
'dst': imported_dotfile['dst'],
|
||||
'src': imported_dotfile['name'],
|
||||
},
|
||||
},
|
||||
'profiles': {
|
||||
'host1': {
|
||||
'dotfiles': [imported_dotfile['key']],
|
||||
},
|
||||
},
|
||||
}
|
||||
importing = {
|
||||
'config': {
|
||||
'dotpath': 'importing',
|
||||
},
|
||||
'dotfiles': {
|
||||
importing_dotfile['key']: {
|
||||
'dst': importing_dotfile['dst'],
|
||||
'src': importing_dotfile['src'],
|
||||
},
|
||||
},
|
||||
'profiles': {
|
||||
'host2': {
|
||||
'dotfiles': [importing_dotfile['key']],
|
||||
'include': ['host1'],
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
# create the imported base config file
|
||||
imported_path = create_fake_config(tmp,
|
||||
configname='config-2.yaml',
|
||||
**imported['config'])
|
||||
# create the importing base config file
|
||||
importing_path = create_fake_config(tmp,
|
||||
configname='config.yaml',
|
||||
import_configs=('config-*.yaml',),
|
||||
**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'
|
||||
})
|
||||
|
||||
# install them
|
||||
o = load_options(importing_path, 'host2')
|
||||
o.safe = False
|
||||
o.install_showdiff = True
|
||||
o.variables = {}
|
||||
cmd_install(o)
|
||||
|
||||
# now compare the generated files
|
||||
self.assertTrue(os.path.exists(importing_dotfile['dst']))
|
||||
self.assertTrue(os.path.exists(imported_dotfile['dst']))
|
||||
|
||||
def test_link_children(self):
|
||||
"""test the link children"""
|
||||
# create source dir
|
||||
|
||||
Reference in New Issue
Block a user