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 missing go dependency exception with default options #98

Merged
merged 3 commits into from
Jul 14, 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
2 changes: 1 addition & 1 deletion flask_minify/about.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.47"
__version__ = "0.48"
__doc__ = "Flask extension to minify html, css, js and less."
__license__ = "MIT"
__author__ = "Mohamed Feddad"
Expand Down
9 changes: 7 additions & 2 deletions flask_minify/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,18 +125,23 @@ def __init__(
runtime_options={},
go=False,
):
self.default_parsers = self._go_default_parsers if go else self._default_parsers
self.go = go
self.parsers = {**self.default_parsers, **parsers}
self.runtime_options = {**runtime_options}
self.fail_safe = fail_safe
self.go = go

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

@property
def default_parsers(self):
return (
self._go_default_parsers if self.go and minify_go else self._default_parsers
)

@property
def has_go_parser(self):
return any(p.go for p in self.parsers.values())
Expand Down
24 changes: 24 additions & 0 deletions tests/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ def setup(self):
self.bypass = []
self.bypass_caching = []
self.caching_limit = 1
self.parsers = {}
self.go = True
self.patch = mock.patch.multiple("flask_minify.main", request=self.mock_request)

self.patch.start()
Expand All @@ -76,6 +78,8 @@ def minify_defaults(self):
self.bypass,
self.bypass_caching,
self.caching_limit,
parsers=self.parsers,
go=self.go,
)

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

def test_default_parsers_when_go_enabled_and_dependancy_missing(self):
with mock.patch("flask_minify.parsers.minify_go", None):
parser = parsers.Parser(go=True)
assert parser.default_parsers == parser._default_parsers

def test_default_parsers_when_go_enabled_and_dependency_present(self):
with mock.patch("flask_minify.parsers.minify_go"):
parser = parsers.Parser(go=True)
assert parser.default_parsers == parser._go_default_parsers

def test_default_parsers_when_go_disabled_and_dependency_present(self):
with mock.patch("flask_minify.parsers.minify_go"):
parser = parsers.Parser(go=False)
assert parser.default_parsers == parser._default_parsers

def test_go_parsers_passed_with_go_enabled_and_dependency_missing_exception(self):
with mock.patch("flask_minify.parsers.minify_go", None):
with pytest.raises(FlaskMinifyException):
parsers.Parser(parsers=parsers.Parser._go_default_parsers, go=True)


class TestMemoryCache:
def setup(self):
Expand Down
Loading