Skip to content

Commit 6884507

Browse files
authored
Merge pull request #98 from mrf345/testing
Fix missing go dependency exception with default options
2 parents b7ce727 + 63c24ea commit 6884507

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

flask_minify/about.py

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

flask_minify/parsers.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -125,18 +125,23 @@ def __init__(
125125
runtime_options={},
126126
go=False,
127127
):
128-
self.default_parsers = self._go_default_parsers if go else self._default_parsers
128+
self.go = go
129129
self.parsers = {**self.default_parsers, **parsers}
130130
self.runtime_options = {**runtime_options}
131131
self.fail_safe = fail_safe
132-
self.go = go
133132

134133
if self.has_go_parser and not minify_go:
135134
raise FlaskMinifyException(
136135
f"Cannot use any Go parsers without installing "
137136
"Go optional dependency: `pip install flask-minify[go]`"
138137
)
139138

139+
@property
140+
def default_parsers(self):
141+
return (
142+
self._go_default_parsers if self.go and minify_go else self._default_parsers
143+
)
144+
140145
@property
141146
def has_go_parser(self):
142147
return any(p.go for p in self.parsers.values())

tests/units.py

+24
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ def setup(self):
5959
self.bypass = []
6060
self.bypass_caching = []
6161
self.caching_limit = 1
62+
self.parsers = {}
63+
self.go = True
6264
self.patch = mock.patch.multiple("flask_minify.main", request=self.mock_request)
6365

6466
self.patch.start()
@@ -76,6 +78,8 @@ def minify_defaults(self):
7678
self.bypass,
7779
self.bypass_caching,
7880
self.caching_limit,
81+
parsers=self.parsers,
82+
go=self.go,
7983
)
8084

8185
def test_request_falsy_endpoint(self):
@@ -127,6 +131,26 @@ def executer(self, content, **options):
127131
with pytest.raises(FlaskMinifyException):
128132
parser.minify(LESS_RAW, "style")
129133

134+
def test_default_parsers_when_go_enabled_and_dependancy_missing(self):
135+
with mock.patch("flask_minify.parsers.minify_go", None):
136+
parser = parsers.Parser(go=True)
137+
assert parser.default_parsers == parser._default_parsers
138+
139+
def test_default_parsers_when_go_enabled_and_dependency_present(self):
140+
with mock.patch("flask_minify.parsers.minify_go"):
141+
parser = parsers.Parser(go=True)
142+
assert parser.default_parsers == parser._go_default_parsers
143+
144+
def test_default_parsers_when_go_disabled_and_dependency_present(self):
145+
with mock.patch("flask_minify.parsers.minify_go"):
146+
parser = parsers.Parser(go=False)
147+
assert parser.default_parsers == parser._default_parsers
148+
149+
def test_go_parsers_passed_with_go_enabled_and_dependency_missing_exception(self):
150+
with mock.patch("flask_minify.parsers.minify_go", None):
151+
with pytest.raises(FlaskMinifyException):
152+
parsers.Parser(parsers=parsers.Parser._go_default_parsers, go=True)
153+
130154

131155
class TestMemoryCache:
132156
def setup(self):

0 commit comments

Comments
 (0)