Skip to content

Fix handling of debug flag when gunicorn is not present #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
- Fix handling of `--debug` flag when gunicorn is not present ([#44])

## [1.4.1] - 2020-05-07
### Fixed
- Fix Windows support ([#38])

## [1.4.0] - 2020-05-06
### Changed
- Use gunicorn as a production HTTP server

## [1.3.0] - 2020-04-13
### Added
- Add support for running `python -m functions_framework` ([#31])

### Changed
- Move `functions_framework.cli.cli` to `functions_framework._cli._cli`
- Adjust path handling for robots.txt and favicon.ico ([#33])

Expand Down Expand Up @@ -56,6 +63,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[1.0.1]: https://github.com/GoogleCloudPlatform/functions-framework-python/releases/tag/v1.0.1
[1.0.0]: https://github.com/GoogleCloudPlatform/functions-framework-python/releases/tag/v1.0.0

[#44]: https://github.com/GoogleCloudPlatform/functions-framework-python/pull/44
[#38]: https://github.com/GoogleCloudPlatform/functions-framework-python/pull/38
[#33]: https://github.com/GoogleCloudPlatform/functions-framework-python/pull/33
[#31]: https://github.com/GoogleCloudPlatform/functions-framework-python/pull/31
Expand Down
7 changes: 5 additions & 2 deletions src/functions_framework/_http/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
class HTTPServer:
def __init__(self, app, debug, **options):
self.app = app
self.debug = debug
self.options = options

if debug:
if self.debug:
self.server_class = FlaskApplication
else:
try:
Expand All @@ -31,7 +32,9 @@ def __init__(self, app, debug, **options):
self.server_class = FlaskApplication

def run(self, host, port):
http_server = self.server_class(self.app, host, port, **self.options)
http_server = self.server_class(
self.app, host, port, self.debug, **self.options
)
http_server.run()


Expand Down
5 changes: 3 additions & 2 deletions src/functions_framework/_http/flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@


class FlaskApplication:
def __init__(self, app, host, port, **options):
def __init__(self, app, host, port, debug, **options):
self.app = app
self.host = host
self.port = port
self.debug = debug
self.options = options

def run(self):
self.app.run(self.host, self.port, debug=True, **self.options)
self.app.run(self.host, self.port, debug=self.debug, **self.options)
2 changes: 1 addition & 1 deletion src/functions_framework/_http/gunicorn.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


class GunicornApplication(gunicorn.app.base.BaseApplication):
def __init__(self, app, host, port, **options):
def __init__(self, app, host, port, debug, **options):
self.options = {
"bind": "%s:%s" % (host, port),
"workers": 1,
Expand Down
17 changes: 11 additions & 6 deletions tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,15 @@ def test_httpserver(monkeypatch, debug, gunicorn_missing, expected):

wrapper.run(host, port)

assert wrapper.server_class.calls == [pretend.call(app, host, port, **options)]
assert wrapper.server_class.calls == [
pretend.call(app, host, port, debug, **options)
]
assert http_server.run.calls == [pretend.call()]


@pytest.mark.skipif("platform.system() == 'Windows'")
def test_gunicorn_application():
@pytest.mark.parametrize("debug", [True, False])
def test_gunicorn_application(debug):
app = pretend.stub()
host = "1.2.3.4"
port = "1234"
Expand All @@ -89,7 +92,7 @@ def test_gunicorn_application():
import functions_framework._http.gunicorn

gunicorn_app = functions_framework._http.gunicorn.GunicornApplication(
app, host, port, **options
app, host, port, debug, **options
)

assert gunicorn_app.app == app
Expand All @@ -107,23 +110,25 @@ def test_gunicorn_application():
assert gunicorn_app.load() == app


def test_flask_application():
@pytest.mark.parametrize("debug", [True, False])
def test_flask_application(debug):
app = pretend.stub(run=pretend.call_recorder(lambda *a, **kw: None))
host = pretend.stub()
port = pretend.stub()
options = {"a": pretend.stub(), "b": pretend.stub()}

flask_app = functions_framework._http.flask.FlaskApplication(
app, host, port, **options
app, host, port, debug, **options
)

assert flask_app.app == app
assert flask_app.host == host
assert flask_app.port == port
assert flask_app.debug == debug
assert flask_app.options == options

flask_app.run()

assert app.run.calls == [
pretend.call(host, port, debug=True, a=options["a"], b=options["b"]),
pretend.call(host, port, debug=debug, a=options["a"], b=options["b"]),
]