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

Test that translation files are compiled #1207

Merged
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
6 changes: 6 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ updates:
directory: "/requirements"
schedule:
interval: "daily"

- package-ecosystem: "github-actions"
# Workflow files stored in the default location of `.github/workflows`
directory: "/"
schedule:
interval: "weekly"
56 changes: 40 additions & 16 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,25 +65,16 @@ jobs:
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- name: Get pip cache dir
id: pip-cache
run: |
echo "dir=$(pip cache dir)" >> $GITHUB_OUTPUT

- name: Cache
uses: actions/cache@v3
with:
path: ${{ steps.pip-cache.outputs.dir }}
key:
${{ matrix.python-version }}-v1-${{ hashFiles('**/setup.py') }}-${{ hashFiles('**/tox.ini') }}
restore-keys: |
${{ matrix.python-version }}-v1-
cache: 'pip'
cache-dependency-path: |
setup.py
tox.ini
requirements/*.txt

- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install -r requirements/tox.txt
pip install -r requirements/tox.txt
valberg marked this conversation as resolved.
Show resolved Hide resolved

- name: Tox tests
run: |
Expand All @@ -94,4 +85,37 @@ jobs:
- name: Upload coverage
uses: codecov/codecov-action@v3
with:
name: Python ${{ matrix.python-version }}
name: Python ${{ matrix.python-version }}, Django ${{ matrix.django-version }}


generated_file_checks:
name: Check generated files
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Install gettext
run: |
sudo apt-get update
sudo apt-get install -y gettext

- name: Set up newest stable Python version
uses: actions/setup-python@v4
with:
python-version: 3.11
cache: 'pip'
# Invalidate the cache when this file updates, as the dependencies' versions
# are pinned in the step below
cache-dependency-path: '.github/workflows/test.yml'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
# Install this project in editable mode, so that its package metadata can be queried
pip install -e .
# Install the latest minor version of Django we support
pip install Django==4.2

- name: Check translation files are updated
run: python -m simple_history.tests.generated_file_checks.check_translations
16 changes: 13 additions & 3 deletions runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def __getitem__(self, item):
},
},
}
DEFAULT_DATABASE_NAME = "sqlite3"


DEFAULT_SETTINGS = dict( # nosec
Expand Down Expand Up @@ -156,16 +157,25 @@ def __getitem__(self, item):
}


def get_default_settings(*, database_name=DEFAULT_DATABASE_NAME):
return {
**DEFAULT_SETTINGS,
"DATABASES": DATABASE_NAME_TO_DATABASE_SETTINGS[database_name],
}


def main():
parser = ArgumentParser(description="Run package tests.")
parser.add_argument("--database", action="store", nargs="?", default="sqlite3")
parser.add_argument(
"--database", action="store", nargs="?", default=DEFAULT_DATABASE_NAME
)
parser.add_argument("--failfast", action="store_true")
parser.add_argument("--pdb", action="store_true")
parser.add_argument("--tag", action="append", nargs="?")
namespace = parser.parse_args()
db_settings = DATABASE_NAME_TO_DATABASE_SETTINGS[namespace.database]
if not settings.configured:
settings.configure(**DEFAULT_SETTINGS, DATABASES=db_settings)
default_settings = get_default_settings(database_name=namespace.database)
settings.configure(**default_settings)

django.setup()

Expand Down
Binary file modified simple_history/locale/de/LC_MESSAGES/django.mo
Binary file not shown.
Binary file modified simple_history/locale/id/LC_MESSAGES/django.mo
Binary file not shown.
Binary file modified simple_history/locale/pl/LC_MESSAGES/django.mo
Binary file not shown.
Binary file modified simple_history/locale/pt_BR/LC_MESSAGES/django.mo
Binary file not shown.
Empty file.
71 changes: 71 additions & 0 deletions simple_history/tests/generated_file_checks/check_translations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import subprocess # nosec
import sys
from glob import glob
from pathlib import Path

import django
from django.conf import settings
from django.core.management import call_command

from runtests import get_default_settings


def log(*args, **kwargs):
# Flush so that all printed messages appear in the correct order, not matter what
# `file` argument is passed (e.g. `sys.stdout` (default) or `sys.stderr`)
print(*args, **{"flush": True, **kwargs})


def log_err(*args, **kwargs):
log(*args, **{"file": sys.stderr, **kwargs})


# For some reason, changes in the .po files are often not reflected in the .mo files
# when running 'compilemessages' - unless the .mo files are deleted first,
# in which case they seem to be consistently updated
def delete_mo_files():
locale_dir = Path("simple_history/locale")
log(
f"Deleting the following files inside '{locale_dir}'"
f" so that they can be regenerated by 'compilemessages':"
)
for file_path in glob("**/*.mo", root_dir=locale_dir, recursive=True):
log(f"\t{file_path}")
(locale_dir / file_path).unlink()


# Code based on
# https://github.com/stefanfoulis/django-phonenumber-field/blob/e653a0972b56d39f1f51fa1f5124b70c2a5549bc/check-translations
def main():
# Delete the .mo files before regenerating them below
delete_mo_files()

log("Running 'compilemessages'...")
call_command("compilemessages")

log("\nRunning 'git status'...")
result = subprocess.run( # nosec
["git", "status", "--porcelain"],
check=True,
stdout=subprocess.PIPE,
)
assert result.stderr is None # nosec
stdout = result.stdout.decode()
if stdout:
log_err(f"Unexpected changes found in the workspace:\n\n{stdout}")
if ".mo" in stdout:
log_err(
"To properly update any '.mo' files,"
" try deleting them before running 'compilemessages'."
)
sys.exit(1)
else:
# Print the human-readable status to the console
subprocess.run(["git", "status"]) # nosec


if __name__ == "__main__":
if not settings.configured:
settings.configure(**get_default_settings())
django.setup()
main()