Skip to content

Commit e2a4f8f

Browse files
Preliminary testing
1 parent 9239962 commit e2a4f8f

File tree

6 files changed

+169
-41
lines changed

6 files changed

+169
-41
lines changed

.github/workflows/run-tests.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: Run test suite
2+
on:
3+
pull_request:
4+
workflow_dispatch:
5+
push:
6+
7+
jobs:
8+
test:
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- uses: actions/checkout@v4
13+
with:
14+
persist-credentials: false
15+
- uses: actions/setup-python@v5
16+
with:
17+
python-version: "3.x"
18+
- run: python -m unittest discover -s tests

build_status.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,3 @@ def get_languages(http: PoolManager) -> Iterator[tuple[str, bool]]:
2121
language_code = code.lower().replace('_', '-')
2222
in_switcher = language.get('in_prod', config['defaults']['in_prod'])
2323
yield language_code, in_switcher
24-
25-
26-
def main() -> None:
27-
languages = {
28-
language: in_switcher for language, in_switcher in get_languages(PoolManager())
29-
}
30-
print(languages)
31-
for code in ('en', 'pl', 'ar', 'zh-cn', 'id'):
32-
print(f'{code}: {code in languages} {languages.get(code)}')
33-
34-
35-
if __name__ == '__main__':
36-
main()

tests/support.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import sys
2+
import contextlib
3+
4+
# -------------------------------- Imports ----------------------------------- #
5+
6+
7+
# Support functions borrowed from CPython's test.support
8+
@contextlib.contextmanager
9+
def import_scripts(dir='..'):
10+
with DirsOnSysPath(dir) as cm:
11+
yield cm
12+
13+
14+
class DirsOnSysPath(object):
15+
def __init__(self, *paths):
16+
self.original_value = sys.path[:]
17+
self.original_object = sys.path
18+
sys.path.extend(paths)
19+
20+
def __enter__(self):
21+
return self
22+
23+
def __exit__(self, *ignore_exc):
24+
sys.path = self.original_object
25+
sys.path[:] = self.original_value

tests/test_build_status.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import unittest
2+
import support
3+
4+
from urllib3 import PoolManager
5+
6+
with support.import_scripts():
7+
import build_status
8+
9+
10+
class testBuildStatus(unittest.TestCase):
11+
def test_get_languages(self):
12+
result = {
13+
language: in_switcher
14+
for language, in_switcher in build_status.get_languages(PoolManager())
15+
}
16+
17+
self.assertIn('en', result)
18+
self.assertIn('pl', result)
19+
self.assertIn('zh-cn', result)
20+
21+
self.assertEqual(result.get('en'), True)
22+
self.assertEqual(result.get('pl'), True)
23+
24+
25+
if __name__ == '__main__':
26+
unittest.main()

