mirror of
https://github.com/deadc0de6/dotdrop.git
synced 2026-02-11 09:04:18 +00:00
Adding stub of profile import functionality
This commit is contained in:
@@ -38,6 +38,7 @@ class Cfg:
|
|||||||
# import keys
|
# import keys
|
||||||
key_import_vars = 'import_variables'
|
key_import_vars = 'import_variables'
|
||||||
key_import_actions = 'import_actions'
|
key_import_actions = 'import_actions'
|
||||||
|
key_import_profiles = 'import_profiles'
|
||||||
|
|
||||||
# actions keys
|
# actions keys
|
||||||
key_actions = 'actions'
|
key_actions = 'actions'
|
||||||
@@ -259,6 +260,18 @@ class Cfg:
|
|||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
# parse external profiles
|
||||||
|
try:
|
||||||
|
ext_configs = self.lnk_settings[self.key_import_profiles]
|
||||||
|
for config in ext_configs:
|
||||||
|
ext_config = Cfg(config)
|
||||||
|
self.dotfiles.update(ext_config.dotfiles)
|
||||||
|
self.lnk_profiles.update(ext_config.lnk_profiles)
|
||||||
|
self.prodots.update(ext_config.prodots)
|
||||||
|
# need variables, actions and so on
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
|
||||||
# parse local actions
|
# parse local actions
|
||||||
# If local actions are None, replaces them with empty dict
|
# If local actions are None, replaces them with empty dict
|
||||||
try:
|
try:
|
||||||
@@ -758,9 +771,12 @@ class Cfg:
|
|||||||
def _dotfile_exists(self, dotfile):
|
def _dotfile_exists(self, dotfile):
|
||||||
"""return True and the existing dotfile key
|
"""return True and the existing dotfile key
|
||||||
if it already exists, False and a new unique key otherwise"""
|
if it already exists, False and a new unique key otherwise"""
|
||||||
dsts = [(k, d.dst) for k, d in self.dotfiles.items()]
|
try:
|
||||||
if dotfile.dst in [x[1] for x in dsts]:
|
return True, next(key
|
||||||
return True, [x[0] for x in dsts if x[1] == dotfile.dst][0]
|
for key, d in self.dotfiles.items()
|
||||||
|
if d.dst == dotfile.dst)
|
||||||
|
except StopIteration:
|
||||||
|
pass
|
||||||
# return key for this new dotfile
|
# return key for this new dotfile
|
||||||
path = os.path.expanduser(dotfile.dst)
|
path = os.path.expanduser(dotfile.dst)
|
||||||
keys = self.dotfiles.keys()
|
keys = self.dotfiles.keys()
|
||||||
|
|||||||
8
tests.sh
8
tests.sh
@@ -34,6 +34,8 @@ PYTHONPATH=dotdrop ${nosebin} -s --with-coverage --cover-package=dotdrop
|
|||||||
#PYTHONPATH=dotdrop python3 -m pytest tests
|
#PYTHONPATH=dotdrop python3 -m pytest tests
|
||||||
|
|
||||||
## execute bash script tests
|
## execute bash script tests
|
||||||
for scr in tests-ng/*.sh; do
|
[ "$1" = '--python-only' ] || {
|
||||||
${scr}
|
for scr in tests-ng/*.sh; do
|
||||||
done
|
${scr}
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import shutil
|
|||||||
import string
|
import string
|
||||||
import random
|
import random
|
||||||
import tempfile
|
import tempfile
|
||||||
|
import yaml
|
||||||
|
|
||||||
from dotdrop.options import Options, ENV_NODEBUG
|
from dotdrop.options import Options, ENV_NODEBUG
|
||||||
from dotdrop.linktypes import LinkTypes
|
from dotdrop.linktypes import LinkTypes
|
||||||
@@ -149,8 +150,14 @@ def get_dotfile_from_yaml(dic, path):
|
|||||||
return [d for d in dotfiles.values() if d['src'] == src][0]
|
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',
|
def create_fake_config(directory, configname='config.yaml',
|
||||||
dotpath='dotfiles', backup=True, create=True):
|
dotpath='dotfiles', backup=True, create=True,
|
||||||
|
import_profiles=()):
|
||||||
"""Create a fake config file"""
|
"""Create a fake config file"""
|
||||||
path = os.path.join(directory, configname)
|
path = os.path.join(directory, configname)
|
||||||
workdir = os.path.join(directory, 'workdir')
|
workdir = os.path.join(directory, 'workdir')
|
||||||
@@ -160,7 +167,27 @@ def create_fake_config(directory, configname='config.yaml',
|
|||||||
f.write(' create: {}\n'.format(str(create)))
|
f.write(' create: {}\n'.format(str(create)))
|
||||||
f.write(' dotpath: {}\n'.format(dotpath))
|
f.write(' dotpath: {}\n'.format(dotpath))
|
||||||
f.write(' workdir: {}\n'.format(workdir))
|
f.write(' workdir: {}\n'.format(workdir))
|
||||||
|
if import_profiles:
|
||||||
|
f.write(' import_profiles:\n')
|
||||||
|
f.write(yaml_dashed_list(import_profiles, 4))
|
||||||
f.write('dotfiles:\n')
|
f.write('dotfiles:\n')
|
||||||
f.write('profiles:\n')
|
f.write('profiles:\n')
|
||||||
f.write('actions:\n')
|
f.write('actions:\n')
|
||||||
return path
|
return path
|
||||||
|
|
||||||
|
|
||||||
|
def populate_fake_config(config, dotfiles=(), profiles=()):
|
||||||
|
"""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
|
||||||
|
|
||||||
|
if is_path:
|
||||||
|
with open(config_path, 'w') as config_file:
|
||||||
|
yaml.safe_dump(config, config_file, default_flow_style=False,
|
||||||
|
indent=2)
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ from dotdrop.config import Cfg
|
|||||||
from dotdrop.options import Options
|
from dotdrop.options import Options
|
||||||
from dotdrop.linktypes import LinkTypes
|
from dotdrop.linktypes import LinkTypes
|
||||||
from tests.helpers import get_tempdir, clean, \
|
from tests.helpers import get_tempdir, clean, \
|
||||||
create_fake_config, _fake_args
|
create_fake_config, _fake_args, populate_fake_config
|
||||||
|
|
||||||
|
|
||||||
class TestConfig(unittest.TestCase):
|
class TestConfig(unittest.TestCase):
|
||||||
@@ -24,6 +24,7 @@ class TestConfig(unittest.TestCase):
|
|||||||
CONFIG_DOTPATH = 'dotfiles'
|
CONFIG_DOTPATH = 'dotfiles'
|
||||||
TMPSUFFIX = '.dotdrop'
|
TMPSUFFIX = '.dotdrop'
|
||||||
CONFIG_NAME = 'config.yaml'
|
CONFIG_NAME = 'config.yaml'
|
||||||
|
CONFIG_NAME_2 = 'config-2.yaml'
|
||||||
|
|
||||||
def test_config(self):
|
def test_config(self):
|
||||||
"""Test the config class"""
|
"""Test the config class"""
|
||||||
@@ -199,6 +200,79 @@ profiles:
|
|||||||
conf = Cfg(confpath)
|
conf = Cfg(confpath)
|
||||||
self.assertTrue(conf is not None)
|
self.assertTrue(conf is not None)
|
||||||
|
|
||||||
|
def test_include_profiles(self):
|
||||||
|
tmp = get_tempdir()
|
||||||
|
self.assertTrue(os.path.exists(tmp))
|
||||||
|
self.addCleanup(clean, tmp)
|
||||||
|
|
||||||
|
# create the imported base config file
|
||||||
|
imported = create_fake_config(tmp,
|
||||||
|
configname=self.CONFIG_NAME_2,
|
||||||
|
dotpath=self.CONFIG_DOTPATH,
|
||||||
|
backup=self.CONFIG_BACKUP,
|
||||||
|
create=self.CONFIG_CREATE)
|
||||||
|
# create the importing base config file
|
||||||
|
importing = create_fake_config(tmp,
|
||||||
|
configname=self.CONFIG_NAME,
|
||||||
|
dotpath=self.CONFIG_DOTPATH,
|
||||||
|
backup=self.CONFIG_BACKUP,
|
||||||
|
create=self.CONFIG_CREATE,
|
||||||
|
import_profiles=(imported,))
|
||||||
|
|
||||||
|
# keys
|
||||||
|
keys = {
|
||||||
|
'dotfile1': 'f_vimrc',
|
||||||
|
'dotfile2': 'f_xinitrc',
|
||||||
|
'profile1': 'host1',
|
||||||
|
'profile2': 'host2',
|
||||||
|
}
|
||||||
|
|
||||||
|
# edit the imported config
|
||||||
|
dotfiles_imported = {
|
||||||
|
keys['dotfile1']: {'dst': '~/.vimrc', 'src': 'vimrc'},
|
||||||
|
}
|
||||||
|
profiles_imported = {
|
||||||
|
keys['profile1']: {'dotfiles': [keys['dotfile1']]},
|
||||||
|
}
|
||||||
|
populate_fake_config(imported,
|
||||||
|
dotfiles=dotfiles_imported,
|
||||||
|
profiles=profiles_imported)
|
||||||
|
|
||||||
|
# edit the importing config
|
||||||
|
dotfiles_importing = {
|
||||||
|
keys['dotfile2']: {'dst': '~/.vimrc', 'src': 'vimrc'},
|
||||||
|
}
|
||||||
|
profiles_importing = {
|
||||||
|
keys['profile2']: {
|
||||||
|
'dotfiles': [keys['dotfile2']],
|
||||||
|
'include': [keys['profile1']],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
populate_fake_config(importing,
|
||||||
|
dotfiles=dotfiles_importing,
|
||||||
|
profiles=profiles_importing)
|
||||||
|
|
||||||
|
# do the tests
|
||||||
|
importing_cfg = Cfg(importing)
|
||||||
|
self.assertIsNotNone(importing_cfg)
|
||||||
|
|
||||||
|
# test profile
|
||||||
|
profiles = importing_cfg.get_profiles()
|
||||||
|
self.assertIn(keys['profile2'], profiles)
|
||||||
|
|
||||||
|
# test dotfiles
|
||||||
|
importing_cfg_dotfiles = [
|
||||||
|
(dotfile.key, {'src': dotfile.src, 'dst': dotfile.dst})
|
||||||
|
for dotfile in importing_cfg.prodots[keys['profile2']]
|
||||||
|
]
|
||||||
|
|
||||||
|
self.assertIn(
|
||||||
|
(keys['dotfile2'], dotfiles_importing[keys['dotfile2']]),
|
||||||
|
importing_cfg_dotfiles)
|
||||||
|
self.assertIn(
|
||||||
|
(keys['dotfile1'], dotfiles_imported[keys['dotfile1']]),
|
||||||
|
importing_cfg_dotfiles)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|||||||
Reference in New Issue
Block a user