From e2a4f8f728a3a674027c1b5a03c57e263a81e6e9 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Thu, 31 Jul 2025 18:45:47 +0200 Subject: [PATCH 1/4] Preliminary testing --- .github/workflows/run-tests.yml | 18 ++++++ build_status.py | 13 ----- tests/support.py | 25 +++++++++ tests/test_build_status.py | 26 +++++++++ tests/test_translators.py | 97 +++++++++++++++++++++++++++++++++ translators.py | 31 +---------- 6 files changed, 169 insertions(+), 41 deletions(-) create mode 100644 .github/workflows/run-tests.yml create mode 100644 tests/support.py create mode 100644 tests/test_build_status.py create mode 100644 tests/test_translators.py diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml new file mode 100644 index 000000000..b34d0e971 --- /dev/null +++ b/.github/workflows/run-tests.yml @@ -0,0 +1,18 @@ +name: Run test suite +on: + pull_request: + workflow_dispatch: + push: + +jobs: + test: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + with: + persist-credentials: false + - uses: actions/setup-python@v5 + with: + python-version: "3.x" + - run: python -m unittest discover -s tests diff --git a/build_status.py b/build_status.py index fdc2323a4..135681163 100644 --- a/build_status.py +++ b/build_status.py @@ -21,16 +21,3 @@ def get_languages(http: PoolManager) -> Iterator[tuple[str, bool]]: language_code = code.lower().replace('_', '-') in_switcher = language.get('in_prod', config['defaults']['in_prod']) yield language_code, in_switcher - - -def main() -> None: - languages = { - language: in_switcher for language, in_switcher in get_languages(PoolManager()) - } - print(languages) - for code in ('en', 'pl', 'ar', 'zh-cn', 'id'): - print(f'{code}: {code in languages} {languages.get(code)}') - - -if __name__ == '__main__': - main() diff --git a/tests/support.py b/tests/support.py new file mode 100644 index 000000000..0841ce479 --- /dev/null +++ b/tests/support.py @@ -0,0 +1,25 @@ +import sys +import contextlib + +# -------------------------------- Imports ----------------------------------- # + + +# Support functions borrowed from CPython's test.support +@contextlib.contextmanager +def import_scripts(dir='..'): + with DirsOnSysPath(dir) as cm: + yield cm + + +class DirsOnSysPath(object): + def __init__(self, *paths): + self.original_value = sys.path[:] + self.original_object = sys.path + sys.path.extend(paths) + + def __enter__(self): + return self + + def __exit__(self, *ignore_exc): + sys.path = self.original_object + sys.path[:] = self.original_value diff --git a/tests/test_build_status.py b/tests/test_build_status.py new file mode 100644 index 000000000..ce8005242 --- /dev/null +++ b/tests/test_build_status.py @@ -0,0 +1,26 @@ +import unittest +import support + +from urllib3 import PoolManager + +with support.import_scripts(): + import build_status + + +class testBuildStatus(unittest.TestCase): + def test_get_languages(self): + result = { + language: in_switcher + for language, in_switcher in build_status.get_languages(PoolManager()) + } + + self.assertIn('en', result) + self.assertIn('pl', result) + self.assertIn('zh-cn', result) + + self.assertEqual(result.get('en'), True) + self.assertEqual(result.get('pl'), True) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_translators.py b/tests/test_translators.py new file mode 100644 index 000000000..97950b657 --- /dev/null +++ b/tests/test_translators.py @@ -0,0 +1,97 @@ +import unittest +import support +import tempfile +import shutil + +from pathlib import Path +from git import Repo + +with support.import_scripts(): + import translators + +TRANSLATORS_BASIC = """ +John Cleese +Graham Chapman +Terry Jones +Michael Palin +Terry Gilliam +""" + +TRANSLATORS_TITLE_AND_COMMENTS = """ +Translators +John Cleese +Graham Chapman +Terry Jones +# Also Translators +Michael Palin +Terry Gilliam +""" + +TRANSLATORS_COMPLEX = """ +Translators +John Cleese +----- + + +Graham Chapman +Graham Chapman +# I'm a line +Terry Jones +# # # +- +Michael Palin +Terry Gilliam +""" + + +class testTranslators(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.tempdir = tempfile.mkdtemp() + cls.test_repo_path = Path(cls.tempdir) / 'python-docs-pl' + cls.test_repo = Repo.clone_from( + 'https://github.com/python/python-docs-pl', + cls.test_repo_path, + branch='3.14', + ) + cls.test_repo.git.checkout('d242ceee48cfa6d70dbc0bc75f5043d1e6c5efeb') + + @classmethod + def tearDownClass(cls): + shutil.rmtree(cls.tempdir) + + def test_yield_from_headers(self): + translators_list = set(translators.yield_from_headers(self.test_repo_path)) + self.assertEqual(len(translators_list), 25) + + def test_get_number_from_git_history(self): + # [" 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'] + self.assertEqual( + translators.get_number_from_git_history(self.test_repo_path), 9 + ) + + def test_get_translators_from_file(self): + for i, file in enumerate( + (TRANSLATORS_BASIC, TRANSLATORS_TITLE_AND_COMMENTS, TRANSLATORS_COMPLEX) + ): + with self.subTest(i=i, file=file): + folder = Path(self.tempdir) / f'folder-{i}' + folder.mkdir() + (folder / 'TRANSLATORS').write_text(file) + + result = translators.get_from_translators_file(folder) + self.assertEqual(len(result), 5) + + # No TRANSLATORS file + result = translators.get_from_translators_file(Path(self.tempdir) / 'empty') + self.assertEqual(result, set()) + + def test_get_number(self): + (self.test_repo_path / 'TRANSLATORS').write_text(TRANSLATORS_COMPLEX) + + result = translators.get_number(self.test_repo_path) + self.assertEqual(result, 25) + + +if __name__ == '__main__': + unittest.main() diff --git a/translators.py b/translators.py index aaae2399f..b6f784f27 100644 --- a/translators.py +++ b/translators.py @@ -1,7 +1,6 @@ from collections.abc import Iterator from pathlib import Path from re import fullmatch -from tempfile import TemporaryDirectory from typing import Literal from git import Repo @@ -36,10 +35,10 @@ def yield_from_headers(path: Path) -> Iterator[str]: yield translator -def get_from_translators_file(path: Path) -> list[str]: +def get_from_translators_file(path: Path) -> set[str]: if not (file := path.joinpath('TRANSLATORS')).exists(): - return [] - return list( + return set() + return set( line for line in file.read_text().splitlines() if line != 'Translators' @@ -53,27 +52,3 @@ def get_link(clone_path: Path, repo: str, branch: str) -> str | Literal[False]: clone_path.joinpath('TRANSLATORS').exists() and f'https://github.com/{repo}/blob/{branch}/TRANSLATORS' ) - - -if __name__ == '__main__': - for lang, branch in ( - ('es', '3.13'), - ('hu', '3.6'), - ('pl', '3.13'), - ('zh-tw', '3.13'), - ('id', '3.9'), - ('fr', '3.13'), - ('hu', '3.6'), - ): - with TemporaryDirectory() as directory: - Repo.clone_from( - f'https://github.com/python/python-docs-{lang}', - directory, - branch=branch, - ) - from_headers = len(set(yield_from_headers(path := Path(directory)))) - from_git_history = get_number_from_git_history(path) - from_translators_file = len(get_from_translators_file(path)) - print( - f'{lang}: {from_headers=}, {from_git_history=}, {from_translators_file=}' - ) From 25ca7e85a03b80b116bc64107a2c776273efaa69 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Thu, 31 Jul 2025 18:51:55 +0200 Subject: [PATCH 2/4] Fixup workflow --- .github/workflows/run-tests.yml | 1 + tests/support.py | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index b34d0e971..0c968d339 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -15,4 +15,5 @@ jobs: - uses: actions/setup-python@v5 with: python-version: "3.x" + - run: pip install -r requirements.txt - run: python -m unittest discover -s tests diff --git a/tests/support.py b/tests/support.py index 0841ce479..2225dac1f 100644 --- a/tests/support.py +++ b/tests/support.py @@ -3,7 +3,6 @@ # -------------------------------- Imports ----------------------------------- # - # Support functions borrowed from CPython's test.support @contextlib.contextmanager def import_scripts(dir='..'): From 399250b1920ddc90028bf246df8b5064e4a30ee1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 31 Jul 2025 16:52:09 +0000 Subject: [PATCH 3/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/support.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/support.py b/tests/support.py index 2225dac1f..0841ce479 100644 --- a/tests/support.py +++ b/tests/support.py @@ -3,6 +3,7 @@ # -------------------------------- Imports ----------------------------------- # + # Support functions borrowed from CPython's test.support @contextlib.contextmanager def import_scripts(dir='..'): From 2688a6848e65248e198272e114b8f758f93a5c6f Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Sat, 9 Aug 2025 18:33:49 +0200 Subject: [PATCH 4/4] Commit --- tests/test_build_status.py | 9 ++-- tests/test_translators.py | 97 -------------------------------------- 2 files changed, 5 insertions(+), 101 deletions(-) delete mode 100644 tests/test_translators.py diff --git a/tests/test_build_status.py b/tests/test_build_status.py index ce8005242..b7106e8ba 100644 --- a/tests/test_build_status.py +++ b/tests/test_build_status.py @@ -10,16 +10,17 @@ class testBuildStatus(unittest.TestCase): def test_get_languages(self): result = { - language: in_switcher - for language, in_switcher in build_status.get_languages(PoolManager()) + language: translated_name + for language, translated_name in build_status.get_languages(PoolManager()) } self.assertIn('en', result) self.assertIn('pl', result) self.assertIn('zh-cn', result) - self.assertEqual(result.get('en'), True) - self.assertEqual(result.get('pl'), True) + self.assertEqual(result.get('en'), None) + self.assertEqual(result.get('pl'), 'polski') + self.assertEqual(result.get('zh-cn'), '简体中文') if __name__ == '__main__': diff --git a/tests/test_translators.py b/tests/test_translators.py deleted file mode 100644 index 97950b657..000000000 --- a/tests/test_translators.py +++ /dev/null @@ -1,97 +0,0 @@ -import unittest -import support -import tempfile -import shutil - -from pathlib import Path -from git import Repo - -with support.import_scripts(): - import translators - -TRANSLATORS_BASIC = """ -John Cleese -Graham Chapman -Terry Jones -Michael Palin -Terry Gilliam -""" - -TRANSLATORS_TITLE_AND_COMMENTS = """ -Translators -John Cleese -Graham Chapman -Terry Jones -# Also Translators -Michael Palin -Terry Gilliam -""" - -TRANSLATORS_COMPLEX = """ -Translators -John Cleese ------ - - -Graham Chapman -Graham Chapman -# I'm a line -Terry Jones -# # # -- -Michael Palin -Terry Gilliam -""" - - -class testTranslators(unittest.TestCase): - @classmethod - def setUpClass(cls): - cls.tempdir = tempfile.mkdtemp() - cls.test_repo_path = Path(cls.tempdir) / 'python-docs-pl' - cls.test_repo = Repo.clone_from( - 'https://github.com/python/python-docs-pl', - cls.test_repo_path, - branch='3.14', - ) - cls.test_repo.git.checkout('d242ceee48cfa6d70dbc0bc75f5043d1e6c5efeb') - - @classmethod - def tearDownClass(cls): - shutil.rmtree(cls.tempdir) - - def test_yield_from_headers(self): - translators_list = set(translators.yield_from_headers(self.test_repo_path)) - self.assertEqual(len(translators_list), 25) - - def test_get_number_from_git_history(self): - # [" 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'] - self.assertEqual( - translators.get_number_from_git_history(self.test_repo_path), 9 - ) - - def test_get_translators_from_file(self): - for i, file in enumerate( - (TRANSLATORS_BASIC, TRANSLATORS_TITLE_AND_COMMENTS, TRANSLATORS_COMPLEX) - ): - with self.subTest(i=i, file=file): - folder = Path(self.tempdir) / f'folder-{i}' - folder.mkdir() - (folder / 'TRANSLATORS').write_text(file) - - result = translators.get_from_translators_file(folder) - self.assertEqual(len(result), 5) - - # No TRANSLATORS file - result = translators.get_from_translators_file(Path(self.tempdir) / 'empty') - self.assertEqual(result, set()) - - def test_get_number(self): - (self.test_repo_path / 'TRANSLATORS').write_text(TRANSLATORS_COMPLEX) - - result = translators.get_number(self.test_repo_path) - self.assertEqual(result, 25) - - -if __name__ == '__main__': - unittest.main()