From 80d2c609badde4888f3469c0a77396ddd880de64 Mon Sep 17 00:00:00 2001 From: Suhas Date: Sun, 24 Jan 2021 07:18:48 -0500 Subject: [PATCH] begin TDD of dot notated overrides --- features/overrides.feature | 12 ++++++++++-- jrnl/args.py | 10 ++++++---- tests/test_config.py | 23 +++++++++++++++++++++-- tests/test_parse_args.py | 5 ++++- 4 files changed, 41 insertions(+), 9 deletions(-) diff --git a/features/overrides.feature b/features/overrides.feature index 07aa45d2..3108b407 100644 --- a/features/overrides.feature +++ b/features/overrides.feature @@ -2,12 +2,12 @@ Feature: Implementing Runtime Overrides for Select Configuration Keys Scenario: Override configured editor with built-in input === editor:'' Given we use the config "editor-args.yaml" -When we run "jrnl --config-override '{\"editor\": \"\""}'" +When we run "jrnl --config-override '{"editor": """}'" Then the editor "" should have been called Scenario: Override configured editor with 'nano' Given we use the config "editor.yaml" -When we run "jrnl --config-override '{\"editor\": \"nano\"}'" +When we run "jrnl --config-override '{"editor": "nano"}'" Then the editor "nano" should have been called Scenario: Override configured linewrap with a value of 23 @@ -28,4 +28,12 @@ Then the output should be ┠╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤ ┃ But I'm better. │ ┖─────────────────────┘ +""" + +Scenario: Override color selections with runtime overrides +Given we use the config "no_colors.yaml" +When we run "jrnl --config-override '{"colors.body": "blue"}'" +Then the config should have "colors" set to +""" +'body': 'blue' """ \ No newline at end of file diff --git a/jrnl/args.py b/jrnl/args.py index efbe8138..d991783f 100644 --- a/jrnl/args.py +++ b/jrnl/args.py @@ -315,11 +315,11 @@ def parse_args(args=[]): help=argparse.SUPPRESS, ) - overrides = parser.add_argument_group( + config_overrides = parser.add_argument_group( "Config file overrides", textwrap.dedent("These are one-off overrides of the config file options"), ) - overrides.add_argument( + config_overrides.add_argument( "--config-override", dest="config_override", action="store", @@ -330,9 +330,11 @@ def parse_args(args=[]): help=""" Override configured key-value pairs with CONFIG_KV_PAIR for this command invocation only. - For example, to use a different editor for this jrnl entry, call: + Examples: + - Use a different editor for this jrnl entry, call: jrnl --config-override '{"editor": "nano"}' - + - Override color selections + jrnl --config-override '{"colors.body":"blue", "colors.title": "green"} """, ) diff --git a/tests/test_config.py b/tests/test_config.py index f094313c..d41b2de2 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -4,7 +4,7 @@ import mock import yaml from jrnl.args import parse_args -from jrnl.jrnl import run +from jrnl.jrnl import run, search_mode from jrnl import install @@ -43,7 +43,7 @@ def test_override_configured_editor( cli_args = ["--config-override", '{"editor": "nano"}'] parser = parse_args(cli_args) assert parser.config_override.__len__() == 1 - + assert "editor" in parser.config_override.keys() def mock_editor_launch(editor): print("%s launched! Success!" % editor) @@ -55,3 +55,22 @@ def test_override_configured_editor( ) 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"] = "features/journals/simple.journal" + yield exp_out_cfg + +# @mock.patch.object(install,'load_or_install_jrnl') +# @mock.patch('subprocess.call') +# def test_override_configured_colors(mock_load_or_install, mock_subprocess_call, minimal_config, expected_color_override, capsys): +# mock_load_or_install.return_value = minimal_config + +# cli_args=["--config-override",'{"colors.body": "blue"}'] +# parser = parse_args(cli_args) +# assert "colors.body" in parser.config_override.keys() +# run(parser) + + diff --git a/tests/test_parse_args.py b/tests/test_parse_args.py index 5520e3d6..b71173aa 100644 --- a/tests/test_parse_args.py +++ b/tests/test_parse_args.py @@ -211,7 +211,10 @@ def test_editor_override(): assert cli_as_dict('--config-override \'{"editor": "nano"}\'') == 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"} + ) # @see https://github.com/jrnl-org/jrnl/issues/520 @pytest.mark.parametrize(