def test_update(self):
        """Test the update function"""
        # setup some directories
        fold_config = os.path.join(os.path.expanduser('~'), '.config')
        create_dir(fold_config)
        fold_subcfg = os.path.join(os.path.expanduser('~'), '.config',
                                   get_string(5))
        create_dir(fold_subcfg)
        self.addCleanup(clean, fold_subcfg)
        fold_tmp = get_tempdir()
        create_dir(fold_tmp)
        self.addCleanup(clean, fold_tmp)

        # create the directories
        tmp = get_tempdir()
        self.assertTrue(os.path.exists(tmp))
        self.addCleanup(clean, tmp)

        dotfilespath = get_tempdir()
        self.assertTrue(os.path.exists(dotfilespath))
        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_config)
        self.assertTrue(os.path.exists(d2))
        self.addCleanup(clean, d2)

        # create the directory to test
        dpath = os.path.join(fold_config, get_string(5))
        dir1 = create_dir(dpath)
        dirf1, _ = create_random_file(dpath)
        self.addCleanup(clean, dir1)

        # create the config file
        profile = get_string(5)
        confpath = create_fake_config(dotfilespath,
                                      configname=self.CONFIG_NAME,
                                      dotpath=self.CONFIG_DOTPATH,
                                      backup=self.CONFIG_BACKUP,
                                      create=self.CONFIG_CREATE)
        self.assertTrue(os.path.exists(confpath))
        conf, opts = load_config(confpath, profile)
        dfiles = [d1, dir1, d2]

        # import the files
        cmd_importer(opts, conf, dfiles)
        conf, opts = load_config(confpath, profile)

        # edit the files
        edit_content(d1, 'newcontent')
        edit_content(dirf1, 'newcontent')

        # add more file
        dirf2, _ = create_random_file(dpath)

        # add more dirs
        dpath = os.path.join(dpath, get_string(5))
        create_dir(dpath)
        create_random_file(dpath)

        # update it
        opts['safe'] = False
        opts['debug'] = True
        cmd_update(opts, conf, [d1, dir1])

        # test content
        newcontent = open(d1, 'r').read()
        self.assertTrue(newcontent == 'newcontent')
        newcontent = open(dirf1, 'r').read()
        self.assertTrue(newcontent == 'newcontent')

        edit_content(d2, 'newcontentbykey')

        # update it by key
        dfiles = conf.get_dotfiles(profile)
        d2key = ''
        for ds in dfiles:
            t = os.path.expanduser(ds.dst)
            if t == d2:
                d2key = ds.key
                break
        self.assertTrue(d2key != '')
        opts['safe'] = False
        opts['debug'] = True
        cmd_update(opts, conf, [d2key], iskey=True)

        # test content
        newcontent = open(d2, 'r').read()
        self.assertTrue(newcontent == 'newcontentbykey')
Beispiel #2
0
    def test_listings(self):
        """Test the compare function"""
        # setup some directories
        fold_config = os.path.join(os.path.expanduser('~'), '.config')
        create_dir(fold_config)
        fold_subcfg = os.path.join(os.path.expanduser('~'), '.config',
                                   get_string(5))
        create_dir(fold_subcfg)
        self.addCleanup(clean, fold_subcfg)
        fold_tmp = get_tempdir()
        create_dir(fold_tmp)
        self.addCleanup(clean, fold_tmp)

        # create the directories
        tmp = get_tempdir()
        self.assertTrue(os.path.exists(tmp))
        self.addCleanup(clean, tmp)

        dotfilespath = get_tempdir()
        self.assertTrue(os.path.exists(dotfilespath))
        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))

        # create the config file
        profile = get_string(5)
        confpath = create_fake_config(dotfilespath,
                                      configname=self.CONFIG_NAME,
                                      dotpath=self.CONFIG_DOTPATH,
                                      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]

        # import the files
        o.import_path = dfiles
        cmd_importer(o)
        o = load_options(confpath, profile)

        # files
        cmd_list_profiles(o)

        # list files
        o.files_templateonly = False
        cmd_files(o)
        o.files_templateonly = True
        cmd_files(o)

        # details
        o.detail_keys = None
        cmd_detail(o)
