Skip to content

Commit 3b2927f

Browse files
committed
Write generated files as UTF-8 with LF newlines on every platform
The test fixture wrote its throwaway repo tree with the platform default encoding, which cannot represent the Japanese fixtures on Windows; write it (and the tool's own generated pages and state) explicitly as UTF-8 with LF newlines so the output is identical everywhere.
1 parent 5150037 commit 3b2927f

2 files changed

Lines changed: 26 additions & 19 deletions

File tree

scripts/docs/translations.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ def load_state(code: str) -> dict[str, Any]:
730730

731731
def save_state(code: str, state: dict[str, Any]) -> None:
732732
text = json.dumps(state, ensure_ascii=False, indent=2, sort_keys=True) + "\n"
733-
(I18N / code / "state.json").write_text(text, encoding="utf-8")
733+
(I18N / code / "state.json").write_text(text, encoding="utf-8", newline="")
734734

735735

736736
def page_status(page: str, code: str, state: dict[str, Any], inputs: Inputs) -> str:
@@ -802,7 +802,7 @@ def stage(language: Language, site_url: str, registry: Registry) -> Path:
802802
"translations_url": relative(page, "translations.md"),
803803
}
804804
banner = "\n\n".join(banners[kind].strip().format(**variables) for kind in kinds)
805-
(target / page).write_text(inject_banner(body, banner), encoding="utf-8")
805+
(target / page).write_text(inject_banner(body, banner), encoding="utf-8", newline="")
806806
return target
807807

808808

@@ -952,7 +952,7 @@ def command_translate(registry: Registry, args: argparse.Namespace) -> int:
952952
print("\n".join(f" {problem}" for problem in problems), file=sys.stderr)
953953
continue
954954
target.parent.mkdir(parents=True, exist_ok=True)
955-
target.write_text(text, encoding="utf-8")
955+
target.write_text(text, encoding="utf-8", newline="")
956956
state["pages"][page] = {"source_hash": sha256(english), "block_hashes": block_hashes(source), **stamp}
957957
save_state(language.code, state)
958958
print(f"{'unchanged' if text == previous else 'translated'}: {page}")

tests/test_docs_i18n.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -176,28 +176,35 @@ def test_a_page_the_reviewer_still_flags_after_the_correction_round_is_not_publi
176176
assert "words the translation never contained" not in model.requests[2]
177177

178178

179+
def write(path: Path, text: str) -> None:
180+
"""Write a repo file the way the tool reads it: UTF-8 with `\\n` line endings, on every platform."""
181+
path.parent.mkdir(parents=True, exist_ok=True)
182+
path.write_text(text, encoding="utf-8", newline="")
183+
184+
179185
@pytest.fixture
180186
def docs_repo(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
181187
"""A repository tree with two English pages, one Japanese translation and the ja UI strings."""
182-
(tmp_path / "docs").mkdir()
183-
(tmp_path / "docs" / "index.md").write_text("# Home\n\nSee [tools](tools.md) and the [API](api/mcp/index.md).\n")
184-
(tmp_path / "docs" / "tools.md").write_text("# Tools\n\nEnglish body.\n")
185-
(tmp_path / "docs" / "translations.md").write_text("# Translations\n")
186-
(tmp_path / "mkdocs.yml").write_text(
188+
write(tmp_path / "docs" / "index.md", "# Home\n\nSee [tools](tools.md) and the [API](api/mcp/index.md).\n")
189+
write(tmp_path / "docs" / "tools.md", "# Tools\n\nEnglish body.\n")
190+
write(tmp_path / "docs" / "translations.md", "# Translations\n")
191+
write(
192+
tmp_path / "mkdocs.yml",
187193
"site_url: https://docs.example/\nnav:\n - Home: index.md\n - Tools: tools.md\n"
188-
" - Translations: translations.md\n - API Reference: api/\n"
194+
" - Translations: translations.md\n - API Reference: api/\n",
189195
)
190-
(tmp_path / "i18n" / "ja" / "pages").mkdir(parents=True)
191-
(tmp_path / "i18n" / "languages.yml").write_text(
196+
write(
197+
tmp_path / "i18n" / "languages.yml",
192198
"languages:\n - {code: ja, name: 日本語}\nexclude_pages: [translations.md]\n"
193199
"models: {translate: t, verify: v}\nbanners:\n disclosure: 'MT: [English]({english_url})'\n"
194-
" outdated: 'behind'\n untranslated: 'English shown, see [how]({translations_url})'\n"
200+
" outdated: 'behind'\n untranslated: 'English shown, see [how]({translations_url})'\n",
195201
)
196-
(tmp_path / "i18n" / "general-prompt.md").write_text("rules")
197-
(tmp_path / "i18n" / "ja" / "instructions.md").write_text("instructions")
198-
(tmp_path / "i18n" / "ja" / "glossary.json").write_text(json.dumps(GLOSSARY))
199-
(tmp_path / "i18n" / "ja" / "pages" / "index.md").write_text(
200-
"# ホーム {#home}\n\n[ツール](tools.md) と [API](api/mcp/index.md) を参照。\n"
202+
write(tmp_path / "i18n" / "general-prompt.md", "rules")
203+
write(tmp_path / "i18n" / "ja" / "instructions.md", "instructions")
204+
write(tmp_path / "i18n" / "ja" / "glossary.json", json.dumps(GLOSSARY))
205+
write(
206+
tmp_path / "i18n" / "ja" / "pages" / "index.md",
207+
"# ホーム {#home}\n\n[ツール](tools.md) と [API](api/mcp/index.md) を参照。\n",
201208
)
202209
for name, path in (("ROOT", tmp_path), ("DOCS", tmp_path / "docs"), ("I18N", tmp_path / "i18n")):
203210
monkeypatch.setattr(i18n, name, path)
@@ -216,7 +223,7 @@ def docs_repo(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
216223
"model": "t",
217224
"translated_at": "now",
218225
}
219-
(tmp_path / "i18n" / "ja" / "state.json").write_text(json.dumps({"pages": {"index.md": page_record}, "ui": ui}))
226+
write(tmp_path / "i18n" / "ja" / "state.json", json.dumps({"pages": {"index.md": page_record}, "ui": ui}))
220227
return tmp_path
221228

222229

@@ -255,7 +262,7 @@ def test_status_marks_pages_current_outdated_and_missing_from_content_and_input_
255262
) -> None:
256263
"""SDK-defined: a page is current only while both the English text and the prompt inputs it was translated from
257264
are unchanged; editing the English makes it outdated, and a page with no translation is missing."""
258-
(docs_repo / "docs" / "index.md").write_text("# Home\n\nThe English changed since translation.\n")
265+
write(docs_repo / "docs" / "index.md", "# Home\n\nThe English changed since translation.\n")
259266

260267
exit_code = i18n.command_status(i18n.load_registry(), None)
261268

0 commit comments

Comments
 (0)