Skip to content

Commit 76e77b7

Browse files
authored
Merge pull request #82 from mrf345/testing
fix lazy initialization missing app bug, and refactor
2 parents 6d80c17 + db38400 commit 76e77b7

File tree

6 files changed

+27
-29
lines changed

6 files changed

+27
-29
lines changed

flask_minify/about.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "0.40"
1+
__version__ = "0.41"
22
__doc__ = "Flask extension to minify html, css, js and less."
33
__license__ = "MIT"
44
__author__ = "Mohamed Feddad"

flask_minify/exceptions.py

-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,2 @@
11
class FlaskMinifyException(Exception):
22
"""FlaskMinify base exception"""
3-
4-
pass
5-
6-
7-
class MissingApp(FlaskMinifyException):
8-
"""Raised when the flask app is accessed before it's set"""
9-
10-
pass

flask_minify/main.py

+4-14
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
from itertools import tee
22
from re import compile as compile_re
33

4-
from flask import request
4+
from flask import current_app, request
55

66
from flask_minify.cache import MemoryCache
7-
from flask_minify.exceptions import MissingApp
87
from flask_minify.parsers import Parser
98
from flask_minify.utils import does_content_type_match
109

@@ -113,27 +112,18 @@ def get_endpoint(self):
113112

114113
@property
115114
def app(self):
116-
"""If app was passed take it, otherwise raise an exception.
115+
"""If app was passed take it, otherwise fallback to `Flask.current_app`.
117116
118117
Returns
119118
-------
120119
Flask App
121120
The current Flask application.
122-
123-
Raises
124-
------
125-
MissingApp
126121
"""
127-
if not self._app:
128-
raise MissingApp(
129-
"Flask app has not been passed to the extension `Minify(app=None)`, "
130-
"nor lazy initialized with `.init_app(app)`"
131-
)
132-
133-
return self._app
122+
return self._app or current_app
134123

135124
def init_app(self, app):
136125
"""Handle initiation of multiple apps NOTE:Factory Method"""
126+
self._app = app
137127
app.after_request(self.main)
138128
app.teardown_appcontext(self.teardown)
139129

flask_minify/parsers.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from lesscpy import compile as compile_less
66
from rcssmin import cssmin
77

8+
from flask_minify.exceptions import FlaskMinifyException
89
from flask_minify.utils import get_tag_contents
910

1011

@@ -112,7 +113,10 @@ def minify(self, content, tag):
112113
minified_or_content = parser.executer(content, **runtime_options)
113114
except Exception as e:
114115
if not self.fail_safe:
115-
raise e
116+
raise FlaskMinifyException(
117+
'Failed to minify "{0}" content'.format(tag)
118+
) from e
119+
116120
minified_or_content = content
117121

118122
return minified_or_content

requirements/main.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
six
12
flask
23
lesscpy
34
htmlmin

tests/units.py

+16-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from flask_minify import minify, parsers
66
from flask_minify.cache import MemoryCache
7-
from flask_minify.exceptions import MissingApp
7+
from flask_minify.exceptions import FlaskMinifyException
88
from flask_minify.utils import does_content_type_match, is_empty
99

1010
from .constants import (
@@ -89,13 +89,14 @@ def test_request_falsy_endpoint(self):
8989

9090
assert (list(matches), exists) == ([], False)
9191

92-
def test_access_missing_app_raises_exception(self):
93-
"""test accessing a missing flask app raises an exception"""
92+
def test_access_app_after_lazy_initialization(self):
93+
""""""
9494
self.mock_app = None
9595
ext = self.minify_defaults
96+
mock_app = mock.MagicMock()
97+
ext.init_app(mock_app)
9698

97-
with pytest.raises(MissingApp):
98-
ext.app
99+
assert ext.app == mock_app
99100

100101

101102
class TestParsers:
@@ -116,6 +117,16 @@ class CustomParser(parsers.Lesscpy):
116117

117118
assert minified == COMPILED_LESS_RAW
118119

120+
def test_parser_raising_exception_with_failsafe_disabled(self):
121+
class CustomParser(parsers.Lesscpy):
122+
def executer(self, content, **options):
123+
raise Exception("something went wrong")
124+
125+
parser = parsers.Parser({"style": CustomParser})
126+
127+
with pytest.raises(FlaskMinifyException):
128+
parser.minify(LESS_RAW, "style")
129+
119130

120131
class TestMemoryCache:
121132
def setup(self):

0 commit comments

Comments
 (0)