rework mocks in test suite for new messaging functionality

This commit is contained in:
Jonathan Wren 2022-05-28 15:13:27 -07:00
parent d2f2967bc1
commit 34df64c989
4 changed files with 41 additions and 45 deletions

View file

@ -69,7 +69,7 @@ Feature: Reading and writing to journal with custom date formats
Scenario: Writing an entry at the prompt with custom date Scenario: Writing an entry at the prompt with custom date
Given we use the config "little_endian_dates.yaml" Given we use the config "little_endian_dates.yaml"
When we run "jrnl" and enter "2013-05-10: I saw Elvis. He's alive." When we run "jrnl" and pipe "2013-05-10: I saw Elvis. He's alive."
Then we should get no error Then we should get no error
When we run "jrnl -999" When we run "jrnl -999"
Then the output should contain "10.05.2013 09:00 I saw Elvis." Then the output should contain "10.05.2013 09:00 I saw Elvis."

View file

@ -86,7 +86,7 @@ def cli_run(
mock_editor, mock_editor,
mock_user_input, mock_user_input,
mock_overrides, mock_overrides,
mock_password, mock_piped_input,
): ):
# Check if we need more mocks # Check if we need more mocks
mock_factories.update(mock_args) mock_factories.update(mock_args)
@ -95,7 +95,7 @@ def cli_run(
mock_factories.update(mock_editor) mock_factories.update(mock_editor)
mock_factories.update(mock_config_path) mock_factories.update(mock_config_path)
mock_factories.update(mock_user_input) mock_factories.update(mock_user_input)
mock_factories.update(mock_password) mock_factories.update(mock_piped_input)
return { return {
"status": 0, "status": 0,
@ -181,32 +181,6 @@ def toml_version(working_dir):
return pyproject_contents["tool"]["poetry"]["version"] return pyproject_contents["tool"]["poetry"]["version"]
@fixture
def mock_password(request):
def _mock_password():
from rich.console import Console
password = get_fixture(request, "password")
user_input = get_fixture(request, "user_input")
if password:
password = password.splitlines()
elif user_input:
password = user_input.splitlines()
if not password:
password = Exception("Unexpected call for password")
mock_console = Mock(wraps=Console(stderr=True))
mock_console.input = Mock()
mock_console.input.side_effect = password
return patch("jrnl.output._get_console", return_value=mock_console)
return {"rich_console_input": _mock_password}
@fixture @fixture
def input_method(): def input_method():
return "" return ""
@ -228,23 +202,43 @@ def should_not():
@fixture @fixture
def mock_user_input(request, is_tty): def mock_piped_input(request, is_tty):
def _generator(target): def _mock_piped_input():
def _mock_user_input(): piped_input = get_fixture(request, "all_input", None)
user_input = get_fixture(request, "user_input", None)
if user_input is None: if piped_input is None:
user_input = Exception("Unexpected call for user input") piped_input = Exception("Unexpected call for piped input")
else: else:
user_input = user_input.splitlines() if is_tty else [user_input] piped_input = piped_input.splitlines()
return patch(target, side_effect=user_input) if is_tty:
piped_input = []
return _mock_user_input return patch("sys.stdin.read", side_effect=piped_input)
return {"piped_input": _mock_piped_input}
@fixture
def mock_user_input(request):
def _mock_user_input():
from rich.console import Console
user_input = get_fixture(request, "all_input", None)
if user_input is None:
user_input = Exception("Unexpected call for user input")
else:
user_input = user_input.splitlines()
mock_console = Mock(wraps=Console(stderr=True))
mock_console.input = Mock()
mock_console.input.side_effect = user_input
return patch("jrnl.output._get_console", return_value=mock_console)
return { return {
"stdin": _generator("sys.stdin.read"), "user_input": _mock_user_input
"input": _generator("builtins.input"),
} }

View file

@ -61,7 +61,9 @@ def output_should_contain(
assert (expected_output in cli_run["stderr"]) == we_should, output_str assert (expected_output in cli_run["stderr"]) == we_should, output_str
else: else:
assert (expected_output in cli_run[which_output_stream]) == we_should, output_str assert (
expected_output in cli_run[which_output_stream]
) == we_should, output_str
@then(parse("the output should not contain\n{expected_output}")) @then(parse("the output should not contain\n{expected_output}"))

View file

@ -22,11 +22,11 @@ def when_we_change_directory(directory_name):
# These variables are used in the `@when(re(...))` section below # These variables are used in the `@when(re(...))` section below
command = '(?P<command>[^"]*)' command = '(?P<command>[^"]*)'
input_method = "(?P<input_method>enter|pipe)" input_method = "(?P<input_method>enter|pipe)"
user_input = '("(?P<user_input>[^"]*)")' all_input = '("(?P<all_input>[^"]*)")'
@when(parse('we run "jrnl {command}" and {input_method}\n{user_input}')) @when(parse('we run "jrnl {command}" and {input_method}\n{all_input}'))
@when(re(f'we run "jrnl ?{command}" and {input_method} {user_input}')) @when(re(f'we run "jrnl ?{command}" and {input_method} {all_input}'))
@when(parse('we run "jrnl {command}"')) @when(parse('we run "jrnl {command}"'))
@when('we run "jrnl"') @when('we run "jrnl"')
def we_run_jrnl(cli_run, capsys, keyring): def we_run_jrnl(cli_run, capsys, keyring):