Skip to content
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

fix(flask): handle SyntaxErrors when parsing app.py #612

Merged
merged 2 commits into from
Jun 25, 2024
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
9 changes: 7 additions & 2 deletions rockcraft/extensions/gunicorn.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,14 @@ def _wsgi_path_error_messages(self):
return [
"flask application can not be imported from app:app, no app.py file found in the project root."
]
if not self.has_global_variable(app_file, "app"):
try:
has_app = self.has_global_variable(app_file, "app")
except SyntaxError as err:
return [f"error parsing app.py: {err.msg}"]

if not has_app:
return [
"flask application can not be imported from app:app in file app.py file in the project root."
"flask application can not be imported from app:app in app.py in the project root."
]

return []
Expand Down
29 changes: 29 additions & 0 deletions tests/unit/extensions/test_gunicorn.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import textwrap

import pytest
from rockcraft import extensions
Expand Down Expand Up @@ -333,6 +334,34 @@ def test_flask_extension_requirements_txt_no_flask_error(tmp_path):
)


@pytest.mark.usefixtures("flask_extension")
def test_flask_extension_bad_app_py(tmp_path):
bad_code = textwrap.dedent(
"""
import flask

app = flask.Flask()

bad = "my value
"""
).strip()
(tmp_path / "app.py").write_text(bad_code)
(tmp_path / "requirements.txt").write_text("flask")
flask_input_yaml = {
"extensions": ["flask-framework"],
"base": "bare",
"build-base": "[email protected]",
"platforms": {"amd64": {}},
}
with pytest.raises(ExtensionError) as exc:
extensions.apply_extensions(tmp_path, flask_input_yaml)

expected = (
"- error parsing app.py: unterminated string literal (detected at line 5)"
)
assert str(exc.value) == expected


@pytest.mark.usefixtures("flask_extension")
def test_flask_extension_no_requirements_txt_no_app_py_error(tmp_path):
flask_input_yaml = {
Expand Down
Loading