jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1332465?usp=email )
Change subject: make_i18n_dict: Fix pathlib conversion regressions ......................................................................
make_i18n_dict: Fix pathlib conversion regressions
Create parent directories for a new i18n output tree and deserialize existing JSON text with json.loads(). This restores the behavior from before the pathlib conversion.
Change-Id: I650f508e9f67587264332214f3fb04c471e02f34 --- M scripts/maintenance/make_i18n_dict.py A tests/make_i18n_dict_tests.py 2 files changed, 56 insertions(+), 2 deletions(-)
Approvals: jenkins-bot: Verified Xqt: Looks good to me, approved
diff --git a/scripts/maintenance/make_i18n_dict.py b/scripts/maintenance/make_i18n_dict.py index 701caba..65877ee 100755 --- a/scripts/maintenance/make_i18n_dict.py +++ b/scripts/maintenance/make_i18n_dict.py @@ -132,14 +132,14 @@ if not self.dict: self.run(quiet) json_dir = Path(config.base_dir, 'scripts/i18n', self.scriptname) - json_dir.mkdir(exist_ok=True) + json_dir.mkdir(parents=True, exist_ok=True)
for lang in self.dict: new_dict = {}
file_path = json_dir / f'{lang}.json' if file_path.is_file(): - new_dict = json.load(file_path.read_text(encoding='utf-8')) + new_dict = json.loads(file_path.read_text(encoding='utf-8'))
new_dict['@metadata'] = new_dict.get('@metadata', {'authors': []}) new_dict.update(self.dict[lang]) diff --git a/tests/make_i18n_dict_tests.py b/tests/make_i18n_dict_tests.py new file mode 100755 index 0000000..204ef6f --- /dev/null +++ b/tests/make_i18n_dict_tests.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python3 +# +# (C) Pywikibot team, 2026 +# +# Distributed under the terms of the MIT license. +# +"""Tests for the make_i18n_dict maintenance script.""" +from __future__ import annotations + +import json +import unittest +from pathlib import Path +from tempfile import TemporaryDirectory +from unittest.mock import patch + +from scripts.maintenance import make_i18n_dict +from tests.aspects import TestCase + + +class MakeI18nDictTestCase(TestCase): + + """Test :class:`make_i18n_dict.i18nBot`.""" + + net = False + + def test_to_json_creates_and_updates_file(self) -> None: + """Test creating directories and updating an existing JSON file.""" + bot = object.__new__(make_i18n_dict.i18nBot) + bot.scriptname = 'sample' + bot.dict = {'en': {'sample-first': 'first'}} + + with TemporaryDirectory() as directory: + with patch.object(make_i18n_dict.config, 'base_dir', directory): + bot.to_json() + + file_path = Path(directory, 'scripts/i18n/sample/en.json') + data = json.loads(file_path.read_text(encoding='utf-8')) + data['@metadata']['authors'].extend(['mahveotm', 'xqt']) + file_path.write_text(json.dumps(data), encoding='utf-8') + + bot.dict = {'en': {'sample-second': 'second'}} + bot.to_json() + + data = json.loads(file_path.read_text(encoding='utf-8')) + + self.assertEqual(data, { + '@metadata': {'authors': ['mahveotm', 'xqt']}, + 'sample-first': 'first', + 'sample-second': 'second', + }) + + +if __name__ == '__main__': + unittest.main()
pywikibot-commits@lists.wikimedia.org