From 3819511e725c375533197ea75fb757071b483098 Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Tue, 25 Sep 2018 08:51:56 +0200 Subject: [PATCH] update tests and improve --- tests/helpers.py | 9 +++++++++ tests/test_compare.py | 18 ++++++------------ tests/test_import.py | 3 +-- tests/test_install.py | 14 +++++++++++++- tests/test_update.py | 16 +++++++--------- 5 files changed, 36 insertions(+), 24 deletions(-) diff --git a/tests/helpers.py b/tests/helpers.py index 293777d..4ebec85 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -55,6 +55,15 @@ def create_random_file(directory, content=None, binary=False): return path, content +def edit_content(path, newcontent, binary=False): + '''edit file content''' + mode = 'w' + if binary: + mode = 'wb' + with open(path, mode) as f: + f.write(newcontent) + + def create_dir(path): '''Create a directory''' if not os.path.exists(path): diff --git a/tests/test_compare.py b/tests/test_compare.py index 649f47a..3e04ed2 100644 --- a/tests/test_compare.py +++ b/tests/test_compare.py @@ -41,7 +41,8 @@ class TestCompare(unittest.TestCase): if not ret: results[path] = False continue - diff = comp.compare(insttmp, dotfile.dst) + diff = comp.compare(insttmp, dotfile.dst, + ignore=['whatever', 'whatelse']) print('XXXX diff for {} and {}:\n{}'.format(dotfile.src, dotfile.dst, diff)) @@ -49,13 +50,6 @@ class TestCompare(unittest.TestCase): results[path] = diff == '' return results - def edit_content(self, path, newcontent, binary=False): - mode = 'w' - if binary: - mode = 'wb' - with open(path, mode) as f: - f.write(newcontent) - def test_compare(self): '''Test the compare function''' # setup some directories @@ -118,13 +112,13 @@ class TestCompare(unittest.TestCase): self.assertTrue(results == expected) # modify file - self.edit_content(d1, get_string(20)) + 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) + 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) @@ -137,8 +131,8 @@ class TestCompare(unittest.TestCase): self.assertTrue(results == expected) # modify all files - self.edit_content(d2, get_string(20)) - self.edit_content(d3, get_string(21)) + edit_content(d2, get_string(20)) + 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) diff --git a/tests/test_import.py b/tests/test_import.py index 0c35f36..75b0edc 100644 --- a/tests/test_import.py +++ b/tests/test_import.py @@ -199,8 +199,7 @@ class TestImport(unittest.TestCase): # fake test update editcontent = 'edited' - with open(dotfile1, 'w') as f: - f.write('edited') + edit_content(dotfile1, editcontent) opts['safe'] = False update(opts, conf, [dotfile1]) c2 = open(indt1, 'r').read() diff --git a/tests/test_install.py b/tests/test_install.py index 02103f0..3c7145a 100644 --- a/tests/test_install.py +++ b/tests/test_install.py @@ -156,10 +156,16 @@ exec bspwm dst9 = os.path.join(dst, get_string(6)) d9 = Dotfile(get_string(6), dst9, os.path.basename(f9), trans=[tr]) + # to test template + f10, _ = create_random_file(tmp, content='{{@@ profile @@}}') + dst10 = os.path.join(dst, get_string(6)) + d10 = Dotfile(get_string(6), dst10, os.path.basename(f10)) + # generate the config and stuff profile = get_string(5) confpath = os.path.join(tmp, self.CONFIG_NAME) - self.fake_config(confpath, [d1, d2, d3, d4, d5, d6, d7, d8, d9, ddot], + dotfiles = [d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, ddot] + self.fake_config(confpath, dotfiles, profile, tmp, [act1], [tr]) conf = Cfg(confpath) self.assertTrue(conf is not None) @@ -179,6 +185,7 @@ exec bspwm self.assertTrue(os.path.exists(dst6)) self.assertTrue(os.path.exists(dst7)) self.assertTrue(os.path.exists(dst8)) + self.assertTrue(os.path.exists(dst10)) self.assertTrue(os.path.exists(fd)) # check if 'dst5' is a link whose target is 'f5' @@ -209,6 +216,11 @@ exec bspwm transcontent = open(dst9, 'r').read().rstrip() self.assertTrue(transcontent == trans2) + # test template has been remplaced + self.assertTrue(os.path.exists(dst10)) + tempcontent = open(dst10, 'r').read().rstrip() + self.assertTrue(tempcontent == profile) + def main(): unittest.main() diff --git a/tests/test_update.py b/tests/test_update.py index 3a226b8..6f7dfc6 100644 --- a/tests/test_update.py +++ b/tests/test_update.py @@ -24,13 +24,6 @@ class TestUpdate(unittest.TestCase): CONFIG_DOTPATH = 'dotfiles' CONFIG_NAME = 'config.yaml' - def edit_content(self, path, newcontent, binary=False): - mode = 'w' - if binary: - mode = 'wb' - with open(path, mode) as f: - f.write(newcontent) - def test_update(self): '''Test the update function''' # setup some directories @@ -79,12 +72,17 @@ class TestUpdate(unittest.TestCase): conf, opts = load_config(confpath, profile) # edit the files - self.edit_content(d1, 'newcontent') - self.edit_content(dirf1, 'newcontent') + 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