Skip to content

Commit

Permalink
Merge pull request #80 from mrf345/testing
Browse files Browse the repository at this point in the history
refactor: remove the use of `_app_ctx_stack`
  • Loading branch information
mrf345 authored Oct 2, 2022
2 parents 4b48318 + 8d1bb28 commit 6d80c17
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ c ?= 1
test: install
test -f .venv/bin/activate && source .venv/bin/activate && python -m bandit -c bandit.yml -r . && python -m pytest --count=$(c)
lint: install
source .venv/bin/activate && python -m isort --profile black --check . && python -m black --check .
source .venv/bin/activate && python -m isort -sg "**/.venv*" --profile black --check . && python -m black --check .
format: install
test -f .venv/bin/activate && source .venv/bin/activate && python -m isort --profile black . && python -m black .
test -f .venv/bin/activate && source .venv/bin/activate && python -m isort -sg "**/.venv*" --profile black . && python -m black .
run: install
python tests/integration.py
release: install-dev clean
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ the **default** parsers are set to `{"html": Html, "script": Jsmin, "style": Rcs

## Breaking changes

#### `0.40`

Due to a future deprecation in Flask 2.3, the extension is no longer going to fallback to `Flask._app_ctx_stack`, it will raise an exception instead (`flask_minify.exceptions.MissingApp`)

#### `0.33`

introduces a breaking change to the expected output, in this release `lesscpy` will be replaced by `cssmin` as
Expand Down
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.39"
__version__ = "0.40"
__doc__ = "Flask extension to minify html, css, js and less."
__license__ = "MIT"
__author__ = "Mohamed Feddad"
Expand Down
10 changes: 10 additions & 0 deletions flask_minify/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class FlaskMinifyException(Exception):
"""FlaskMinify base exception"""

pass


class MissingApp(FlaskMinifyException):
"""Raised when the flask app is accessed before it's set"""

pass
17 changes: 14 additions & 3 deletions flask_minify/main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from itertools import tee
from re import compile as compile_re

from flask import _app_ctx_stack, request
from flask import request

from flask_minify.cache import MemoryCache
from flask_minify.exceptions import MissingApp
from flask_minify.parsers import Parser
from flask_minify.utils import does_content_type_match

Expand Down Expand Up @@ -112,14 +113,24 @@ def get_endpoint(self):

@property
def app(self):
"""If app was passed take it, if not get the one on top.
"""If app was passed take it, otherwise raise an exception.
Returns
-------
Flask App
The current Flask application.
Raises
------
MissingApp
"""
return self._app or (_app_ctx_stack.top and _app_ctx_stack.top.app)
if not self._app:
raise MissingApp(
"Flask app has not been passed to the extension `Minify(app=None)`, "
"nor lazy initialized with `.init_app(app)`"
)

return self._app

def init_app(self, app):
"""Handle initiation of multiple apps NOTE:Factory Method"""
Expand Down
12 changes: 11 additions & 1 deletion tests/units.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from random import randint
from unittest import mock

import pytest

from flask_minify import minify, parsers
from flask_minify.cache import MemoryCache
from flask_minify.exceptions import MissingApp
from flask_minify.utils import does_content_type_match, is_empty

from .constants import (
Expand Down Expand Up @@ -87,6 +89,14 @@ def test_request_falsy_endpoint(self):

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

def test_access_missing_app_raises_exception(self):
"""test accessing a missing flask app raises an exception"""
self.mock_app = None
ext = self.minify_defaults

with pytest.raises(MissingApp):
ext.app


class TestParsers:
def test_css_edge_cases_with_rcssmin(self):
Expand Down

0 comments on commit 6d80c17

Please sign in to comment.