From 659d7e551efc494603685da4fb84e92714d5787b Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Wed, 18 Dec 2024 12:33:17 -0600 Subject: [PATCH] fix: only read each config file once on startup --- src/wuttjamaican/conf.py | 5 ++++- tests/test_conf.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/wuttjamaican/conf.py b/src/wuttjamaican/conf.py index 785ce7b..2c71e28 100644 --- a/src/wuttjamaican/conf.py +++ b/src/wuttjamaican/conf.py @@ -201,7 +201,6 @@ class WuttaConfig: self.files_read = [] for path in files: self._load_ini_configs(path, configs, require=True) - log.debug("config files were: %s", self.files_read) # add config for use w/ setdefault() self.defaults = configuration.Configuration(defaults) @@ -249,6 +248,10 @@ class WuttaConfig: def _load_ini_configs(self, path, configs, require=True): path = os.path.abspath(path) + # no need to read a file twice; its first appearance sets priority + if path in self.files_read: + return + # try to load config from the given path try: config = configuration.config_from_ini(path, read_from_file=True) diff --git a/tests/test_conf.py b/tests/test_conf.py index 9ef533e..b5b8771 100644 --- a/tests/test_conf.py +++ b/tests/test_conf.py @@ -109,6 +109,39 @@ baz = B self.assertIsNone(config.get('foo.bar')) self.assertEqual(config.get('foo.baz'), 'B') + def test_files_only_read_once(self): + base = self.write_file('base.conf', """ +[foo] +bar = 1 +baz = A +""") + + middle = self.write_file('middle.conf', """ +[wutta.config] +require = %(here)s/base.conf + +[foo] +baz = B +""") + + top = self.write_file('top.conf', """ +[wutta.config] +require = %(here)s/middle.conf + +[foo] +baz = C +""") + + config = conf.WuttaConfig(files=[top, middle, base]) + self.assertEqual(len(config.files_read), 3) + # nb. files_read listing is in order of "priority" which is + # same the as order in which files were initially read + self.assertEqual(config.files_read[0], top) + self.assertEqual(config.files_read[1], middle) + self.assertEqual(config.files_read[2], base) + self.assertEqual(config.get('foo.bar'), '1') + self.assertEqual(config.get('foo.baz'), 'C') + def test_prioritized_files(self): first = self.write_file('first.conf', """\ [foo]