From dbca159a46423e4798fe71d05dd6833dc489c3e2 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Wed, 17 Jul 2024 21:57:13 +0100 Subject: [PATCH] Add a test run without Flask-Babel installed Adds a `flask_babel` mark to any tests that explicitly require `Flask-Babel` to be installed. Configures tox and GitHub Actions to run a test suite with Flask-Babel removed. This validates a codepath through `flask_admin.babel`, which should essentially be passthrough/noop behaviour if Flask-Babel is unavailable. --- .github/workflows/tests.yaml | 11 +++++++++-- flask_admin/tests/sqla/test_basic.py | 2 ++ flask_admin/tests/sqla/test_translation.py | 3 +++ pyproject.toml | 3 +++ tox.ini | 6 ++++++ 5 files changed, 23 insertions(+), 2 deletions(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 19087e5937..d468c965f6 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -15,13 +15,20 @@ on: - '*.rst' jobs: tests: - name: ${{ format('py{0}-wtforms{1}', matrix.python, matrix.wtforms) }} + name: ${{ matrix.tox == 'normal' && format('py{0}-flask{1}-wtforms{2}', matrix.python, matrix.flask, matrix.wtforms) || matrix.tox }} runs-on: ${{ matrix.os || 'ubuntu-latest' }} strategy: fail-fast: false matrix: python: ['3.8', '3.9', '3.10', '3.11', '3.12'] + flask: ['2'] wtforms: ['2'] + tox: ['normal'] + include: + - python: '3.12' + flask: '2' + wtforms: '2' + tox: 'py312-flask2-wtforms2-no-flask-babel' services: # Label used to access the service container postgres: @@ -68,7 +75,7 @@ jobs: PGPASSWORD: postgres run: psql -U postgres -h localhost -c 'CREATE EXTENSION hstore;' flask_admin_test - run: pip install tox - - run: tox run -e ${{ matrix.tox || format('py{0}-wtforms{1}', matrix.python, matrix.wtforms) }} + - run: tox run -e ${{ matrix.tox == 'normal' && format('py{0}-flask{1}-wtforms{2}', matrix.python, matrix.flask, matrix.wtforms) || matrix.tox }} not_tests: name: ${{ matrix.tox }} runs-on: ubuntu-latest diff --git a/flask_admin/tests/sqla/test_basic.py b/flask_admin/tests/sqla/test_basic.py index 5a053cc186..9f3b08e16f 100644 --- a/flask_admin/tests/sqla/test_basic.py +++ b/flask_admin/tests/sqla/test_basic.py @@ -2181,6 +2181,7 @@ def test_extra_field_order(app, db, admin): ('zh_TW', '首頁'), ) ) +@pytest.mark.flask_babel def test_modelview_localization(request, app, locale, expect_text): # We need to configure the default Babel locale _before_ the `babel` fixture is # initialised, so we have to use `request.getfixturevalue` to pull the fixture @@ -2210,6 +2211,7 @@ def test_modelview_localization(request, app, locale, expect_text): assert rv.status_code == 200 +@pytest.mark.flask_babel def test_modelview_named_filter_localization(request, app): # We need to configure the default Babel locale _before_ the `babel` fixture is # initialised, so we have to use `request.getfixturevalue` to pull the fixture diff --git a/flask_admin/tests/sqla/test_translation.py b/flask_admin/tests/sqla/test_translation.py index 99150f1c4a..3adfca7ec5 100644 --- a/flask_admin/tests/sqla/test_translation.py +++ b/flask_admin/tests/sqla/test_translation.py @@ -1,8 +1,11 @@ +import pytest + from flask_admin.babel import lazy_gettext from .test_basic import CustomModelView, create_models +@pytest.mark.flask_babel def test_column_label_translation(request, app): # We need to configure the default Babel locale _before_ the `babel` fixture is # initialised, so we have to use `request.getfixturevalue` to pull the fixture diff --git a/pyproject.toml b/pyproject.toml index 44b0bbcc17..ac2e3bd8d3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -41,6 +41,9 @@ name = "flask_admin" [tool.pytest.ini_options] testpaths = ["tests"] +markers = [ + "flask_babel: requires Flask-Babel to be installed" +] filterwarnings = [ "error", # TODO: remove the ignored deprecation warning when support for WTForms 3 has been added. diff --git a/tox.ini b/tox.ini index 377218a318..476ba1c587 100644 --- a/tox.ini +++ b/tox.ini @@ -1,6 +1,7 @@ [tox] envlist = py3{8,9,10,11,12}-flask{2}-wtforms{2} + py312-flask2-wtforms2-no-flask-babel # only tested against latest of all configurations, sans flask-babel # style typing docs @@ -23,6 +24,11 @@ deps = wtforms2: WTForms>=2.0,<3 commands = pytest -v --tb=short --basetemp={envtmpdir} flask_admin/tests {posargs} +[testenv:py312-flask2-wtforms2-no-flask-babel] +commands = + pip uninstall -y flask-babel + pytest -v --tb=short --basetemp={envtmpdir} flask_admin/tests -m "not flask_babel" {posargs} + [testenv:style] deps = pre-commit skip_install = true