diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/helpers.py b/tests/helpers.py new file mode 100644 index 0000000..21cf62d --- /dev/null +++ b/tests/helpers.py @@ -0,0 +1,65 @@ +""" +author: deadc0de6 (https://github.com/deadc0de6) +Copyright (c) 2017, deadc0de6 +helpers for the unittests +""" + +import os +import shutil +import string +import random +import tempfile + +TMPSUFFIX = '.dotdrop' + + +def clean(path): + '''Delete file or folder.''' + if not os.path.exists(path): + return + if os.path.isdir(path): + shutil.rmtree(path) + else: + os.remove(path) + + +def get_string(length): + '''Get a random string of length "length".''' + alpha = string.ascii_uppercase + string.digits + return ''.join(random.choice(alpha) for _ in range(length)) + + +def get_tempfolder(): + '''Get a temporary folder''' + return tempfile.mkdtemp(suffix=TMPSUFFIX) + + +def create_random_file(folder, content=None): + '''Create a new file in folder with random content.''' + fname = get_string(8) + if content is None: + content = get_string(100) + path = os.path.join(folder, fname) + with open(path, 'w') as f: + f.write(content) + return path, content + + +def create_dir(path): + '''Create a folder''' + os.mkdir(path) + return path + + +def create_fake_config(folder, configname='config.yaml', + dotpath='dotfiles', backup=True, create=True): + '''Create a fake config file''' + path = os.path.join(folder, configname) + with open(path, 'w') as f: + f.write('config:\n') + f.write(' backup: %s\n' % (str(backup))) + f.write(' create: %s\n' % (str(create))) + f.write(' dotpath: %s\n' % (dotpath)) + f.write('dotfiles:\n') + f.write('profiles:\n') + return path diff --git a/tests/test_config.py b/tests/test_config.py new file mode 100644 index 0000000..552b75a --- /dev/null +++ b/tests/test_config.py @@ -0,0 +1,53 @@ +""" +author: deadc0de6 (https://github.com/deadc0de6) +Copyright (c) 2017, deadc0de6 +basic unittest for the config parser +""" + + +import unittest +import os +import tempfile +import shutil + +from dotdrop.config import Cfg +from tests.helpers import * + + +class TestConfig(unittest.TestCase): + + CONFIG_BACKUP = True + CONFIG_CREATE = True + CONFIG_DOTPATH = 'dotfiles' + TMPSUFFIX = '.dotdrop' + CONFIG_NAME = 'config.yaml' + + def test_config(self): + '''Test the config class''' + tmp = get_tempfolder() + self.assertTrue(os.path.exists(tmp)) + self.addCleanup(clean, tmp) + + confpath = create_fake_config(tmp, + configname=self.CONFIG_NAME, + dotpath=self.CONFIG_DOTPATH, + backup=self.CONFIG_BACKUP, + create=self.CONFIG_CREATE) + conf = Cfg(confpath, self.CONFIG_DOTPATH) + self.assertTrue(conf is not None) + + opts = conf.get_configs() + self.assertTrue(opts is not None) + self.assertTrue(opts != {}) + self.assertTrue(opts['backup'] == self.CONFIG_BACKUP) + self.assertTrue(opts['create'] == self.CONFIG_CREATE) + dotpath = os.path.join(tmp, self.CONFIG_DOTPATH) + self.assertTrue(opts['dotpath'] == dotpath) + self.assertTrue(conf._is_valid()) + + +def main(): + unittest.main() + +if __name__ == '__main__': + main() diff --git a/tests/test_import.py b/tests/test_import.py new file mode 100644 index 0000000..995ff7a --- /dev/null +++ b/tests/test_import.py @@ -0,0 +1,128 @@ +""" +author: deadc0de6 (https://github.com/deadc0de6) +Copyright (c) 2017, deadc0de6 +basic unittest for the import function +""" + + +import unittest +import os +import yaml + +from dotdrop.config import Cfg +from dotdrop.dotdrop import importer + +from tests.helpers import * + + +class TestImport(unittest.TestCase): + + CONFIG_BACKUP = True + CONFIG_CREATE = True + CONFIG_DOTPATH = 'dotfiles' + CONFIG_NAME = 'config.yaml' + + def load_config(self, confpath, profile): + '''Load the config file from path''' + conf = Cfg(confpath, self.CONFIG_DOTPATH) + self.assertTrue(conf is not None) + opts = conf.get_configs() + opts['dry'] = False + opts['profile'] = profile + opts['safe'] = True + opts['installdiff'] = True + return conf, opts + + def load_yaml(self, path): + '''Load yaml to dict''' + self.assertTrue(os.path.exists(path)) + content = '' + with open(path, 'r') as f: + content = yaml.load(f) + return content + + def get_path_strip_version(self, path): + '''Strip a file path for conf tests''' + self.assertTrue(os.path.exists(path)) + strip = path + home = os.path.expanduser('~') + if strip.startswith(home): + strip = strip[len(home):] + strip = strip.lstrip('.' + os.sep) + return strip + + def assert_file(self, path, conf, profile): + '''Make sure "path" has been inserted in "conf" for "profile"''' + strip = self.get_path_strip_version(path) + self.assertTrue(strip in [x.src for x in conf.get_dotfiles(profile)]) + dsts = [os.path.expanduser(x.dst) for x in conf.get_dotfiles(profile)] + self.assertTrue(path in dsts) + + def assert_in_yaml(self, path, dic): + '''Make sure "path" is in the "dic" representing the yaml file''' + strip = self.get_path_strip_version(path) + self.assertTrue(strip in [x['src'] for x in dic['dotfiles'].values()]) + dsts = [os.path.expanduser(x['dst']) for x in dic['dotfiles'].values()] + self.assertTrue(path in dsts) + + 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 = self.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') + 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) + + # import the dotfiles + importer(opts, conf, [dotfile1, dotfile2, dotfile3]) + + # reload the config + conf, opts = self.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) + + # 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) + + # 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))) + + +def main(): + unittest.main() + +if __name__ == '__main__': + main()