diff --git a/features/overrides.feature b/features/overrides.feature index 80fdf47c..eef89eec 100644 --- a/features/overrides.feature +++ b/features/overrides.feature @@ -28,7 +28,6 @@ Feature: Implementing Runtime Overrides for Select Configuration Keys """ @skip_win - @wip Scenario: Override color selections with runtime overrides Given we use the config "tiny.yaml" When we run jrnl with -1 --config-override colors.body:blue diff --git a/features/steps/override.py b/features/steps/override.py index b0edc864..ac531235 100644 --- a/features/steps/override.py +++ b/features/steps/override.py @@ -1,5 +1,3 @@ -from tests.test_config import expected_override -from jrnl.editor import get_text_from_stdin from jrnl.jrnl import run from jrnl.os_compat import split_args from unittest import mock @@ -12,16 +10,18 @@ import yaml from yaml.loader import FullLoader import jrnl + + def _mock_time_parse(context): - original_parse = jrnl.time.parse - if "now" not in context: - return original_parse + original_parse = jrnl.time.parse + if "now" not in context: + return original_parse - def wrapper(input, *args, **kwargs): - input = context.now if input == "now" else input - return original_parse(input, *args, **kwargs) + def wrapper(input, *args, **kwargs): + input = context.now if input == "now" else input + return original_parse(input, *args, **kwargs) - return wrapper + return wrapper @given("we use the config {config_file}") @@ -42,10 +42,7 @@ def run_command(context, args): @then("the runtime config should have {key_as_dots} set to {override_value}") def config_override(context, key_as_dots: str, override_value: str): key_as_vec = key_as_dots.split(".") - # with open(context.config_path) as f: - # loaded_cfg = yaml.load(f, Loader=yaml.FullLoader) - # loaded_cfg["journal"] = "features/journals/simple.journal" - + def _mock_callback(**args): print("callback executed") @@ -61,7 +58,7 @@ def config_override(context, key_as_dots: str, override_value: str): : run(context.parser) runtime_cfg = mock_recurse.call_args_list[0][0][0] - assert mock_recurse.call_count == key_as_vec.__len__() + for k in key_as_vec: runtime_cfg = runtime_cfg['%s'%k] @@ -73,15 +70,13 @@ def config_override(context, key_as_dots: str, override_value: str): @then("the editor {editor} should have been called") def editor_override(context, editor): - def _mock_write_in_editor(config): - editor = config['editor'] - journal = '/tmp/journal.jrnl' + editor = config["editor"] + journal = "features/journals/journal.jrnl" context.tmpfile = journal - print("%s has been launched"%editor) + print("%s has been launched" % editor) return journal - - + # fmt: off # see: https://github.com/psf/black/issues/664 with \ @@ -97,7 +92,7 @@ def editor_override(context, editor): context.editor = mock_write_in_editor expected_config = context.cfg expected_config['editor'] = '%s'%editor - expected_config['journal'] ='/tmp/journal.jrnl' + expected_config['journal'] ='features/journals/journal.jrnl' assert mock_write_in_editor.call_count == 1 assert mock_write_in_editor.call_args[0][0]['editor']==editor @@ -105,17 +100,24 @@ def editor_override(context, editor): context.exit_status = e.code # fmt: on -@then("the stdin prompt must be launched") -def override_editor_to_use_stdin(context): - try: - with \ - mock.patch('sys.stdin.read', return_value='Zwei peanuts walk into a bar und one of zem was a-salted')as mock_stdin_read, \ - mock.patch("jrnl.install.load_or_install_jrnl", return_value=context.cfg), \ - mock.patch("jrnl.Journal.open_journal", spec=False, return_value='/tmp/journal.jrnl'): +@then("the stdin prompt must be launched") +def override_editor_to_use_stdin(context): + + try: + with mock.patch( + "sys.stdin.read", + return_value="Zwei peanuts walk into a bar und one of zem was a-salted", + ) as mock_stdin_read, mock.patch( + "jrnl.install.load_or_install_jrnl", return_value=context.cfg + ), mock.patch( + "jrnl.Journal.open_journal", + spec=False, + return_value="features/journals/journal.jrnl", + ): run(context.parser) context.exit_status = 0 mock_stdin_read.assert_called_once() - except SystemExit as e : - context.exit_status = e.code \ No newline at end of file + except SystemExit as e: + context.exit_status = e.code diff --git a/jrnl/args.py b/jrnl/args.py index e1d73a8c..f2ea288c 100644 --- a/jrnl/args.py +++ b/jrnl/args.py @@ -4,7 +4,6 @@ import argparse import re import textwrap -import json from .commands import postconfig_decrypt from .commands import postconfig_encrypt @@ -24,11 +23,11 @@ def deserialize_config_args(input: str) -> dict: for _p in _kvpairs: l, r = _p.strip().split(":") r = r.strip() - if r.isdigit(): - r = int(r) - elif r.lower() == "true": - r = True - elif r.lower() == "false": + if r.isdigit(): + r = int(r) + elif r.lower() == "true": + r = True + elif r.lower() == "false": r = False runtime_modifications[l] = r return runtime_modifications diff --git a/tests/test_config.py b/tests/test_config.py index aa4d7079..dc2650a8 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -2,7 +2,6 @@ import shlex import pytest import mock -import yaml from jrnl.args import parse_args from jrnl.jrnl import run diff --git a/tests/test_parse_args.py b/tests/test_parse_args.py index 8e314aad..a49c99cb 100644 --- a/tests/test_parse_args.py +++ b/tests/test_parse_args.py @@ -1,4 +1,3 @@ -from features.steps.override import config_override import shlex import pytest @@ -6,6 +5,7 @@ import pytest from jrnl.args import parse_args from jrnl.args import deserialize_config_args + def cli_as_dict(str): cli = shlex.split(str) args = parse_args(cli) @@ -206,6 +206,7 @@ def test_version_alone(): assert cli_as_dict("--version") == expected_args(preconfig_cmd=preconfig_version) + class TestDeserialization: @pytest.mark.parametrize( "input_str", @@ -215,8 +216,7 @@ class TestDeserialization: 'editor:"nano", colors.title:blue, default:"/tmp/eg\ g.txt"', ], ) - def test_deserialize_multiword_strings(self,input_str): - + def test_deserialize_multiword_strings(self, input_str): runtime_config = deserialize_config_args(input_str) assert runtime_config.__class__ == dict @@ -224,18 +224,19 @@ class TestDeserialization: assert "colors.title" in runtime_config.keys() assert "default" in runtime_config.keys() - def test_deserialize_int(self): - input = 'linewrap: 23, default_hour: 19' + 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 runtime_config["linewrap"] == 23 + assert runtime_config["default_hour"] == 19 - def test_deserialize_multiple_datatypes(self): + def test_deserialize_multiple_datatypes(self): input = 'linewrap: 23, encrypt: false, editor:"vi -c startinsert"' - cfg = deserialize_config_args(input) - assert cfg['encrypt'] == False - assert cfg['linewrap'] == 23 - assert cfg['editor'] == '"vi -c startinsert"' + cfg = deserialize_config_args(input) + assert cfg["encrypt"] == False + assert cfg["linewrap"] == 23 + assert cfg["editor"] == '"vi -c startinsert"' + def test_editor_override():