summaryrefslogtreecommitdiffstats
path: root/features
diff options
context:
space:
mode:
Diffstat (limited to 'features')
-rw-r--r--features/overrides.feature98
-rw-r--r--features/steps/core.py41
-rw-r--r--features/steps/override.py77
3 files changed, 212 insertions, 4 deletions
diff --git a/features/overrides.feature b/features/overrides.feature
new file mode 100644
index 00000000..e0cdd9f0
--- /dev/null
+++ b/features/overrides.feature
@@ -0,0 +1,98 @@
+Feature: Implementing Runtime Overrides for Select Configuration Keys
+
+ Scenario: Override configured editor with built-in input === editor:''
+ Given we use the config "basic_encrypted.yaml"
+ And we use the password "test" if prompted
+ When we run "jrnl --config-override editor ''"
+ Then the stdin prompt should have been called
+
+ Scenario: Postconfig commands with overrides
+ Given We use the config "basic_encrypted.yaml"
+ And we use the password "test" if prompted
+ When we run "jrnl --decrypt --config-override highlight false --config-override editor nano"
+ Then the config should have "highlight" set to "bool:false"
+ And no editor should have been called
+
+ Scenario: Override configured linewrap with a value of 23
+ Given we use the config "simple.yaml"
+ And we use the password "test" if prompted
+ When we run "jrnl -2 --config-override linewrap 23 --format fancy"
+ Then the output should be
+
+ """
+ ┎─────╮2013-06-09 15:39
+ ┃ My ╘═══════════════╕
+ ┃ fir st ent ry. │
+ ┠╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
+ ┃ Everything is │
+ ┃ alright │
+ ┖─────────────────────┘
+ ┎─────╮2013-06-10 15:40
+ ┃ Lif ╘═══════════════╕
+ ┃ e is goo d. │
+ ┠╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
+ ┃ But I'm better. │
+ ┖─────────────────────┘
+ """
+
+ Scenario: Override color selections with runtime overrides
+ Given we use the config "basic_encrypted.yaml"
+ And we use the password "test" if prompted
+ When we run "jrnl -1 --config-override colors.body blue"
+ Then the config should have "colors.body" set to "blue"
+
+ Scenario: Apply multiple config overrides
+ Given we use the config "basic_encrypted.yaml"
+ And we use the password "test" if prompted
+ When we run "jrnl -1 --config-override colors.body green --config-override editor 'nano'"
+ Then the config should have "colors.body" set to "green"
+ And the config should have "editor" set to "nano"
+
+
+ Scenario Outline: Override configured editor
+ Given we use the config "basic_encrypted.yaml"
+ And we use the password "test" if prompted
+ When we run "jrnl --config-override editor '<editor>'"
+ Then the editor <editor> should have been called
+ Examples: Editor Commands
+ | editor |
+ | nano |
+ | vi -c startinsert |
+ | code -w |
+
+ Scenario: Override default journal
+ Given we use the config "basic_dayone.yaml"
+ And we use the password "test" if prompted
+ When we run "jrnl --debug --config-override journals.default features/journals/simple.journal 20 Mar 2000: The rain in Spain comes from clouds"
+ Then we should get no error
+ And we should see the message "Entry added"
+ When we run "jrnl -3 --debug --config-override journals.default features/journals/simple.journal"
+ Then the output should be
+ """
+ 2000-03-20 09:00 The rain in Spain comes from clouds
+
+ 2013-06-09 15:39 My first entry.
+ | Everything is alright
+
+ 2013-06-10 15:40 Life is good.
+ | But I'm better.
+ """
+
+
+ Scenario: Make an entry into an overridden journal
+ Given we use the config "basic_dayone.yaml"
+ And we use the password "test" if prompted
+ When we run "jrnl --config-override journals.temp features/journals/simple.journal temp Sep 06 1969: @say Ni"
+ Then we should get no error
+ And we should see the message "Entry added"
+ When we run "jrnl --config-override journals.temp features/journals/simple.journal temp -3"
+ Then the output should be
+ """
+ 1969-09-06 09:00 @say Ni
+
+ 2013-06-09 15:39 My first entry.
+ | Everything is alright
+
+ 2013-06-10 15:40 Life is good.
+ | But I'm better.
+ """
diff --git a/features/steps/core.py b/features/steps/core.py
index abac4917..f471acfb 100644
--- a/features/steps/core.py
+++ b/features/steps/core.py
@@ -3,6 +3,7 @@
import ast
from collections import defaultdict
+from jrnl.args import parse_args
import os
from pathlib import Path
import re
@@ -13,8 +14,11 @@ from behave import given
from behave import then
from behave import when
import keyring
+
import toml
import yaml
+from yaml.loader import FullLoader
+
import jrnl.time
from jrnl import Journal
@@ -23,6 +27,7 @@ from jrnl import plugins
from jrnl.cli import cli
from jrnl.config import load_config
from jrnl.os_compat import split_args
+from jrnl.override import apply_overrides, _recursively_apply
try:
import parsedatetime.parsedatetime_consts as pdt
@@ -114,8 +119,15 @@ def read_value_from_string(string):
return ast.literal_eval(string)
# Takes strings like "bool:true" or "int:32" and coerces them into proper type
- t, value = string.split(":")
- value = {"bool": lambda v: v.lower() == "true", "int": int, "str": str}[t](value)
+ string_parts = string.split(":")
+ if len(string_parts) > 1:
+ type = string_parts[0]
+ value = string_parts[1:][0] # rest of the text
+ value = {"bool": lambda v: v.lower() == "true", "int": int, "str": str}[type](
+ value
+ )
+ else:
+ value = string_parts[0]
return value
@@ -315,6 +327,7 @@ def run_with_input(context, command, inputs=""):
text = iter([inputs])
args = split_args(command)[1:]
+ context.args = args
def _mock_editor(command):
context.editor_command = command
@@ -397,8 +410,13 @@ def run(context, command, text=""):
if "cache_dir" in context and context.cache_dir is not None:
cache_dir = os.path.join("features", "cache", context.cache_dir)
command = command.format(cache_dir=cache_dir)
+ if "config_path" in context and context.config_path is not None:
+ with open(context.config_path, "r") as f:
+ cfg = yaml.load(f, Loader=FullLoader)
+ context.jrnl_config = cfg
args = split_args(command)
+ context.args = args[1:]
def _mock_editor(command):
context.editor_command = command
@@ -604,14 +622,29 @@ def journal_exists(context, journal_name="default"):
@then('the config should have "{key}" set to "{value}"')
@then('the config for journal "{journal}" should have "{key}" set to "{value}"')
def config_var(context, key, value="", journal=None):
+ key_as_vec = key.split(".")
+
+ if "args" in context:
+ parsed = parse_args(context.args)
+ overrides = parsed.config_override
value = read_value_from_string(value or context.text or "")
configuration = load_config(context.config_path)
if journal:
configuration = configuration["journals"][journal]
- assert key in configuration
- assert configuration[key] == value
+ if overrides:
+ with patch.object(
+ jrnl.override, "_recursively_apply", wraps=_recursively_apply
+ ) as spy_recurse:
+ configuration = apply_overrides(overrides, configuration)
+ runtime_cfg = spy_recurse.call_args_list[0][0][0]
+ else:
+ runtime_cfg = configuration
+ # extract the value of the desired key from the configuration after overrides have been applied
+ for k in key_as_vec:
+ runtime_cfg = runtime_cfg["%s" % k]
+ assert runtime_cfg == value
@then('the config for journal "{journal}" should not have "{key}" set')
diff --git a/features/steps/override.py b/features/steps/override.py
new file mode 100644
index 00000000..ff1760ed
--- /dev/null
+++ b/features/steps/override.py
@@ -0,0 +1,77 @@
+from jrnl.jrnl import run
+from unittest import mock
+
+# from __future__ import with_statement
+from jrnl.args import parse_args
+from behave import then
+
+from features.steps.core import _mock_getpass, _mock_time_parse
+
+
+@then("the editor {editor} should have been called")
+@then("No editor should have been called")
+def editor_override(context, editor=None):
+ def _mock_write_in_editor(config):
+ editor = config["editor"]
+ journal = "features/journals/journal.jrnl"
+ context.tmpfile = journal
+ print("%s has been launched" % editor)
+ return journal
+
+ if "password" in context:
+ password = context.password
+ else:
+ password = ""
+ # fmt: off
+ # see: https://github.com/psf/black/issues/664
+ with \
+ mock.patch("jrnl.jrnl._write_in_editor", side_effect=_mock_write_in_editor(context.jrnl_config)) as mock_write_in_editor, \
+ mock.patch("sys.stdin.isatty", return_value=True), \
+ mock.patch('getpass.getpass',side_effect=_mock_getpass(password)), \
+ mock.patch("jrnl.time.parse", side_effect = _mock_time_parse(context)), \
+ mock.patch("jrnl.config.get_config_path", side_effect=lambda: context.config_path), \
+ mock.patch("jrnl.install.get_config_path", side_effect=lambda: context.config_path) \
+ :
+ try :
+ parsed_args = parse_args(context.args)
+ run(parsed_args)
+ context.exit_status = 0
+ context.editor = mock_write_in_editor
+ expected_config = context.jrnl_config
+ expected_config['editor'] = '%s'%editor
+ expected_config['journal'] ='features/journals/journal.jrnl'
+
+ if editor is not None:
+ assert mock_write_in_editor.call_count == 1
+ assert mock_write_in_editor.call_args[0][0]['editor']==editor
+ else:
+ # Expect that editor is *never* called
+ mock_write_in_editor.assert_not_called()
+ except SystemExit as e:
+ context.exit_status = e.code
+ # fmt: on
+
+
+@then("the stdin prompt should have been called")
+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.jrnl_config
+ ), mock.patch(
+ "jrnl.Journal.open_journal",
+ spec=False,
+ return_value="features/journals/journal.jrnl",
+ ), mock.patch(
+ "getpass.getpass", side_effect=_mock_getpass("test")
+ ):
+ parsed_args = parse_args(context.args)
+ run(parsed_args)
+ context.exit_status = 0
+ mock_stdin_read.assert_called_once()
+
+ except SystemExit as e:
+ context.exit_status = e.code