Beispiel #3
0
    def test_compare(self):
        """Test the compare function"""
        # setup some directories
        fold_config = os.path.join(os.path.expanduser('~'), '.config')
        create_dir(fold_config)
        fold_subcfg = os.path.join(os.path.expanduser('~'), '.config',
                                   get_string(5))
        create_dir(fold_subcfg)
        self.addCleanup(clean, fold_subcfg)
        fold_tmp = get_tempdir()
        create_dir(fold_tmp)
        self.addCleanup(clean, fold_tmp)

        # create the directories
        tmp = get_tempdir()
        self.assertTrue(os.path.exists(tmp))
        self.addCleanup(clean, tmp)

        dotfilespath = get_tempdir()
        self.assertTrue(os.path.exists(dotfilespath))
        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))

        d9 = get_tempdir()
        self.assertTrue(os.path.exists(d9))
        self.addCleanup(clean, d9)
        d9sub = os.path.join(d9, get_string(5))
        create_dir(d9sub)
        d9f1, _ = create_random_file(d9sub)

        # create the config file
        profile = get_string(5)
        confpath = create_fake_config(dotfilespath,
                                      configname=self.CONFIG_NAME,
                                      dotpath=self.CONFIG_DOTPATH,
                                      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]

        # import the files
        o.import_path = dfiles
        cmd_importer(o)
        o = 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))
        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))
        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))
        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))
        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))
        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))
        self.assertTrue(results == expected)

        # test compare from dotdrop
        self.assertFalse(cmd_compare(o, tmp))
        # test focus
        o.compare_focus = [d4]
        self.assertFalse(cmd_compare(o, tmp))
        o.compare_focus = ['/tmp/fake']
        self.assertFalse(cmd_compare(o, tmp))
Beispiel #4
0
    def test_update(self):
        """Test the update function"""
        # setup some directories
        fold_config = os.path.join(os.path.expanduser('~'), '.config')
        create_dir(fold_config)
        fold_subcfg = os.path.join(os.path.expanduser('~'), '.config',
                                   get_string(5))
        create_dir(fold_subcfg)
        self.addCleanup(clean, fold_subcfg)
        fold_tmp = get_tempdir()
        create_dir(fold_tmp)
        self.addCleanup(clean, fold_tmp)

        # create the directories
        tmp = get_tempdir()
        self.assertTrue(os.path.exists(tmp))
        self.addCleanup(clean, tmp)

        dotfilespath = get_tempdir()
        self.assertTrue(os.path.exists(dotfilespath))
        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_config)
        self.assertTrue(os.path.exists(d2))
        self.addCleanup(clean, d2)

        # template
        d3t, c3t = create_random_file(fold_config)
        self.assertTrue(os.path.exists(d3t))
        self.addCleanup(clean, d3t)

        # sub dirs
        dsubstmp = get_tempdir()
        self.assertTrue(os.path.exists(dsubstmp))
        self.addCleanup(clean, dsubstmp)
        dirsubs = os.path.basename(dsubstmp)

        dir1string = 'somedir'
        dir1 = os.path.join(dsubstmp, dir1string)
        create_dir(dir1)
        dir1sub1str = 'sub1'
        sub1 = os.path.join(dir1, dir1sub1str)
        create_dir(sub1)
        dir1sub2str = 'sub2'
        sub2 = os.path.join(dir1, dir1sub2str)
        create_dir(sub2)
        f1s1, f1s1c1 = create_random_file(sub1)
        self.assertTrue(os.path.exists(f1s1))
        f1s2, f1s2c1 = create_random_file(sub2)
        self.assertTrue(os.path.exists(f1s2))

        # create the directory to test
        dpath = os.path.join(fold_config, get_string(5))
        dir1 = create_dir(dpath)
        dirf1, _ = create_random_file(dpath)
        self.addCleanup(clean, dir1)

        # create the config file
        profile = get_string(5)
        confpath = create_fake_config(dotfilespath,
                                      configname=self.CONFIG_NAME,
                                      dotpath=self.CONFIG_DOTPATH,
                                      backup=self.CONFIG_BACKUP,
                                      create=self.CONFIG_CREATE)
        self.assertTrue(os.path.exists(confpath))
        o = load_options(confpath, profile)
        o.debug = True
        o.update_showpatch = True
        dfiles = [d1, dir1, d2, d3t, dsubstmp]

        # import the files
        o.import_path = dfiles
        cmd_importer(o)

        # get new config
        o = load_options(confpath, profile)
        o.safe = False
        o.debug = True
        o.update_showpatch = True
        trans = Transform('trans', 'cp -r {0} {1}')
        d3tb = os.path.basename(d3t)
        for dotfile in o.dotfiles:
            if os.path.basename(dotfile.dst) == d3tb:
                # patch the template
                src = os.path.join(o.dotpath, dotfile.src)
                src = os.path.expanduser(src)
                edit_content(src, '{{@@ profile @@}}')
            if os.path.basename(dotfile.dst) == dirsubs:
                # retrieve the path of the sub in the dotpath
                d1indotpath = os.path.join(o.dotpath, dotfile.src)
                d1indotpath = os.path.expanduser(d1indotpath)
            dotfile.trans_w = trans

        # update template
        o.update_path = [d3t]
        self.assertFalse(cmd_update(o))

        # update sub dirs
        gone = os.path.join(d1indotpath, dir1string)
        gone = os.path.join(gone, dir1sub1str)
        self.assertTrue(os.path.exists(gone))
        clean(sub1)  # dir1sub1str
        self.assertTrue(os.path.exists(gone))
        o.update_path = [dsubstmp]
        cmd_update(o)
        self.assertFalse(os.path.exists(gone))

        # edit the files
        edit_content(d1, 'newcontent')
        edit_content(dirf1, 'newcontent')

        # add more file
        dirf2, _ = create_random_file(dpath)

        # add more dirs
        dpath = os.path.join(dpath, get_string(5))
        create_dir(dpath)
        create_random_file(dpath)

        # update it
        o.update_path = [d1, dir1]
        cmd_update(o)

        # test content
        newcontent = open(d1, 'r').read()
        self.assertTrue(newcontent == 'newcontent')
        newcontent = open(dirf1, 'r').read()
        self.assertTrue(newcontent == 'newcontent')

        edit_content(d2, 'newcontentbykey')

        # update it by key
        dfiles = o.dotfiles
        d2key = ''
        for ds in dfiles:
            t = os.path.expanduser(ds.dst)
            if t == d2:
                d2key = ds.key
                break
        self.assertTrue(d2key != '')
        o.update_path = [d2key]
        o.update_iskey = True
        cmd_update(o)

        # test content
        newcontent = open(d2, 'r').read()
        self.assertTrue(newcontent == 'newcontentbykey')
