Skip to content

Commit

Permalink
update tests and improve
Browse files Browse the repository at this point in the history
  • Loading branch information
deadc0de6 committed Sep 25, 2018
1 parent 8d60e0c commit 3819511
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 24 deletions.
9 changes: 9 additions & 0 deletions tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
18 changes: 6 additions & 12 deletions tests/test_compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,15 @@ def compare(self, opts, conf, tmp, nbdotfiles):
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))
path = os.path.expanduser(dotfile.dst)
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
Expand Down Expand Up @@ -118,13 +112,13 @@ def test_compare(self):
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)
Expand All @@ -137,8 +131,8 @@ def test_compare(self):
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)
Expand Down
3 changes: 1 addition & 2 deletions tests/test_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,7 @@ def test_import(self):

# 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()
Expand Down
14 changes: 13 additions & 1 deletion tests/test_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,16 @@ def test_install(self):
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)
Expand All @@ -179,6 +185,7 @@ def test_install(self):
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'
Expand Down Expand Up @@ -209,6 +216,11 @@ def test_install(self):
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()
Expand Down
16 changes: 7 additions & 9 deletions tests/test_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -79,12 +72,17 @@ def test_update(self):
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
Expand Down

0 comments on commit 3819511

Please sign in to comment.