mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-07-04 23:46:14 +02:00
Simplify config override syntax (#5)
* update tests and expected behavior * clean up arg parsing tests * update deserialization * update deserialization * config argparse action * update override application logic * update tests; delete unused imports * override param must be list * update docstring * update test input to SUT * update remaining override unittests * make format * forgot to update CLI syntax
This commit is contained in:
parent
40f93a9322
commit
6e658c31df
6 changed files with 82 additions and 114 deletions
|
@ -43,10 +43,10 @@ def test_override_configured_editor(
|
|||
mock_load_or_install.return_value = minimal_config
|
||||
mock_isatty.return_value = True
|
||||
|
||||
cli_args = shlex.split('--config-override editor:"nano"')
|
||||
cli_args = shlex.split('--config-override editor "nano"')
|
||||
parser = parse_args(cli_args)
|
||||
assert parser.config_override.__len__() == 1
|
||||
assert "editor" in parser.config_override.keys()
|
||||
assert {"editor": "nano"} in parser.config_override
|
||||
|
||||
def mock_editor_launch(editor):
|
||||
print("%s launched! Success!" % editor)
|
||||
|
@ -54,7 +54,7 @@ def test_override_configured_editor(
|
|||
with mock.patch.object(
|
||||
jrnl,
|
||||
"_write_in_editor",
|
||||
side_effect=mock_editor_launch(parser.config_override["editor"]),
|
||||
side_effect=mock_editor_launch("TODO: replace"),
|
||||
return_value="note_contents",
|
||||
) as mock_write_in_editor:
|
||||
run(parser)
|
||||
|
@ -84,9 +84,9 @@ def test_override_configured_colors(
|
|||
):
|
||||
mock_load_or_install.return_value = minimal_config
|
||||
|
||||
cli_args = shlex.split("--config-override colors.body:blue")
|
||||
cli_args = shlex.split("--config-override colors.body blue")
|
||||
parser = parse_args(cli_args)
|
||||
assert "colors.body" in parser.config_override.keys()
|
||||
assert {"colors.body": "blue"} in parser.config_override
|
||||
with mock.patch.object(
|
||||
jrnl,
|
||||
"_write_in_editor",
|
||||
|
|
|
@ -16,24 +16,24 @@ def minimal_config():
|
|||
|
||||
def test_apply_override(minimal_config):
|
||||
config = minimal_config.copy()
|
||||
overrides = {"editor": "nano"}
|
||||
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"}
|
||||
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
|
||||
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"
|
||||
|
|
|
@ -36,7 +36,7 @@ def expected_args(**kwargs):
|
|||
"strict": False,
|
||||
"tags": False,
|
||||
"text": [],
|
||||
"config_override": None,
|
||||
"config_override": [],
|
||||
}
|
||||
return {**default_args, **kwargs}
|
||||
|
||||
|
@ -209,26 +209,26 @@ def test_version_alone():
|
|||
|
||||
def test_editor_override():
|
||||
|
||||
parsed_args = cli_as_dict('--config-override editor:"nano"')
|
||||
assert parsed_args == expected_args(config_override={"editor": "nano"})
|
||||
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"}
|
||||
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,editor:"nano",journal.scratchpad:"/tmp/scratchpad"'
|
||||
'--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",
|
||||
"journal.scratchpad": "/tmp/scratchpad",
|
||||
"editor": "nano",
|
||||
}
|
||||
config_override=[
|
||||
{"colors.title": "green"},
|
||||
{"editor": "nano"},
|
||||
{"journal.scratchpad": "/tmp/scratchpad"},
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
|
@ -266,49 +266,27 @@ class TestDeserialization:
|
|||
@pytest.mark.parametrize(
|
||||
"input_str",
|
||||
[
|
||||
'editor:"nano", colors.title:blue, default:"/tmp/egg.txt"',
|
||||
'editor:"vi -c startinsert", colors.title:blue, default:"/tmp/egg.txt"',
|
||||
'editor:"nano", colors.title:blue, default:"/tmp/eg\ g.txt"',
|
||||
["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 "editor" in runtime_config.keys()
|
||||
assert "colors.title" in runtime_config.keys()
|
||||
assert "default" in runtime_config.keys()
|
||||
|
||||
def test_deserialize_int(self):
|
||||
input = "linewrap: 23, default_hour: 19"
|
||||
runtime_config = deserialize_config_args(input)
|
||||
assert runtime_config["linewrap"] == 23
|
||||
assert runtime_config["default_hour"] == 19
|
||||
assert input_str[0] in runtime_config.keys()
|
||||
assert runtime_config[input_str[0]] == input_str[1]
|
||||
|
||||
def test_deserialize_multiple_datatypes(self):
|
||||
input = (
|
||||
'linewrap: 23, encrypt: false, editor:"vi -c startinsert", highlight: true'
|
||||
)
|
||||
cfg = deserialize_config_args(input)
|
||||
assert cfg["encrypt"] == False
|
||||
assert cfg["highlight"] == True
|
||||
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"'
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"delimiter",
|
||||
[
|
||||
".",
|
||||
":",
|
||||
", ", # note the whitespaces
|
||||
"-|-", # no reason not to handle multi-character delimiters
|
||||
],
|
||||
)
|
||||
def test_split_at_delimiter(self, delimiter):
|
||||
input = delimiter.join(
|
||||
["eggs ", "ba con", "ham"]
|
||||
) # The whitespaces are deliberate
|
||||
from jrnl.args import _split_at_delimiter
|
||||
|
||||
assert _split_at_delimiter(input, delimiter) == ["eggs ", "ba con", "ham"]
|
||||
assert _split_at_delimiter(input, delimiter, " ") == ["eggs ", "ba con", "ham"]
|
||||
cfg = deserialize_config_args(["highlight", "true"])
|
||||
assert cfg["highlight"] == True
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue