diff --git a/scripts/test-website.py b/scripts/test-website.py index 9cedd6a09..f3206a68d 100644 --- a/scripts/test-website.py +++ b/scripts/test-website.py @@ -2,7 +2,9 @@ import contextlib import importlib.util +from html import escape import io +import json import re from pathlib import Path import shutil @@ -245,13 +247,22 @@ class WebsiteMaintenanceChecks(unittest.TestCase): def site_copy(directory): - """Copy the authored website inputs into a temporary repository root.""" + """Copy the authored website inputs without existing translated pages.""" root = Path(directory) for name in ("public", "templates", "locales"): shutil.copytree(ROOT / "website" / name, root / "website" / name) + for code in WEBSITE.LANGUAGES: + if code != "en": + shutil.rmtree(root / "website/public" / code, ignore_errors=True) return root +def zh_hant(key): + """Return one zh-Hant UI string, so assertions follow the locale file.""" + strings = json.loads((ROOT / "website/locales/zh-hant.json").read_text())["strings"] + return escape(strings[key]) + + def quietly(function, *arguments): """Run a command helper without its progress output.""" with contextlib.redirect_stdout(io.StringIO()): @@ -291,14 +302,15 @@ class WebsiteTranslationChecks(unittest.TestCase): {"source-sha256": WEBSITE.source_digest(english), "status": "draft"}, ) self.assertIn('href="../../assets/styles.css"', source) - self.assertIn('', source) - self.assertIn('href="../../guides/javascript/"', source) self.assertIn('Get started', source + '' + f'{zh_hant("navGetStarted")}', + source, ) self.assertIn( - 'Showcases', + f'{zh_hant("navShowcases")}' + '', source, ) self.assertIn('
Restart your client when setup finishes.
" - changed = english_path.read_text().replace(text, "Restart your client after setup.
", 1) + # A text change marks the translation out of date. + unchanged = english_path.read_text() + changed = unchanged.replace(" ", "A new English paragraph.
\n ", 1) english_path.write_text(changed) self.assertEqual(quietly(WEBSITE.sync_pages, root, True), [page]) errors, warnings = translation_errors(root) self.assertEqual(errors, []) self.assertTrue(any("has changed since this translation" in message for _, message in warnings)) quietly(WEBSITE.sync_pages, root) + link = f'{zh_hant("staleNoticeLink")}' self.assertIn( - 'The English page has changed since this translation. ' - 'Read the English version.
', + f'{zh_hant("staleNotice").replace("{link}", link)}
', page.read_text(), ) report = quietly(WEBSITE.translation_report, root) self.assertIn(f' after updating the translation, record source-sha256="{WEBSITE.source_digest(changed)}"', report) - english_path.write_text(changed.replace("Restart your client after setup.
", text, 1)) + english_path.write_text(unchanged) quietly(WEBSITE.sync_pages, root) self.assertNotIn('class="translation-notice"', page.read_text()) - page.write_text(page.read_text().replace("rea-agents@latest", "rea-agents@1", 1)) - self.assertTrue(any("install-cli must match" in error for error in translation_errors(root)[0])) + page.write_text(block.sub(lambda match: match.group(1) + "edited" + match.group(3), page.read_text())) + self.assertTrue(any(f"{identifier} must match" in error for error in translation_errors(root)[0])) def test_translations_keep_structure_and_cjk_spacing(self): with tempfile.TemporaryDirectory() as directory: root = site_copy(directory) page = quietly(WEBSITE.new_translation, root, "zh-hant", "get-started") original = page.read_text() + first, second = WEBSITE.copy_targets(original)[:2] + + def added(paragraph): + return (" ", f" {paragraph}\n ") + cases = ( - ("Restart your client when setup finishes.
", "完成後\n 重新啟動。
", "renders as a gap"), - ('id="install-node"', 'id="node"', "element IDs differ"), - ('data-copy="install-cli"', 'data-copy="node-check"', "copy buttons differ"), + (*added("完成後\n 重新啟動。
"), "renders as a gap"), + ('\n', "", "draft translation needs"), ('status="draft"', 'status="final"', "status must be draft or reviewed"), ) @@ -423,9 +444,9 @@ class WebsiteTranslationChecks(unittest.TestCase): self.assertIn(old, original) page.write_text(original.replace(old, new, 1)) self.assertTrue(any(message in error for error in translation_errors(root)[0])) - page.write_text(original.replace("Restart your client when setup finishes.
", "重新啟動 REA 用戶端。
", 1)) + page.write_text(original.replace(*added("重新啟動 REA 用戶端。
"), 1)) self.assertEqual(translation_errors(root)[0], []) - page.write_text(original.replace("node -v", "node -V", 1))
+ page.write_text(original.replace(*added("extra-command