tests/test_translators.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import unittest
2+
import support
3+
import tempfile
4+
import shutil
5+
6+
from pathlib import Path
7+
from git import Repo
8+
9+
with support.import_scripts():
10+
import translators
11+
12+
TRANSLATORS_BASIC = """
13+
John Cleese
14+
Graham Chapman
15+
Terry Jones
16+
Michael Palin
17+
Terry Gilliam
18+
"""
19+
20+
TRANSLATORS_TITLE_AND_COMMENTS = """
21+
Translators
22+
John Cleese
23+
Graham Chapman
24+
Terry Jones
25+
# Also Translators
26+
Michael Palin
27+
Terry Gilliam
28+
"""
29+
30+
TRANSLATORS_COMPLEX = """
31+
Translators
32+
John Cleese
33+
-----
34+
35+
36+
Graham Chapman
37+
Graham Chapman
38+
# I'm a line
39+
Terry Jones
40+
# # #
41+
-
42+
Michael Palin
43+
Terry Gilliam
44+
"""
45+
46+
47+
class testTranslators(unittest.TestCase):
48+
@classmethod
49+
def setUpClass(cls):
50+
cls.tempdir = tempfile.mkdtemp()
51+
cls.test_repo_path = Path(cls.tempdir) / 'python-docs-pl'
52+
cls.test_repo = Repo.clone_from(
53+
'https://github.com/python/python-docs-pl',
54+
cls.test_repo_path,
55+
branch='3.14',
56+
)
57+
cls.test_repo.git.checkout('d242ceee48cfa6d70dbc0bc75f5043d1e6c5efeb')
58+
59+
@classmethod
60+
def tearDownClass(cls):
61+
shutil.rmtree(cls.tempdir)
62+
63+
def test_yield_from_headers(self):
64+
translators_list = set(translators.yield_from_headers(self.test_repo_path))
65+
self.assertEqual(len(translators_list), 25)
66+
67+
def test_get_number_from_git_history(self):
68+
# [" 183\tGitHub Action's commit-build job", " 84\tGitHub Action's update-chart job", " 6\tGitHub Action's update-readme job", " 1342\tGitHub Action's update-translation job", ' 174\tMaciej Olko', ' 3\tStan U.', ' 20\tStan Ulbrych', ' 1\tciarbin', ' 5\tm-aciek']
69+
self.assertEqual(
70+
translators.get_number_from_git_history(self.test_repo_path), 9
71+
)
72+
73+
def test_get_translators_from_file(self):
74+
for i, file in enumerate(
75+
(TRANSLATORS_BASIC, TRANSLATORS_TITLE_AND_COMMENTS, TRANSLATORS_COMPLEX)
76+
):
77+
with self.subTest(i=i, file=file):
78+
folder = Path(self.tempdir) / f'folder-{i}'
79+
folder.mkdir()
80+
(folder / 'TRANSLATORS').write_text(file)
81+
82+
result = translators.get_from_translators_file(folder)
83+
self.assertEqual(len(result), 5)
84+
85+
# No TRANSLATORS file
86+
result = translators.get_from_translators_file(Path(self.tempdir) / 'empty')
87+
self.assertEqual(result, set())
88+
89+
def test_get_number(self):
90+
(self.test_repo_path / 'TRANSLATORS').write_text(TRANSLATORS_COMPLEX)
91+
92+
result = translators.get_number(self.test_repo_path)
93+
self.assertEqual(result, 25)
94+
95+
96+
if __name__ == '__main__':
97+
unittest.main()

translators.py

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from collections.abc import Iterator
22
from pathlib import Path
33
from re import fullmatch
4-
from tempfile import TemporaryDirectory
54
from typing import Literal
65

76
from git import Repo
@@ -36,10 +35,10 @@ def yield_from_headers(path: Path) -> Iterator[str]:
3635
yield translator
3736

3837

39-
def get_from_translators_file(path: Path) -> list[str]:
38+
def get_from_translators_file(path: Path) -> set[str]:
4039
if not (file := path.joinpath('TRANSLATORS')).exists():
41-
return []
42-
return list(
40+
return set()
41+
return set(
4342
line
4443
for line in file.read_text().splitlines()
4544
if line != 'Translators'
@@ -53,27 +52,3 @@ def get_link(clone_path: Path, repo: str, branch: str) -> str | Literal[False]:
5352
clone_path.joinpath('TRANSLATORS').exists()
5453
and f'https://github.com/{repo}/blob/{branch}/TRANSLATORS'
5554
)
56-
57-
58-
if __name__ == '__main__':
59-
for lang, branch in (
60-
('es', '3.13'),
61-
('hu', '3.6'),
62-
('pl', '3.13'),
63-
('zh-tw', '3.13'),
64-
('id', '3.9'),
65-
('fr', '3.13'),
66-
('hu', '3.6'),
67-
):
68-
with TemporaryDirectory() as directory:
69-
Repo.clone_from(
70-
f'https://github.com/python/python-docs-{lang}',
71-
directory,
72-
branch=branch,
73-
)
74-
from_headers = len(set(yield_from_headers(path := Path(directory))))
75-
from_git_history = get_number_from_git_history(path)
76-
from_translators_file = len(get_from_translators_file(path))
77-
print(
78-
f'{lang}: {from_headers=}, {from_git_history=}, {from_translators_file=}'
79-
)

0 commit comments

Comments
 (0)