Skip to content

Commit

Permalink
fix: on CLI mode , hooks file existence in cwd ,but ModuleNotFoundE…
Browse files Browse the repository at this point in the history
…rror
  • Loading branch information
dongfangtianyu authored and Stranger6667 committed May 13, 2020
1 parent e846790 commit 422d98c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/schemathesis/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import sys
import traceback
from enum import Enum
Expand Down Expand Up @@ -281,6 +282,7 @@ def get_output_handler(workers_num: int) -> EventHandler:
def load_hook(module_name: str) -> None:
"""Load the given hook by importing it."""
try:
sys.path.extend([os.getcwd()]) # fix ModuleNotFoundError module in cwd
__import__(module_name)
except Exception:
click.secho("An exception happened during the hook loading:\n", fg="red")
Expand Down
17 changes: 17 additions & 0 deletions test/cli/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,23 @@ def test_pre_run_hook_invalid(testdir, cli):
assert lines[9] == "Aborted!"


def test_pre_run_hook_module_not_found(testdir, cli):
import os, sys

testdir.makepyfile(hook="1 / 0")
result = cli.main("--pre-run", "hook", "run", "http://127.0.0.1:1")

assert os.getcwd() in sys.path

# Then CLI run should fail
assert result.exit_code == ExitCode.TESTS_FAILED
assert "ModuleNotFoundError" not in result.stdout
lines = result.stdout.strip().split("\n")
assert lines[0] == "An exception happened during the hook loading:"
assert lines[7] == "ZeroDivisionError: division by zero"
assert lines[9] == "Aborted!"


@pytest.fixture()
def new_check(testdir, cli):
module = testdir.make_importable_pyfile(
Expand Down

0 comments on commit 422d98c

Please sign in to comment.