Skip to content

Commit

Permalink
Merge branch 'gs-jackal-patch-1'
Browse files Browse the repository at this point in the history
  • Loading branch information
logandk committed Dec 18, 2018
2 parents 393c3a5 + 52ece83 commit 70e632c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
10 changes: 7 additions & 3 deletions wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import json
import os
import sys
import traceback

# Call decompression helper from `serverless-python-requirements` if
# available. See: https://github.com/UnitedIncome/serverless-python-requirements#dealing-with-lambdas-size-limitations
Expand Down Expand Up @@ -39,9 +40,13 @@ def import_app(config):
root = os.path.abspath(os.path.dirname(__file__))
sys.path.insert(0, os.path.join(root, wsgi_fqn_parts[0]))

wsgi_module = importlib.import_module(wsgi_fqn_parts[-1])
try:
wsgi_module = importlib.import_module(wsgi_fqn_parts[-1])

return getattr(wsgi_module, wsgi_fqn[1])
return getattr(wsgi_module, wsgi_fqn[1])
except: # noqa
traceback.print_exc()
raise Exception("Unable to import {}".format(config["app"]))


def append_text_mime_types(config):
Expand All @@ -57,7 +62,6 @@ def handler(event, context):
if "_serverless-wsgi" in event:
import shlex
import subprocess
import traceback
from werkzeug._compat import StringIO, to_native

native_stdout = sys.stdout
Expand Down
15 changes: 15 additions & 0 deletions wsgi_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ class MockObject:
return mock_app


@pytest.fixture
def mock_app_with_import_error(monkeypatch):
def mock_importlib(module):
raise ImportError("No module named {}".format(module))

monkeypatch.setattr(importlib, "import_module", mock_importlib)


@pytest.fixture
def mock_wsgi_app_file(monkeypatch):
monkeypatch.setattr(os.path, "abspath", lambda x: "/tmp")
Expand Down Expand Up @@ -634,3 +642,10 @@ def test_command_unknown(mock_wsgi_app_file, mock_app, wsgi):

assert "Traceback (most recent call last):" in response
assert "Exception: Uknown command: unknown" in response


def test_app_import_error(mock_wsgi_app_file, mock_app_with_import_error, event):
with pytest.raises(Exception, message="Unable to import app.app"):
if "wsgi" in sys.modules:
del sys.modules["wsgi"]
import wsgi # noqa: F401

0 comments on commit 70e632c

Please sign in to comment.