|
5 | 5 | import sys |
6 | 6 | import tempfile |
7 | 7 | import unittest |
| 8 | +from unittest import mock |
| 9 | +from urllib.error import HTTPError, URLError |
8 | 10 |
|
9 | 11 | sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts")) |
10 | 12 | import translations |
@@ -191,6 +193,105 @@ def test_incomplete_download_does_not_change_repository(self): |
191 | 193 | self.assertEqual(config_before, (self.destination / ".tx/config").read_bytes()) |
192 | 194 | self.assertFalse((self.destination / "builtins/functions.po").exists()) |
193 | 195 |
|
| 196 | + def test_sync_skips_unregistered_resource_and_picks_it_up_later(self): |
| 197 | + catalogs = ["bugs.po", "c-api/pyexpat.po"] |
| 198 | + write_config(self.source, catalogs) |
| 199 | + download = self.source / "ja/LC_MESSAGES" |
| 200 | + write_po(download, "bugs.po", "新しい訳") |
| 201 | + obsolete = write_po(self.destination, "removed.po", "古い訳") |
| 202 | + self.track() |
| 203 | + |
| 204 | + with mock.patch.object(translations, "resource_exists", return_value=False) as exists: |
| 205 | + translations.sync(self.source, self.destination, "ja", |
| 206 | + skip_missing_resources=True) |
| 207 | + exists.assert_called_once_with("resource1") |
| 208 | + self.assertFalse(obsolete.exists()) |
| 209 | + self.assertFalse((self.destination / "c-api/pyexpat.po").exists()) |
| 210 | + self.assertEqual(translations.active_catalogs(self.destination / ".tx/config"), |
| 211 | + [Path("bugs.po")]) |
| 212 | + translations.require_catalogs( |
| 213 | + self.destination, translations.active_catalogs(self.destination / ".tx/config") |
| 214 | + ) |
| 215 | + # The generated source config remains complete for subsequent runs. |
| 216 | + self.assertEqual(translations.active_catalogs(self.source / ".tx/config"), |
| 217 | + [Path(path) for path in catalogs]) |
| 218 | + write_po(download, "c-api/pyexpat.po", "追加された訳") |
| 219 | + with mock.patch.object(translations, "resource_exists") as exists: |
| 220 | + translations.sync(self.source, self.destination, "ja", |
| 221 | + skip_missing_resources=True) |
| 222 | + exists.assert_not_called() |
| 223 | + self.assertEqual((download / "c-api/pyexpat.po").read_bytes(), |
| 224 | + (self.destination / "c-api/pyexpat.po").read_bytes()) |
| 225 | + |
| 226 | + def test_sync_preserves_existing_po_for_unavailable_resource(self): |
| 227 | + catalogs = ["bugs.po", "c-api/pyexpat.po"] |
| 228 | + write_config(self.source, catalogs) |
| 229 | + write_po(self.source / "ja/LC_MESSAGES", "bugs.po", "新しい訳") |
| 230 | + old = write_po(self.destination, "c-api/pyexpat.po", "既存の訳") |
| 231 | + self.track() |
| 232 | + before = old.read_bytes() |
| 233 | + with mock.patch.object(translations, "resource_exists", return_value=False): |
| 234 | + translations.sync(self.source, self.destination, "ja", |
| 235 | + skip_missing_resources=True) |
| 236 | + self.assertEqual(old.read_bytes(), before) |
| 237 | + self.assertEqual(translations.active_catalogs(self.destination / ".tx/config"), |
| 238 | + [Path(path) for path in catalogs]) |
| 239 | + |
| 240 | + def test_skip_does_not_hide_incomplete_download_or_api_failure(self): |
| 241 | + write_config(self.source, ["bugs.po", "new.po", "missing.po"]) |
| 242 | + write_po(self.source / "ja/LC_MESSAGES", "bugs.po", "新しい訳") |
| 243 | + write_config(self.destination, ["old.po"]) |
| 244 | + old = write_po(self.destination, "old.po", "既存の訳") |
| 245 | + self.track() |
| 246 | + before = old.read_bytes() |
| 247 | + config_before = (self.destination / ".tx/config").read_bytes() |
| 248 | + for outcome, error in [(True, FileNotFoundError), |
| 249 | + (URLError("offline"), URLError)]: |
| 250 | + with self.subTest(outcome=outcome): |
| 251 | + with mock.patch.object(translations, "resource_exists", |
| 252 | + side_effect=[False, outcome]): |
| 253 | + with self.assertRaises(error): |
| 254 | + translations.sync(self.source, self.destination, "ja", |
| 255 | + skip_missing_resources=True) |
| 256 | + self.assertEqual(old.read_bytes(), before) |
| 257 | + self.assertEqual((self.destination / ".tx/config").read_bytes(), |
| 258 | + config_before) |
| 259 | + self.assertFalse((self.destination / "bugs.po").exists()) |
| 260 | + |
| 261 | + def test_skip_refuses_empty_download(self): |
| 262 | + write_config(self.source, ["new.po"]) |
| 263 | + old = write_po(self.destination, "old.po", "既存の訳") |
| 264 | + self.track() |
| 265 | + with mock.patch.object(translations, "resource_exists", return_value=False): |
| 266 | + with self.assertRaisesRegex(ValueError, "No PO catalogs downloaded"): |
| 267 | + translations.sync(self.source, self.destination, "ja", |
| 268 | + skip_missing_resources=True) |
| 269 | + self.assertTrue(old.exists()) |
| 270 | + self.assertFalse((self.destination / ".tx/config").exists()) |
| 271 | + |
| 272 | + def test_resource_exists_only_skips_404(self): |
| 273 | + resource = "o:python-doc:p:python-newest:r:c-api--pyexpat" |
| 274 | + with mock.patch.dict(translations.os.environ, {"TX_TOKEN": "test-token"}): |
| 275 | + with mock.patch.object(translations, "urlopen") as open_url: |
| 276 | + self.assertTrue(translations.resource_exists(resource)) |
| 277 | + request = open_url.call_args.args[0] |
| 278 | + self.assertEqual(request.full_url, |
| 279 | + "https://rest.api.transifex.com/resources/" + resource) |
| 280 | + self.assertEqual(request.get_header("Authorization"), "Bearer test-token") |
| 281 | + self.assertEqual(open_url.call_args.kwargs, {"timeout": 30}) |
| 282 | + for code in [404, 401, 403, 429, 500]: |
| 283 | + with self.subTest(code=code): |
| 284 | + error = HTTPError("https://rest.api.transifex.com/", code, "error", {}, None) |
| 285 | + with mock.patch.object(translations, "urlopen", side_effect=error): |
| 286 | + if code == 404: |
| 287 | + self.assertFalse(translations.resource_exists(resource)) |
| 288 | + else: |
| 289 | + with self.assertRaises(HTTPError): |
| 290 | + translations.resource_exists(resource) |
| 291 | + with mock.patch.object(translations, "urlopen", side_effect=TimeoutError): |
| 292 | + with self.assertRaises(TimeoutError): |
| 293 | + translations.resource_exists(resource) |
| 294 | + |
194 | 295 | def test_empty_config_does_not_delete_catalogs(self): |
195 | 296 | write_config(self.source, []) |
196 | 297 | old = write_po(self.destination, "library/functions.po", "古い訳") |
|
0 commit comments