From 75837890589ab90aa193e534cc539ce4cddef33a Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Wed, 15 Mar 2017 21:40:54 +0100 Subject: [PATCH] adding tests for compare --- tests/test_compare.py | 116 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 tests/test_compare.py diff --git a/tests/test_compare.py b/tests/test_compare.py new file mode 100644 index 0000000..3f0af8a --- /dev/null +++ b/tests/test_compare.py @@ -0,0 +1,116 @@ +""" +author: deadc0de6 (https://github.com/deadc0de6) +Copyright (c) 2017, deadc0de6 +basic unittest for the compare function +""" + + +import unittest +import os +import yaml + +from dotdrop.config import Cfg +from dotdrop.dotdrop import importer +from dotdrop.dotfile import Dotfile +from dotdrop.installer import Installer +from dotdrop.templategen import Templategen + +from tests.helpers import * + + +class TestCompare(unittest.TestCase): + + CONFIG_BACKUP = False + CONFIG_CREATE = True + CONFIG_DOTPATH = 'dotfiles' + CONFIG_NAME = 'config.yaml' + + def compare(self, opts, conf, tmp, nbdotfiles): + dotfiles = conf.get_dotfiles(opts['profile']) + self.assertTrue(len(dotfiles) == nbdotfiles) + t = Templategen(base=opts['dotpath']) + inst = Installer(create=opts['create'], backup=opts['backup'], + dry=opts['dry'], base=opts['dotpath'], quiet=True) + results = {} + for dotfile in dotfiles: + diffval = inst.compare(t, tmp, opts['profile'], + dotfile.src, dotfile.dst) + print("diffval: ", diffval) + path = os.path.expanduser(dotfile.dst) + results[path] = diffval + return results + + def edit_content(self, path, newcontent): + with open(path, 'w') as f: + f.write(newcontent) + + def test_compare(self): + '''Test the compare function''' + # setup some folders + fold_config = os.path.join(os.path.expanduser('~'), '.config') + create_dir(fold_config) + self.addCleanup(clean, 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.addCleanup(clean, d1) + d2, c2 = create_random_file(fold_subcfg) + self.addCleanup(clean, d2) + d3, c3 = create_random_file(fold_tmp) + self.addCleanup(clean, d3) + + # 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, self.CONFIG_DOTPATH, profile) + dfiles = [d1, d2, d3] + + # import the files + importer(opts, conf, dfiles) + conf, opts = load_config(confpath, self.CONFIG_DOTPATH, profile) + + # compare the files + expected = {d1: True, d2: True, d3: 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} + 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} + results = self.compare(opts, conf, tmp, len(dfiles)) + self.assertTrue(results == expected) + + +def main(): + unittest.main() + +if __name__ == '__main__': + main()