fix more tests

This commit is contained in:
Jonathan Wren 2022-06-04 18:56:36 -07:00
parent 5cb46f572d
commit 57f53e96ce
5 changed files with 53 additions and 34 deletions

View file

@ -3,7 +3,7 @@ Feature: Implementing Runtime Overrides for Select Configuration Keys
Scenario: Override configured editor with built-in input === editor:'' Scenario: Override configured editor with built-in input === editor:''
Given we use the config "basic_encrypted.yaml" Given we use the config "basic_encrypted.yaml"
And we use the password "test" if prompted And we use the password "test" if prompted
When we run "jrnl --config-override editor ''" and enter When we run "jrnl --config-override editor ''" and pipe
This is a journal entry This is a journal entry
Then the stdin prompt should have been called Then the stdin prompt should have been called
And the editor should not have been called And the editor should not have been called

View file

@ -41,7 +41,7 @@ Feature: Upgrading Journals from 1.x.x to 2.x.x
Scenario: Upgrade with missing journal Scenario: Upgrade with missing journal
Given we use the config "upgrade_from_195_with_missing_journal.json" Given we use the config "upgrade_from_195_with_missing_journal.json"
When we run "jrnl --list" and enter "Y" When we run "jrnl --list" and enter "Y"
Then the output should contain "Error: features/journals/missing.journal does not exist." Then the output should contain "features/journals/missing.journal does not exist"
And we should get no error And we should get no error
Scenario: Upgrade with missing encrypted journal Scenario: Upgrade with missing encrypted journal
@ -49,6 +49,6 @@ Feature: Upgrading Journals from 1.x.x to 2.x.x
When we run "jrnl --list" and enter When we run "jrnl --list" and enter
Y Y
bad doggie no biscuit bad doggie no biscuit
Then the output should contain "features/journals/missing.journal does not exist." Then the output should contain "features/journals/missing.journal does not exist"
And the output should contain "We're all done" And the output should contain "We're all done"
And we should get no error And we should get no error

View file

@ -6,6 +6,7 @@ import os
from pathlib import Path from pathlib import Path
import tempfile import tempfile
from collections.abc import Iterable
from keyring import backend from keyring import backend
from keyring import errors from keyring import errors
from pytest import fixture from pytest import fixture
@ -13,6 +14,7 @@ from unittest.mock import patch
from unittest.mock import Mock from unittest.mock import Mock
from .helpers import get_fixture from .helpers import get_fixture
import toml import toml
from rich.console import Console
from jrnl.config import load_config from jrnl.config import load_config
from jrnl.os_compat import split_args from jrnl.os_compat import split_args
@ -86,7 +88,6 @@ def cli_run(
mock_editor, mock_editor,
mock_user_input, mock_user_input,
mock_overrides, mock_overrides,
mock_piped_input,
): ):
# Check if we need more mocks # Check if we need more mocks
mock_factories.update(mock_args) mock_factories.update(mock_args)
@ -95,7 +96,6 @@ def cli_run(
mock_factories.update(mock_editor) mock_factories.update(mock_editor)
mock_factories.update(mock_config_path) mock_factories.update(mock_config_path)
mock_factories.update(mock_user_input) mock_factories.update(mock_user_input)
mock_factories.update(mock_piped_input)
return { return {
"status": 0, "status": 0,
@ -203,53 +203,72 @@ def should_not():
# @todo is this named properly? does it need to be merged with user_input? does # @todo is this named properly? does it need to be merged with user_input? does
# it only mock piped input? or also user input? # it only mock piped input? or also user input?
@fixture # @fixture
def mock_piped_input(request, is_tty): # def mock_stdin_input(request, is_tty):
def _mock_piped_input(): # def _mock_stdin_input():
piped_input = get_fixture(request, "all_input", None) # stdin_input = get_fixture(request, "all_input", None)
if piped_input is None: # if stdin_input is None:
piped_input = Exception("Unexpected call for piped input") # stdin_input = Exception("Unexpected call for piped input")
else: # else:
piped_input = [piped_input] # stdin_input = [stdin_input]
if is_tty: # if is_tty:
piped_input = [] # stdin_input = []
return patch("sys.stdin.read", side_effect=piped_input) # return patch("sys.stdin.read", side_effect=stdin_input)
return {"piped_input": _mock_piped_input} # return {"stdin_input": _mock_stdin_input}
@fixture @fixture
def mock_user_input(request): def mock_user_input(request, password_input, stdin_input):
def _mock_user_input(): def _mock_user_input():
from rich.console import Console # user_input needs to be here because we don't know it until cli_run starts
user_input = get_fixture(request, "all_input", None) user_input = get_fixture(request, "all_input", None)
password_input = get_fixture(request, "password", None)
if user_input is None: if user_input is None:
user_input = Exception("Unexpected call for user input") user_input = Exception("Unexpected call for user input")
else: else:
user_input = iter(user_input.splitlines()) user_input = iter(user_input.splitlines())
if password_input is None:
password_input = Exception("Unexpected call for password input")
def mock_console_input(**kwargs): def mock_console_input(**kwargs):
if kwargs["password"]: # import ipdb; ipdb.sset_trace()
if kwargs["password"] and not isinstance(password_input, Exception):
return password_input return password_input
else:
if isinstance(user_input, Iterable):
return next(user_input) return next(user_input)
# exceptions
return user_input if not kwargs["password"] else password_input
mock_console = Mock(wraps=Console(stderr=True)) mock_console = Mock(wraps=Console(stderr=True))
mock_console.input = Mock() mock_console.input = Mock(side_effect=mock_console_input)
mock_console.input = mock_console_input
return patch("jrnl.output._get_console", return_value=mock_console) return patch("jrnl.output._get_console", return_value=mock_console)
return {"user_input": _mock_user_input} return {
"user_input": _mock_user_input,
"stdin_input": lambda: patch("sys.stdin.read", side_effect=stdin_input),
}
@fixture
def password_input(request):
password_input = get_fixture(request, "password", None)
if password_input is None:
password_input = Exception("Unexpected call for password input")
return password_input
@fixture
def stdin_input(request, is_tty):
stdin_input = get_fixture(request, "all_input", None)
if stdin_input is None or is_tty:
stdin_input = Exception("Unexpected call for stdin input")
else:
stdin_input = [stdin_input]
return stdin_input
@fixture @fixture

View file

@ -120,9 +120,9 @@ def config_exists(config_file, temp_dir, working_dir):
shutil.copy2(config_source, config_dest) shutil.copy2(config_source, config_dest)
@given(parse('we use the password "{pw}" if prompted'), target_fixture="password") @given(parse('we use the password "{password}" if prompted'))
def use_password_forever(pw): def use_password_forever(password):
return pw return password
@given("we create a cache directory", target_fixture="cache_dir") @given("we create a cache directory", target_fixture="cache_dir")

View file

@ -364,7 +364,7 @@ def count_editor_args(num_args, cli_run, editor_state, should_or_should_not):
def stdin_prompt_called(cli_run, should_or_should_not): def stdin_prompt_called(cli_run, should_or_should_not):
we_should = parse_should_or_should_not(should_or_should_not) we_should = parse_should_or_should_not(should_or_should_not)
assert cli_run["mocks"]["piped_input"].called == we_should assert cli_run["mocks"]["stdin_input"].called == we_should
@then(parse('the editor filename should end with "{suffix}"')) @then(parse('the editor filename should end with "{suffix}"'))