From b7f5495f0902b52a98d4530db01d9b8659403232 Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Tue, 4 Jun 2019 23:42:49 +0200 Subject: [PATCH] add remove tests and fix bugs --- dotdrop/dotdrop.py | 12 ++-- tests/test_remove.py | 136 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 143 insertions(+), 5 deletions(-) create mode 100644 tests/test_remove.py diff --git a/dotdrop/dotdrop.py b/dotdrop/dotdrop.py index 86a964b..31eb1cf 100644 --- a/dotdrop/dotdrop.py +++ b/dotdrop/dotdrop.py @@ -420,17 +420,13 @@ def cmd_remove(o): LOG.log('no dotfile to remove') return False if o.debug: - LOG.dbg('dotfile to remove: {}'.format(paths)) + LOG.dbg('dotfile(s) to remove: {}'.format(','.join(paths))) removed = [] for key in paths: - if o.debug: - LOG.dbg('removing {}'.format(key)) if not iskey: # by path - print(key) dotfile = o.conf.get_dotfile_by_dst(key) - print(dotfile) if not dotfile: LOG.warn('{} ignored, does not exist'.format(key)) continue @@ -438,7 +434,13 @@ def cmd_remove(o): else: # by key dotfile = o.conf.get_dotfile(key) + if not dotfile: + LOG.warn('{} ignored, does not exist'.format(key)) + continue k = key + if o.debug: + LOG.dbg('removing {}'.format(key)) + # make sure is part of the profile if dotfile.key not in [d.key for d in o.dotfiles]: LOG.warn('{} ignored, not associated to this profile'.format(key)) diff --git a/tests/test_remove.py b/tests/test_remove.py new file mode 100644 index 0000000..df01dce --- /dev/null +++ b/tests/test_remove.py @@ -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()