Beispiel #5
0
    def test_import(self):
        """Test the import function"""
        # on filesystem
        src = get_tempdir()
        self.assertTrue(os.path.exists(src))
        self.addCleanup(clean, src)

        # in dotdrop
        dotfilespath = get_tempdir()
        self.assertTrue(os.path.exists(dotfilespath))
        self.addCleanup(clean, dotfilespath)

        profile = get_string(10)
        confpath = create_fake_config(dotfilespath,
                                      configname=self.CONFIG_NAME,
                                      dotpath=self.CONFIG_DOTPATH,
                                      backup=self.CONFIG_BACKUP,
                                      create=self.CONFIG_CREATE)
        self.assertTrue(os.path.exists(confpath))
        o = load_options(confpath, profile)

        # create some random dotfiles
        dotfile1, content1 = create_random_file(src)
        self.addCleanup(clean, dotfile1)
        dotfile2, content2 = create_random_file(os.path.expanduser('~'))
        self.addCleanup(clean, dotfile2)
        homeconf = os.path.join(os.path.expanduser('~'), '.config')
        if not os.path.exists(homeconf):
            os.mkdir(homeconf)
            self.addCleanup(clean, homeconf)
        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)
        self.addCleanup(clean, dotfile4)

        # fake a directory containing dotfiles
        dotfile5 = get_tempdir()
        self.assertTrue(os.path.exists(dotfile5))
        self.addCleanup(clean, dotfile5)
        sub1, _ = create_random_file(dotfile5)
        sub2, _ = create_random_file(dotfile5)

        # fake a file for symlink
        dotfile6, content6 = create_random_file(dotconfig)
        self.addCleanup(clean, dotfile6)

        # fake a directory for symlink
        dotfile7 = get_tempdir()
        self.assertTrue(os.path.exists(dotfile7))
        self.addCleanup(clean, dotfile7)
        sub3, _ = create_random_file(dotfile7)
        sub4, _ = create_random_file(dotfile7)

        # import the dotfiles
        dfiles = [dotfile1, dotfile2, dotfile3, dotfile4, dotfile5]
        o.import_path = dfiles
        cmd_importer(o)
        # import symlink
        o.import_link = LinkTypes.LINK
        sfiles = [dotfile6, dotfile7]
        o.import_path = sfiles
        cmd_importer(o)
        o.import_link = LinkTypes.NOLINK

        # reload the config
        o = 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)

        # 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)

        # test have been imported in dotdrop dotpath directory
        indt1 = os.path.join(dotfilespath, self.CONFIG_DOTPATH,
                             get_path_strip_version(dotfile1))
        self.assertTrue(os.path.exists(indt1))
        indt2 = os.path.join(dotfilespath, self.CONFIG_DOTPATH,
                             get_path_strip_version(dotfile2))
        self.assertTrue(os.path.exists(indt2))
        indt3 = os.path.join(dotfilespath, self.CONFIG_DOTPATH,
                             get_path_strip_version(dotfile3))
        self.assertTrue(os.path.exists(indt3))
        indt4 = os.path.join(dotfilespath, self.CONFIG_DOTPATH,
                             get_path_strip_version(dotfile4))
        self.assertTrue(os.path.exists(indt4))
        indt5 = os.path.join(dotfilespath, self.CONFIG_DOTPATH,
                             get_path_strip_version(dotfile5))
        self.assertTrue(os.path.exists(indt5))
        s1 = 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.CONFIG_DOTPATH,
                          get_path_strip_version(dotfile6), sub2)
        self.assertTrue(os.path.exists(s2))
        indt6 = os.path.join(dotfilespath, self.CONFIG_DOTPATH,
                             get_path_strip_version(dotfile6))
        self.assertTrue(os.path.exists(indt6))
        indt7 = os.path.join(dotfilespath, self.CONFIG_DOTPATH,
                             get_path_strip_version(dotfile7))
        self.assertTrue(os.path.exists(indt7))
        s3 = 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.CONFIG_DOTPATH,
                          get_path_strip_version(dotfile7), sub4)
        self.assertTrue(os.path.exists(s4))

        cmd_list_profiles(o)
        cmd_files(o)

        # 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)
