Skip to content

Commit 9a6755e

Browse files
authored
Merge pull request #1207 from ddabble/feature/test-translations-are-compiled
Test that translation files are compiled
2 parents e375098 + 1fba14e commit 9a6755e

File tree

9 files changed

+130
-19
lines changed

9 files changed

+130
-19
lines changed

.github/dependabot.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,9 @@ updates:
55
directory: "/requirements"
66
schedule:
77
interval: "daily"
8+
9+
- package-ecosystem: "github-actions"
10+
# Workflow files stored in the default location of `.github/workflows`
11+
directory: "/"
12+
schedule:
13+
interval: "weekly"

.github/workflows/test.yml

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,25 +65,16 @@ jobs:
6565
uses: actions/setup-python@v4
6666
with:
6767
python-version: ${{ matrix.python-version }}
68-
69-
- name: Get pip cache dir
70-
id: pip-cache
71-
run: |
72-
echo "dir=$(pip cache dir)" >> $GITHUB_OUTPUT
73-
74-
- name: Cache
75-
uses: actions/cache@v3
76-
with:
77-
path: ${{ steps.pip-cache.outputs.dir }}
78-
key:
79-
${{ matrix.python-version }}-v1-${{ hashFiles('**/setup.py') }}-${{ hashFiles('**/tox.ini') }}
80-
restore-keys: |
81-
${{ matrix.python-version }}-v1-
68+
cache: 'pip'
69+
cache-dependency-path: |
70+
setup.py
71+
tox.ini
72+
requirements/*.txt
8273
8374
- name: Install dependencies
8475
run: |
8576
python -m pip install --upgrade pip
86-
python -m pip install -r requirements/tox.txt
77+
pip install -r requirements/tox.txt
8778
8879
- name: Tox tests
8980
run: |
@@ -94,4 +85,37 @@ jobs:
9485
- name: Upload coverage
9586
uses: codecov/codecov-action@v3
9687
with:
97-
name: Python ${{ matrix.python-version }}
88+
name: Python ${{ matrix.python-version }}, Django ${{ matrix.django-version }}
89+
90+
91+
generated_file_checks:
92+
name: Check generated files
93+
runs-on: ubuntu-latest
94+
95+
steps:
96+
- uses: actions/checkout@v3
97+
98+
- name: Install gettext
99+
run: |
100+
sudo apt-get update
101+
sudo apt-get install -y gettext
102+
103+
- name: Set up newest stable Python version
104+
uses: actions/setup-python@v4
105+
with:
106+
python-version: 3.11
107+
cache: 'pip'
108+
# Invalidate the cache when this file updates, as the dependencies' versions
109+
# are pinned in the step below
110+
cache-dependency-path: '.github/workflows/test.yml'
111+
112+
- name: Install dependencies
113+
run: |
114+
python -m pip install --upgrade pip
115+
# Install this project in editable mode, so that its package metadata can be queried
116+
pip install -e .
117+
# Install the latest minor version of Django we support
118+
pip install Django==4.2
119+
120+
- name: Check translation files are updated
121+
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

-60 Bytes
Binary file not shown.
-41 Bytes
Binary file not shown.
-41 Bytes
Binary file not shown.
-41 Bytes
Binary file not shown.

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)