diff --git a/tests/helpers.py b/tests/helpers.py index 3c50a7a..ac58e7f 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -117,7 +117,7 @@ def create_dir(path): def _fake_args(): args = {} - args['--verbose'] = False + args['--verbose'] = True args['--no-banner'] = False args['--dry'] = False args['--force'] = False diff --git a/tests/test_options.py b/tests/test_options.py index 84aec04..27d3fc2 100644 --- a/tests/test_options.py +++ b/tests/test_options.py @@ -12,6 +12,7 @@ import os import unittest from unittest.mock import patch from dotdrop.options import Options, Logger +from dotdrop.exceptions import YamlException class FakeOptions(Options): @@ -101,8 +102,6 @@ class TestOptions(unittest.TestCase): home = os.path.expanduser('~/.config') expected = f'{home}/dotdrop/config.yaml' mock_exists.side_effect = side_effect(valid=expected) - log = Logger(debug=True) - log.dbg(f'expected: {expected}') args = get_args({'--cfg': ''}) os.environ['XDG_CONFIG_HOME'] = home fake = FakeOptions(args) @@ -115,52 +114,121 @@ class TestOptions(unittest.TestCase): home = os.path.expanduser('~/.config') expected = f'{home}/dotdrop/config.toml' mock_exists.side_effect = side_effect(valid=expected) - log = Logger(debug=True) - log.dbg(f'expected: {expected}') args = get_args({'--cfg': ''}) os.environ['XDG_CONFIG_HOME'] = home fake = FakeOptions(args) self.assertEqual(fake._get_config_path(), expected) @patch('os.path.exists') - def test_get_path_fs_yaml(self, mock_exists): + def test_get_path_fs_xdg_yaml(self, mock_exists): """from fs yaml""" clean_setup() home = os.path.expanduser('~/.config') - expected = f'{home}/dotdrop/config.toml' + expected = f'{home}/dotdrop/config.yaml' mock_exists.side_effect = side_effect(valid=expected) - log = Logger(debug=True) - log.dbg(f'expected: {expected}') args = get_args({'--cfg': ''}) fake = FakeOptions(args) self.assertEqual(fake._get_config_path(), expected) @patch('os.path.exists') - def test_get_path_fs_xdg(self, mock_exists): + def test_get_path_fs_xdg_etc_yaml(self, mock_exists): + """from fs xdg""" + clean_setup() + home = os.path.expanduser('/etc/xdg') + expected = f'{home}/dotdrop/config.yaml' + mock_exists.side_effect = side_effect(valid=expected) + args = get_args({'--cfg': ''}) + fake = FakeOptions(args) + self.assertEqual(fake._get_config_path(), expected) + + @patch('os.path.exists') + def test_get_path_fs_etc_dotdrop_yaml(self, mock_exists): + """from fs etc""" + clean_setup() + home = os.path.expanduser('/etc') + expected = f'{home}/dotdrop/config.yaml' + mock_exists.side_effect = side_effect(valid=expected) + args = get_args({'--cfg': ''}) + fake = FakeOptions(args) + self.assertEqual(fake._get_config_path(), expected) + + @patch('os.path.exists') + def test_get_path_fs_etc_xdg_yaml(self, mock_exists): + """from fs etc/xdg""" + clean_setup() + home = os.path.expanduser('/etc/xdg') + expected = f'{home}/dotdrop/config.yaml' + mock_exists.side_effect = side_effect(valid=expected) + args = get_args({'--cfg': ''}) + fake = FakeOptions(args) + self.assertEqual(fake._get_config_path(), expected) + + @patch('os.path.exists') + def test_get_path_fs_xdg_toml(self, mock_exists): + """from fs toml""" + clean_setup() + home = os.path.expanduser('~/.config') + expected = f'{home}/dotdrop/config.toml' + mock_exists.side_effect = side_effect(valid=expected) + args = get_args({'--cfg': ''}) + fake = FakeOptions(args) + self.assertEqual(fake._get_config_path(), expected) + + @patch('os.path.exists') + def test_get_path_fs_xdg_etc_toml(self, mock_exists): """from fs xdg""" clean_setup() home = os.path.expanduser('/etc/xdg') expected = f'{home}/dotdrop/config.toml' mock_exists.side_effect = side_effect(valid=expected) - log = Logger(debug=True) - log.dbg(f'expected: {expected}') args = get_args({'--cfg': ''}) fake = FakeOptions(args) self.assertEqual(fake._get_config_path(), expected) @patch('os.path.exists') - def test_get_path_fs_etc(self, mock_exists): + def test_get_path_fs_etc_dotdrop_toml(self, mock_exists): """from fs etc""" clean_setup() home = os.path.expanduser('/etc') expected = f'{home}/dotdrop/config.toml' mock_exists.side_effect = side_effect(valid=expected) - log = Logger(debug=True) - log.dbg(f'expected: {expected}') args = get_args({'--cfg': ''}) fake = FakeOptions(args) self.assertEqual(fake._get_config_path(), expected) + @patch('os.path.exists') + def test_get_path_fs_etc_xdg_toml(self, mock_exists): + """from fs etc/xdg""" + clean_setup() + home = os.path.expanduser('/etc/xdg') + expected = f'{home}/dotdrop/config.toml' + mock_exists.side_effect = side_effect(valid=expected) + args = get_args({'--cfg': ''}) + fake = FakeOptions(args) + self.assertEqual(fake._get_config_path(), expected) + + @patch('os.path.exists') + def test_get_path_none(self, mock_exists): + """path is none""" + clean_setup() + mock_exists.return_value = False + args = get_args({}) + fake = FakeOptions(args) + self.assertEqual(None, fake._get_config_path()) + + @patch('os.path.exists') + def test_options_debug(self, mock_exists): + """test debug""" + mock_exists.return_value = False + args = { + '--verbose': True, + '--dry': False, + '--cfg': 'path', + '--profile': 'profile', + } + with self.assertRaises(YamlException): + Options(args) + def main(): """entry point""" diff --git a/tests/test_yamlcfg.py b/tests/test_yamlcfg.py index 1b61e74..8a5a5ec 100644 --- a/tests/test_yamlcfg.py +++ b/tests/test_yamlcfg.py @@ -52,6 +52,25 @@ class TestConfig(SubsetTestCase): self.assertTrue(dpath == self.CONFIG_DOTPATH) self.assertTrue(conf.dump() != '') + def test_raises(self): + """test raises on not existing path""" + with self.assertRaises(YamlException): + Cfg('path', debug=True) + + def test_resolve_link(self): + """test bad link value""" + tmp = get_tempdir() + 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) + cfg = Cfg(confpath, debug=True) + with self.assertRaises(YamlException): + cfg._resolve_dotfile_link('fake') + def test_def_link(self): """unittest""" # pylint: disable=E1120