Beispiel #6
0
    def test_ext_config_yaml_not_mix(self):
        """Test whether the import_configs mixes yaml files upon importing."""
        # dotfiles on filesystem
        src = get_tempdir()
        self.assertTrue(os.path.exists(src))
        self.addCleanup(clean, src)

        # create some random dotfiles
        dotfiles = []
        for _ in range(3):
            dotfile, _ = create_random_file(src)
            dotfiles.append(dotfile)
            self.addCleanup(clean, dotfile)
        self.assertTrue(all(map(os.path.exists, dotfiles)))

        # create dotdrop home
        dotdrop_home = get_tempdir()
        self.assertTrue(os.path.exists(dotdrop_home))
        self.addCleanup(clean, dotdrop_home)

        dotpath_ed = 'imported'
        imported = {
            'config': {
                'dotpath': dotpath_ed,
            },
            'dotfiles': {},
            'profiles': {
                'host1': {
                    'dotfiles': [],
                },
            },
            'actions': {
                'pre': {
                    'a_pre_log_ed': 'echo pre 2',
                },
                'post': {
                    'a_post_log_ed': 'echo post 2',
                },
                'a_log_ed': 'echo 2',
            },
            'trans': {
                't_log_ed': 'echo 3',
            },
            'trans_write': {
                'tw_log_ed': 'echo 4',
            },
            'variables': {
                'v_log_ed': '42',
            },
            'dynvariables': {
                'dv_log_ed': 'echo 5',
            },
        }
        dotpath_ing = 'importing'
        importing = {
            'config': {
                'dotpath': dotpath_ing,
            },
            'dotfiles': {},
            'profiles': {
                'host2': {
                    'dotfiles': [],
                    'include': ['host1'],
                },
            },
            'actions': {
                'pre': {
                    'a_pre_log_ing': 'echo pre a',
                },
                'post': {
                    'a_post_log_ing': 'echo post a',
                },
                'a_log_ing': 'echo a',
            },
            'trans': {
                't_log_ing': 'echo b',
            },
            'trans_write': {
                'tw_log_ing': 'echo c',
            },
            'variables': {
                'v_log_ing': 'd',
            },
            'dynvariables': {
                'dv_log_ing': 'echo e',
            },
        }

        dotfiles_ing, dotfiles_ed = dotfiles[:-1], dotfiles[-1:]

        # create the imported base config file
        imported_path = create_fake_config(dotdrop_home,
                                           configname='config-2.yaml',
                                           **imported['config'])
        # create the importing base config file
        importing_path = create_fake_config(dotdrop_home,
                                            configname='config.yaml',
                                            import_configs=['config-2.yaml'],
                                            **importing['config'])

        # edit the imported config
        populate_fake_config(
            imported_path,
            **{k: v
               for k, v in imported.items() if k != 'config'})

        # edit the importing config
        populate_fake_config(
            importing_path,
            **{k: v
               for k, v in importing.items() if k != 'config'})

        # import the dotfiles
        o = load_options(imported_path, 'host1')
        o.import_path = dotfiles_ed
        cmd_importer(o)

        o = load_options(importing_path, 'host2')
        o.import_path = dotfiles_ing
        cmd_importer(o)

        # reload the config
        o = load_options(importing_path, 'host2')

        # test imported config
        y = self.load_yaml(imported_path)

        # testing dotfiles
        self.assertTrue(all(file_in_yaml(y, df) for df in dotfiles_ed))
        self.assertFalse(any(file_in_yaml(y, df) for df in dotfiles_ing))

        # testing profiles
        profiles = y['profiles'].keys()
        self.assertTrue('host1' in profiles)
        self.assertFalse('host2' in profiles)

        # testing actions
        actions = y['actions']['pre']
        actions.update(y['actions']['post'])
        actions.update({
            k: v
            for k, v in y['actions'].items() if k not in ('pre', 'post')
        })
        actions = actions.keys()
        self.assertTrue(all(a.endswith('ed') for a in actions))
        self.assertFalse(any(a.endswith('ing') for a in actions))

        # testing transformations
        transformations = y['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()
        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())
        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()
        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)

        # testing dotfiles
        self.assertTrue(all(file_in_yaml(y, df) for df in dotfiles_ing))
        self.assertFalse(any(file_in_yaml(y, df) for df in dotfiles_ed))

        # testing profiles
        profiles = y['profiles'].keys()
        self.assertTrue('host2' in profiles)
        self.assertFalse('host1' in profiles)

        # testing actions
        actions = y['actions']['pre']
        actions.update(y['actions']['post'])
        actions.update({
            k: v
            for k, v in y['actions'].items() if k not in ('pre', 'post')
        })
        actions = actions.keys()
        self.assertTrue(all(action.endswith('ing') for action in actions))
        self.assertFalse(any(action.endswith('ed') for action in actions))

        # testing transformations
        transformations = y['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()
        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())
        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()
        self.assertTrue(all(dv.endswith('ing') for dv in dyn_variables))
        self.assertFalse(any(dv.endswith('ed') for dv in dyn_variables))
