diff --git a/bedrock/base/tests/test_accepted_locales.py b/bedrock/base/tests/test_accepted_locales.py index 8ad26806a2a..62c7d22fb83 100644 --- a/bedrock/base/tests/test_accepted_locales.py +++ b/bedrock/base/tests/test_accepted_locales.py @@ -2,13 +2,16 @@ # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at https://mozilla.org/MPL/2.0/. -import os import shutil +import tempfile +from pathlib import Path +from unittest import mock from django.conf import settings from django.test import TestCase -from bedrock.settings.base import data_path, get_dev_languages +from bedrock.settings import base as base_settings +from bedrock.settings.base import get_dev_languages class AcceptedLocalesTest(TestCase): @@ -20,50 +23,44 @@ class AcceptedLocalesTest(TestCase): """ - locale = data_path("www-l10n") - locale_bkp = data_path("www-l10n_bkp") - @classmethod def setup_class(cls): - """Create a directory structure for locale/. + """Point FLUENT_REPO_PATH at a throwaway l10n checkout. + + The stand-in contains: - Back up the existing project/locale/ directory and create the following - hierarchy in its place: + - en-US/ + - fr/ + - metadata/ + - empty_file - - project/locale/en-US/LC_MESSAGES - - project/locale/fr/LC_MESSAGES - - project/locale/templates/LC_MESSAGES - - project/locale/empty_file + A temporary directory rather than the real data/www-l10n keeps this + class from disturbing other tests that may be reading the Fluent + files at the same time in another pytest worker. Also, set PROD_LANGUAGES to ('en-US',). """ - if os.path.exists(cls.locale_bkp): - raise Exception( - "A backup of locale/ exists at %s which might mean that previous tests didn't end cleanly. Skipping the test suite." % cls.locale_bkp - ) cls.DEV = settings.DEV cls.PROD_LANGUAGES = settings.PROD_LANGUAGES cls.DEV_LANGUAGES = settings.DEV_LANGUAGES settings.PROD_LANGUAGES = ("en-US",) - if os.path.exists(cls.locale): - shutil.move(cls.locale, cls.locale_bkp) - else: - cls.locale_bkp = None + cls.locale = Path(tempfile.mkdtemp()) for loc in ("en-US", "fr", "metadata"): - os.makedirs(os.path.join(cls.locale, loc)) - open(os.path.join(cls.locale, "empty_file"), "w").close() + (cls.locale / loc).mkdir() + (cls.locale / "empty_file").touch() + cls.repo_path_patch = mock.patch.object(base_settings, "FLUENT_REPO_PATH", cls.locale) + cls.repo_path_patch.start() @classmethod def teardown_class(cls): - """Remove the testing locale/ dir and bring back the backup.""" + """Remove the testing locale dir and restore the patched settings.""" + cls.repo_path_patch.stop() settings.DEV = cls.DEV settings.PROD_LANGUAGES = cls.PROD_LANGUAGES settings.DEV_LANGUAGES = cls.DEV_LANGUAGES shutil.rmtree(cls.locale) - if cls.locale_bkp: - shutil.move(cls.locale_bkp, cls.locale) def test_build_dev_languages(self): """Test that the list of dev locales is built properly. diff --git a/bedrock/conftest.py b/bedrock/conftest.py new file mode 100644 index 00000000000..b2069788b22 --- /dev/null +++ b/bedrock/conftest.py @@ -0,0 +1,48 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at https://mozilla.org/MPL/2.0/. + +# Isolation fixtures for every test under bedrock/. They matter most when the suite runs in +# parallel, because each worker runs the tests in a different order from the serial run and so +# inherits different leftover state from its predecessor. + +from django.core.cache import cache +from django.urls import clear_url_caches +from django.utils import translation + +import pytest + + +@pytest.fixture(autouse=True) +def clear_django_cache(): + """Start each test with an empty cache. + + Wagtail keeps the site root paths in the cache and waffle keeps switch state there. A + test's database rollback does not invalidate either, so without this a later test can + build URLs from root pages that no longer exist or see switch overrides from an earlier + test.""" + cache.clear() + yield + + +@pytest.fixture(autouse=True) +def reset_url_resolvers(): + """Start each test with unpopulated URL resolvers. + + Django caches reversed URLs per language, keyed on its own get_language(). The locale + prefix in those URLs comes from LocalePrefixPattern, which reads the language through + lib.l10n_utils.translation instead. A test that mocks django.utils.translation.get_language + while a URL is reversed fills the cache entry for the real language with the mocked + language's prefix, and every later test in the process reverses wrong URLs.""" + clear_url_caches() + yield + + +@pytest.fixture(autouse=True) +def reset_active_language(): + """Deactivate whatever language a test left active. + + Requests to locale-prefixed URLs (e.g. /pt-PT/...) call translation.activate() inside + the URL resolver, and page.localized resolves against the active language.""" + yield + translation.deactivate() diff --git a/bin/integration_tests/run_integration_tests.sh b/bin/integration_tests/run_integration_tests.sh index 6476b279f21..ee0832c00a9 100755 --- a/bin/integration_tests/run_integration_tests.sh +++ b/bin/integration_tests/run_integration_tests.sh @@ -17,7 +17,7 @@ set -xe CMD="pytest" CMD="${CMD} -r a" CMD="${CMD} --verbose" -CMD="${CMD} --workers ${PYTEST_PROCESSES}" +CMD="${CMD} -n ${PYTEST_PROCESSES}" CMD="${CMD} --base-url ${BASE_URL}" CMD="${CMD} --reruns ${RERUNS_ALLOWED}" CMD="${CMD} --reruns-delay ${RERUNS_DELAY_SECS}" diff --git a/bin/run-tests.sh b/bin/run-tests.sh index cb75a10b5a1..ec3b16e35d0 100755 --- a/bin/run-tests.sh +++ b/bin/run-tests.sh @@ -12,7 +12,9 @@ python manage.py lint_ftl -q python manage.py version python manage.py migrate --noinput python manage.py makemigrations --check +# Set PYTEST_XDIST_WORKERS=0 to run the suite in a single process when debugging. pytest lib bedrock \ + -n "${PYTEST_XDIST_WORKERS:-auto}" \ --durations=30 \ --cov-config=.coveragerc \ --cov-report=html \ diff --git a/pyproject.toml b/pyproject.toml index 51268d8c468..ed14a0b0ce5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -79,8 +79,8 @@ dev = [ "pytest-django==4.14.0", "pytest-html==4.2.0", # Provides the --html option used to generate the integration test report "pytest-mock==3.17.0", - "pytest-parallel==0.1.1", "pytest-rerunfailures==16.7", + "pytest-xdist==3.8.0", "responses==0.26.3", "ruff==0.17.0", "selenium==4.51.0", diff --git a/uv.lock b/uv.lock index 10252ad12c4..5c2938216d9 100644 --- a/uv.lock +++ b/uv.lock @@ -191,8 +191,8 @@ dev = [ { name = "pytest-django" }, { name = "pytest-html" }, { name = "pytest-mock" }, - { name = "pytest-parallel" }, { name = "pytest-rerunfailures" }, + { name = "pytest-xdist" }, { name = "responses" }, { name = "ruff" }, { name = "selenium" }, @@ -283,8 +283,8 @@ dev = [ { name = "pytest-django", specifier = "==4.14.0" }, { name = "pytest-html", specifier = "==4.2.0" }, { name = "pytest-mock", specifier = "==3.17.0" }, - { name = "pytest-parallel", specifier = "==0.1.1" }, { name = "pytest-rerunfailures", specifier = "==16.7" }, + { name = "pytest-xdist", specifier = "==3.8.0" }, { name = "responses", specifier = "==0.26.3" }, { name = "ruff", specifier = "==0.17.0" }, { name = "selenium", specifier = "==4.51.0" }, @@ -892,6 +892,15 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/69/39/2633de27c0ff431d9c4b99f2d8fe45c75046aad4786f1514e1c99e62c444/everett-3.5.0-py3-none-any.whl", hash = "sha256:996fccda259510676e599cdad4d6837dc6e0194389fcfcb5b66ac867ad2b9430", size = 34198, upload-time = "2025-10-15T12:34:11.708Z" }, ] +[[package]] +name = "execnet" +version = "2.1.2" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/bf/89/780e11f9588d9e7128a3f87788354c7946a9cbb1401ad38a48c4db9a4f07/execnet-2.1.2.tar.gz", hash = "sha256:63d83bfdd9a23e35b9c6a3261412324f964c2ec8dcd8d3c6916ee9373e0befcd", size = 166622, upload-time = "2025-11-12T09:56:37.75Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/ab/84/02fc1827e8cdded4aa65baef11296a9bbe595c474f0d6d758af082d849fd/execnet-2.1.2-py3-none-any.whl", hash = "sha256:67fba928dd5a544b783f6056f449e5e3931a5c378b128bc18501f7ea79e296ec", size = 40708, upload-time = "2025-11-12T09:56:36.333Z" }, +] + [[package]] name = "factory-boy" version = "3.3.3" @@ -1898,29 +1907,29 @@ wheels = [ ] [[package]] -name = "pytest-parallel" -version = "0.1.1" +name = "pytest-rerunfailures" +version = "16.7" source = { registry = "https://pypi.org/simple" } dependencies = [ + { name = "packaging" }, { name = "pytest" }, - { name = "tblib" }, ] -sdist = { url = "https://files.pythonhosted.org/packages/ed/0e/a74218b99ae0fbab09fabc0ad01e763b32abbeaa96a27188782e9d6289db/pytest-parallel-0.1.1.tar.gz", hash = "sha256:9aac3fc199a168c0a8559b60249d9eb254de7af58c12cee0310b54d4affdbfab", size = 9547, upload-time = "2021-10-10T15:39:20.209Z" } +sdist = { url = "https://files.pythonhosted.org/packages/d1/b0/6b5337f9d59b26b0069ea3d5e863c31dc04b69e51bbfb1cf2ea6328fba87/pytest_rerunfailures-16.7.tar.gz", hash = "sha256:6956ddfb65ca1d07e7e3d99e2c5359d82300f4cb062e6049563bd2c106f72d5c", size = 59666, upload-time = "2026-09-17T07:08:48.871Z" } wheels = [ - { url = "https://files.pythonhosted.org/packages/14/d2/a2cf7da29753a222d19a682d50fb3cb605544cec66770553611119c857d2/pytest_parallel-0.1.1-py3-none-any.whl", hash = "sha256:9e3703015b0eda52be9e07d2ba3498f09340a56d5c79a39b50f22fc5c38212fe", size = 6967, upload-time = "2021-10-10T15:39:19.068Z" }, + { url = "https://files.pythonhosted.org/packages/c1/d3/07ea35102cf2020ddaaac368d2a6f0bcc63d6523fb8918805bac3eb8b98f/pytest_rerunfailures-16.7-py3-none-any.whl", hash = "sha256:edf1886209c2b7dafe35b5bf1708d6ec40ccf6c6b357f0f02807efcec0204c99", size = 23952, upload-time = "2026-09-17T07:08:47.635Z" }, ] [[package]] -name = "pytest-rerunfailures" -version = "16.7" +name = "pytest-xdist" +version = "3.8.0" source = { registry = "https://pypi.org/simple" } dependencies = [ - { name = "packaging" }, + { name = "execnet" }, { name = "pytest" }, ] -sdist = { url = "https://files.pythonhosted.org/packages/d1/b0/6b5337f9d59b26b0069ea3d5e863c31dc04b69e51bbfb1cf2ea6328fba87/pytest_rerunfailures-16.7.tar.gz", hash = "sha256:6956ddfb65ca1d07e7e3d99e2c5359d82300f4cb062e6049563bd2c106f72d5c", size = 59666, upload-time = "2026-09-17T07:08:48.871Z" } +sdist = { url = "https://files.pythonhosted.org/packages/78/b4/439b179d1ff526791eb921115fca8e44e596a13efeda518b9d845a619450/pytest_xdist-3.8.0.tar.gz", hash = "sha256:7e578125ec9bc6050861aa93f2d59f1d8d085595d6551c2c90b6f4fad8d3a9f1", size = 88069, upload-time = "2025-07-01T13:30:59.346Z" } wheels = [ - { url = "https://files.pythonhosted.org/packages/c1/d3/07ea35102cf2020ddaaac368d2a6f0bcc63d6523fb8918805bac3eb8b98f/pytest_rerunfailures-16.7-py3-none-any.whl", hash = "sha256:edf1886209c2b7dafe35b5bf1708d6ec40ccf6c6b357f0f02807efcec0204c99", size = 23952, upload-time = "2026-09-17T07:08:47.635Z" }, + { url = "https://files.pythonhosted.org/packages/ca/31/d4e37e9e550c2b92a9cbc2e4d0b7420a27224968580b5a447f420847c975/pytest_xdist-3.8.0-py3-none-any.whl", hash = "sha256:202ca578cfeb7370784a8c33d6d05bc6e13b4f25b5053c30a152269fd10f0b88", size = 46396, upload-time = "2025-07-01T13:30:56.632Z" }, ] [[package]] @@ -2177,15 +2186,6 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/0e/65/5e726c372da8a5e35022a94388b12252710aad0c2351699c3d76ae8dba78/supervisor-4.3.0-py2.py3-none-any.whl", hash = "sha256:0bcb763fddafba410f35cbde226aa7f8514b9fb82eb05a0c85f6588d1c13f8db", size = 320736, upload-time = "2025-08-23T18:25:00.767Z" }, ] -[[package]] -name = "tblib" -version = "3.2.2" -source = { registry = "https://pypi.org/simple" } -sdist = { url = "https://files.pythonhosted.org/packages/f4/8a/14c15ae154895cc131174f858c707790d416c444fc69f93918adfd8c4c0b/tblib-3.2.2.tar.gz", hash = "sha256:e9a652692d91bf4f743d4a15bc174c0b76afc750fe8c7b6d195cc1c1d6d2ccec", size = 35046, upload-time = "2025-11-12T12:21:16.572Z" } -wheels = [ - { url = "https://files.pythonhosted.org/packages/02/be/5d2d47b1fb58943194fb59dcf222f7c4e35122ec0ffe8c36e18b5d728f0b/tblib-3.2.2-py3-none-any.whl", hash = "sha256:26bdccf339bcce6a88b2b5432c988b266ebbe63a4e593f6b578b1d2e723d2b76", size = 12893, upload-time = "2025-11-12T12:21:14.407Z" }, -] - [[package]] name = "telepath" version = "0.3.1"