From 3a4c2a887db0d97d7827d4a6fc1271a8b798b822 Mon Sep 17 00:00:00 2001 From: Tomas Korbar Date: Tue, 21 Aug 2018 15:42:52 +0200 Subject: [PATCH] Add ConfigurationTestCase - tests prove descending hiearchy of config system - env vars, local config file, global config file --- tests/test_configuration.py | 86 +++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 tests/test_configuration.py diff --git a/tests/test_configuration.py b/tests/test_configuration.py new file mode 100644 index 0000000..187ef16 --- /dev/null +++ b/tests/test_configuration.py @@ -0,0 +1,86 @@ +import unittest2 +import os +import shutil +from cheat.configuration import Configuration + + +def _set_loc_conf(key, value): + _path = os.path.dirname(os.path.abspath(__file__)) + '/home/.config/cheat/cheat' + if value == None: + os.remove(_path) + else: + if not os.path.exists(os.path.dirname(_path)): + os.makedirs(os.path.dirname(_path)) + f = open(_path,"w+") + f.write('{"'+ key +'":"'+ value +'"}') + f.close() + + +def _set_glob_conf(key, value): + _path = os.path.dirname(os.path.abspath(__file__))+ "/etc/cheat" + if value == None: + os.remove(_path) + else: + if not os.path.exists(os.path.dirname(_path)): + os.mkdir(os.path.dirname(_path)) + f = open(_path,"w+") + f.write('{"'+ key +'":"'+ value +'"}' ) + f.close() + + +def _set_env_var(key, value): + if value == None: + del os.environ[key] + else: + os.environ[key] = value + + +def _configuration_key_test(TestConfiguration, key,values, conf_get_method): + for glob_conf in values: + _set_glob_conf(key,glob_conf) + for loc_conf in values: + _set_loc_conf(key,loc_conf) + for env_conf in values: + _set_env_var(key,env_conf) + if env_conf: + TestConfiguration.assertEqual(conf_get_method(Configuration()),env_conf) + elif loc_conf: + TestConfiguration.assertEqual(conf_get_method(Configuration()),loc_conf) + elif glob_conf: + TestConfiguration.assertEqual(conf_get_method(Configuration()),glob_conf) + else: + TestConfiguration.assertEqual(conf_get_method(Configuration()),None) + + +class ConfigurationTestCase(unittest2.TestCase): + + + def setUp(self): + os.environ['CHEAT_GLOBAL_CONF_PATH'] = os.path.dirname(os.path.abspath(__file__)) \ + + '/etc/cheat' + os.environ['CHEAT_LOCAL_CONF_PATH'] = os.path.dirname(os.path.abspath(__file__)) \ + + '/home/.config/cheat/cheat' + + + def test_get_editor(self): + _configuration_key_test(self,"EDITOR",["nano","vim","gedit",None], + Configuration.get_editor) + + + def test_get_cheatcolors(self): + _configuration_key_test(self,"CHEATCOLORS",["true",None], + Configuration.get_cheatcolors) + + + def test_get_cheatpath(self): + _configuration_key_test(self,"CHEATPATH",["/etc/myglobalcheats", + "/etc/anotherglobalcheats","/rootcheats",None],Configuration.get_cheatpath) + + + def test_get_defaultcheatdir(self): + _configuration_key_test(self,"DEFAULT_CHEAT_DIR",["/etc/myglobalcheats", + "/etc/anotherglobalcheats","/rootcheats",None],Configuration.get_default_cheat_dir) + + def tearDown(self): + shutil.rmtree(os.path.dirname(os.path.abspath(__file__)) +'/etc') + shutil.rmtree(os.path.dirname(os.path.abspath(__file__)) +'/home')