1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-04 11:01:45 +00:00
This commit is contained in:
deadc0de6
2023-01-28 16:14:54 +01:00
committed by deadc0de
parent 7e7bb0bd73
commit f6dbdad63d
11 changed files with 475 additions and 421 deletions

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env python3
# pylint: disable-msg=C0103
"""
author: deadc0de6 (https://github.com/deadc0de6)
Copyright (c) 2020, deadc0de6
@@ -32,6 +33,7 @@ def run_test(logfd, path):
if logfd:
logfd.write(f'starting test \"{path}\"\n')
logfd.flush()
# pylint: disable=R1732
proc = subprocess.Popen(path, shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
@@ -73,6 +75,7 @@ def main():
logfd = sys.stdout
if not is_cicd():
# pylint: disable=R1732
logfd = open(LOG_FILE, 'w', encoding='utf-8')
if max_jobs:
logfd.write(f'run tests with {max_jobs} parallel job(s)\n')
@@ -95,6 +98,7 @@ def main():
for test in futures.as_completed(wait_for.keys()):
try:
ret, reason, name, log = test.result()
# pylint: disable=W0703
except Exception as exc:
print()
print(f'test \"{wait_for[test]}\" failed: {exc}')

View File

@@ -10,11 +10,13 @@ import dotdrop
class TestDummy(unittest.TestCase):
"""test case"""
dotdrop.main()
def main():
"""entry point"""
unittest.main()

View File

@@ -21,27 +21,30 @@ TMPSUFFIX = '-dotdrop-tests'
class SubsetTestCase(TestCase):
def assertIsSubset(self, sub, sup):
for subKey, subValue in sub.items():
self.assertIn(subKey, sup)
supValue = sup[subKey]
"""test case"""
if isinstance(subValue, str):
self.assertEqual(subValue, supValue)
def assert_is_subset(self, sub, sup):
"""ensure it's a subset"""
for sub_key, sub_val in sub.items():
self.assertIn(sub_key, sup)
subvalue = sup[sub_key]
if isinstance(sub_val, str):
self.assertEqual(sub_val, subvalue)
continue
if isinstance(subValue, dict):
self.assertIsSubset(subValue, supValue)
if isinstance(sub_val, dict):
self.assert_is_subset(sub_val, subvalue)
continue
try:
iter(subValue)
iter(sub_val)
self.assertTrue(all(
subItem in supValue
for subItem in subValue
subItem in subvalue
for subItem in sub_val
))
except TypeError:
self.assertEqual(subValue, supValue)
self.assertEqual(sub_val, subvalue)
def clean(path):
@@ -60,7 +63,7 @@ def get_string(length):
"""Get a random string of length 'length'"""
alpha = string.ascii_uppercase + string.digits
temp = ''.join(random.choice(alpha) for _ in range(length))
return 'tmp.{}{}'.format(temp, TMPSUFFIX)
return f'tmp.{temp}{TMPSUFFIX}'
def get_tempdir():
@@ -82,15 +85,16 @@ def create_random_file(directory, content=None,
pre = bytes()
if template:
pre = bytes('{{@@ header() @@}}\n', 'ascii')
content = bytes('{}{}\n'.format(pre, get_string(100)), 'ascii')
content = bytes(f'{pre}{get_string(100)}\n', 'ascii')
else:
pre = ''
if template:
pre = '{{@@ header() @@}}\n'
content = '{}{}\n'.format(pre, get_string(100))
content = f'{pre}{get_string(100)}\n'
path = os.path.join(directory, fname)
with open(path, mode) as f:
f.write(content)
# pylint: disable=W1514
with open(path, mode) as file:
file.write(content)
return path, content
@@ -99,8 +103,9 @@ def edit_content(path, newcontent, binary=False):
mode = 'w'
if binary:
mode = 'wb'
with open(path, mode) as f:
f.write(newcontent)
# pylint: disable=W1514
with open(path, mode) as file:
file.write(newcontent)
def create_dir(path):
@@ -159,15 +164,16 @@ def load_options(confpath, profile):
args['--profile'] = profile
args['--verbose'] = True
# and get the options
o = Options(args=args)
o.profile = profile
o.dry = False
o.safe = False
o.install_diff = True
o.import_link = LinkTypes.NOLINK
o.install_showdiff = True
o.debug = True
return o
opt = Options(args=args)
opt.profile = profile
opt.dry = False
opt.safe = False
opt.install_diff = True
opt.import_link = LinkTypes.NOLINK
opt.install_showdiff = True
opt.debug = True
opt.dotpath = os.path.join(os.path.dirname(confpath), 'dotfiles')
return opt
def get_path_strip_version(path):
@@ -192,7 +198,9 @@ def get_dotfile_from_yaml(dic, path):
def yaml_dashed_list(items, indent=0):
return ('\n'.join('{}- {}'.format(' ' * indent, item) for item in items)
"""yaml dashed list"""
ind = ' ' * indent
return ('\n'.join(f'{ind}- {item}' for item in items)
+ '\n')
@@ -203,28 +211,29 @@ def create_fake_config(directory, configname='config.yaml',
"""Create a fake config file"""
path = os.path.join(directory, configname)
workdir = os.path.join(directory, 'workdir')
with open(path, 'w') as f:
f.write('config:\n')
f.write(' backup: {}\n'.format(str(backup)))
f.write(' create: {}\n'.format(str(create)))
f.write(' dotpath: {}\n'.format(dotpath))
f.write(' workdir: {}\n'.format(workdir))
with open(path, 'w', encoding='utf-8') as file:
file.write('config:\n')
file.write(f' backup: {backup}\n')
file.write(f' create: {create}\n')
file.write(f' dotpath: {dotpath}\n')
file.write(f' workdir: {workdir}\n')
if import_actions:
f.write(' import_actions:\n')
f.write(yaml_dashed_list(import_actions, 4))
file.write(' import_actions:\n')
file.write(yaml_dashed_list(import_actions, 4))
if import_configs:
f.write(' import_configs:\n')
f.write(yaml_dashed_list(import_configs, 4))
file.write(' import_configs:\n')
file.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')
file.write(' import_variables:\n')
file.write(yaml_dashed_list(import_variables, 4))
file.write('dotfiles:\n')
file.write('profiles:\n')
file.write('actions:\n')
return path
def create_yaml_keyval(pairs, parent_dir=None, top_key=None):
"""create key val for yaml"""
if top_key:
pairs = {top_key: pairs}
if not parent_dir:
@@ -235,6 +244,7 @@ def create_yaml_keyval(pairs, parent_dir=None, top_key=None):
return file_name
# pylint: disable=W0102
def populate_fake_config(config, dotfiles={}, profiles={}, actions={},
trans={}, trans_write={}, variables={},
dynvariables={}):
@@ -267,14 +277,14 @@ def file_in_yaml(yaml_file, path, link=False):
dotfiles = yaml_conf['dotfiles'].values()
in_src = any([x['src'].endswith(strip) 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:
df = get_dotfile_from_yaml(yaml_conf, path)
dotfile = get_dotfile_from_yaml(yaml_conf, path)
has_link = False
if df:
has_link = 'link' in df
if dotfile:
has_link = 'link' in dotfile
else:
return False
return in_src and in_dst and has_link
@@ -282,15 +292,17 @@ def file_in_yaml(yaml_file, path, link=False):
def yaml_load(path):
with open(path, 'r') as f:
content = yaml(typ='safe').load(f)
"""load yaml"""
with open(path, 'r', encoding='utf-8') as file:
content = yaml(typ='safe').load(file)
return content
def yaml_dump(content, path):
with open(path, 'w') as f:
y = yaml()
y.default_flow_style = False
y.indent = 2
y.typ = 'safe'
y.dump(content, f)
"""dump yaml"""
with open(path, 'w', encoding='utf-8') as file:
cont = yaml()
cont.default_flow_style = False
cont.indent = 2
cont.typ = 'safe'
cont.dump(content, file)

View File

@@ -20,24 +20,26 @@ from tests.helpers import create_dir, get_string, get_tempdir, clean, \
class TestCompare(unittest.TestCase):
"""test case"""
CONFIG_BACKUP = False
CONFIG_CREATE = True
CONFIG_DOTPATH = 'dotfiles'
CONFIG_NAME = 'config.yaml'
def compare(self, o, tmp, nbdotfiles):
dotfiles = o.dotfiles
def compare(self, opt, tmp, nbdotfiles):
"""compare"""
dotfiles = opt.dotfiles
self.assertTrue(len(dotfiles) == nbdotfiles)
t = Templategen(base=o.dotpath, debug=True)
inst = Installer(create=o.create, backup=o.backup,
dry=o.dry, base=o.dotpath, debug=o.debug)
templ = Templategen(base=opt.dotpath, debug=True)
inst = Installer(create=opt.create, backup=opt.backup,
dry=opt.dry, base=opt.dotpath, debug=opt.debug)
comp = Comparator()
results = {}
for dotfile in dotfiles:
path = os.path.expanduser(dotfile.dst)
ret, err, insttmp = inst.install_to_temp(t, tmp, dotfile.src,
dotfile.dst)
ret, _, insttmp = inst.install_to_temp(templ, tmp, dotfile.src,
dotfile.dst)
if not ret:
results[path] = False
continue
@@ -47,9 +49,10 @@ class TestCompare(unittest.TestCase):
return results
def test_none(self):
t = Templategen(base=self.CONFIG_DOTPATH,
debug=True, variables=None)
self.assertTrue(t is not None)
"""test none"""
templ = Templategen(base=self.CONFIG_DOTPATH,
debug=True, variables=None)
self.assertTrue(templ is not None)
def test_compare(self):
"""Test the compare function"""
@@ -74,33 +77,33 @@ class TestCompare(unittest.TestCase):
self.addCleanup(clean, dotfilespath)
# create the dotfiles to test
d1, c1 = create_random_file(fold_config)
self.assertTrue(os.path.exists(d1))
self.addCleanup(clean, d1)
df1, _ = create_random_file(fold_config)
self.assertTrue(os.path.exists(df1))
self.addCleanup(clean, df1)
d2, c2 = create_random_file(fold_subcfg)
self.assertTrue(os.path.exists(d2))
self.addCleanup(clean, d2)
df2, _ = create_random_file(fold_subcfg)
self.assertTrue(os.path.exists(df2))
self.addCleanup(clean, df2)
d3, c3 = create_random_file(fold_tmp)
self.assertTrue(os.path.exists(d3))
self.addCleanup(clean, d3)
df3, _ = create_random_file(fold_tmp)
self.assertTrue(os.path.exists(df3))
self.addCleanup(clean, df3)
d4, c4 = create_random_file(fold_tmp, binary=True)
self.assertTrue(os.path.exists(d4))
self.addCleanup(clean, d4)
df4, _ = create_random_file(fold_tmp, binary=True)
self.assertTrue(os.path.exists(df4))
self.addCleanup(clean, df4)
d5 = get_tempdir()
self.assertTrue(os.path.exists(d5))
self.addCleanup(clean, d5)
df5 = get_tempdir()
self.assertTrue(os.path.exists(df5))
self.addCleanup(clean, df5)
d6, _ = create_random_file(d5)
self.assertTrue(os.path.exists(d6))
df6, _ = create_random_file(df5)
self.assertTrue(os.path.exists(df6))
d9 = get_tempdir()
self.assertTrue(os.path.exists(d9))
self.addCleanup(clean, d9)
d9sub = os.path.join(d9, get_string(5))
df9 = get_tempdir()
self.assertTrue(os.path.exists(df9))
self.addCleanup(clean, df9)
d9sub = os.path.join(df9, get_string(5))
create_dir(d9sub)
d9f1, _ = create_random_file(d9sub)
@@ -112,69 +115,70 @@ class TestCompare(unittest.TestCase):
backup=self.CONFIG_BACKUP,
create=self.CONFIG_CREATE)
self.assertTrue(os.path.exists(confpath))
o = load_options(confpath, profile)
o.longkey = True
o.debug = True
dfiles = [d1, d2, d3, d4, d5, d9]
opt = load_options(confpath, profile)
opt.longkey = True
opt.debug = True
dfiles = [df1, df2, df3, df4, df5, df9]
# import the files
o.import_path = dfiles
cmd_importer(o)
o = load_options(confpath, profile)
opt.import_path = dfiles
cmd_importer(opt)
opt = load_options(confpath, profile)
# compare the files
expected = {d1: True, d2: True, d3: True, d4: True,
d5: True, d9: True}
results = self.compare(o, tmp, len(dfiles))
expected = {df1: True, df2: True, df3: True, df4: True,
df5: True, df9: True}
results = self.compare(opt, tmp, len(dfiles))
self.assertTrue(results == expected)
# modify file
edit_content(d1, get_string(20))
expected = {d1: False, d2: True, d3: True, d4: True,
d5: True, d9: True}
results = self.compare(o, tmp, len(dfiles))
edit_content(df1, get_string(20))
expected = {df1: False, df2: True, df3: True, df4: True,
df5: True, df9: True}
results = self.compare(opt, tmp, len(dfiles))
self.assertTrue(results == expected)
# modify binary file
edit_content(d4, bytes(get_string(20), 'ascii'), binary=True)
expected = {d1: False, d2: True, d3: True, d4: False,
d5: True, d9: True}
results = self.compare(o, tmp, len(dfiles))
edit_content(df4, bytes(get_string(20), 'ascii'), binary=True)
expected = {df1: False, df2: True, df3: True, df4: False,
df5: True, df9: True}
results = self.compare(opt, tmp, len(dfiles))
self.assertTrue(results == expected)
# add file in directory
d7, _ = create_random_file(d5)
self.assertTrue(os.path.exists(d7))
expected = {d1: False, d2: True, d3: True, d4: False,
d5: False, d9: True}
results = self.compare(o, tmp, len(dfiles))
df7, _ = create_random_file(df5)
self.assertTrue(os.path.exists(df7))
expected = {df1: False, df2: True, df3: True, df4: False,
df5: False, df9: True}
results = self.compare(opt, tmp, len(dfiles))
self.assertTrue(results == expected)
# modify all files
edit_content(d2, get_string(20))
edit_content(d3, get_string(21))
expected = {d1: False, d2: False, d3: False, d4: False,
d5: False, d9: True}
results = self.compare(o, tmp, len(dfiles))
edit_content(df2, get_string(20))
edit_content(df3, get_string(21))
expected = {df1: False, df2: False, df3: False, df4: False,
df5: False, df9: True}
results = self.compare(opt, tmp, len(dfiles))
self.assertTrue(results == expected)
# edit sub file
edit_content(d9f1, get_string(12))
expected = {d1: False, d2: False, d3: False, d4: False,
d5: False, d9: False}
results = self.compare(o, tmp, len(dfiles))
expected = {df1: False, df2: False, df3: False, df4: False,
df5: False, df9: False}
results = self.compare(opt, tmp, len(dfiles))
self.assertTrue(results == expected)
# test compare from dotdrop
self.assertFalse(cmd_compare(o, tmp))
self.assertFalse(cmd_compare(opt, tmp))
# test focus
o.compare_focus = [d4]
self.assertFalse(cmd_compare(o, tmp))
o.compare_focus = ['/tmp/fake']
self.assertFalse(cmd_compare(o, tmp))
opt.compare_focus = [df4]
self.assertFalse(cmd_compare(opt, tmp))
opt.compare_focus = ['/tmp/fake']
self.assertFalse(cmd_compare(opt, tmp))
def main():
"""entry point"""
unittest.main()

View File

@@ -22,6 +22,7 @@ from tests.helpers import (clean, create_dir, create_fake_config,
class TestImport(unittest.TestCase):
"""test case"""
CONFIG_BACKUP = False
CONFIG_CREATE = True
@@ -33,11 +34,11 @@ class TestImport(unittest.TestCase):
self.assertTrue(os.path.exists(path))
return yaml_load(path)
def assert_file(self, path, o, profile):
def assert_file(self, path, opt, _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(any(x.src.endswith(strip) for x in opt.dotfiles))
dsts = (os.path.expanduser(x.dst) for x in opt.dotfiles)
self.assertTrue(path in dsts)
def assert_in_yaml(self, path, dic, link=False):
@@ -63,12 +64,12 @@ class TestImport(unittest.TestCase):
backup=self.CONFIG_BACKUP,
create=self.CONFIG_CREATE)
self.assertTrue(os.path.exists(confpath))
o = load_options(confpath, profile)
opt = load_options(confpath, profile)
# create some random dotfiles
dotfile1, content1 = create_random_file(src)
dotfile1, _ = create_random_file(src)
self.addCleanup(clean, dotfile1)
dotfile2, content2 = create_random_file(os.path.expanduser('~'))
dotfile2, _ = create_random_file(os.path.expanduser('~'))
self.addCleanup(clean, dotfile2)
homeconf = os.path.join(os.path.expanduser('~'), '.config')
if not os.path.exists(homeconf):
@@ -77,8 +78,8 @@ class TestImport(unittest.TestCase):
dotconfig = os.path.join(homeconf, get_string(5))
create_dir(dotconfig)
self.addCleanup(clean, dotconfig)
dotfile3, content3 = create_random_file(dotconfig)
dotfile4, content3 = create_random_file(homeconf)
dotfile3, _ = create_random_file(dotconfig)
dotfile4, _ = create_random_file(homeconf)
self.addCleanup(clean, dotfile4)
# fake a directory containing dotfiles
@@ -89,7 +90,7 @@ class TestImport(unittest.TestCase):
sub2, _ = create_random_file(dotfile5)
# fake a file for symlink
dotfile6, content6 = create_random_file(dotconfig)
dotfile6, _ = create_random_file(dotconfig)
self.addCleanup(clean, dotfile6)
# fake a directory for symlink
@@ -101,37 +102,37 @@ class TestImport(unittest.TestCase):
# import the dotfiles
dfiles = [dotfile1, dotfile2, dotfile3, dotfile4, dotfile5]
o.import_path = dfiles
cmd_importer(o)
opt.import_path = dfiles
cmd_importer(opt)
# import symlink
o.import_link = LinkTypes.LINK
opt.import_link = LinkTypes.LINK
sfiles = [dotfile6, dotfile7]
o.import_path = sfiles
cmd_importer(o)
o.import_link = LinkTypes.NOLINK
opt.import_path = sfiles
cmd_importer(opt)
opt.import_link = LinkTypes.NOLINK
# reload the config
o = load_options(confpath, profile)
opt = load_options(confpath, profile)
# test dotfiles in config class
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)
self.assert_file(dotfile4, o, profile)
self.assert_file(dotfile5, o, profile)
self.assert_file(dotfile6, o, profile)
self.assert_file(dotfile7, o, profile)
self.assertTrue(profile in [p.key for p in opt.profiles])
self.assert_file(dotfile1, opt, profile)
self.assert_file(dotfile2, opt, profile)
self.assert_file(dotfile3, opt, profile)
self.assert_file(dotfile4, opt, profile)
self.assert_file(dotfile5, opt, profile)
self.assert_file(dotfile6, opt, profile)
self.assert_file(dotfile7, opt, profile)
# test dotfiles in yaml file
y = self.load_yaml(confpath)
self.assert_in_yaml(dotfile1, y)
self.assert_in_yaml(dotfile2, y)
self.assert_in_yaml(dotfile3, y)
self.assert_in_yaml(dotfile4, y)
self.assert_in_yaml(dotfile5, y)
self.assert_in_yaml(dotfile6, y, link=True)
self.assert_in_yaml(dotfile7, y, link=True)
cont2 = self.load_yaml(confpath)
self.assert_in_yaml(dotfile1, cont2)
self.assert_in_yaml(dotfile2, cont2)
self.assert_in_yaml(dotfile3, cont2)
self.assert_in_yaml(dotfile4, cont2)
self.assert_in_yaml(dotfile5, cont2)
self.assert_in_yaml(dotfile6, cont2, link=True)
self.assert_in_yaml(dotfile7, cont2, link=True)
# test have been imported in dotdrop dotpath directory
indt1 = os.path.join(dotfilespath,
@@ -154,16 +155,16 @@ class TestImport(unittest.TestCase):
self.CONFIG_DOTPATH,
get_path_strip_version(dotfile5))
self.assertTrue(os.path.exists(indt5))
s1 = os.path.join(dotfilespath,
fsb1 = os.path.join(dotfilespath,
self.CONFIG_DOTPATH,
get_path_strip_version(dotfile6),
sub1)
self.assertTrue(os.path.exists(s1))
s2 = os.path.join(dotfilespath,
self.assertTrue(os.path.exists(fsb1))
fsb2 = os.path.join(dotfilespath,
self.CONFIG_DOTPATH,
get_path_strip_version(dotfile6),
sub2)
self.assertTrue(os.path.exists(s2))
self.assertTrue(os.path.exists(fsb2))
indt6 = os.path.join(dotfilespath,
self.CONFIG_DOTPATH,
get_path_strip_version(dotfile6))
@@ -172,29 +173,31 @@ class TestImport(unittest.TestCase):
self.CONFIG_DOTPATH,
get_path_strip_version(dotfile7))
self.assertTrue(os.path.exists(indt7))
s3 = os.path.join(dotfilespath,
fsb3 = os.path.join(dotfilespath,
self.CONFIG_DOTPATH,
get_path_strip_version(dotfile7),
sub3)
self.assertTrue(os.path.exists(s3))
s4 = os.path.join(dotfilespath,
self.assertTrue(os.path.exists(fsb3))
fsb4 = os.path.join(dotfilespath,
self.CONFIG_DOTPATH,
get_path_strip_version(dotfile7),
sub4)
self.assertTrue(os.path.exists(s4))
self.assertTrue(os.path.exists(fsb4))
cmd_list_profiles(o)
cmd_files(o)
cmd_list_profiles(opt)
cmd_files(opt)
# fake test update
editcontent = 'edited'
edit_content(dotfile1, editcontent)
o.safe = False
o.update_path = [dotfile1]
o.debug = True
cmd_update(o)
c2 = open(indt1, 'r').read()
self.assertTrue(editcontent == c2)
opt.safe = False
opt.update_path = [dotfile1]
opt.debug = True
cmd_update(opt)
cont = ''
with open(indt1, 'r', encoding='utf-8') as file:
cont = file.read()
self.assertTrue(editcontent == cont)
def test_ext_config_yaml_not_mix(self):
"""Test whether the import_configs mixes yaml files upon importing."""
@@ -311,37 +314,37 @@ class TestImport(unittest.TestCase):
})
# import the dotfiles
o = load_options(imported_path, 'host1')
o.import_path = dotfiles_ed
cmd_importer(o)
opt = load_options(imported_path, 'host1')
opt.import_path = dotfiles_ed
cmd_importer(opt)
o = load_options(importing_path, 'host2')
o.import_path = dotfiles_ing
cmd_importer(o)
opt = load_options(importing_path, 'host2')
opt.import_path = dotfiles_ing
cmd_importer(opt)
# reload the config
o = load_options(importing_path, 'host2')
opt = load_options(importing_path, 'host2')
# test imported config
y = self.load_yaml(imported_path)
ycont = self.load_yaml(imported_path)
# testing dotfiles
self.assertTrue(all(file_in_yaml(y, df)
self.assertTrue(all(file_in_yaml(ycont, df)
for df in dotfiles_ed))
self.assertFalse(any(file_in_yaml(y, df)
self.assertFalse(any(file_in_yaml(ycont, df)
for df in dotfiles_ing))
# testing profiles
profiles = y['profiles'].keys()
profiles = ycont['profiles'].keys()
self.assertTrue('host1' in profiles)
self.assertFalse('host2' in profiles)
# testing actions
actions = y['actions']['pre']
actions.update(y['actions']['post'])
actions = ycont['actions']['pre']
actions.update(ycont['actions']['post'])
actions.update({
k: v
for k, v in y['actions'].items()
for k, v in ycont['actions'].items()
if k not in ('pre', 'post')
})
actions = actions.keys()
@@ -349,41 +352,41 @@ class TestImport(unittest.TestCase):
self.assertFalse(any(a.endswith('ing') for a in actions))
# testing transformations
transformations = y['trans_read'].keys()
transformations = ycont['trans_read'].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()
transformations = ycont['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 = self._remove_priv_vars(y['variables'].keys())
variables = self._remove_priv_vars(ycont['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()
dyn_variables = ycont['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)
ycont = self.load_yaml(importing_path)
# testing dotfiles
self.assertTrue(all(file_in_yaml(y, df)
self.assertTrue(all(file_in_yaml(ycont, df)
for df in dotfiles_ing))
self.assertFalse(any(file_in_yaml(y, df)
self.assertFalse(any(file_in_yaml(ycont, df)
for df in dotfiles_ed))
# testing profiles
profiles = y['profiles'].keys()
profiles = ycont['profiles'].keys()
self.assertTrue('host2' in profiles)
self.assertFalse('host1' in profiles)
# testing actions
actions = y['actions']['pre']
actions.update(y['actions']['post'])
actions = ycont['actions']['pre']
actions.update(ycont['actions']['post'])
actions.update({
k: v
for k, v in y['actions'].items()
for k, v in ycont['actions'].items()
if k not in ('pre', 'post')
})
actions = actions.keys()
@@ -391,18 +394,18 @@ class TestImport(unittest.TestCase):
self.assertFalse(any(action.endswith('ed') for action in actions))
# testing transformations
transformations = y['trans_read'].keys()
transformations = ycont['trans_read'].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()
transformations = ycont['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 = self._remove_priv_vars(y['variables'].keys())
variables = self._remove_priv_vars(ycont['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()
dyn_variables = ycont['dynvariables'].keys()
self.assertTrue(all(dv.endswith('ing') for dv in dyn_variables))
self.assertFalse(any(dv.endswith('ed') for dv in dyn_variables))
@@ -414,6 +417,7 @@ class TestImport(unittest.TestCase):
def main():
"""entry point"""
unittest.main()

View File

@@ -8,11 +8,10 @@ import os
import unittest
from unittest.mock import MagicMock
import filecmp
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)
from dotdrop.cfg_aggregator import CfgAggregator as Cfg
from dotdrop.dotfile import Dotfile
from dotdrop.installer import Installer
from dotdrop.action import Action
@@ -23,6 +22,7 @@ from dotdrop.linktypes import LinkTypes
class TestInstall(unittest.TestCase):
"""test case"""
CONFIG_NAME = 'config.yaml'
@@ -40,37 +40,38 @@ exec bspwm
'''
def fake_config(self, path, dotfiles, profile,
dotpath, actions, trans):
dotpath, actions, transs):
"""Create a fake config file"""
with open(path, 'w') as f:
f.write('actions:\n')
with open(path, 'w', encoding='utf-8') as file:
file.write('actions:\n')
for action in actions:
f.write(' {}: {}\n'.format(action.key, action.action))
f.write('trans:\n')
for tr in trans:
f.write(' {}: {}\n'.format(tr.key, tr.action))
f.write('config:\n')
f.write(' backup: true\n')
f.write(' create: true\n')
f.write(' dotpath: {}\n'.format(dotpath))
f.write('dotfiles:\n')
for d in dotfiles:
f.write(' {}:\n'.format(d.key))
f.write(' dst: {}\n'.format(d.dst))
f.write(' src: {}\n'.format(d.src))
f.write(' link: {}\n'.format(d.link.name.lower()))
if len(d.actions) > 0:
f.write(' actions:\n')
for action in d.actions:
f.write(' - {}\n'.format(action.key))
if d.trans_r:
for tr in d.trans_r:
f.write(' trans_read: {}\n'.format(tr.key))
f.write('profiles:\n')
f.write(' {}:\n'.format(profile))
f.write(' dotfiles:\n')
for d in dotfiles:
f.write(' - {}\n'.format(d.key))
file.write(f' {action.key}: {action.action}\n')
file.write('trans:\n')
for trans in transs:
file.write(f' {trans.key}: {trans.action}\n')
file.write('config:\n')
file.write(' backup: true\n')
file.write(' create: true\n')
file.write(f' dotpath: {dotpath}\n')
file.write('dotfiles:\n')
for dotfile in dotfiles:
linkval = dotfile.link.name.lower()
file.write(f' {dotfile.key}:\n')
file.write(f' dst: {dotfile.dst}\n')
file.write(f' src: {dotfile.src}\n')
file.write(f' link: {linkval}\n')
if len(dotfile.actions) > 0:
file.write(' actions:\n')
for action in dotfile.actions:
file.write(f' - {action.key}\n')
if dotfile.trans_r:
for trans in dotfile.trans_r:
file.write(f' trans_read: {trans.key}\n')
file.write('profiles:\n')
file.write(f' {profile}:\n')
file.write(' dotfiles:\n')
for dotfile in dotfiles:
file.write(f' - {dotfile.key}\n')
return path
def test_install(self):
@@ -87,40 +88,43 @@ exec bspwm
self.addCleanup(clean, dst)
# create the dotfile in dotdrop
f1, c1 = create_random_file(tmp)
fcontent1, _ = create_random_file(tmp)
dst1 = os.path.join(dst, get_string(6))
d1 = Dotfile(get_string(5), dst1, os.path.basename(f1))
dotfile1 = Dotfile(get_string(5), dst1, os.path.basename(fcontent1))
# fake a __str__
self.assertTrue(str(d1) != '')
f2, c2 = create_random_file(tmp)
self.assertTrue(str(dotfile1) != '')
fcontent2, _ = create_random_file(tmp)
dst2 = os.path.join(dst, get_string(6))
d2 = Dotfile(get_string(5), dst2, os.path.basename(f2))
with open(f2, 'w') as f:
f.write(self.TEMPLATE)
f3, _ = create_random_file(tmp, binary=True)
dotfile2 = Dotfile(get_string(5), dst2, os.path.basename(fcontent2))
with open(fcontent2, 'w', encoding='utf-8') as file:
file.write(self.TEMPLATE)
fcontent3, _ = create_random_file(tmp, binary=True)
dst3 = os.path.join(dst, get_string(6))
d3 = Dotfile(get_string(5), dst3, os.path.basename(f3))
dotfile3 = Dotfile(get_string(5), dst3, os.path.basename(fcontent3))
# create a directory dotfile
dir1 = os.path.join(tmp, 'somedir')
create_dir(dir1)
fd, _ = create_random_file(dir1)
fildfd, _ = create_random_file(dir1)
dstd = os.path.join(dst, get_string(6))
ddot = Dotfile(get_string(5), dstd, os.path.basename(dir1))
# to test backup
f4, c4 = create_random_file(tmp)
fcontent4, _ = create_random_file(tmp)
dst4 = os.path.join(dst, get_string(6))
d4 = Dotfile(key=get_string(6), dst=dst4, src=os.path.basename(f4))
with open(dst4, 'w') as f:
f.write(get_string(16))
dotfile4 = Dotfile(key=get_string(6),
dst=dst4,
src=os.path.basename(fcontent4))
with open(dst4, 'w',
encoding='utf-8') as file:
file.write(get_string(16))
# to test link
f5, c5 = create_random_file(tmp)
fcontent5, _ = create_random_file(tmp)
dst5 = os.path.join(dst, get_string(6))
self.addCleanup(clean, dst5)
d5 = Dotfile(get_string(6), dst5,
os.path.basename(f5), link=LinkTypes.LINK)
dotfile5 = Dotfile(get_string(6), dst5,
os.path.basename(fcontent5), link=LinkTypes.LINK)
# create the dotfile directories in dotdrop
dir1 = create_dir(os.path.join(tmp, get_string(6)))
@@ -133,7 +137,7 @@ exec bspwm
sub2, _ = create_random_file(dir1)
self.assertTrue(os.path.exists(sub2))
# make up the dotfile
d6 = Dotfile(get_string(6), dst6, os.path.basename(dir1))
dotfile6 = Dotfile(get_string(6), dst6, os.path.basename(dir1))
# to test symlink directories
dir2 = create_dir(os.path.join(tmp, get_string(6)))
@@ -146,47 +150,52 @@ exec bspwm
sub4, _ = create_random_file(dir2)
self.assertTrue(os.path.exists(sub4))
# make up the dotfile
d7 = Dotfile(get_string(6), dst7,
os.path.basename(dir2), link=LinkTypes.LINK)
dotfile7 = Dotfile(get_string(6), dst7,
os.path.basename(dir2), link=LinkTypes.LINK)
# to test actions
value = get_string(12)
fact = '/tmp/action'
self.addCleanup(clean, fact)
act1 = Action('testaction', 'post', 'echo "{}" > {}'.format(value,
fact))
f8, c8 = create_random_file(tmp)
act1 = Action('testaction', 'post', f'echo "{value}" > {fact}')
fcontent8, _ = create_random_file(tmp)
dst8 = os.path.join(dst, get_string(6))
d8 = Dotfile(get_string(6), dst8, os.path.basename(f8), actions=[act1])
dotfile8 = Dotfile(get_string(6), dst8, os.path.basename(fcontent8),
actions=[act1])
# to test transformations
trans1 = 'trans1'
trans2 = 'trans2'
# pylint: disable=C0209
cmd = 'cat {0} | sed \'s/%s/%s/g\' > {1}' % (trans1, trans2)
tr = Action('testtrans', 'post', cmd)
f9, c9 = create_random_file(tmp, content=trans1)
self.addCleanup(clean, trans2)
the_trans = Action('testtrans', 'post', cmd)
fcontent9, _ = create_random_file(tmp, content=trans1)
dst9 = os.path.join(dst, get_string(6))
d9 = Dotfile(get_string(6), dst9, os.path.basename(f9), trans_r=[tr])
dotfile9 = Dotfile(get_string(6), dst9, os.path.basename(fcontent9),
trans_r=[the_trans])
# to test template
f10, _ = create_random_file(tmp, content='{{@@ header() @@}}')
dst10 = os.path.join(dst, get_string(6))
d10 = Dotfile(get_string(6), dst10, os.path.basename(f10))
dotfile10 = Dotfile(get_string(6), dst10, os.path.basename(f10))
# generate the config and stuff
profile = get_string(5)
confpath = os.path.join(tmp, self.CONFIG_NAME)
dotfiles = [d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, ddot]
dotfiles = [dotfile1, dotfile2, dotfile3, dotfile4,
dotfile5, dotfile6, dotfile7, dotfile8,
dotfile9, dotfile10, ddot]
self.fake_config(confpath, dotfiles,
profile, tmp, [act1], [tr])
profile, tmp, [act1], [the_trans])
conf = Cfg(confpath, profile, debug=True)
self.assertTrue(conf is not None)
# install them
o = load_options(confpath, profile)
o.safe = False
o.install_showdiff = True
cmd_install(o)
opt = load_options(confpath, profile)
opt.safe = False
opt.install_showdiff = True
cmd_install(opt)
# now compare the generated files
self.assertTrue(os.path.exists(dst1))
@@ -197,39 +206,47 @@ exec bspwm
self.assertTrue(os.path.exists(dst7))
self.assertTrue(os.path.exists(dst8))
self.assertTrue(os.path.exists(dst10))
self.assertTrue(os.path.exists(fd))
self.assertTrue(os.path.exists(fildfd))
# check if 'dst5' is a link whose target is 'f5'
self.assertTrue(os.path.islink(dst5))
self.assertTrue(os.path.realpath(dst5) == os.path.realpath(f5))
self.assertTrue(os.path.realpath(dst5) == os.path.realpath(fcontent5))
# check if 'dst7' is a link whose target is 'dir2'
self.assertTrue(os.path.islink(dst7))
self.assertTrue(os.path.realpath(dst7) == os.path.realpath(dir2))
# make sure backup is there
b = dst4 + BACKUP_SUFFIX
self.assertTrue(os.path.exists(b))
backupf = dst4 + BACKUP_SUFFIX
self.assertTrue(os.path.exists(backupf))
self.assertTrue(filecmp.cmp(f1, dst1, shallow=True))
f2content = open(dst2, 'r').read()
self.assertTrue(filecmp.cmp(fcontent1, dst1, shallow=True))
f2content = ''
with open(dst2, 'r', encoding='utf-8') as file:
f2content = file.read()
self.assertTrue(f2content == self.RESULT)
self.assertTrue(filecmp.cmp(f3, dst3, shallow=True))
self.assertTrue(filecmp.cmp(fcontent3, dst3, shallow=True))
# test action has been executed
self.assertTrue(os.path.exists(fact))
self.assertTrue(str(act1) != '')
actcontent = open(fact, 'r').read().rstrip()
actcontent = ''
with open(fact, 'r', encoding='utf-8') as file:
actcontent = file.read().rstrip()
self.assertTrue(actcontent == value)
# test transformation has been done
self.assertTrue(os.path.exists(dst9))
transcontent = open(dst9, 'r').read().rstrip()
transcontent = ''
with open(dst9, 'r', encoding='utf-8') as file:
transcontent = file.read().rstrip()
self.assertTrue(transcontent == trans2)
# test template has been remplaced
self.assertTrue(os.path.exists(dst10))
tempcontent = open(dst10, 'r').read().rstrip()
tempcontent = ''
with open(dst10, 'r', encoding='utf-8') as file:
tempcontent = file.read().rstrip()
self.assertTrue(tempcontent == header())
def test_install_import_configs(self):
@@ -251,7 +268,7 @@ exec bspwm
imported_dotfile, _ = create_random_file(os.path.join(tmp, 'imported'))
imported_dotfile = {
'dst': os.path.join(dst, imported_dotfile),
'key': 'f_{}'.format(imported_dotfile),
'key': f'f_{imported_dotfile}',
'name': imported_dotfile,
'src': os.path.join(tmp, 'imported', imported_dotfile),
}
@@ -259,7 +276,7 @@ exec bspwm
create_random_file(os.path.join(tmp, 'importing'))
importing_dotfile = {
'dst': os.path.join(dst, importing_dotfile),
'key': 'f_{}'.format(importing_dotfile),
'key': f'f_{importing_dotfile}',
'name': importing_dotfile,
'src': os.path.join(tmp, 'imported', importing_dotfile),
}
@@ -323,11 +340,11 @@ exec bspwm
})
# install them
o = load_options(importing_path, 'host2')
o.safe = False
o.install_showdiff = True
o.variables = {}
cmd_install(o)
opt = load_options(importing_path, 'host2')
opt.safe = False
opt.install_showdiff = True
opt.variables = {}
cmd_install(opt)
# now compare the generated files
self.assertTrue(os.path.exists(importing_dotfile['dst']))
@@ -355,8 +372,8 @@ exec bspwm
# Ensure all destination files point to source
for src in srcs:
dst = os.path.join(dst_dir, src)
self.assertEqual(os.path.realpath(dst), os.path.realpath(src))
xyz = os.path.join(dst_dir, src)
self.assertEqual(os.path.realpath(xyz), os.path.realpath(src))
def test_fails_without_src(self):
"""test fails without src"""
@@ -372,8 +389,8 @@ exec bspwm
actionexec=None)
self.assertFalse(res)
e = 'source dotfile does not exist: {}'.format(src)
self.assertEqual(err, e)
exp = f'source dotfile does not exist: {src}'
self.assertEqual(err, exp)
def test_fails_when_src_file(self):
"""test fails when src file"""
@@ -397,8 +414,8 @@ exec bspwm
# ensure nothing performed
self.assertFalse(res)
e = 'source dotfile is not a directory: {}'.format(src)
self.assertEqual(err, e)
exp = f'source dotfile is not a directory: {src}'
self.assertEqual(err, exp)
def test_creates_dst(self):
"""test creates dst"""
@@ -435,7 +452,7 @@ exec bspwm
# Create destination file to be replaced
dst = os.path.join(dst_dir, get_string(6))
with open(dst, 'w'):
with open(dst, 'w', encoding='utf=8'):
pass
self.assertTrue(os.path.isfile(dst))
@@ -457,8 +474,7 @@ exec bspwm
# ensure prompted
ask.assert_called_with(
'Remove regular file {} and replace with empty directory?'
.format(dst))
f'Remove regular file {dst} and replace with empty directory?')
def test_runs_templater(self):
"""test runs templater"""
@@ -484,15 +500,16 @@ exec bspwm
linktype=LinkTypes.LINK_CHILDREN, actionexec=None)
for src in srcs:
dst = os.path.join(dst_dir, os.path.basename(src))
xyz = os.path.join(dst_dir, os.path.basename(src))
# ensure dst is link
self.assertTrue(os.path.islink(dst))
self.assertTrue(os.path.islink(xyz))
# ensure dst not directly linked to src
self.assertNotEqual(os.path.realpath(dst), src)
self.assertNotEqual(os.path.realpath(xyz), src)
def main():
"""entry point"""
unittest.main()

View File

@@ -6,16 +6,16 @@ basic unittest for jhelpers
import os
import unittest
from dotdrop.cfg_aggregator import CfgAggregator
from tests.helpers import (clean,
create_random_file, get_string, get_tempdir,
load_options)
from dotdrop.cfg_aggregator import CfgAggregator
from dotdrop.dotfile import Dotfile
from dotdrop.dotdrop import cmd_install
class TestJhelpers(unittest.TestCase):
"""test case"""
CONFIG_NAME = 'config.yaml'
@@ -58,19 +58,19 @@ dirname: /tmp/a/b
def fake_config(self, path, dotfile, profile, dotpath):
"""Create a fake config file"""
with open(path, 'w') as f:
f.write('config:\n')
f.write(' backup: true\n')
f.write(' create: true\n')
f.write(' dotpath: {}\n'.format(dotpath))
f.write('dotfiles:\n')
f.write(' {}:\n'.format(dotfile.key))
f.write(' dst: {}\n'.format(dotfile.dst))
f.write(' src: {}\n'.format(dotfile.src))
f.write('profiles:\n')
f.write(' {}:\n'.format(profile))
f.write(' dotfiles:\n')
f.write(' - {}\n'.format(dotfile.key))
with open(path, 'w', encoding='utf-8') as file:
file.write('config:\n')
file.write(' backup: true\n')
file.write(' create: true\n')
file.write(f' dotpath: {dotpath}\n')
file.write('dotfiles:\n')
file.write(f' {dotfile.key}:\n')
file.write(f' dst: {dotfile.dst}\n')
file.write(f' src: {dotfile.src}\n')
file.write('profiles:\n')
file.write(f' {profile}:\n')
file.write(' dotfiles:\n')
file.write(f' - {dotfile.key}\n')
return path
def test_jhelpers(self):
@@ -87,34 +87,37 @@ dirname: /tmp/a/b
self.addCleanup(clean, dst)
# create the dotfile in dotdrop
f1, c1 = create_random_file(tmp)
with open(f1, 'w') as f:
f.write(self.TEMPLATE)
file1, _ = create_random_file(tmp)
with open(file1, 'w', encoding='utf-8') as file:
file.write(self.TEMPLATE)
dst1 = os.path.join(dst, get_string(6))
d1 = Dotfile(get_string(5), dst1, os.path.basename(f1))
dotfile1 = Dotfile(get_string(5), dst1, os.path.basename(file1))
# generate the config and stuff
profile = get_string(5)
confpath = os.path.join(tmp, self.CONFIG_NAME)
self.fake_config(confpath, d1, profile, tmp)
self.fake_config(confpath, dotfile1, profile, tmp)
conf = CfgAggregator(confpath, profile, debug=True)
self.assertTrue(conf is not None)
# install them
o = load_options(confpath, profile)
o.safe = False
o.install_showdiff = True
o.variables = {}
o.debug = True
cmd_install(o)
opt = load_options(confpath, profile)
opt.safe = False
opt.install_showdiff = True
opt.variables = {}
opt.debug = True
cmd_install(opt)
# now compare the generated files
self.assertTrue(os.path.exists(dst1))
f1content = open(dst1, 'r').read()
f1content = ''
with open(dst1, 'r', encoding='utf-8') as file:
f1content = file.read()
self.assertTrue(f1content == self.RESULT)
def main():
"""entry point"""
unittest.main()

View File

@@ -49,23 +49,23 @@ class TestListings(unittest.TestCase):
self.addCleanup(clean, dotfilespath)
# create the dotfiles to test
d1, c1 = create_random_file(fold_config)
self.assertTrue(os.path.exists(d1))
self.addCleanup(clean, d1)
d2, c2 = create_random_file(fold_subcfg)
self.assertTrue(os.path.exists(d2))
self.addCleanup(clean, d2)
d3, c3 = create_random_file(fold_tmp)
self.assertTrue(os.path.exists(d3))
self.addCleanup(clean, d3)
d4, c4 = create_random_file(fold_tmp, binary=True)
self.assertTrue(os.path.exists(d4))
self.addCleanup(clean, d4)
d5 = get_tempdir()
self.assertTrue(os.path.exists(d5))
self.addCleanup(clean, d5)
d6, _ = create_random_file(d5)
self.assertTrue(os.path.exists(d6))
file1, _ = create_random_file(fold_config)
self.assertTrue(os.path.exists(file1))
self.addCleanup(clean, file1)
file2, _ = create_random_file(fold_subcfg)
self.assertTrue(os.path.exists(file2))
self.addCleanup(clean, file2)
file3, _ = create_random_file(fold_tmp)
self.assertTrue(os.path.exists(file3))
self.addCleanup(clean, file3)
file4, _ = create_random_file(fold_tmp, binary=True)
self.assertTrue(os.path.exists(file4))
self.addCleanup(clean, file4)
file5 = get_tempdir()
self.assertTrue(os.path.exists(file5))
self.addCleanup(clean, file5)
file6, _ = create_random_file(file5)
self.assertTrue(os.path.exists(file6))
# create the config file
profile = get_string(5)
@@ -75,29 +75,30 @@ class TestListings(unittest.TestCase):
backup=self.CONFIG_BACKUP,
create=self.CONFIG_CREATE)
self.assertTrue(os.path.exists(confpath))
o = load_options(confpath, profile)
dfiles = [d1, d2, d3, d4, d5]
opt = load_options(confpath, profile)
dfiles = [file1, file2, file3, file4, file5]
# import the files
o.import_path = dfiles
cmd_importer(o)
o = load_options(confpath, profile)
opt.import_path = dfiles
cmd_importer(opt)
opt = load_options(confpath, profile)
# files
cmd_list_profiles(o)
cmd_list_profiles(opt)
# list files
o.files_templateonly = False
cmd_files(o)
o.files_templateonly = True
cmd_files(o)
opt.files_templateonly = False
cmd_files(opt)
opt.files_templateonly = True
cmd_files(opt)
# details
o.detail_keys = None
cmd_detail(o)
opt.detail_keys = None
cmd_detail(opt)
def main():
"""entry point"""
unittest.main()

View File

@@ -15,6 +15,7 @@ from tests.helpers import (clean, create_dir,
class TestRemove(unittest.TestCase):
"""test case"""
def load_yaml(self, path):
"""Load yaml to dict"""
@@ -68,13 +69,13 @@ class TestRemove(unittest.TestCase):
}
yaml_dump(configdic, confpath)
o = load_options(confpath, 'host1')
o.remove_path = ['f_test1']
o.remove_iskey = True
o.debug = True
o.safe = False
opt = load_options(confpath, 'host1')
opt.remove_path = ['f_test1']
opt.remove_iskey = True
opt.debug = True
opt.safe = False
# by key
cmd_remove(o)
cmd_remove(opt)
# ensure file is deleted
self.assertFalse(os.path.exists(df1))
@@ -82,48 +83,49 @@ class TestRemove(unittest.TestCase):
self.assertTrue(os.path.exists(df3))
# load dict
y = yaml_load(confpath)
cont = yaml_load(confpath)
# ensure not present
self.assertTrue('f_test1' not in y['dotfiles'])
self.assertTrue('f_test1' not in y['profiles']['host1']['dotfiles'])
self.assertTrue('host2' not in y['profiles'])
self.assertTrue('f_test1' not in cont['dotfiles'])
self.assertTrue('f_test1' not in cont['profiles']['host1']['dotfiles'])
self.assertTrue('host2' not in cont['profiles'])
# assert rest is intact
self.assertTrue('f_test2' in y['dotfiles'].keys())
self.assertTrue('f_test3' in y['dotfiles'].keys())
self.assertTrue('f_test2' in y['profiles']['host1']['dotfiles'])
self.assertTrue('f_test3' in y['profiles']['host1']['dotfiles'])
self.assertTrue(y['profiles']['host3']['dotfiles'] == ['f_test2'])
self.assertTrue('f_test2' in cont['dotfiles'].keys())
self.assertTrue('f_test3' in cont['dotfiles'].keys())
self.assertTrue('f_test2' in cont['profiles']['host1']['dotfiles'])
self.assertTrue('f_test3' in cont['profiles']['host1']['dotfiles'])
self.assertTrue(cont['profiles']['host3']['dotfiles'] == ['f_test2'])
o = load_options(confpath, 'host1')
o.remove_path = ['/tmp/some-fake-path']
o.remove_iskey = False
o.debug = True
o.safe = False
opt = load_options(confpath, 'host1')
opt.remove_path = ['/tmp/some-fake-path']
opt.remove_iskey = False
opt.debug = True
opt.safe = False
# by path
cmd_remove(o)
cmd_remove(opt)
# ensure file is deleted
self.assertTrue(os.path.exists(df2))
self.assertFalse(os.path.exists(df3))
# load dict
y = yaml_load(confpath)
cont = yaml_load(confpath)
# ensure not present
self.assertTrue('f_test3' not in y['dotfiles'])
self.assertTrue('f_test3' not in y['profiles']['host1']['dotfiles'])
self.assertTrue('f_test3' not in cont['dotfiles'])
self.assertTrue('f_test3' not in cont['profiles']['host1']['dotfiles'])
# assert rest is intact
self.assertTrue('host1' in y['profiles'].keys())
self.assertFalse('host2' in y['profiles'].keys())
self.assertTrue('host3' in y['profiles'].keys())
self.assertTrue(y['profiles']['host1']['dotfiles'] == ['f_test2'])
self.assertTrue(y['profiles']['host3']['dotfiles'] == ['f_test2'])
self.assertTrue('host1' in cont['profiles'].keys())
self.assertFalse('host2' in cont['profiles'].keys())
self.assertTrue('host3' in cont['profiles'].keys())
self.assertTrue(cont['profiles']['host1']['dotfiles'] == ['f_test2'])
self.assertTrue(cont['profiles']['host3']['dotfiles'] == ['f_test2'])
def main():
"""entry point"""
unittest.main()

View File

@@ -47,13 +47,13 @@ class TestUpdate(unittest.TestCase):
self.addCleanup(clean, dotfilespath)
# create the dotfiles to test
d1, _ = create_random_file(fold_config)
self.assertTrue(os.path.exists(d1))
self.addCleanup(clean, d1)
dotfilefile1, _ = create_random_file(fold_config)
self.assertTrue(os.path.exists(dotfilefile1))
self.addCleanup(clean, dotfilefile1)
d2, _ = create_random_file(fold_config)
self.assertTrue(os.path.exists(d2))
self.addCleanup(clean, d2)
dotfilefile2, _ = create_random_file(fold_config)
self.assertTrue(os.path.exists(dotfilefile2))
self.addCleanup(clean, dotfilefile2)
# template
d3t, _ = create_random_file(fold_config)
@@ -96,7 +96,7 @@ class TestUpdate(unittest.TestCase):
self.assertTrue(os.path.exists(confpath))
opt = load_options(confpath, profile)
opt.update_showpatch = True
dfiles = [d1, dir1, d2, d3t, dsubstmp]
dfiles = [dotfilefile1, dir1, dotfilefile2, d3t, dsubstmp]
# import the files
opt.import_path = dfiles
@@ -138,7 +138,7 @@ class TestUpdate(unittest.TestCase):
self.assertFalse(os.path.exists(gone))
# edit the files
edit_content(d1, 'newcontent')
edit_content(dotfilefile1, 'newcontent')
edit_content(dirf1, 'newcontent')
# add more file
@@ -150,12 +150,12 @@ class TestUpdate(unittest.TestCase):
create_random_file(dpath)
# update it
opt.update_path = [d1, dir1]
opt.update_path = [dotfilefile1, dir1]
cmd_update(opt)
# test content
newcontent = ''
with open(d1, 'r', encoding='utf-8') as file:
with open(dotfilefile1, 'r', encoding='utf-8') as file:
newcontent = file.read()
self.assertTrue(newcontent == 'newcontent')
newcontent = ''
@@ -163,14 +163,14 @@ class TestUpdate(unittest.TestCase):
newcontent = file.read()
self.assertTrue(newcontent == 'newcontent')
edit_content(d2, 'newcontentbykey')
edit_content(dotfilefile2, 'newcontentbykey')
# update it by key
dfiles = opt.dotfiles
d2key = ''
for dotfile in dfiles:
src = os.path.expanduser(dotfile.dst)
if src == d2:
if src == dotfilefile2:
d2key = dotfile.key
break
self.assertTrue(d2key != '')
@@ -180,7 +180,7 @@ class TestUpdate(unittest.TestCase):
# test content
newcontent = ''
with open(d2, 'r', encoding='utf-8') as file:
with open(dotfilefile2, 'r', encoding='utf-8') as file:
newcontent = file.read()
self.assertTrue(newcontent == 'newcontentbykey')

View File

@@ -19,6 +19,7 @@ from tests.helpers import (SubsetTestCase, _fake_args, clean,
class TestConfig(SubsetTestCase):
"""test case"""
CONFIG_BACKUP = False
CONFIG_CREATE = True
@@ -52,6 +53,8 @@ class TestConfig(SubsetTestCase):
self.assertTrue(conf.dump() != '')
def test_def_link(self):
"""unittest"""
# pylint: disable=E1120
self._test_link_import('nolink', LinkTypes.ABSOLUTE, 'absolute')
self._test_link_import('nolink', LinkTypes.RELATIVE, 'relative')
self._test_link_import('nolink', LinkTypes.NOLINK, 'nolink')
@@ -75,7 +78,7 @@ class TestConfig(SubsetTestCase):
@patch('dotdrop.cfg_yaml.os.path.exists', create=True)
def _test_link_import(self, cfgstring, expected,
cliargs, mock_exists, mock_open):
data = '''
data = f'''
config:
backup: true
create: true
@@ -83,11 +86,11 @@ config:
banner: true
longkey: false
keepdot: false
link_on_import: {}
link_on_import: {cfgstring}
link_dotfile_default: nolink
dotfiles:
profiles:
'''.format(cfgstring)
'''
mock_open.side_effect = [
unittest.mock.mock_open(read_data=data).return_value,
@@ -100,14 +103,14 @@ profiles:
args['--cfg'] = 'mocked'
args['--link'] = cliargs
args['--verbose'] = True
o = Options(args=args)
opt = Options(args=args)
self.assertTrue(o.import_link == expected)
self.assertTrue(opt.import_link == expected)
@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 = '''
data = f'''
config:
backup: true
create: true
@@ -115,11 +118,11 @@ config:
banner: true
longkey: false
keepdot: false
link_on_import: {}
link_on_import: {value}
link_dotfile_default: nolink
dotfiles:
profiles:
'''.format(value)
'''
mock_open.side_effect = [
unittest.mock.mock_open(read_data=data).return_value
@@ -132,10 +135,11 @@ profiles:
args['--verbose'] = True
with self.assertRaises(YamlException):
o = Options(args=args)
print(o.import_link)
opt = Options(args=args)
print(opt.import_link)
def test_include(self):
"""unittest"""
tmp = get_tempdir()
self.assertTrue(os.path.exists(tmp))
self.addCleanup(clean, tmp)
@@ -358,32 +362,32 @@ profiles:
self.assertIsNotNone(imported_cfg)
# test profiles
self.assertIsSubset(imported_cfg.profiles,
importing_cfg.profiles)
self.assert_is_subset(imported_cfg.profiles,
importing_cfg.profiles)
# test dotfiles
self.assertIsSubset(imported_cfg.dotfiles, importing_cfg.dotfiles)
self.assert_is_subset(imported_cfg.dotfiles, importing_cfg.dotfiles)
# test actions
pre_ed = post_ed = pre_ing = post_ing = {}
for k, v in imported_cfg.actions.items():
kind, _ = v
for k, val in imported_cfg.actions.items():
kind, _ = val
if kind == 'pre':
pre_ed[k] = v
pre_ed[k] = val
elif kind == 'post':
post_ed[k] = v
for k, v in importing_cfg.actions.items():
kind, _ = v
post_ed[k] = val
for k, val in importing_cfg.actions.items():
kind, _ = val
if kind == 'pre':
pre_ing[k] = v
pre_ing[k] = val
elif kind == 'post':
post_ing[k] = v
self.assertIsSubset(pre_ed, pre_ing)
self.assertIsSubset(post_ed, post_ing)
post_ing[k] = val
self.assert_is_subset(pre_ed, pre_ing)
self.assert_is_subset(post_ed, post_ing)
# test transactions
self.assertIsSubset(imported_cfg.trans_r, importing_cfg.trans_r)
self.assertIsSubset(imported_cfg.trans_w, importing_cfg.trans_w)
self.assert_is_subset(imported_cfg.trans_r, importing_cfg.trans_r)
self.assert_is_subset(imported_cfg.trans_w, importing_cfg.trans_w)
# test variables
imported_vars = {
@@ -396,10 +400,10 @@ profiles:
for k, v in importing_cfg.variables.items()
if not k.startswith('_')
}
self.assertIsSubset(imported_vars, importing_vars)
self.assert_is_subset(imported_vars, importing_vars)
# test prodots
self.assertIsSubset(imported_cfg.profiles, importing_cfg.profiles)
self.assert_is_subset(imported_cfg.profiles, importing_cfg.profiles)
def test_import_configs_override(self):
"""Test import_configs when some config keys overlap."""
@@ -563,8 +567,8 @@ profiles:
self.assertIsNotNone(imported_cfg)
# test profiles
self.assertIsSubset(imported_cfg.profiles,
importing_cfg.profiles)
self.assert_is_subset(imported_cfg.profiles,
importing_cfg.profiles)
# test dotfiles
self.assertEqual(importing_cfg.dotfiles['f_vimrc'],
@@ -609,6 +613,7 @@ profiles:
def main():
"""entry point"""
unittest.main()