Skip to content
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
47 changes: 22 additions & 25 deletions bedrock/base/tests/test_accepted_locales.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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.
Expand Down
48 changes: 48 additions & 0 deletions bedrock/conftest.py
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 1 addition & 1 deletion bin/integration_tests/run_integration_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
2 changes: 2 additions & 0 deletions bin/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
42 changes: 21 additions & 21 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading