mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-20 04:58:32 +02:00
defer modification of loaded configuration to update_config
remove unused fixtures delete complicated UT since behavior is covered in overrides.feature integ test delete redundant UT
This commit is contained in:
parent
f36c5c61dd
commit
4ef277e2cf
2 changed files with 9 additions and 101 deletions
|
@ -1,3 +1,5 @@
|
||||||
|
from .config import update_config
|
||||||
|
|
||||||
# import logging
|
# import logging
|
||||||
def apply_overrides(overrides: list, base_config: dict) -> dict:
|
def apply_overrides(overrides: list, base_config: dict) -> dict:
|
||||||
"""Unpack CLI provided overrides into the configuration tree.
|
"""Unpack CLI provided overrides into the configuration tree.
|
||||||
|
@ -9,14 +11,17 @@ def apply_overrides(overrides: list, base_config: dict) -> dict:
|
||||||
:return: Configuration to be used during runtime with the overrides applied
|
:return: Configuration to be used during runtime with the overrides applied
|
||||||
:rtype: dict
|
:rtype: dict
|
||||||
"""
|
"""
|
||||||
config = base_config.copy()
|
cfg_with_overrides = base_config.copy()
|
||||||
for pairs in overrides:
|
for pairs in overrides:
|
||||||
|
|
||||||
key_as_dots, override_value = _get_key_and_value_from_pair(pairs)
|
key_as_dots, override_value = _get_key_and_value_from_pair(pairs)
|
||||||
keys = _convert_dots_to_list(key_as_dots)
|
keys = _convert_dots_to_list(key_as_dots)
|
||||||
config.update(_recursively_apply(config, keys, override_value))
|
cfg_with_overrides = _recursively_apply(
|
||||||
|
cfg_with_overrides, keys, override_value
|
||||||
|
)
|
||||||
|
|
||||||
return config
|
update_config(base_config, cfg_with_overrides, None)
|
||||||
|
return base_config
|
||||||
|
|
||||||
|
|
||||||
def _get_key_and_value_from_pair(pairs):
|
def _get_key_and_value_from_pair(pairs):
|
||||||
|
@ -26,7 +31,7 @@ def _get_key_and_value_from_pair(pairs):
|
||||||
|
|
||||||
def _convert_dots_to_list(key_as_dots):
|
def _convert_dots_to_list(key_as_dots):
|
||||||
keys = key_as_dots.split(".")
|
keys = key_as_dots.split(".")
|
||||||
keys = [k for k in keys if k != ""]
|
keys = [k for k in keys if k != ""] # remove empty elements
|
||||||
return keys
|
return keys
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,97 +0,0 @@
|
||||||
import shlex
|
|
||||||
import pytest
|
|
||||||
from unittest import mock
|
|
||||||
|
|
||||||
|
|
||||||
from jrnl.args import parse_args
|
|
||||||
from jrnl.jrnl import run
|
|
||||||
from jrnl import install
|
|
||||||
from jrnl import jrnl
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture()
|
|
||||||
def minimal_config():
|
|
||||||
cfg = {
|
|
||||||
"colors": {"body": "red", "date": "green"},
|
|
||||||
"default": "/tmp/journal.jrnl",
|
|
||||||
"editor": "vim",
|
|
||||||
"encrypt": False,
|
|
||||||
"journals": {"default": "/tmp/journals/journal.jrnl"},
|
|
||||||
}
|
|
||||||
yield cfg
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture()
|
|
||||||
def expected_override(minimal_config):
|
|
||||||
exp_out_cfg = minimal_config.copy()
|
|
||||||
exp_out_cfg["editor"] = "nano"
|
|
||||||
exp_out_cfg["journal"] = "/tmp/journals/journal.jrnl"
|
|
||||||
yield exp_out_cfg
|
|
||||||
|
|
||||||
|
|
||||||
@mock.patch("sys.stdin.isatty")
|
|
||||||
@mock.patch.object(install, "load_or_install_jrnl")
|
|
||||||
@mock.patch("subprocess.call")
|
|
||||||
def test_override_configured_editor(
|
|
||||||
mock_subprocess_call,
|
|
||||||
mock_load_or_install,
|
|
||||||
mock_isatty,
|
|
||||||
minimal_config,
|
|
||||||
expected_override,
|
|
||||||
capsys,
|
|
||||||
):
|
|
||||||
mock_load_or_install.return_value = minimal_config
|
|
||||||
mock_isatty.return_value = True
|
|
||||||
|
|
||||||
cli_args = shlex.split('--config-override editor "nano"')
|
|
||||||
parser = parse_args(cli_args)
|
|
||||||
assert parser.config_override.__len__() == 1
|
|
||||||
assert {"editor": "nano"} in parser.config_override
|
|
||||||
|
|
||||||
def mock_editor_launch(editor):
|
|
||||||
print("%s launched! Success!" % editor)
|
|
||||||
|
|
||||||
with mock.patch.object(
|
|
||||||
jrnl,
|
|
||||||
"_write_in_editor",
|
|
||||||
side_effect=mock_editor_launch("TODO: replace"),
|
|
||||||
return_value="note_contents",
|
|
||||||
) as mock_write_in_editor:
|
|
||||||
run(parser)
|
|
||||||
mock_write_in_editor.assert_called_once_with(expected_override)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture()
|
|
||||||
def expected_color_override(minimal_config):
|
|
||||||
exp_out_cfg = minimal_config.copy()
|
|
||||||
exp_out_cfg["colors"]["body"] = "blue"
|
|
||||||
exp_out_cfg["journal"] = "/tmp/journals/journal.jrnl"
|
|
||||||
yield exp_out_cfg
|
|
||||||
|
|
||||||
|
|
||||||
@mock.patch("sys.stdin.isatty")
|
|
||||||
@mock.patch(
|
|
||||||
"jrnl.install.load_or_install_jrnl", wraps=jrnl.install.load_or_install_jrnl
|
|
||||||
)
|
|
||||||
@mock.patch("subprocess.call")
|
|
||||||
def test_override_configured_colors(
|
|
||||||
mock_isatty,
|
|
||||||
mock_load_or_install,
|
|
||||||
mock_subprocess_call,
|
|
||||||
minimal_config,
|
|
||||||
expected_color_override,
|
|
||||||
capsys,
|
|
||||||
):
|
|
||||||
mock_load_or_install.return_value = minimal_config
|
|
||||||
|
|
||||||
cli_args = shlex.split("--config-override colors.body blue")
|
|
||||||
parser = parse_args(cli_args)
|
|
||||||
assert {"colors.body": "blue"} in parser.config_override
|
|
||||||
with mock.patch.object(
|
|
||||||
jrnl,
|
|
||||||
"_write_in_editor",
|
|
||||||
side_effect=print("side effect!"),
|
|
||||||
return_value="note_contents",
|
|
||||||
) as mock_write_in_editor:
|
|
||||||
run(parser)
|
|
||||||
mock_write_in_editor.assert_called_once_with(expected_color_override)
|
|
Loading…
Add table
Reference in a new issue