Improve handling of mocking logic in pytest (#1382)

* WIP

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

* take out redundant pytest step

* fix handling of 'we should' statements

* fix test that doesn't use a config file

* fix another test that uses stdin

Co-authored-by: Micah Jerome Ellison <micah.jerome.ellison@gmail.com>

* remove .tool-versions file per PR feedback

* add comment to clarify why disembodied variables are here

Co-authored-by: Micah Jerome Ellison <micah.jerome.ellison@gmail.com>
This commit is contained in:
Jonathan Wren 2021-12-11 12:35:32 -08:00 committed by GitHub
parent 3518e37087
commit 2ab485de8c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 259 additions and 197 deletions

View file

@ -30,38 +30,47 @@ def output_should_match(regex, cli_run):
assert matches, f"\nRegex didn't match:\n{regex}\n{str(out)}\n{str(matches)}"
@then(parse("the output should contain\n{expected_output}"))
@then(parse('the output should contain "{expected_output}"'))
@then('the output should contain "<expected_output>"')
@then(parse("the {which_output_stream} output should contain\n{expected_output}"))
@then(parse('the {which_output_stream} output should contain "{expected_output}"'))
def output_should_contain(expected_output, which_output_stream, cli_run):
@then(parse("the output {should_or_should_not} contain\n{expected_output}"))
@then(parse('the output {should_or_should_not} contain "{expected_output}"'))
@then(
parse(
"the {which_output_stream} output {should_or_should_not} contain\n{expected_output}"
)
)
@then(
parse(
'the {which_output_stream} output {should_or_should_not} contain "{expected_output}"'
)
)
def output_should_contain(
expected_output, which_output_stream, cli_run, should_or_should_not
):
we_should = parse_should_or_should_not(should_or_should_not)
assert expected_output
if which_output_stream is None:
assert (expected_output in cli_run["stdout"]) or (
expected_output in cli_run["stderr"]
assert ((expected_output in cli_run["stdout"]) == we_should) or (
(expected_output in cli_run["stderr"]) == we_should
)
elif which_output_stream == "standard":
assert expected_output in cli_run["stdout"]
assert (expected_output in cli_run["stdout"]) == we_should
elif which_output_stream == "error":
assert expected_output in cli_run["stderr"]
assert (expected_output in cli_run["stderr"]) == we_should
else:
assert expected_output in cli_run[which_output_stream]
assert (expected_output in cli_run[which_output_stream]) == we_should
@then(parse("the output should not contain\n{expected_output}"))
@then(parse('the output should not contain "{expected_output}"'))
@then('the output should not contain "<expected_output>"')
def output_should_not_contain(expected_output, cli_run):
assert expected_output not in cli_run["stdout"]
@then(parse("the output should be\n{expected_output}"))
@then(parse('the output should be "{expected_output}"'))
@then('the output should be "<expected_output>"')
def output_should_be(expected_output, cli_run):
actual = cli_run["stdout"].strip()
expected = expected_output.strip()
@ -75,7 +84,6 @@ def output_should_be_empty(cli_run):
@then(parse('the output should contain the date "{date}"'))
@then('the output should contain the date "<date>"')
def output_should_contain_date(date, cli_run):
assert date and date in cli_run["stdout"]
@ -94,12 +102,6 @@ def output_should_be_columns_wide(cli_run, width):
assert len(line) <= width
@then(parse('we should see the message "{text}"'))
def should_see_the_message(text, cli_run):
out = cli_run["stderr"]
assert text in out, [text, out]
@then(
parse(
'the config for journal "{journal_name}" {should_or_should_not} contain "{some_yaml}"'
@ -126,10 +128,7 @@ def config_var_on_disk(config_on_disk, journal_name, should_or_should_not, some_
# `expected` objects formatted in yaml only compare one level deep
actual_slice = {key: actual.get(key, None) for key in expected.keys()}
if we_should:
assert expected == actual_slice
else:
assert expected != actual_slice
assert (expected == actual_slice) == we_should
@then(
@ -160,10 +159,7 @@ def config_var_in_memory(
# `expected` objects formatted in yaml only compare one level deep
actual_slice = {key: get_nested_val(actual, key) for key in expected.keys()}
if we_should:
assert expected == actual_slice
else:
assert expected != actual_slice
assert (expected == actual_slice) == we_should
@then("we should be prompted for a password")
@ -355,10 +351,7 @@ def count_elements(number, item, cli_run):
def count_editor_args(num_args, cli_run, editor_state, should_or_should_not):
we_should = parse_should_or_should_not(should_or_should_not)
if we_should:
assert cli_run["mocks"]["editor"].called
else:
assert not cli_run["mocks"]["editor"].called
assert cli_run["mocks"]["editor"].called == we_should
if isinstance(num_args, int):
assert len(editor_state["command"]) == int(num_args)
@ -368,10 +361,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):
we_should = parse_should_or_should_not(should_or_should_not)
if we_should:
assert cli_run["mocks"]["stdin"].called
else:
assert not cli_run["mocks"]["stdin"].called
assert cli_run["mocks"]["stdin"].called == we_should
@then(parse('the editor filename should end with "{suffix}"'))