cleaner message when CLI can't load app

When loading the app fails for the --help command, only the error
message is shown, then the help text. The full traceback is shown for
other exceptions. Also show the message when loading fails while
getting a command, instead of only "command not found". The error
message goes to stderr to match other error behavior, and is in red
with an extra newline to make it more obvious next to the help text.

Also fixes an issue with the test_apps fixture that caused an imported
app to still be importable after the test was over and the path was
reset. Now the module cache is reset as well.
This commit is contained in:
David Lord 2020-07-30 18:36:55 -07:00
parent fd0a608449
commit 253570784c
No known key found for this signature in database
GPG key ID: 7A1C87E3F5BC42A8
4 changed files with 59 additions and 38 deletions

View file

@ -73,9 +73,15 @@ def client(app):
@pytest.fixture
def test_apps(monkeypatch):
monkeypatch.syspath_prepend(
os.path.abspath(os.path.join(os.path.dirname(__file__), "test_apps"))
)
monkeypatch.syspath_prepend(os.path.join(os.path.dirname(__file__), "test_apps"))
original_modules = set(sys.modules.keys())
yield
# Remove any imports cached during the test. Otherwise "import app"
# will work in the next test even though it's no longer on the path.
for key in sys.modules.keys() - original_modules:
sys.modules.pop(key)
@pytest.fixture(autouse=True)