Skip to content

Commit

Permalink
fix: Possible IndexError when a user-defined check raises an except…
Browse files Browse the repository at this point in the history
…ion without a message
  • Loading branch information
Stranger6667 committed Sep 17, 2020
1 parent 8992f64 commit 4460cbd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
6 changes: 6 additions & 0 deletions docs/changelog.rst
Expand Up @@ -12,6 +12,11 @@ Changed
- Hide ``Case.endpoint`` from representation. Its representation decreases the usability of pytest's output. `#719`_
- Return registered functions from ``register_target`` and ``register_check`` decorators. `#721`_

Fixed
~~~~~

- Possible ``IndexError`` when a user-defined check raises an exception without a message. `#718`_

`2.4.0`_ - 2020-09-15
---------------------

Expand Down Expand Up @@ -1341,6 +1346,7 @@ Fixed

.. _#721: https://github.com/schemathesis/schemathesis/issues/721
.. _#719: https://github.com/schemathesis/schemathesis/issues/719
.. _#718: https://github.com/schemathesis/schemathesis/issues/718
.. _#708: https://github.com/schemathesis/schemathesis/issues/708
.. _#705: https://github.com/schemathesis/schemathesis/issues/705
.. _#692: https://github.com/schemathesis/schemathesis/issues/692
Expand Down
6 changes: 5 additions & 1 deletion src/schemathesis/runner/impl/core.py
Expand Up @@ -226,8 +226,12 @@ def run_checks(case: Case, checks: Iterable[CheckFunction], result: TestResult,
if not skip_check:
result.add_success(check_name, case)
except AssertionError as exc:
message = str(exc)
if not message:
message = f"Check '{check_name}' failed"
exc.args = (message,)
errors.append(exc)
result.add_failure(check_name, case, str(exc))
result.add_failure(check_name, case, message)

if errors:
raise get_grouped_exception(*errors)
Expand Down
19 changes: 13 additions & 6 deletions test/cli/test_commands.py
Expand Up @@ -1002,18 +1002,24 @@ def reset_hooks():
reset_checks()


@pytest.fixture()
def new_check(testdir, cli):
@pytest.fixture(
params=[
('AssertionError("Custom check failed!")', "1. Custom check failed!"),
("AssertionError", "1. Check 'new_check' failed"),
]
)
def new_check(request, testdir, cli):
exception, message = request.param
module = testdir.make_importable_pyfile(
hook="""
hook=f"""
import schemathesis
@schemathesis.register_check
def new_check(response, result):
raise AssertionError("Custom check failed!")
raise {exception}
"""
)
yield module
yield module, message
reset_checks()
# To verify that "new_check" is unregistered
result = cli.run("--help")
Expand All @@ -1026,6 +1032,7 @@ def new_check(response, result):

@pytest.mark.endpoints("success")
def test_register_check(new_check, cli, schema_url):
new_check, message = new_check
# When `--pre-run` hook is passed to the CLI call
# And it contains registering a new check, which always fails for the testing purposes
result = cli.main("--pre-run", new_check.purebasename, "run", "-c", "new_check", schema_url)
Expand All @@ -1034,7 +1041,7 @@ def test_register_check(new_check, cli, schema_url):
assert result.exit_code == ExitCode.TESTS_FAILED, result.stdout
# And a message from the new check should be displayed
lines = result.stdout.strip().split("\n")
assert lines[14] == "1. Custom check failed!"
assert lines[14] == message


def assert_threaded_executor_interruption(lines, expected, optional_interrupt=False):
Expand Down

0 comments on commit 4460cbd

Please sign in to comment.