fix handling of user input (stdin, input, getpass)

This commit is contained in:
Jonathan Wren 2021-11-27 14:21:23 -08:00
parent f7c12fbede
commit a6694f5273
3 changed files with 31 additions and 18 deletions

View file

@ -88,6 +88,7 @@ Feature: Multiple journals
Scenario: Don't overwrite main config when decrypting a journal in an alternate config
Given the config "editor_encrypted.yaml" exists
And we use the password "bad doggie no biscuit" if prompted
And we use the config "basic_encrypted.yaml"
When we run "jrnl --cf editor_encrypted.yaml --decrypt"
Then the config should contain "encrypt: true"

View file

@ -182,19 +182,22 @@ def toml_version(working_dir):
@fixture
def mock_password(request):
password = get_fixture(request, "password")
user_input = get_fixture(request, "user_input")
def _mock_password():
password = get_fixture(request, "password")
user_input = get_fixture(request, "user_input")
if password:
password = password.splitlines()
if password:
password = password.splitlines()
elif user_input:
password = user_input.splitlines()
elif user_input:
password = user_input.splitlines()
if not password:
return {}
if not password:
password = Exception("Unexpected call for password")
return {"getpass": lambda: patch("getpass.getpass", side_effect=password)}
return patch("getpass.getpass", side_effect=password)
return {"getpass": _mock_password}
@fixture
@ -219,14 +222,21 @@ def should_not():
@fixture
def mock_user_input(request, is_tty):
user_input = get_fixture(request, "user_input", "")
user_input = user_input.splitlines() if is_tty else [user_input]
if not user_input:
return {}
def _generator(target):
def _mock_user_input():
user_input = get_fixture(request, "user_input", "")
user_input = user_input.splitlines() if is_tty else [user_input]
if not user_input:
user_input = Exception("Unexpected call for user input")
return patch(target, side_effect=user_input)
return _mock_user_input
return {
"stdin": lambda: patch("sys.stdin.read", side_effect=user_input),
"input": lambda: patch("builtins.input", side_effect=user_input),
"stdin": _generator("sys.stdin.read"),
"input": _generator("builtins.input"),
}

View file

@ -20,10 +20,11 @@ def when_we_change_directory(directory_name):
command = '(?P<command>[^"]+)'
input_method = '(?P<input_method>enter|pipe)'
input_method = "(?P<input_method>enter|pipe)"
user_input = '(?P<user_input>[^"]+)'
@when(re(f'we run "jrnl {command}" and {input_method}\n{user_input}'))
@when(re(f'we run "jrnl" and {input_method}\n{user_input}'))
@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(re(f'we run "jrnl" and {input_method} "{user_input}"'))
@when(parse('we run "jrnl {command}"'))
@ -36,6 +37,7 @@ def we_run_jrnl(cli_run, capsys, keyring):
with ExitStack() as stack:
mocks = cli_run["mocks"]
factories = cli_run["mock_factories"]
for id in factories:
mocks[id] = stack.enter_context(factories[id]())