Beispiel #7
0
    def test_import(self):
        """Test the import function"""
        # on filesystem
        src = get_tempdir()
        self.assertTrue(os.path.exists(src))
        self.addCleanup(clean, src)

        # in dotdrop
        dotfilespath = get_tempdir()
        self.assertTrue(os.path.exists(dotfilespath))
        self.addCleanup(clean, dotfilespath)

        profile = get_string(10)
        confpath = create_fake_config(dotfilespath,
                                      configname=self.CONFIG_NAME,
                                      dotpath=self.CONFIG_DOTPATH,
                                      backup=self.CONFIG_BACKUP,
                                      create=self.CONFIG_CREATE)
        self.assertTrue(os.path.exists(confpath))
        conf, opts = load_config(confpath, profile)

        # create some random dotfiles
        dotfile1, content1 = create_random_file(src)
        self.addCleanup(clean, dotfile1)
        dotfile2, content2 = create_random_file(os.path.expanduser('~'))
        self.addCleanup(clean, dotfile2)
        homeconf = os.path.join(os.path.expanduser('~'), '.config')
        if not os.path.exists(homeconf):
            os.mkdir(homeconf)
            self.addCleanup(clean, homeconf)
        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)
        self.addCleanup(clean, dotfile4)

        # fake a directory containing dotfiles
        dotfile5 = get_tempdir()
        self.assertTrue(os.path.exists(dotfile5))
        self.addCleanup(clean, dotfile5)
        sub1, _ = create_random_file(dotfile5)
        sub2, _ = create_random_file(dotfile5)

        # fake a file for symlink
        dotfile6, content6 = create_random_file(dotconfig)
        self.addCleanup(clean, dotfile6)

        # fake a directory for symlink
        dotfile7 = get_tempdir()
        self.assertTrue(os.path.exists(dotfile7))
        self.addCleanup(clean, dotfile7)
        sub3, _ = create_random_file(dotfile7)
        sub4, _ = create_random_file(dotfile7)

        # import the dotfiles
        dfiles = [dotfile1, dotfile2, dotfile3, dotfile4, dotfile5]
        cmd_importer(opts, conf, dfiles)
        # import symlink
        opts[Cfg.key_dotfiles_link] = True
        sfiles = [dotfile6, dotfile7]
        cmd_importer(opts, conf, sfiles)
        opts[Cfg.key_dotfiles_link] = False

        # reload the config
        conf, opts = load_config(confpath, profile)

        # test dotfiles in config class
        self.assertTrue(profile in conf.get_profiles())
        self.assert_file(dotfile1, conf, profile)
        self.assert_file(dotfile2, conf, profile)
        self.assert_file(dotfile3, conf, profile)
        self.assert_file(dotfile4, conf, profile)
        self.assert_file(dotfile5, conf, profile)
        self.assert_file(dotfile6, conf, profile)
        self.assert_file(dotfile7, conf, 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)

        # test have been imported in dotdrop dotpath directory
        indt1 = os.path.join(dotfilespath, self.CONFIG_DOTPATH,
                             get_path_strip_version(dotfile1))
        self.assertTrue(os.path.exists(indt1))
        indt2 = os.path.join(dotfilespath, self.CONFIG_DOTPATH,
                             get_path_strip_version(dotfile2))
        self.assertTrue(os.path.exists(indt2))
        indt3 = os.path.join(dotfilespath, self.CONFIG_DOTPATH,
                             get_path_strip_version(dotfile3))
        self.assertTrue(os.path.exists(indt3))
        indt4 = os.path.join(dotfilespath, self.CONFIG_DOTPATH,
                             get_path_strip_version(dotfile4))
        self.assertTrue(os.path.exists(indt4))
        indt5 = os.path.join(dotfilespath, self.CONFIG_DOTPATH,
                             get_path_strip_version(dotfile5))
        self.assertTrue(os.path.exists(indt5))
        s1 = 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.CONFIG_DOTPATH,
                          get_path_strip_version(dotfile6), sub2)
        self.assertTrue(os.path.exists(s2))
        indt6 = os.path.join(dotfilespath, self.CONFIG_DOTPATH,
                             get_path_strip_version(dotfile6))
        self.assertTrue(os.path.exists(indt6))
        indt7 = os.path.join(dotfilespath, self.CONFIG_DOTPATH,
                             get_path_strip_version(dotfile7))
        self.assertTrue(os.path.exists(indt7))
        s3 = 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.CONFIG_DOTPATH,
                          get_path_strip_version(dotfile7), sub4)
        self.assertTrue(os.path.exists(s4))

        # test symlink on filesystem
        self.assertTrue(os.path.islink(dotfile6))
        self.assertTrue(os.path.realpath(dotfile6) == indt6)
        self.assertTrue(os.path.islink(dotfile7))
        self.assertTrue(os.path.realpath(dotfile7) == indt7)

        cmd_list_profiles(conf)
        cmd_list_files(opts, conf)
        _header()

        # fake test update
        editcontent = 'edited'
        edit_content(dotfile1, editcontent)
        opts['safe'] = False
        cmd_update(opts, conf, [dotfile1])
        c2 = open(indt1, 'r').read()
        self.assertTrue(editcontent == c2)