mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-19 20:48:31 +02:00
better, more specific behave tests
This commit is contained in:
parent
abbb2c502f
commit
0f5dfade18
1 changed files with 57 additions and 24 deletions
|
@ -1,3 +1,5 @@
|
|||
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
|
||||
|
@ -10,7 +12,16 @@ import yaml
|
|||
from yaml.loader import FullLoader
|
||||
|
||||
import jrnl
|
||||
from jrnl.cli import cli
|
||||
def _mock_time_parse(context):
|
||||
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)
|
||||
|
||||
return wrapper
|
||||
|
||||
|
||||
@given("we use the config {config_file}")
|
||||
|
@ -31,13 +42,9 @@ 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(".")
|
||||
expected_call_args_list = [
|
||||
(context.cfg, key_as_vec, override_value),
|
||||
(context.cfg[key_as_vec[0]], key_as_vec[1], override_value),
|
||||
]
|
||||
with open(context.config_path) as f:
|
||||
loaded_cfg = yaml.load(f, Loader=yaml.FullLoader)
|
||||
loaded_cfg["journal"] = "features/journals/simple.journal"
|
||||
# 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")
|
||||
|
@ -45,16 +52,20 @@ def config_override(context, key_as_dots: str, override_value: str):
|
|||
# fmt: off
|
||||
try:
|
||||
with \
|
||||
mock.patch("jrnl.jrnl.search_mode"), \
|
||||
mock.patch.object(jrnl.override,"_recursively_apply",wraps=jrnl.override._recursively_apply) as mock_recurse, \
|
||||
mock.patch('jrnl.install.load_or_install_jrnl', return_value=context.cfg), \
|
||||
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) \
|
||||
:
|
||||
run(context.parser)
|
||||
|
||||
assert mock_recurse.call_count >= 2
|
||||
mock_recurse.call_args_list = expected_call_args_list
|
||||
|
||||
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]
|
||||
|
||||
assert runtime_cfg == override_value
|
||||
except SystemExit as e :
|
||||
context.exit_status = e.code
|
||||
# fmt: on
|
||||
|
@ -62,27 +73,49 @@ 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_editor(command_and_journal_file):
|
||||
editor = command_and_journal_file[0]
|
||||
tmpfile = command_and_journal_file[-1]
|
||||
context.tmpfile = tmpfile
|
||||
print("%s has been launched" % editor)
|
||||
return tmpfile
|
||||
|
||||
|
||||
def _mock_write_in_editor(config):
|
||||
editor = config['editor']
|
||||
journal = '/tmp/journal.jrnl'
|
||||
context.tmpfile = journal
|
||||
print("%s has been launched"%editor)
|
||||
return journal
|
||||
|
||||
|
||||
# fmt: off
|
||||
# see: https://github.com/psf/black/issues/664
|
||||
with \
|
||||
mock.patch("subprocess.call", side_effect=_mock_editor) as mock_editor, \
|
||||
mock.patch("jrnl.jrnl._write_in_editor", side_effect=_mock_write_in_editor(context.cfg)) as mock_write_in_editor, \
|
||||
mock.patch("sys.stdin.isatty", return_value=True), \
|
||||
mock.patch("jrnl.time.parse"), \
|
||||
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 :
|
||||
cli(['--config-override','{"editor": "%s"}'%editor])
|
||||
run(context.parser)
|
||||
context.exit_status = 0
|
||||
context.editor = mock_editor
|
||||
assert mock_editor.assert_called_once_with(editor, context.tmpfile)
|
||||
context.editor = mock_write_in_editor
|
||||
expected_config = context.cfg
|
||||
expected_config['editor'] = '%s'%editor
|
||||
expected_config['journal'] ='/tmp/journal.jrnl'
|
||||
|
||||
assert mock_write_in_editor.call_count == 1
|
||||
assert mock_write_in_editor.call_args[0][0]['editor']==editor
|
||||
except SystemExit as e:
|
||||
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'):
|
||||
run(context.parser)
|
||||
context.exit_status = 0
|
||||
mock_stdin_read.assert_called_once()
|
||||
|
||||
except SystemExit as e :
|
||||
context.exit_status = e.code
|
Loading…
Add table
Reference in a new issue