class SimpleInitTest(unittest.TestCase): def setUp(self): self.cfg = Config("hello", "world", search_path="testdata") def test_simple_init(self): self.assertTrue(self.cfg.has_section("section1")) def test_get(self): self.assertEqual(self.cfg.get("section1", "var1"), "foo") self.assertEqual(self.cfg.get("section1", "var2"), "bar") self.assertEqual(self.cfg.get("section2", "var1"), "baz") def test_no_option_error(self): self.assertIs(self.cfg.get("section1", "b", default=None), None) def test_no_section_error(self): self.assertIs(self.cfg.get("a", "b", default=None), None)
class SimpleInitTest(unittest.TestCase): def setUp(self): self.cfg = Config('hello', 'world', search_path='testdata') def test_simple_init(self): self.assertTrue(self.cfg.has_section('section1')) def test_get(self): self.assertEqual(self.cfg.get('section1', 'var1'), 'foo') self.assertEqual(self.cfg.get('section1', 'var2'), 'bar') self.assertEqual(self.cfg.get('section2', 'var1'), 'baz') def test_no_option_error(self): self.assertIs(self.cfg.get('section1', 'b'), None) def test_no_section_error(self): self.assertIs(self.cfg.get('a', 'b'), None)
def test_search_path(self): cfg = Config('hello', 'world', search_path='testdata:testdata/a:testdata/b') self.assertTrue(cfg.has_section('section3')) self.assertEqual(cfg.get('section1', 'var1'), 'frob') self.assertEqual( cfg.loaded_files, ['testdata/app.ini', 'testdata/a/app.ini', 'testdata/b/app.ini'])
def test_search_path(self): cfg = Config('hello', 'world', search_path='testdata:testdata/a:testdata/b') self.assertTrue(cfg.has_section('section3')) self.assertEqual(cfg.get('section1', 'var1'), 'frob') self.assertEqual( cfg.loaded_files, ['testdata/app.ini', 'testdata/a/app.ini', 'testdata/b/app.ini'])
class SimpleInitTest(unittest.TestCase): def setUp(self): self.cfg = Config('hello', 'world', search_path='testdata') def test_simple_init(self): self.assertTrue(self.cfg.has_section('section1')) def test_get(self): self.assertEqual(self.cfg.get('section1', 'var1'), 'foo') self.assertEqual(self.cfg.get('section1', 'var2'), 'bar') self.assertEqual(self.cfg.get('section2', 'var1'), 'baz') def test_no_option_error(self): self.assertIs(self.cfg.get('section1', 'b'), None) def test_no_section_error(self): self.assertIs(self.cfg.get('a', 'b'), None)
def freshdb(request): from sqlalchemy import create_engine from config_resolver import Config conf = Config('mamerwiselen', 'lost-tracker', require_load=True) dsn = conf.get('db', 'dsn') engine = create_engine(dsn) connection = engine.raw_connection() cursor = connection.cursor() reseed_db(cursor) connection.commit() def disconnect(): engine.dispose() request.addfinalizer(disconnect) return conf
class SimpleInitFromContent(TestBase): ''' Tests loading a config string from memory ''' def setUp(self): super(SimpleInitFromContent, self).setUp() self.cfg = Config('not', 'existing', search_path='testdata') self.cfg.read_string(dedent( '''\ [section_mem] val = 1 ''' )) def test_sections_available(self): self.assertTrue(self.cfg.has_section('section_mem')) def test_getting_values(self): self.assertEqual(self.cfg.get('section_mem', 'val'), '1')
def test_filename(self): cfg = Config('hello', 'world', filename='test.ini', search_path='testdata') self.assertEqual(cfg.get('section2', 'var1'), 'baz')
class FunctionalityTests(unittest.TestCase): def setUp(self): self.cfg = Config("hello", "world", search_path="testdata") def test_mandatory_section(self): with self.assertRaises(NoSectionError): self.cfg.get("nosuchsection", "nosuchoption") def test_mandatory_option(self): with self.assertRaises(NoOptionError): self.cfg.get("section1", "nosuchoption") def test_unsecured_logmessage(self): logger = logging.getLogger("config_resolver") catcher = TestableHandler() logger.addHandler(catcher) SecuredConfig("hello", "world", filename="test.ini", search_path="testdata") expected_message = "File 'testdata/test.ini' is not secure enough. " "Change it's mode to 600" result = catcher.contains("config_resolver.hello.world", logging.WARNING, expected_message) self.assertTrue(result, "Expected log message: {!r} not found in " "logger!".format(expected_message)) def test_unsecured_file(self): conf = SecuredConfig("hello", "world", filename="test.ini", search_path="testdata") self.assertNotIn(join("testdata", "test.ini"), conf.loaded_files) def test_secured_file(self): conf = SecuredConfig("hello", "world", filename="secure.ini", search_path="testdata") self.assertIn(join("testdata", "secure.ini"), conf.loaded_files) def test_secured_nonexisting_file(self): conf = SecuredConfig("hello", "world", filename="nonexisting.ini", search_path="testdata") self.assertNotIn(join("testdata", "nonexisting.ini"), conf.loaded_files) def test_file_not_found_exception(self): with self.assertRaises(IOError): Config("hello", "world", filename="nonexisting.ini", search_path="testdata", require_load=True) def test_no_version_found_warning(self): with self.assertRaises(NoVersionError): Config("hello", "world", search_path="testdata", version="1.1") def test_mismatching_major(self): with self.assertRaises(IncompatibleVersion): Config("hello", "world", search_path="testdata/versioned", version="1.1") def test_mismatching_minor(self): logger = logging.getLogger("config_resolver") catcher = TestableHandler() logger.addHandler(catcher) Config("hello", "world", search_path="testdata/versioned", version="2.0") result = catcher.contains("config_resolver.hello.world", logging.WARNING, "Mismatching minor version number") self.assertTrue(result) def test_xdg_config_dirs(self): with environment(XDG_CONFIG_DIRS="/xdgpath1:/xdgpath2", XDG_CONFIG_HOME=""): cfg = Config("foo", "bar") self.assertEqual( [ "/etc/foo/bar/app.ini", "/xdgpath2/foo/bar/app.ini", "/xdgpath1/foo/bar/app.ini", expanduser("~/.foo/bar/app.ini"), expanduser("~/.config/foo/bar/app.ini"), abspath(".foo/bar/app.ini"), ], cfg.active_path, ) def test_xdg_empty_config_dirs(self): with environment(XDG_CONFIG_DIRS="", XDG_CONFIG_HOME=""): cfg = Config("foo", "bar") self.assertEqual( [ "/etc/foo/bar/app.ini", "/etc/xdg/foo/bar/app.ini", expanduser("~/.foo/bar/app.ini"), expanduser("~/.config/foo/bar/app.ini"), abspath(".foo/bar/app.ini"), ], cfg.active_path, ) def test_xdg_config_home(self): with environment(XDG_CONFIG_HOME="/path/to/config/home", XDG_CONFIG_DIRS=""): cfg = Config("foo", "bar") self.assertEqual( [ "/etc/foo/bar/app.ini", "/etc/xdg/foo/bar/app.ini", expanduser("~/.foo/bar/app.ini"), "/path/to/config/home/foo/bar/app.ini", abspath(".foo/bar/app.ini"), ], cfg.active_path, ) def test_xdg_empty_config_home(self): with environment(XDG_CONFIG_HOME="", XDG_CONFIG_DIRS=""): cfg = Config("foo", "bar") self.assertEqual( [ "/etc/foo/bar/app.ini", "/etc/xdg/foo/bar/app.ini", expanduser("~/.foo/bar/app.ini"), expanduser("~/.config/foo/bar/app.ini"), abspath(".foo/bar/app.ini"), ], cfg.active_path, ) def test_both_xdg_variables(self): with environment(XDG_CONFIG_DIRS="/xdgpath1:/xdgpath2", XDG_CONFIG_HOME="/xdg/config/home"): cfg = Config("foo", "bar") self.assertEqual( [ "/etc/foo/bar/app.ini", "/xdgpath2/foo/bar/app.ini", "/xdgpath1/foo/bar/app.ini", expanduser("~/.foo/bar/app.ini"), "/xdg/config/home/foo/bar/app.ini", abspath(".foo/bar/app.ini"), ], cfg.active_path, ) @unittest.skipUnless(have_mock, "mock module is not available") def test_xdg_deprecation(self): """ ~/.group/app/app.ini should issue a deprecation warning. NOTE: This is a *user* warning. Not a developer warning! So we'll use the logging module instead of the warnings module! """ with patch("config_resolver.Config.check_file") as checker_mock: checker_mock.return_value = (True, "") logger = logging.getLogger("config_resolver") catcher = TestableHandler() logger.addHandler(catcher) Config("hello", "world") expected_message = ( "DEPRECATION WARNING: The file '{home}/.hello/world/app.ini' " "was loaded. The XDG Basedir standard requires this file to " "be in '{home}/.config/hello/world/app.ini'! This location " "will no longer be parsed in a future version of " "config_resolver! You can already (and should) move the " "file!".format(home=expanduser("~")) ) result = catcher.contains("config_resolver", logging.WARNING, expected_message) self.assertTrue(result, "Expected log message: {!r} not found in " "logger!".format(expected_message))
def test_filename(self): cfg = Config("hello", "world", filename="test.ini", search_path="testdata") self.assertEqual(cfg.get("section2", "var1"), "baz")
def test_search_path(self): cfg = Config("hello", "world", search_path="testdata:testdata/a:testdata/b") self.assertTrue(cfg.has_section("section3")) self.assertEqual(cfg.get("section1", "var1"), "frob") self.assertEqual(cfg.loaded_files, ["testdata/app.ini", "testdata/a/app.ini", "testdata/b/app.ini"])
def test_filename(self): cfg = Config('hello', 'world', filename='test.ini', search_path='testdata') self.assertEqual(cfg.get('section2', 'var1'), 'baz')
def get_url(): config = Config('mamerwiselen', 'lost-tracker', version='2.1', require_load=True) return config.get('db', 'dsn')
def test_mandatory_option(self): config = Config('hello', 'world', search_path='testdata') with self.assertRaises(NoOptionError): config.get('section1', 'nosuchoption')