def test_include(self): tmp = get_tempdir() self.assertTrue(os.path.exists(tmp)) self.addCleanup(clean, tmp) # create a base config file confpath = create_fake_config(tmp, configname=self.CONFIG_NAME, dotpath=self.CONFIG_DOTPATH, backup=self.CONFIG_BACKUP, create=self.CONFIG_CREATE) # edit the config content = yaml_load(confpath) # adding dotfiles df1key = 'f_vimrc' df2key = 'f_xinitrc' content['dotfiles'] = { df1key: { 'dst': '~/.vimrc', 'src': 'vimrc' }, df2key: { 'dst': '~/.xinitrc', 'src': 'xinitrc' } } # adding profiles pf1key = 'host1' pf2key = 'host2' content['profiles'] = { pf1key: { 'dotfiles': [df2key], 'include': ['host2'] }, pf2key: { 'dotfiles': [df1key] } } # save the new config yaml_dump(content, confpath) # do the tests conf = Cfg(confpath, debug=True) self.assertTrue(conf is not None) # test profile profiles = conf.profiles self.assertTrue(pf1key in profiles) self.assertTrue(pf2key in profiles) # test dotfiles dotfiles = conf.profiles[pf1key]['dotfiles'] self.assertTrue(df1key in dotfiles) self.assertTrue(df2key in dotfiles) dotfiles = conf.profiles[pf2key]['dotfiles'] self.assertTrue(df1key in dotfiles) self.assertFalse(df2key in dotfiles) # test not existing included profile # edit the config content = yaml_load(confpath) content['profiles'] = { pf1key: { 'dotfiles': [df2key], 'include': ['host2'] }, pf2key: { 'dotfiles': [df1key], 'include': ['host3'] } } # save the new config yaml_dump(content, confpath) # do the tests conf = Cfg(confpath, debug=True) self.assertTrue(conf is not None)
def load_yaml(self, path): """Load yaml to dict""" self.assertTrue(os.path.exists(path)) return yaml_load(path)
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'], }, }, } yaml_dump(configdic, confpath) 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 = 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']) # 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 = yaml_load(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'])