mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-07-10 01:16:12 +02:00
Merge branch 'develop' into format-pretty-#1172
This commit is contained in:
commit
899fd23539
15 changed files with 750 additions and 55 deletions
97
tests/test_config.py
Normal file
97
tests/test_config.py
Normal file
|
@ -0,0 +1,97 @@
|
|||
import shlex
|
||||
import pytest
|
||||
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)
|
54
tests/test_override.py
Normal file
54
tests/test_override.py
Normal file
|
@ -0,0 +1,54 @@
|
|||
import pytest
|
||||
|
||||
from jrnl.override import apply_overrides, _recursively_apply, _get_config_node
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def minimal_config():
|
||||
cfg = {
|
||||
"colors": {"body": "red", "date": "green"},
|
||||
"default": "/tmp/journal.jrnl",
|
||||
"editor": "vim",
|
||||
"journals": {"default": "/tmp/journals/journal.jrnl"},
|
||||
}
|
||||
yield cfg
|
||||
|
||||
|
||||
def test_apply_override(minimal_config):
|
||||
config = minimal_config.copy()
|
||||
overrides = [{"editor": "nano"}]
|
||||
config = apply_overrides(overrides, config)
|
||||
assert config["editor"] == "nano"
|
||||
|
||||
|
||||
def test_override_dot_notation(minimal_config):
|
||||
cfg = minimal_config.copy()
|
||||
overrides = [{"colors.body": "blue"}]
|
||||
cfg = apply_overrides(overrides=overrides, base_config=cfg)
|
||||
assert cfg["colors"] == {"body": "blue", "date": "green"}
|
||||
|
||||
|
||||
def test_multiple_overrides(minimal_config):
|
||||
overrides = [
|
||||
{"colors.title": "magenta"},
|
||||
{"editor": "nano"},
|
||||
{"journals.burner": "/tmp/journals/burner.jrnl"},
|
||||
] # as returned by parse_args, saved in parser.config_override
|
||||
|
||||
cfg = apply_overrides(overrides, minimal_config.copy())
|
||||
assert cfg["editor"] == "nano"
|
||||
assert cfg["colors"]["title"] == "magenta"
|
||||
assert "burner" in cfg["journals"]
|
||||
assert cfg["journals"]["burner"] == "/tmp/journals/burner.jrnl"
|
||||
|
||||
|
||||
def test_recursively_apply():
|
||||
cfg = {"colors": {"body": "red", "title": "green"}}
|
||||
cfg = _recursively_apply(cfg, ["colors", "body"], "blue")
|
||||
assert cfg["colors"]["body"] == "blue"
|
||||
|
||||
|
||||
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
|
|
@ -3,6 +3,7 @@ import shlex
|
|||
import pytest
|
||||
|
||||
from jrnl.args import parse_args
|
||||
from jrnl.args import deserialize_config_args
|
||||
|
||||
|
||||
def cli_as_dict(str):
|
||||
|
@ -35,6 +36,7 @@ def expected_args(**kwargs):
|
|||
"strict": False,
|
||||
"tags": False,
|
||||
"text": [],
|
||||
"config_override": [],
|
||||
}
|
||||
return {**default_args, **kwargs}
|
||||
|
||||
|
@ -205,6 +207,31 @@ def test_version_alone():
|
|||
assert cli_as_dict("--version") == expected_args(preconfig_cmd=preconfig_version)
|
||||
|
||||
|
||||
def test_editor_override():
|
||||
|
||||
parsed_args = cli_as_dict('--config-override editor "nano"')
|
||||
assert parsed_args == expected_args(config_override=[{"editor": "nano"}])
|
||||
|
||||
|
||||
def test_color_override():
|
||||
assert cli_as_dict("--config-override colors.body blue") == expected_args(
|
||||
config_override=[{"colors.body": "blue"}]
|
||||
)
|
||||
|
||||
|
||||
def test_multiple_overrides():
|
||||
parsed_args = cli_as_dict(
|
||||
'--config-override colors.title green --config-override editor "nano" --config-override journal.scratchpad "/tmp/scratchpad"'
|
||||
)
|
||||
assert parsed_args == expected_args(
|
||||
config_override=[
|
||||
{"colors.title": "green"},
|
||||
{"editor": "nano"},
|
||||
{"journal.scratchpad": "/tmp/scratchpad"},
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
# @see https://github.com/jrnl-org/jrnl/issues/520
|
||||
@pytest.mark.parametrize(
|
||||
"cli",
|
||||
|
@ -233,3 +260,33 @@ def test_and_ordering(cli):
|
|||
def test_edit_ordering(cli):
|
||||
result = expected_args(edit=True, text=["second", "@oldtag", "@newtag"])
|
||||
assert cli_as_dict(cli) == result
|
||||
|
||||
|
||||
class TestDeserialization:
|
||||
@pytest.mark.parametrize(
|
||||
"input_str",
|
||||
[
|
||||
["editor", '"nano"'],
|
||||
["colors.title", "blue"],
|
||||
["default", "/tmp/egg.txt"],
|
||||
],
|
||||
)
|
||||
def test_deserialize_multiword_strings(self, input_str):
|
||||
|
||||
runtime_config = deserialize_config_args(input_str)
|
||||
assert runtime_config.__class__ == dict
|
||||
assert input_str[0] in runtime_config.keys()
|
||||
assert runtime_config[input_str[0]] == input_str[1]
|
||||
|
||||
def test_deserialize_multiple_datatypes(self):
|
||||
cfg = deserialize_config_args(["linewrap", "23"])
|
||||
assert cfg["linewrap"] == 23
|
||||
|
||||
cfg = deserialize_config_args(["encrypt", "false"])
|
||||
assert cfg["encrypt"] == False
|
||||
|
||||
cfg = deserialize_config_args(["editor", '"vi -c startinsert"'])
|
||||
assert cfg["editor"] == '"vi -c startinsert"'
|
||||
|
||||
cfg = deserialize_config_args(["highlight", "true"])
|
||||
assert cfg["highlight"] == True
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue