Skip to content

Commit 624708c

Browse files
committed
CI job for checking that translations are compiled
This was based on: stefanfoulis/django-phonenumber-field@e653a09
1 parent c171ccd commit 624708c

File tree

4 files changed

+117
-3
lines changed

4 files changed

+117
-3
lines changed

.github/workflows/test.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,36 @@ jobs:
8585
uses: codecov/codecov-action@v3
8686
with:
8787
name: Python ${{ matrix.python-version }}
88+
89+
90+
generated_file_checks:
91+
name: Check generated files
92+
runs-on: ubuntu-latest
93+
94+
steps:
95+
- uses: actions/checkout@v3
96+
97+
- name: Install gettext
98+
run: |
99+
sudo apt-get update
100+
sudo apt-get install -y gettext
101+
102+
- name: Set up newest stable Python version
103+
uses: actions/setup-python@v4
104+
with:
105+
python-version: 3.11
106+
cache: 'pip'
107+
# Invalidate the cache when this file updates, as the dependencies' versions
108+
# are pinned in the step below
109+
cache-dependency-path: '.github/workflows/test.yml'
110+
111+
- name: Install dependencies
112+
run: |
113+
python -m pip install --upgrade pip
114+
# Install this project in editable mode, so that its package metadata can be queried
115+
pip install -e .
116+
# Install the latest minor version of Django we support
117+
pip install Django==4.2
118+
119+
- name: Check translation files are updated
120+
run: python -m simple_history.tests.generated_file_checks.check_translations

runtests.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ def __getitem__(self, item):
9797
},
9898
},
9999
}
100+
DEFAULT_DATABASE_NAME = "sqlite3"
100101

101102

102103
DEFAULT_SETTINGS = dict( # nosec
@@ -156,16 +157,25 @@ def __getitem__(self, item):
156157
}
157158

158159

160+
def get_default_settings(*, database_name=DEFAULT_DATABASE_NAME):
161+
return {
162+
**DEFAULT_SETTINGS,
163+
"DATABASES": DATABASE_NAME_TO_DATABASE_SETTINGS[database_name],
164+
}
165+
166+
159167
def main():
160168
parser = ArgumentParser(description="Run package tests.")
161-
parser.add_argument("--database", action="store", nargs="?", default="sqlite3")
169+
parser.add_argument(
170+
"--database", action="store", nargs="?", default=DEFAULT_DATABASE_NAME
171+
)
162172
parser.add_argument("--failfast", action="store_true")
163173
parser.add_argument("--pdb", action="store_true")
164174
parser.add_argument("--tag", action="append", nargs="?")
165175
namespace = parser.parse_args()
166-
db_settings = DATABASE_NAME_TO_DATABASE_SETTINGS[namespace.database]
167176
if not settings.configured:
168-
settings.configure(**DEFAULT_SETTINGS, DATABASES=db_settings)
177+
default_settings = get_default_settings(database_name=namespace.database)
178+
settings.configure(**default_settings)
169179

170180
django.setup()
171181

simple_history/tests/generated_file_checks/__init__.py

Whitespace-only changes.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import subprocess # nosec
2+
import sys
3+
from glob import glob
4+
from pathlib import Path
5+
6+
import django
7+
from django.conf import settings
8+
from django.core.management import call_command
9+
10+
from runtests import get_default_settings
11+
12+
13+
def log(*args, **kwargs):
14+
# Flush so that all printed messages appear in the correct order, not matter what
15+
# `file` argument is passed (e.g. `sys.stdout` (default) or `sys.stderr`)
16+
print(*args, **{"flush": True, **kwargs})
17+
18+
19+
def log_err(*args, **kwargs):
20+
log(*args, **{"file": sys.stderr, **kwargs})
21+
22+
23+
# For some reason, changes in the .po files are often not reflected in the .mo files
24+
# when running 'compilemessages' - unless the .mo files are deleted first,
25+
# in which case they seem to be consistently updated
26+
def delete_mo_files():
27+
locale_dir = Path("simple_history/locale")
28+
log(
29+
f"Deleting the following files inside '{locale_dir}'"
30+
f" so that they can be regenerated by 'compilemessages':"
31+
)
32+
for file_path in glob("**/*.mo", root_dir=locale_dir, recursive=True):
33+
log(f"\t{file_path}")
34+
(locale_dir / file_path).unlink()
35+
36+
37+
# Code based on
38+
# https://github.com/stefanfoulis/django-phonenumber-field/blob/e653a0972b56d39f1f51fa1f5124b70c2a5549bc/check-translations
39+
def main():
40+
# Delete the .mo files before regenerating them below
41+
delete_mo_files()
42+
43+
log("Running 'compilemessages'...")
44+
call_command("compilemessages")
45+
46+
log("\nRunning 'git status'...")
47+
result = subprocess.run( # nosec
48+
["git", "status", "--porcelain"],
49+
check=True,
50+
stdout=subprocess.PIPE,
51+
)
52+
assert result.stderr is None # nosec
53+
stdout = result.stdout.decode()
54+
if stdout:
55+
log_err(f"Unexpected changes found in the workspace:\n\n{stdout}")
56+
if ".mo" in stdout:
57+
log_err(
58+
"To properly update any '.mo' files,"
59+
" try deleting them before running 'compilemessages'."
60+
)
61+
sys.exit(1)
62+
else:
63+
# Print the human-readable status to the console
64+
subprocess.run(["git", "status"]) # nosec
65+
66+
67+
if __name__ == "__main__":
68+
if not settings.configured:
69+
settings.configure(**get_default_settings())
70+
django.setup()
71+
main()

0 commit comments

Comments
 (0)