mirror of
https://github.com/deadc0de6/dotdrop.git
synced 2026-02-09 10:04:17 +00:00
add remove tests and fix bugs
This commit is contained in:
@@ -420,17 +420,13 @@ def cmd_remove(o):
|
|||||||
LOG.log('no dotfile to remove')
|
LOG.log('no dotfile to remove')
|
||||||
return False
|
return False
|
||||||
if o.debug:
|
if o.debug:
|
||||||
LOG.dbg('dotfile to remove: {}'.format(paths))
|
LOG.dbg('dotfile(s) to remove: {}'.format(','.join(paths)))
|
||||||
|
|
||||||
removed = []
|
removed = []
|
||||||
for key in paths:
|
for key in paths:
|
||||||
if o.debug:
|
|
||||||
LOG.dbg('removing {}'.format(key))
|
|
||||||
if not iskey:
|
if not iskey:
|
||||||
# by path
|
# by path
|
||||||
print(key)
|
|
||||||
dotfile = o.conf.get_dotfile_by_dst(key)
|
dotfile = o.conf.get_dotfile_by_dst(key)
|
||||||
print(dotfile)
|
|
||||||
if not dotfile:
|
if not dotfile:
|
||||||
LOG.warn('{} ignored, does not exist'.format(key))
|
LOG.warn('{} ignored, does not exist'.format(key))
|
||||||
continue
|
continue
|
||||||
@@ -438,7 +434,13 @@ def cmd_remove(o):
|
|||||||
else:
|
else:
|
||||||
# by key
|
# by key
|
||||||
dotfile = o.conf.get_dotfile(key)
|
dotfile = o.conf.get_dotfile(key)
|
||||||
|
if not dotfile:
|
||||||
|
LOG.warn('{} ignored, does not exist'.format(key))
|
||||||
|
continue
|
||||||
k = key
|
k = key
|
||||||
|
if o.debug:
|
||||||
|
LOG.dbg('removing {}'.format(key))
|
||||||
|
|
||||||
# make sure is part of the profile
|
# make sure is part of the profile
|
||||||
if dotfile.key not in [d.key for d in o.dotfiles]:
|
if dotfile.key not in [d.key for d in o.dotfiles]:
|
||||||
LOG.warn('{} ignored, not associated to this profile'.format(key))
|
LOG.warn('{} ignored, not associated to this profile'.format(key))
|
||||||
|
|||||||
136
tests/test_remove.py
Normal file
136
tests/test_remove.py
Normal file
@@ -0,0 +1,136 @@
|
|||||||
|
"""
|
||||||
|
author: deadc0de6 (https://github.com/deadc0de6)
|
||||||
|
Copyright (c) 2019, deadc0de6
|
||||||
|
basic unittest for the remove function
|
||||||
|
"""
|
||||||
|
|
||||||
|
import yaml
|
||||||
|
import unittest
|
||||||
|
import os
|
||||||
|
|
||||||
|
# local imports
|
||||||
|
from dotdrop.dotdrop import cmd_remove
|
||||||
|
from tests.helpers import (clean, create_dir,
|
||||||
|
create_random_file, load_options,
|
||||||
|
get_tempdir)
|
||||||
|
|
||||||
|
|
||||||
|
class TestRemove(unittest.TestCase):
|
||||||
|
|
||||||
|
def load_yaml(self, path):
|
||||||
|
"""Load yaml to dict"""
|
||||||
|
self.assertTrue(os.path.exists(path))
|
||||||
|
content = ''
|
||||||
|
with open(path, 'r') as f:
|
||||||
|
content = yaml.safe_load(f)
|
||||||
|
return content
|
||||||
|
|
||||||
|
def test_remove(self):
|
||||||
|
"""test the remove command"""
|
||||||
|
|
||||||
|
# dotfiles in dotpath
|
||||||
|
dotdrop_home = get_tempdir()
|
||||||
|
self.assertTrue(os.path.exists(dotdrop_home))
|
||||||
|
self.addCleanup(clean, dotdrop_home)
|
||||||
|
|
||||||
|
dotfilespath = os.path.join(dotdrop_home, 'dotfiles')
|
||||||
|
confpath = os.path.join(dotdrop_home, 'config.yaml')
|
||||||
|
create_dir(dotfilespath)
|
||||||
|
|
||||||
|
df1, _ = create_random_file(dotfilespath)
|
||||||
|
df2, _ = create_random_file(dotfilespath)
|
||||||
|
df3, _ = create_random_file(dotfilespath)
|
||||||
|
configdic = {
|
||||||
|
'config': {
|
||||||
|
'dotpath': 'dotfiles',
|
||||||
|
},
|
||||||
|
'dotfiles': {
|
||||||
|
'f_test1': {
|
||||||
|
'src': df1,
|
||||||
|
'dst': '/dev/null'
|
||||||
|
},
|
||||||
|
'f_test2': {
|
||||||
|
'src': df2,
|
||||||
|
'dst': '/dev/null'
|
||||||
|
},
|
||||||
|
'f_test3': {
|
||||||
|
'src': df3,
|
||||||
|
'dst': '/tmp/some-fake-path'
|
||||||
|
},
|
||||||
|
},
|
||||||
|
'profiles': {
|
||||||
|
'host1': {
|
||||||
|
'dotfiles': ['f_test1', 'f_test2', 'f_test3'],
|
||||||
|
},
|
||||||
|
'host2': {
|
||||||
|
'dotfiles': ['f_test1'],
|
||||||
|
},
|
||||||
|
'host3': {
|
||||||
|
'dotfiles': ['f_test2'],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
with open(confpath, 'w') as f:
|
||||||
|
yaml.safe_dump(configdic, f)
|
||||||
|
o = load_options(confpath, 'host1')
|
||||||
|
o.remove_path = ['f_test1']
|
||||||
|
o.remove_iskey = True
|
||||||
|
o.debug = True
|
||||||
|
o.safe = False
|
||||||
|
# by key
|
||||||
|
cmd_remove(o)
|
||||||
|
|
||||||
|
# ensure file is deleted
|
||||||
|
self.assertFalse(os.path.exists(df1))
|
||||||
|
self.assertTrue(os.path.exists(df2))
|
||||||
|
self.assertTrue(os.path.exists(df3))
|
||||||
|
|
||||||
|
# load dict
|
||||||
|
y = self.load_yaml(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'])
|
||||||
|
|
||||||
|
# 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'])
|
||||||
|
|
||||||
|
o = load_options(confpath, 'host1')
|
||||||
|
o.remove_path = ['/tmp/some-fake-path']
|
||||||
|
o.remove_iskey = False
|
||||||
|
o.debug = True
|
||||||
|
o.safe = False
|
||||||
|
# by path
|
||||||
|
cmd_remove(o)
|
||||||
|
|
||||||
|
# ensure file is deleted
|
||||||
|
self.assertTrue(os.path.exists(df2))
|
||||||
|
self.assertFalse(os.path.exists(df3))
|
||||||
|
|
||||||
|
# load dict
|
||||||
|
y = self.load_yaml(confpath)
|
||||||
|
|
||||||
|
# ensure not present
|
||||||
|
self.assertTrue('f_test3' not in y['dotfiles'])
|
||||||
|
self.assertTrue('f_test3' not in y['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'])
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
unittest.main()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user