1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-11 03:49:16 +00:00

Revert "Import config"

This commit is contained in:
deadc0de
2019-04-28 19:18:48 +02:00
committed by GitHub
parent ed0fddb483
commit 861cb39ad4
7 changed files with 98 additions and 1081 deletions

View File

@@ -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