mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-10 16:48:31 +02:00
Add --config-override feature
* add test and argument handler for runtime override of configurations.
* identify location to apply override in "main"
* update gitignore
* remove unneeded import
* add jrnl interface test for overriden configurations
* trivial whitespace change
* implement runtime override
* make format
* refactor override unittest
* clean up unused import
* start writing integration test
* add linewrap override scenario
* implement editor override step
* add dev dependencies on pytest -mock and -cov
* make format
* remove unused imports
* make format
* rename --override to --config-override
* move override implementation into own module
* begin TDD of dot notated overrides
* rewrite behavior scenario
* implement recursive config overrides
* clean up unittests
* iterate on behave step
* make format
* cleanup
* move override behave tests out of core
* refactor recursive code
* make format
* code cleanup
* remove unused import
* update test config
* rewrite test for better mock call expect
* make format
* binary search misbehaving windows test
* unittest multiple overrides
* uncomment dot notation unittest
* add multiple override scenario spec
* make format
* make format
* update unittests for new syntax
* update integ tests for new syntax
* update gitignore
* guard override application
* deserialize function as return type
* make format
* organize deserialization unittests
* better, more specific behave tests
* test different editor launch commands
* formatting
* handle datatypes in deserialization and update helptext
* stick to config convention in testbed
* update tests ith better verifications
* make format
* space
* review feedbac
* make format
* skip on win
* update deps
* update tests with better verifications
make format
space
review feedbac
* skip on win
* update deps
* refactor deserialization
organize test_parse_args
make format
* skip on win
* refactor deserialization
organize test_parse_args
make format
* update tests ith better verifications
* make format
* space
* make format
* document apply_overrides
* update gitignore
* document config-override enhancement
* 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
* update documentation to sphinx style
* variable renames
* Lockfile merge (#7)
* Add brew and gitter badges to README
* Update changelog [ci skip]
* Make journal selection behavior more consistent when there's a colon with no date (#1164)
* Simplify config override syntax (#8)
* 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
* formatting
* Update pyproject.toml
* update lockfile to remove pytest-cov and pytest-mock deps
* update docs
* reuse existing mock; delete unneeded code
* move overrides earlier in the execution
use existing configs instead of custom
make format
clean up imports
* update for passworded access
context.parser -> parsed_args
* test that no editor is launched
* remove unnecessary mocks
* rename variable for intent
* reinstate getpass deletion
* update gitignore
* capture failure mode
* remove unneeded imports
* renamed variable
* delete redundant step
* comment on step
* clean up step behavior description
* [WIP] lock down journal access behavior
* skip -> wip
* correct command for overriding journal via dot keys
* update wip test for updating a "temp" journal and then reading baack its entries
* remove "mock" from poetry file
* make CI happy
* complex behavior sequence for default journal override
* separate out smaller pieces of logic
test that apply_overrides acts on base configuration and not the copy
* 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
* Update .gitignore
* remove skip_win
* forward override unpacking to yaml library
* merge config override step with existing config_var step in core
delete config_override step
unify step description syntax
* delete unused and redundant code
* rebases are hard
* remove wip tag from test
* remove skipped tests for windows
* Address code review
yield -> return
remove needless copy
adjust spacing
re-inline args return
reset packaging info to e6c0a16342
revert package version for this PR
* consolidate imports
* Defer config_override unpacking to dict *after* base config is loaded
store cli overrides without unpacking just yet
move deserialize_config_args to config module
delete custom Action class for config operations
apply [k,v] -> {k, v} for each override
update test data
update import
* rename deserialize_config_args to better express intent
make format
292 lines
7.8 KiB
Python
292 lines
7.8 KiB
Python
import shlex
|
|
|
|
import pytest
|
|
|
|
from jrnl.args import parse_args
|
|
from jrnl.config import make_yaml_valid_dict
|
|
|
|
|
|
def cli_as_dict(str):
|
|
cli = shlex.split(str)
|
|
args = parse_args(cli)
|
|
return vars(args)
|
|
|
|
|
|
def expected_args(**kwargs):
|
|
default_args = {
|
|
"contains": None,
|
|
"debug": False,
|
|
"delete": False,
|
|
"edit": False,
|
|
"end_date": None,
|
|
"today_in_history": False,
|
|
"month": None,
|
|
"day": None,
|
|
"year": None,
|
|
"excluded": [],
|
|
"export": False,
|
|
"filename": None,
|
|
"limit": None,
|
|
"on_date": None,
|
|
"preconfig_cmd": None,
|
|
"postconfig_cmd": None,
|
|
"short": False,
|
|
"starred": False,
|
|
"start_date": None,
|
|
"strict": False,
|
|
"tags": False,
|
|
"text": [],
|
|
"config_override": [],
|
|
}
|
|
return {**default_args, **kwargs}
|
|
|
|
|
|
def test_empty():
|
|
assert cli_as_dict("") == expected_args()
|
|
|
|
|
|
def test_contains_alone():
|
|
assert cli_as_dict("-contains whatever") == expected_args(contains="whatever")
|
|
|
|
|
|
def test_debug_alone():
|
|
assert cli_as_dict("--debug") == expected_args(debug=True)
|
|
|
|
|
|
def test_delete_alone():
|
|
assert cli_as_dict("--delete") == expected_args(delete=True)
|
|
|
|
|
|
def test_diagnostic_alone():
|
|
from jrnl.commands import preconfig_diagnostic
|
|
|
|
assert cli_as_dict("--diagnostic") == expected_args(
|
|
preconfig_cmd=preconfig_diagnostic
|
|
)
|
|
|
|
|
|
def test_edit_alone():
|
|
assert cli_as_dict("--edit") == expected_args(edit=True)
|
|
|
|
|
|
def test_encrypt_alone():
|
|
from jrnl.commands import postconfig_encrypt
|
|
|
|
assert cli_as_dict("--encrypt") == expected_args(postconfig_cmd=postconfig_encrypt)
|
|
|
|
|
|
def test_decrypt_alone():
|
|
from jrnl.commands import postconfig_decrypt
|
|
|
|
assert cli_as_dict("--decrypt") == expected_args(postconfig_cmd=postconfig_decrypt)
|
|
|
|
|
|
def test_end_date_alone():
|
|
expected = expected_args(end_date="2020-01-01")
|
|
assert expected == cli_as_dict("-until 2020-01-01")
|
|
assert expected == cli_as_dict("-to 2020-01-01")
|
|
|
|
|
|
def test_not_alone():
|
|
assert cli_as_dict("-not test") == expected_args(excluded=["test"])
|
|
|
|
|
|
def test_not_multiple_alone():
|
|
assert cli_as_dict("-not one -not two") == expected_args(excluded=["one", "two"])
|
|
assert cli_as_dict("-not one -not two -not three") == expected_args(
|
|
excluded=["one", "two", "three"]
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"cli",
|
|
[
|
|
"two -not one -not three",
|
|
"-not one two -not three",
|
|
"-not one -not three two",
|
|
],
|
|
)
|
|
def test_not_mixed(cli):
|
|
result = expected_args(excluded=["one", "three"], text=["two"])
|
|
assert cli_as_dict(cli) == result
|
|
|
|
|
|
def test_not_interspersed():
|
|
result = expected_args(excluded=["one", "three"], text=["two", "two", "two"])
|
|
assert cli_as_dict("two -not one two -not three two") == result
|
|
|
|
|
|
def test_export_alone():
|
|
assert cli_as_dict("--export json") == expected_args(export="json")
|
|
|
|
|
|
def test_import_alone():
|
|
from jrnl.commands import postconfig_import
|
|
|
|
assert cli_as_dict("--import") == expected_args(postconfig_cmd=postconfig_import)
|
|
|
|
|
|
def test_file_flag_alone():
|
|
assert cli_as_dict("--file test.txt") == expected_args(filename="test.txt")
|
|
assert cli_as_dict("--file 'lorem ipsum.txt'") == expected_args(
|
|
filename="lorem ipsum.txt"
|
|
)
|
|
|
|
|
|
def test_limit_alone():
|
|
assert cli_as_dict("-n 5") == expected_args(limit=5)
|
|
assert cli_as_dict("-n 999") == expected_args(limit=999)
|
|
|
|
|
|
def test_limit_shorthand_alone():
|
|
assert cli_as_dict("-5") == expected_args(limit=5)
|
|
assert cli_as_dict("-999") == expected_args(limit=999)
|
|
|
|
|
|
def test_list_alone():
|
|
from jrnl.commands import postconfig_list
|
|
|
|
assert cli_as_dict("--ls") == expected_args(postconfig_cmd=postconfig_list)
|
|
|
|
|
|
def test_on_date_alone():
|
|
assert cli_as_dict("-on 'saturday'") == expected_args(on_date="saturday")
|
|
|
|
|
|
def test_month_alone():
|
|
assert cli_as_dict("-month 1") == expected_args(month="1")
|
|
assert cli_as_dict("-month 01") == expected_args(month="01")
|
|
assert cli_as_dict("-month January") == expected_args(month="January")
|
|
assert cli_as_dict("-month Jan") == expected_args(month="Jan")
|
|
|
|
|
|
def test_day_alone():
|
|
assert cli_as_dict("-day 1") == expected_args(day="1")
|
|
assert cli_as_dict("-day 01") == expected_args(day="01")
|
|
|
|
|
|
def test_year_alone():
|
|
assert cli_as_dict("-year 2021") == expected_args(year="2021")
|
|
assert cli_as_dict("-year 21") == expected_args(year="21")
|
|
|
|
|
|
def test_today_in_history_alone():
|
|
assert cli_as_dict("-today-in-history") == expected_args(today_in_history=True)
|
|
|
|
|
|
def test_short_alone():
|
|
assert cli_as_dict("--short") == expected_args(short=True)
|
|
|
|
|
|
def test_starred_alone():
|
|
assert cli_as_dict("-starred") == expected_args(starred=True)
|
|
|
|
|
|
def test_start_date_alone():
|
|
assert cli_as_dict("-from 2020-01-01") == expected_args(start_date="2020-01-01")
|
|
assert cli_as_dict("-from 'January 1st'") == expected_args(start_date="January 1st")
|
|
|
|
|
|
def test_and_alone():
|
|
assert cli_as_dict("-and") == expected_args(strict=True)
|
|
|
|
|
|
def test_tags_alone():
|
|
assert cli_as_dict("--tags") == expected_args(tags=True)
|
|
|
|
|
|
def test_text_alone():
|
|
assert cli_as_dict("lorem ipsum dolor sit amet") == expected_args(
|
|
text=["lorem", "ipsum", "dolor", "sit", "amet"]
|
|
)
|
|
|
|
|
|
def test_version_alone():
|
|
from jrnl.commands import preconfig_version
|
|
|
|
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",
|
|
[
|
|
"-and second @oldtag @newtag",
|
|
"second @oldtag @newtag -and",
|
|
"second -and @oldtag @newtag",
|
|
"second @oldtag -and @newtag",
|
|
],
|
|
)
|
|
def test_and_ordering(cli):
|
|
result = expected_args(strict=True, text=["second", "@oldtag", "@newtag"])
|
|
assert cli_as_dict(cli) == result
|
|
|
|
|
|
# @see https://github.com/jrnl-org/jrnl/issues/520
|
|
@pytest.mark.parametrize(
|
|
"cli",
|
|
[
|
|
"--edit second @oldtag @newtag",
|
|
"second @oldtag @newtag --edit",
|
|
"second --edit @oldtag @newtag",
|
|
"second @oldtag --edit @newtag",
|
|
],
|
|
)
|
|
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 = make_yaml_valid_dict(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 = make_yaml_valid_dict(["linewrap", "23"])
|
|
assert cfg["linewrap"] == 23
|
|
|
|
cfg = make_yaml_valid_dict(["encrypt", "false"])
|
|
assert cfg["encrypt"] == False
|
|
|
|
cfg = make_yaml_valid_dict(["editor", "vi -c startinsert"])
|
|
assert cfg["editor"] == "vi -c startinsert"
|
|
|
|
cfg = make_yaml_valid_dict(["highlight", "true"])
|
|
assert cfg["highlight"] == True
|