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 Scenario: Don't overwrite main config when decrypting a journal in an alternate config
Given the config "editor_encrypted.yaml" exists 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" And we use the config "basic_encrypted.yaml"
When we run "jrnl --cf editor_encrypted.yaml --decrypt" When we run "jrnl --cf editor_encrypted.yaml --decrypt"
Then the config should contain "encrypt: true" Then the config should contain "encrypt: true"

View file

@ -182,19 +182,22 @@ def toml_version(working_dir):
@fixture @fixture
def mock_password(request): def mock_password(request):
password = get_fixture(request, "password") def _mock_password():
user_input = get_fixture(request, "user_input") password = get_fixture(request, "password")
user_input = get_fixture(request, "user_input")
if password: if password:
password = password.splitlines() password = password.splitlines()
elif user_input: elif user_input:
password = user_input.splitlines() password = user_input.splitlines()
if not password: if not password:
return {} 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 @fixture
@ -219,14 +222,21 @@ def should_not():
@fixture @fixture
def mock_user_input(request, is_tty): def mock_user_input(request, is_tty):
user_input = get_fixture(request, "user_input", "") def _generator(target):
user_input = user_input.splitlines() if is_tty else [user_input] def _mock_user_input():
if not user_input: user_input = get_fixture(request, "user_input", "")
return {} 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 { return {
"stdin": lambda: patch("sys.stdin.read", side_effect=user_input), "stdin": _generator("sys.stdin.read"),
"input": lambda: patch("builtins.input", side_effect=user_input), "input": _generator("builtins.input"),
} }

View file

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