Skip to content

Commit

Permalink
Fix config stack loading with unit tests (#6)
Browse files Browse the repository at this point in the history
* Fix config stack loading with unit tests

* Troubleshooting GH test failure

Co-authored-by: Daniel McKnight <[email protected]>
  • Loading branch information
NeonDaniel and NeonDaniel authored Jul 5, 2022
1 parent 17cdc19 commit 4200160
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 2 deletions.
5 changes: 3 additions & 2 deletions ovos_config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ def filter_and_merge(configs):
for index, item in enumerate(configs):
if isinstance(item, str):
configs[index] = LocalConf(item)
elif isinstance(item, dict):
elif not isinstance(item, LocalConf):
configs[index] = LocalConf(None)
configs[index].merge(item)

Expand All @@ -410,7 +410,8 @@ def filter_and_merge(configs):
# Merge all configs into one
base = {}
for cfg in configs:
is_user = cfg.path is None or cfg.path not in [Configuration.default, Configuration.system]
is_user = cfg.path is None or cfg.path not in [Configuration.default.path,
Configuration.system.path]
is_remote = cfg.path == Configuration.remote.path
if (is_remote and skip_remote) or (is_user and skip_user):
continue
Expand Down
21 changes: 21 additions & 0 deletions test/unittests/config_stack/default.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
config_name: default
system_only:
from_sys: False
from_rem: False
from_usr: False
default_spec:
from_sys: False
from_rem: False
from_usr: False
test:
default: True
system: False
user: False
remote: False
default_only: default
system:
protected_keys:
remote:
- default_spec
user:
- default_spec
11 changes: 11 additions & 0 deletions test/unittests/config_stack/remote.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
config_name: remote
system_only:
from_sys: False
from_rem: True
from_usr: False
test:
user: False
remote: True
default_spec:
from_rem: True
remote_only: "remote"
21 changes: 21 additions & 0 deletions test/unittests/config_stack/system.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
config_name: system
system_only:
from_sys: True
from_rem: False
from_usr: False
default_spec:
from_sys: True
test:
system: True
user: False
remote: False

system:
protected_keys:
remote:
- system_only
- test:user
user:
- system_only
- test:remote
- user_only
11 changes: 11 additions & 0 deletions test/unittests/config_stack/user.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
config_name: user
system_only:
from_sys: False
from_rem: False
from_usr: True
test:
user: True
remote: False
default_spec:
from_usr: True
user_only: True
35 changes: 35 additions & 0 deletions test/unittests/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,40 @@ def test_yaml_config_load(self):
self.assertNotIsInstance(d, OrderedDict)
self.assertEqual(json.loads(json.dumps(d)), d)

def test_load_config_stack(self):
test_dir = join(dirname(__file__), "config_stack")
default_config = LocalConf(join(test_dir, "default.yaml"))
system_config = LocalConf(join(test_dir, "system.yaml"))
remote_config = LocalConf(join(test_dir, "remote.yaml"))
user_config = LocalConf(join(test_dir, "user.yaml"))
Configuration.default = default_config
Configuration.system = system_config
Configuration.remote = remote_config
Configuration.xdg_configs = [user_config]
Configuration.__patch = LocalConf(None)
Configuration._old_user = LocalConf(None)
Configuration.load_all_configs()
config = Configuration()
# Test stack load order
self.assertEqual(config["config_name"], "user")
# Test system constraints
self.assertEqual(config["system_only"], {"from_sys": True,
"from_rem": False,
"from_usr": False})
# Test default constraints (overridden)
self.assertEqual(config["default_spec"], {"from_sys": True,
"from_rem": True,
"from_usr": True})
# Test nested constraints
self.assertEqual(config["test"], {"default": True,
"system": True,
"user": True,
"remote": True})
# Test non-overridden default config
self.assertEqual(config["default_only"], "default")
# Test protected key is undefined
self.assertFalse("user_only" in config)
self.assertEqual(config["remote_only"], "remote")

def tearDown(self):
Configuration.load_config_stack([{}], True)

0 comments on commit 4200160

Please sign in to comment.