From b91e4fad59387051b8a71900412302adeb8acbb5 Mon Sep 17 00:00:00 2001 From: Alfredo Deza Date: Tue, 22 Jan 2013 22:38:51 -0500 Subject: [PATCH] a bunch of tests for the env var support --- pecan/tests/test_conf.py | 46 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/pecan/tests/test_conf.py b/pecan/tests/test_conf.py index 4b276cb..5aad67f 100644 --- a/pecan/tests/test_conf.py +++ b/pecan/tests/test_conf.py @@ -110,7 +110,7 @@ class TestConf(TestCase): from pecan import configuration path = ('doesnotexist.py',) configuration.Config({}) - self.assertRaises(IOError, configuration.conf_from_file, os.path.join( + self.assertRaises(RuntimeError, configuration.conf_from_file, os.path.join( __here__, 'config_fixtures', *path @@ -121,7 +121,7 @@ class TestConf(TestCase): path = ('bad', 'bad', 'doesnotexist.py',) configuration.Config({}) - self.assertRaises(IOError, configuration.conf_from_file, os.path.join( + self.assertRaises(RuntimeError, configuration.conf_from_file, os.path.join( __here__, 'config_fixtures', *path @@ -274,6 +274,44 @@ class TestGlobalConfig(TestCase): ) assert dict(configuration._runtime_conf) == {'foo': 'bar'} - def test_set_config_invalid_type(self): + def test_set_config_none_type(self): from pecan import configuration - self.assertRaises(TypeError, configuration.set_config, None) + self.assertRaises(RuntimeError, configuration.set_config, None) + + def test_set_config_to_dir(self): + from pecan import configuration + self.assertRaises(RuntimeError, configuration.set_config, '/') + + +class TestConfFromEnv(TestCase): + + def setUp(self): + self.conf_from_env = self.get_conf_from_env() + os.environ['PECAN_CONFIG'] = '' + + def get_conf_from_env(self): + from pecan import configuration + return configuration.conf_from_env + + def assertRaisesMessage(self, msg, exc, func, *args, **kwargs): + try: + func(*args, **kwargs) + self.assertFail() + except Exception as error: + assert issubclass(exc, error.__class__) + assert error.message == msg + + def test_invalid_path(self): + os.environ['PECAN_CONFIG'] = '/' + msg = "PECAN_CONFIG was set to an invalid path: /" + self.assertRaisesMessage(msg, RuntimeError, self.conf_from_env) + + def test_is_not_set(self): + msg = "PECAN_CONFIG is not set and " \ + "no config file was passed as an argument." + self.assertRaisesMessage(msg, RuntimeError, self.conf_from_env) + + def test_return_valid_path(self): + here = os.path.abspath(__file__) + os.environ['PECAN_CONFIG'] = here + assert self.conf_from_env() == here