1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-04 17:24:46 +00:00
This commit is contained in:
deadc0de6
2023-02-05 18:11:51 +01:00
committed by deadc0de
parent 43b2d9ec54
commit 723cdaef5e

View File

@@ -23,38 +23,42 @@ class FakeOptions(Options):
self.log = Logger(debug=True) self.log = Logger(debug=True)
def clean_setup():
"""clean stuff"""
if 'DOTDROP_CONFIG' in os.environ:
del os.environ['DOTDROP_CONFIG']
if 'XDG_CONFIG_HOME' in os.environ:
del os.environ['XDG_CONFIG_HOME']
def get_args(more):
"""return args dict"""
args = {
'--dry': False,
'--verbose': True,
'--cfg': '',
}
for k, val in more.items():
args[k] = val
return args
def side_effect(valid=''):
"""side effect for os.path.exists"""
def inner(filename):
print(f'checking if {filename} exists')
if filename == valid:
return True
return False
return inner
class TestOptions(unittest.TestCase): class TestOptions(unittest.TestCase):
"""test case""" """test case"""
def clean_setup(self):
"""clean stuff"""
if 'DOTDROP_CONFIG' in os.environ:
del os.environ['DOTDROP_CONFIG']
if 'XDG_CONFIG_HOME' in os.environ:
del os.environ['XDG_CONFIG_HOME']
def _get_args(self, more):
args = {
'--dry': False,
'--verbose': True,
'--cfg': '',
}
for k, val in more.items():
args[k] = val
return args
def side_effect(self, valid=''):
"""side effect for os.path.exists"""
def inner(filename):
print(f'checking if {filename} exists')
if filename == valid:
return True
return False
return inner
def test_get_path_from_cli(self): def test_get_path_from_cli(self):
"""from --cli""" """from --cli"""
self.clean_setup() clean_setup()
expected = 'fakepath' expected = 'fakepath'
args = {} args = {}
args['--cfg'] = expected args['--cfg'] = expected
@@ -63,43 +67,43 @@ class TestOptions(unittest.TestCase):
def test_get_path_from_env(self): def test_get_path_from_env(self):
"""from env""" """from env"""
self.clean_setup() clean_setup()
expected = 'envpath' expected = 'envpath'
os.environ['DOTDROP_CONFIG'] = expected os.environ['DOTDROP_CONFIG'] = expected
args = self._get_args({'--cfg': ''}) args = get_args({'--cfg': ''})
fake = FakeOptions(args) fake = FakeOptions(args)
self.assertEqual(fake._get_config_path(), expected) self.assertEqual(fake._get_config_path(), expected)
@patch('os.path.exists') @patch('os.path.exists')
def test_get_path_from_yaml(self, mock_exists): def test_get_path_from_yaml(self, mock_exists):
"""from yaml""" """from yaml"""
self.clean_setup() clean_setup()
mock_exists.return_value = True mock_exists.return_value = True
expected = 'config.yaml' expected = 'config.yaml'
args = self._get_args({'--cfg': ''}) args = get_args({'--cfg': ''})
fake = FakeOptions(args) fake = FakeOptions(args)
self.assertEqual(fake._get_config_path(), expected) self.assertEqual(fake._get_config_path(), expected)
@patch('os.path.exists') @patch('os.path.exists')
def test_get_path_from_toml(self, mock_exists): def test_get_path_from_toml(self, mock_exists):
"""from toml""" """from toml"""
self.clean_setup() clean_setup()
expected = 'config.toml' expected = 'config.toml'
args = self._get_args({'--cfg': ''}) args = get_args({'--cfg': ''})
mock_exists.side_effect = self.side_effect(valid=expected) mock_exists.side_effect = side_effect(valid=expected)
fake = FakeOptions(args) fake = FakeOptions(args)
self.assertEqual(fake._get_config_path(), expected) self.assertEqual(fake._get_config_path(), expected)
@patch('os.path.exists') @patch('os.path.exists')
def test_get_path_xdg_yaml(self, mock_exists): def test_get_path_xdg_yaml(self, mock_exists):
"""from xdg""" """from xdg"""
self.clean_setup() clean_setup()
home = os.path.expanduser('~/.config') home = os.path.expanduser('~/.config')
expected = f'{home}/dotdrop/config.yaml' expected = f'{home}/dotdrop/config.yaml'
mock_exists.side_effect = self.side_effect(valid=expected) mock_exists.side_effect = side_effect(valid=expected)
log = Logger(debug=True) log = Logger(debug=True)
log.dbg(f'expected: {expected}') log.dbg(f'expected: {expected}')
args = self._get_args({'--cfg': ''}) args = get_args({'--cfg': ''})
os.environ['XDG_CONFIG_HOME'] = home os.environ['XDG_CONFIG_HOME'] = home
fake = FakeOptions(args) fake = FakeOptions(args)
self.assertEqual(fake._get_config_path(), expected) self.assertEqual(fake._get_config_path(), expected)
@@ -107,13 +111,13 @@ class TestOptions(unittest.TestCase):
@patch('os.path.exists') @patch('os.path.exists')
def test_get_path_xdg_toml(self, mock_exists): def test_get_path_xdg_toml(self, mock_exists):
"""from xdg toml""" """from xdg toml"""
self.clean_setup() clean_setup()
home = os.path.expanduser('~/.config') home = os.path.expanduser('~/.config')
expected = f'{home}/dotdrop/config.toml' expected = f'{home}/dotdrop/config.toml'
mock_exists.side_effect = self.side_effect(valid=expected) mock_exists.side_effect = side_effect(valid=expected)
log = Logger(debug=True) log = Logger(debug=True)
log.dbg(f'expected: {expected}') log.dbg(f'expected: {expected}')
args = self._get_args({'--cfg': ''}) args = get_args({'--cfg': ''})
os.environ['XDG_CONFIG_HOME'] = home os.environ['XDG_CONFIG_HOME'] = home
fake = FakeOptions(args) fake = FakeOptions(args)
self.assertEqual(fake._get_config_path(), expected) self.assertEqual(fake._get_config_path(), expected)
@@ -121,39 +125,39 @@ class TestOptions(unittest.TestCase):
@patch('os.path.exists') @patch('os.path.exists')
def test_get_path_fs_yaml(self, mock_exists): def test_get_path_fs_yaml(self, mock_exists):
"""from fs yaml""" """from fs yaml"""
self.clean_setup() clean_setup()
home = os.path.expanduser('~/.config') home = os.path.expanduser('~/.config')
expected = f'{home}/dotdrop/config.toml' expected = f'{home}/dotdrop/config.toml'
mock_exists.side_effect = self.side_effect(valid=expected) mock_exists.side_effect = side_effect(valid=expected)
log = Logger(debug=True) log = Logger(debug=True)
log.dbg(f'expected: {expected}') log.dbg(f'expected: {expected}')
args = self._get_args({'--cfg': ''}) args = get_args({'--cfg': ''})
fake = FakeOptions(args) fake = FakeOptions(args)
self.assertEqual(fake._get_config_path(), expected) self.assertEqual(fake._get_config_path(), expected)
@patch('os.path.exists') @patch('os.path.exists')
def test_get_path_fs_xdg(self, mock_exists): def test_get_path_fs_xdg(self, mock_exists):
"""from fs xdg""" """from fs xdg"""
self.clean_setup() clean_setup()
home = os.path.expanduser('/etc/xdg') home = os.path.expanduser('/etc/xdg')
expected = f'{home}/dotdrop/config.toml' expected = f'{home}/dotdrop/config.toml'
mock_exists.side_effect = self.side_effect(valid=expected) mock_exists.side_effect = side_effect(valid=expected)
log = Logger(debug=True) log = Logger(debug=True)
log.dbg(f'expected: {expected}') log.dbg(f'expected: {expected}')
args = self._get_args({'--cfg': ''}) args = get_args({'--cfg': ''})
fake = FakeOptions(args) fake = FakeOptions(args)
self.assertEqual(fake._get_config_path(), expected) self.assertEqual(fake._get_config_path(), expected)
@patch('os.path.exists') @patch('os.path.exists')
def test_get_path_fs_etc(self, mock_exists): def test_get_path_fs_etc(self, mock_exists):
"""from fs etc""" """from fs etc"""
self.clean_setup() clean_setup()
home = os.path.expanduser('/etc') home = os.path.expanduser('/etc')
expected = f'{home}/dotdrop/config.toml' expected = f'{home}/dotdrop/config.toml'
mock_exists.side_effect = self.side_effect(valid=expected) mock_exists.side_effect = side_effect(valid=expected)
log = Logger(debug=True) log = Logger(debug=True)
log.dbg(f'expected: {expected}') log.dbg(f'expected: {expected}')
args = self._get_args({'--cfg': ''}) args = get_args({'--cfg': ''})
fake = FakeOptions(args) fake = FakeOptions(args)
self.assertEqual(fake._get_config_path(), expected) self.assertEqual(fake._get_config_path(), expected)