Implement stream redirection in pytest-bdd

- Take out old steps from format and input tests

Co-authored-by: Micah Jerome Ellison <micah.jerome.ellison@gmail.com>
This commit is contained in:
Jonathan Wren 2021-05-22 12:32:59 -07:00
parent 512fb63e1f
commit 44b1762b7d
3 changed files with 14 additions and 8 deletions

View file

@ -163,7 +163,6 @@ Feature: Custom formats
More stuff More stuff
more stuff again more stuff again
Then we flush the output
When we run "jrnl -1 --export markdown" When we run "jrnl -1 --export markdown"
Then the output should be Then the output should be
# 2020 # 2020
@ -224,7 +223,6 @@ Feature: Custom formats
[2020-10-29 11:11] First entry. [2020-10-29 11:11] First entry.
[2020-10-29 11:11] Second entry. [2020-10-29 11:11] Second entry.
[2020-10-29 11:13] Third entry. [2020-10-29 11:13] Third entry.
Then we flush the output
When we run "jrnl -3 --format markdown" When we run "jrnl -3 --format markdown"
Then the output should be Then the output should be
# 2020 # 2020

View file

@ -4,8 +4,7 @@ Feature: Importing data
Given we use the config "<config_file>" Given we use the config "<config_file>"
And we use the password "test" if prompted And we use the password "test" if prompted
When we run "jrnl --import" and pipe "[2020-07-05 15:00] Observe and import." When we run "jrnl --import" and pipe "[2020-07-05 15:00] Observe and import."
Then we flush the output When we run "jrnl -9 --short"
When we run "jrnl -c import"
Then the output should contain "Observe and import" Then the output should contain "Observe and import"
Examples: Configs Examples: Configs

View file

@ -160,6 +160,10 @@ def password():
return "" return ""
@fixture
def input_method():
return ""
@fixture @fixture
def now_date(): def now_date():
return {"datetime": datetime, "calendar_parse": __get_pdt_calendar()} return {"datetime": datetime, "calendar_parse": __get_pdt_calendar()}
@ -368,10 +372,10 @@ def use_password_forever(pw):
return pw return pw
@when(parse('we run "jrnl {command}" and enter\n{user_input}')) @when(parse('we run "jrnl {command}" and {input_method}\n{user_input}'))
@when(parsers.re('we run "jrnl (?P<command>[^"]+)" and enter "(?P<user_input>[^"]+)"')) @when(parsers.re('we run "jrnl (?P<command>[^"]+)" and (?P<input_method>enter|pipe) "(?P<user_input>[^"]+)"'))
@when(parse('we run "jrnl" and {input_method} "{user_input}"'))
@when(parse('we run "jrnl {command}"')) @when(parse('we run "jrnl {command}"'))
@when(parse('we run "jrnl" and enter "{user_input}"'))
@when('we run "jrnl <command>"') @when('we run "jrnl <command>"')
@when('we run "jrnl"') @when('we run "jrnl"')
def we_run( def we_run(
@ -385,7 +389,11 @@ def we_run(
editor, editor,
now_date, now_date,
keyring, keyring,
input_method,
): ):
assert input_method in ['', 'enter', 'pipe']
is_tty = input_method != 'pipe'
if cache_dir["exists"]: if cache_dir["exists"]:
command = command.format(cache_dir=cache_dir["path"]) command = command.format(cache_dir=cache_dir["path"])
@ -393,7 +401,7 @@ def we_run(
status = 0 status = 0
if user_input: if user_input:
user_input = user_input.splitlines() user_input = user_input.splitlines() if is_tty else [user_input]
if password: if password:
password = password.splitlines() password = password.splitlines()
@ -406,6 +414,7 @@ def we_run(
with \ with \
patch("sys.argv", ['jrnl'] + args), \ patch("sys.argv", ['jrnl'] + args), \
patch("sys.stdin.read", side_effect=user_input) as mock_stdin, \ 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("builtins.input", side_effect=user_input) as mock_input, \
patch("getpass.getpass", side_effect=password) as mock_getpass, \ patch("getpass.getpass", side_effect=password) as mock_getpass, \
patch("datetime.datetime", new=now_date["datetime"]), \ patch("datetime.datetime", new=now_date["datetime"]), \