def test_import(self): '''Test the import function''' # on filesystem src = get_tempfolder() self.assertTrue(os.path.exists(src)) self.addCleanup(clean, src) # in dotdrop dotfilespath = get_tempfolder() 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 folder containing dotfiles dotfile5 = get_tempfolder() 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 folder for symlink dotfile7 = get_tempfolder() 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] importer(opts, conf, dfiles) # import symlink opts[Cfg.key_dotfiles_link] = True sfiles = [dotfile6, dotfile7] 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 folder 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)
def test_compare(self): '''Test the compare function''' # setup some folders 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_tempfolder() create_dir(fold_tmp) self.addCleanup(clean, fold_tmp) # create the folders tmp = get_tempfolder() self.assertTrue(os.path.exists(tmp)) self.addCleanup(clean, tmp) dotfilespath = get_tempfolder() 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_tempfolder() 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)) conf, opts = load_config(confpath, profile) dfiles = [d1, d2, d3, d4, d5] # import the files importer(opts, conf, dfiles) conf, opts = load_config(confpath, profile) # compare the files expected = {d1: True, d2: True, d3: True, d4: True, d5: True} results = self.compare(opts, conf, tmp, len(dfiles)) self.assertTrue(results == expected) # modify file self.edit_content(d1, get_string(20)) expected = {d1: False, d2: True, d3: True, d4: True, d5: True} results = self.compare(opts, conf, tmp, len(dfiles)) self.assertTrue(results == expected) # modify binary file self.edit_content(d4, bytes(get_string(20), 'ascii'), binary=True) expected = {d1: False, d2: True, d3: True, d4: False, d5: True} results = self.compare(opts, conf, tmp, len(dfiles)) self.assertTrue(results == expected) # add file in folder d7, _ = create_random_file(d5) self.assertTrue(os.path.exists(d7)) expected = {d1: False, d2: True, d3: True, d4: False, d5: False} results = self.compare(opts, conf, tmp, len(dfiles)) self.assertTrue(results == expected) # modify all files self.edit_content(d2, get_string(20)) self.edit_content(d3, get_string(21)) expected = {d1: False, d2: False, d3: False, d4: False, d5: False} results = self.compare(opts, conf, tmp, len(dfiles)) self.assertTrue(results == expected) # test compare from dotdrop self.assertFalse(compare(opts, conf, tmp)) # test focus self.assertFalse(compare(opts, conf, tmp, focus=d4)) self.assertFalse(compare(opts, conf, tmp, focus='/tmp/fake'))
def test_import(self): '''Test the import function''' src = get_tempfolder() self.assertTrue(os.path.exists(src)) self.addCleanup(clean, src) dotfilespath = get_tempfolder() 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, self.CONFIG_DOTPATH, 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 folder containing dotfiles dotfile5 = get_tempfolder() self.assertTrue(os.path.exists(dotfile5)) self.addCleanup(clean, dotfile5) sub1, _ = create_random_file(dotfile5) sub2, _ = create_random_file(dotfile5) # import the dotfiles dfiles = [dotfile1, dotfile2, dotfile3, dotfile4, dotfile5] importer(opts, conf, dfiles) # reload the config conf, opts = load_config(confpath, self.CONFIG_DOTPATH, 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) # 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) # test dotfiles on filesystem self.assertTrue(os.path.exists(os.path.join(dotfilespath, dotfile1))) self.assertTrue(os.path.exists(os.path.join(dotfilespath, dotfile2))) self.assertTrue(os.path.exists(os.path.join(dotfilespath, dotfile3))) self.assertTrue(os.path.exists(os.path.join(dotfilespath, dotfile4))) self.assertTrue(os.path.exists(os.path.join(dotfilespath, dotfile5))) self.assertTrue( os.path.exists(os.path.join(dotfilespath, dotfile5, sub1))) self.assertTrue( os.path.exists(os.path.join(dotfilespath, dotfile5, sub2)))
def test_import(self): '''Test the import function''' # on filesystem src = get_tempfolder() self.assertTrue(os.path.exists(src)) self.addCleanup(clean, src) # in dotdrop dotfilespath = get_tempfolder() 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 folder containing dotfiles dotfile5 = get_tempfolder() 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 folder for symlink dotfile7 = get_tempfolder() 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] importer(opts, conf, dfiles) # import symlink opts[Cfg.key_dotfiles_link] = True sfiles = [dotfile6, dotfile7] 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 folder 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) list_profiles(conf) list_files(opts, conf) header() # fake test update editcontent = 'edited' with open(dotfile1, 'w') as f: f.write('edited') opts['safe'] = False update(opts, conf, dotfile1) c2 = open(indt1, 'r').read() self.assertTrue(editcontent == c2)
def test_compare(self): '''Test the compare function''' # setup some folders 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_tempfolder() create_dir(fold_tmp) self.addCleanup(clean, fold_tmp) # create the folders tmp = get_tempfolder() self.assertTrue(os.path.exists(tmp)) self.addCleanup(clean, tmp) dotfilespath = get_tempfolder() 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_tempfolder() 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)) conf, opts = load_config(confpath, profile) dfiles = [d1, d2, d3, d4, d5] # import the files importer(opts, conf, dfiles) conf, opts = load_config(confpath, profile) # compare the files expected = {d1: True, d2: True, d3: True, d4: True, d5: True} results = self.compare(opts, conf, tmp, len(dfiles)) self.assertTrue(results == expected) # modify file self.edit_content(d1, get_string(20)) expected = {d1: False, d2: True, d3: True, d4: True, d5: True} results = self.compare(opts, conf, tmp, len(dfiles)) self.assertTrue(results == expected) # modify binary file self.edit_content(d4, bytes(get_string(20), 'ascii'), binary=True) expected = {d1: False, d2: True, d3: True, d4: False, d5: True} results = self.compare(opts, conf, tmp, len(dfiles)) self.assertTrue(results == expected) # add file in folder d7, _ = create_random_file(d5) self.assertTrue(os.path.exists(d7)) expected = {d1: False, d2: True, d3: True, d4: False, d5: False} results = self.compare(opts, conf, tmp, len(dfiles)) self.assertTrue(results == expected) # modify all files self.edit_content(d2, get_string(20)) self.edit_content(d3, get_string(21)) expected = {d1: False, d2: False, d3: False, d4: False, d5: False} results = self.compare(opts, conf, tmp, len(dfiles)) self.assertTrue(results == expected) # test compare from dotdrop self.assertTrue(compare(opts, conf, tmp)) # test focus self.assertTrue(compare(opts, conf, tmp, focus=d4)) self.assertFalse(compare(opts, conf, tmp, focus='/tmp/fake'))