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_env_path(self): os.environ['HELLO_WORLD_PATH'] = 'testdata:testdata/a:testdata/b' cfg = Config('hello', 'world') expected = [ 'testdata/app.ini', 'testdata/a/app.ini', 'testdata/b/app.ini' ] self.assertEqual(cfg.active_path, expected)
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 setUp(self): super(SimpleInitFromContent, self).setUp() self.cfg = Config('not', 'existing', search_path='testdata') self.cfg.read_string(dedent( '''\ [section_mem] val = 1 ''' ))
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
def test_env_name(self): os.environ['HELLO_WORLD_CONFIG'] = 'test.ini' cfg = Config('hello', 'world') expected = [ '/etc/hello/world/test.ini', expanduser('~/.hello/world/test.ini'), '{}/test.ini'.format(os.getcwd()) ] self.assertEqual(cfg.active_path, expected)
def test_env_path_add(self): os.environ['HELLO_WORLD_PATH'] = '+testdata:testdata/a:testdata/b' cfg = Config('hello', 'world') expected = [ '/etc/hello/world/app.ini', expanduser('~/.hello/world/app.ini'), '{}/app.ini'.format(os.getcwd()), 'testdata/app.ini', 'testdata/a/app.ini', 'testdata/b/app.ini' ] self.assertEqual(cfg.active_path, expected)
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_mismatching_major(self): config = Config('hello', 'world', search_path='testdata/versioned', version='1.1') self.catcher.assert_contains( 'config_resolver.hello.world', logging.ERROR, 'Invalid major version number') self.catcher.assert_contains( 'config_resolver.hello.world', logging.ERROR, '2.1') self.catcher.assert_contains( 'config_resolver.hello.world', logging.ERROR, '1.1') # Values should not be loaded. Let's check if they really are missing. # They should be! self.assertFalse('section1' in config.sections()) # Also, no files should be added to the "loaded_files" list. self.assertEqual(config.loaded_files, [])
def __init__(self, conf=None): try: self.conf = conf or Config('gefoo', 'ubiety', require_load=True) self.pingers = self.create_pingers() # Setup restapi self.restapi = Bottle() self.restapi.get( path="/pinger/<pinger_name>", callback=self.get_pinger, name="get_pinger" ) self.restapi.get( path="/pinger", callback=self.get_pingers, name="get_pingers" ) self.restapi.install( JSONPlugin( json_dumps=lambda s: jsonify(s, cls=PingerJSONEncoder) ) ) except OSError as exc: raise OSError(exc)
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 setUp(self): super(SimpleInitTest, self).setUp() self.cfg = Config('hello', 'world', search_path='testdata')
def test_mandatory_option(self): config = Config('hello', 'world', search_path='testdata') with self.assertRaises(NoOptionError): config.get('section1', 'nosuchoption')
import logging.handlers from config_resolver import Config LOG = logging.getLogger(__name__) LOG.setLevel(logging.DEBUG) FORMATTER = logging.Formatter( "%(asctime)s - %(name)s - %(levelname)s - %(message)s") CONSOLE_HANDLER = logging.StreamHandler() CONSOLE_HANDLER.setLevel(logging.INFO) CONSOLE_HANDLER.setFormatter(FORMATTER) logging.getLogger().setLevel(logging.DEBUG) logging.getLogger().addHandler(CONSOLE_HANDLER) CONF = Config('wicked', 'metafilter') from metafilter.model import Session, CONFIG, set_dsn from metafilter.model.nodes import update_nodes_from_path, remove_orphans, calc_md5 error_log = expanduser(CONFIG.get('cli_logging', 'error_log', None)) if error_log: if not exists(dirname(error_log)): LOG.info('Creating logging folder: %s' % dirname(error_log)) makedirs(dirname(error_log)) ERROR_HANDLER = logging.handlers.RotatingFileHandler(filename=error_log, maxBytes=102400, backupCount=5) ERROR_HANDLER.setLevel(logging.WARNING) ERROR_HANDLER.setFormatter(FORMATTER) logging.getLogger().addHandler(ERROR_HANDLER)
def setUp(self): self.cfg = Config("hello", "world", search_path="testdata")
def get_url(): config = Config('mamerwiselen', 'lost-tracker', version='2.1', require_load=True) return config.get('db', 'dsn')
def load_config(): return Config('mds', 'pystrip')
def test_filename(self): cfg = Config('hello', 'world', filename='test.ini', search_path='testdata') self.assertEqual(cfg.get('section2', 'var1'), 'baz')
def test_app_group_name(self): cfg = Config('hello', 'world') self.assertEqual(cfg.group_name, 'hello') self.assertEqual(cfg.app_name, 'world')
def setUp(self): self.cfg = Config('hello', 'world', search_path='testdata')
def load_cfg(): ''' return config ''' config = Config('mds', 'onewire') return config
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))