From 44b1762b7d947ab93ec123e48e50b4392ec04ece Mon Sep 17 00:00:00 2001 From: Jonathan Wren Date: Sat, 22 May 2021 12:32:59 -0700 Subject: [PATCH] Implement stream redirection in pytest-bdd - Take out old steps from format and input tests Co-authored-by: Micah Jerome Ellison --- tests/features/format.feature | 2 -- tests/features/import.feature | 3 +-- tests/step_defs/conftest.py | 17 +++++++++++++---- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/tests/features/format.feature b/tests/features/format.feature index 648b4dd0..b9e2e384 100644 --- a/tests/features/format.feature +++ b/tests/features/format.feature @@ -163,7 +163,6 @@ Feature: Custom formats More stuff more stuff again - Then we flush the output When we run "jrnl -1 --export markdown" Then the output should be # 2020 @@ -224,7 +223,6 @@ Feature: Custom formats [2020-10-29 11:11] First entry. [2020-10-29 11:11] Second entry. [2020-10-29 11:13] Third entry. - Then we flush the output When we run "jrnl -3 --format markdown" Then the output should be # 2020 diff --git a/tests/features/import.feature b/tests/features/import.feature index 9de8e216..d75d6017 100644 --- a/tests/features/import.feature +++ b/tests/features/import.feature @@ -4,8 +4,7 @@ Feature: Importing data Given we use the config "" And we use the password "test" if prompted When we run "jrnl --import" and pipe "[2020-07-05 15:00] Observe and import." - Then we flush the output - When we run "jrnl -c import" + When we run "jrnl -9 --short" Then the output should contain "Observe and import" Examples: Configs diff --git a/tests/step_defs/conftest.py b/tests/step_defs/conftest.py index 57a0de03..9e482737 100644 --- a/tests/step_defs/conftest.py +++ b/tests/step_defs/conftest.py @@ -160,6 +160,10 @@ def password(): return "" +@fixture +def input_method(): + return "" + @fixture def now_date(): return {"datetime": datetime, "calendar_parse": __get_pdt_calendar()} @@ -368,10 +372,10 @@ def use_password_forever(pw): return pw -@when(parse('we run "jrnl {command}" and enter\n{user_input}')) -@when(parsers.re('we run "jrnl (?P[^"]+)" and enter "(?P[^"]+)"')) +@when(parse('we run "jrnl {command}" and {input_method}\n{user_input}')) +@when(parsers.re('we run "jrnl (?P[^"]+)" and (?Penter|pipe) "(?P[^"]+)"')) +@when(parse('we run "jrnl" and {input_method} "{user_input}"')) @when(parse('we run "jrnl {command}"')) -@when(parse('we run "jrnl" and enter "{user_input}"')) @when('we run "jrnl "') @when('we run "jrnl"') def we_run( @@ -385,7 +389,11 @@ def we_run( editor, now_date, keyring, + input_method, ): + assert input_method in ['', 'enter', 'pipe'] + is_tty = input_method != 'pipe' + if cache_dir["exists"]: command = command.format(cache_dir=cache_dir["path"]) @@ -393,7 +401,7 @@ def we_run( status = 0 if user_input: - user_input = user_input.splitlines() + user_input = user_input.splitlines() if is_tty else [user_input] if password: password = password.splitlines() @@ -406,6 +414,7 @@ def we_run( with \ patch("sys.argv", ['jrnl'] + args), \ patch("sys.stdin.read", side_effect=user_input) as mock_stdin, \ + patch("sys.stdin.isatty", return_value=is_tty), \ patch("builtins.input", side_effect=user_input) as mock_input, \ patch("getpass.getpass", side_effect=password) as mock_getpass, \ patch("datetime.datetime", new=now_date["datetime"]), \