mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-07-05 07:56:14 +02:00
separate out smaller pieces of logic
test that apply_overrides acts on base configuration and not the copy
This commit is contained in:
parent
531fba3b37
commit
f36c5c61dd
2 changed files with 44 additions and 7 deletions
|
@ -12,13 +12,24 @@ def apply_overrides(overrides: list, base_config: dict) -> dict:
|
|||
config = base_config.copy()
|
||||
for pairs in overrides:
|
||||
|
||||
key_as_dots, override_value = list(pairs.items())[0]
|
||||
keys = key_as_dots.split(".")
|
||||
config = _recursively_apply(config, keys, override_value)
|
||||
key_as_dots, override_value = _get_key_and_value_from_pair(pairs)
|
||||
keys = _convert_dots_to_list(key_as_dots)
|
||||
config.update(_recursively_apply(config, keys, override_value))
|
||||
|
||||
return config
|
||||
|
||||
|
||||
def _get_key_and_value_from_pair(pairs):
|
||||
key_as_dots, override_value = list(pairs.items())[0]
|
||||
return key_as_dots, override_value
|
||||
|
||||
|
||||
def _convert_dots_to_list(key_as_dots):
|
||||
keys = key_as_dots.split(".")
|
||||
keys = [k for k in keys if k != ""]
|
||||
return keys
|
||||
|
||||
|
||||
def _recursively_apply(tree: dict, nodes: list, override_value) -> dict:
|
||||
"""Recurse through configuration and apply overrides at the leaf of the config tree
|
||||
|
||||
|
@ -34,7 +45,8 @@ def _recursively_apply(tree: dict, nodes: list, override_value) -> dict:
|
|||
tree[key] = override_value
|
||||
else:
|
||||
next_key = nodes[1:]
|
||||
_recursively_apply(_get_config_node(tree, key), next_key, override_value)
|
||||
next_node = _get_config_node(tree, key)
|
||||
_recursively_apply(next_node, next_key, override_value)
|
||||
|
||||
return tree
|
||||
|
||||
|
|
|
@ -15,10 +15,9 @@ def minimal_config():
|
|||
|
||||
|
||||
def test_apply_override(minimal_config):
|
||||
config = minimal_config.copy()
|
||||
overrides = [{"editor": "nano"}]
|
||||
config = apply_overrides(overrides, config)
|
||||
assert config["editor"] == "nano"
|
||||
apply_overrides(overrides, minimal_config)
|
||||
assert minimal_config["editor"] == "nano"
|
||||
|
||||
|
||||
def test_override_dot_notation(minimal_config):
|
||||
|
@ -52,3 +51,29 @@ def test_get_config_node(minimal_config):
|
|||
assert len(minimal_config.keys()) == 4
|
||||
assert _get_config_node(minimal_config, "editor") == "vim"
|
||||
assert _get_config_node(minimal_config, "display_format") == None
|
||||
|
||||
|
||||
from jrnl.override import _get_key_and_value_from_pair
|
||||
|
||||
|
||||
def test_get_kv_from_pair():
|
||||
pair = {"ab.cde": "fgh"}
|
||||
k, v = _get_key_and_value_from_pair(pair)
|
||||
assert k == "ab.cde"
|
||||
assert v == "fgh"
|
||||
|
||||
|
||||
from jrnl.override import _convert_dots_to_list
|
||||
|
||||
|
||||
class TestDotNotationToList:
|
||||
def test_unpack_dots_to_list(self):
|
||||
|
||||
keys = "a.b.c.d.e.f"
|
||||
keys_list = _convert_dots_to_list(keys)
|
||||
assert len(keys_list) == 6
|
||||
|
||||
def test_sequential_delimiters(self):
|
||||
k = "g.r..h.v"
|
||||
k_l = _convert_dots_to_list(k)
|
||||
assert len(k_l) == 4
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue