reuse existing mock; delete unneeded code

This commit is contained in:
sriniv27 2021-02-08 20:14:48 -05:00
parent 9b80d56773
commit c8e09b023f

View file

@ -11,17 +11,7 @@ from yaml.loader import FullLoader
import jrnl import jrnl
from features.steps.core import _mock_time_parse
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}") @given("we use the config {config_file}")
@ -30,33 +20,22 @@ def load_config(context, config_file):
context.config_path = os.path.abspath(filepath) context.config_path = os.path.abspath(filepath)
@when("we run jrnl with {args}")
def run_command(context, args):
context.args = split_args("%s" % args)
context.parser = parse_args(context.args)
with open(context.config_path, "r") as f:
cfg = yaml.load(f, Loader=FullLoader)
context.cfg = cfg
@then("the runtime config should have {key_as_dots} set to {override_value}") @then("the runtime config should have {key_as_dots} set to {override_value}")
def config_override(context, key_as_dots: str, override_value: str): def config_override(context, key_as_dots: str, override_value: str):
key_as_vec = key_as_dots.split(".") key_as_vec = key_as_dots.split(".")
def _mock_callback(**args):
print("callback executed")
# fmt: off # fmt: off
try: try:
with \ with \
mock.patch("jrnl.jrnl.search_mode"), \ mock.patch("jrnl.jrnl.search_mode"), \
mock.patch.object(jrnl.override,"_recursively_apply",wraps=jrnl.override._recursively_apply) as mock_recurse, \ 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.install.load_or_install_jrnl', return_value=context.jrnl_config), \
mock.patch('jrnl.time.parse', side_effect=_mock_time_parse(context)), \ 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.config.get_config_path", side_effect=lambda: context.config_path), \
mock.patch("jrnl.install.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) parsed_args = parse_args(context.args)
run(parsed_args)
runtime_cfg = mock_recurse.call_args_list[0][0][0] runtime_cfg = mock_recurse.call_args_list[0][0][0]
for k in key_as_vec: for k in key_as_vec:
@ -80,7 +59,7 @@ def editor_override(context, editor):
# fmt: off # fmt: off
# see: https://github.com/psf/black/issues/664 # see: https://github.com/psf/black/issues/664
with \ with \
mock.patch("jrnl.jrnl._write_in_editor", side_effect=_mock_write_in_editor(context.cfg)) as mock_write_in_editor, \ 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("sys.stdin.isatty", return_value=True), \
mock.patch("jrnl.time.parse", side_effect = _mock_time_parse(context)), \ 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.config.get_config_path", side_effect=lambda: context.config_path), \
@ -90,7 +69,7 @@ def editor_override(context, editor):
run(context.parser) run(context.parser)
context.exit_status = 0 context.exit_status = 0
context.editor = mock_write_in_editor context.editor = mock_write_in_editor
expected_config = context.cfg expected_config = context.jrnl_config
expected_config['editor'] = '%s'%editor expected_config['editor'] = '%s'%editor
expected_config['journal'] ='features/journals/journal.jrnl' expected_config['journal'] ='features/journals/journal.jrnl'
@ -101,7 +80,7 @@ def editor_override(context, editor):
# fmt: on # fmt: on
@then("the stdin prompt must be launched") @then("the stdin prompt should have been called")
def override_editor_to_use_stdin(context): def override_editor_to_use_stdin(context):
try: try:
@ -109,7 +88,7 @@ def override_editor_to_use_stdin(context):
"sys.stdin.read", "sys.stdin.read",
return_value="Zwei peanuts walk into a bar und one of zem was a-salted", return_value="Zwei peanuts walk into a bar und one of zem was a-salted",
) as mock_stdin_read, mock.patch( ) as mock_stdin_read, mock.patch(
"jrnl.install.load_or_install_jrnl", return_value=context.cfg "jrnl.install.load_or_install_jrnl", return_value=context.jrnl_config
), mock.patch( ), mock.patch(
"jrnl.Journal.open_journal", "jrnl.Journal.open_journal",
spec=False, spec=False,