From 34df64c989bf3ed44acafccac8d9ab4ca421799c Mon Sep 17 00:00:00 2001 From: Jonathan Wren Date: Sat, 28 May 2022 15:13:27 -0700 Subject: [PATCH] rework mocks in test suite for new messaging functionality --- tests/bdd/features/datetime.feature | 2 +- tests/lib/fixtures.py | 74 +++++++++++++---------------- tests/lib/then_steps.py | 4 +- tests/lib/when_steps.py | 6 +-- 4 files changed, 41 insertions(+), 45 deletions(-) diff --git a/tests/bdd/features/datetime.feature b/tests/bdd/features/datetime.feature index 7676f1b2..0b5ce472 100644 --- a/tests/bdd/features/datetime.feature +++ b/tests/bdd/features/datetime.feature @@ -69,7 +69,7 @@ Feature: Reading and writing to journal with custom date formats Scenario: Writing an entry at the prompt with custom date 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 When we run "jrnl -999" Then the output should contain "10.05.2013 09:00 I saw Elvis." diff --git a/tests/lib/fixtures.py b/tests/lib/fixtures.py index a294a270..c843eacf 100644 --- a/tests/lib/fixtures.py +++ b/tests/lib/fixtures.py @@ -86,7 +86,7 @@ def cli_run( mock_editor, mock_user_input, mock_overrides, - mock_password, + mock_piped_input, ): # Check if we need more mocks mock_factories.update(mock_args) @@ -95,7 +95,7 @@ def cli_run( mock_factories.update(mock_editor) mock_factories.update(mock_config_path) mock_factories.update(mock_user_input) - mock_factories.update(mock_password) + mock_factories.update(mock_piped_input) return { "status": 0, @@ -181,32 +181,6 @@ def toml_version(working_dir): 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 def input_method(): return "" @@ -228,23 +202,43 @@ def should_not(): @fixture -def mock_user_input(request, is_tty): - def _generator(target): - def _mock_user_input(): - user_input = get_fixture(request, "user_input", None) +def mock_piped_input(request, is_tty): + def _mock_piped_input(): + piped_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() if is_tty else [user_input] + if piped_input is None: + piped_input = Exception("Unexpected call for piped input") + else: + 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 { - "stdin": _generator("sys.stdin.read"), - "input": _generator("builtins.input"), + "user_input": _mock_user_input } diff --git a/tests/lib/then_steps.py b/tests/lib/then_steps.py index 59ee4a2f..f13d27bf 100644 --- a/tests/lib/then_steps.py +++ b/tests/lib/then_steps.py @@ -61,7 +61,9 @@ def output_should_contain( assert (expected_output in cli_run["stderr"]) == we_should, output_str 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}")) diff --git a/tests/lib/when_steps.py b/tests/lib/when_steps.py index 60302c7c..6e60f61c 100644 --- a/tests/lib/when_steps.py +++ b/tests/lib/when_steps.py @@ -22,11 +22,11 @@ def when_we_change_directory(directory_name): # These variables are used in the `@when(re(...))` section below command = '(?P[^"]*)' input_method = "(?Penter|pipe)" -user_input = '("(?P[^"]*)")' +all_input = '("(?P[^"]*)")' -@when(parse('we run "jrnl {command}" and {input_method}\n{user_input}')) -@when(re(f'we run "jrnl ?{command}" and {input_method} {user_input}')) +@when(parse('we run "jrnl {command}" and {input_method}\n{all_input}')) +@when(re(f'we run "jrnl ?{command}" and {input_method} {all_input}')) @when(parse('we run "jrnl {command}"')) @when('we run "jrnl"') def we_run_jrnl(cli_run, capsys, keyring):