3
0
Fork 0

fix: only read each config file once on startup

This commit is contained in:
Lance Edgar 2024-12-18 12:33:17 -06:00
parent 87790bbb1a
commit 659d7e551e
2 changed files with 37 additions and 1 deletions

View file

@ -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)

View file

@ -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]