|
4 | 4 | import datetime |
5 | 5 | import os |
6 | 6 |
|
| 7 | +# lib imports |
| 8 | +import pytest |
| 9 | + |
7 | 10 | # local imports |
8 | 11 | import scripts.localize as localize |
9 | 12 |
|
10 | 13 |
|
| 14 | +@pytest.fixture |
| 15 | +def clean_github_environment(monkeypatch): |
| 16 | + """Remove GitHub runner variables that affect locale context defaults.""" |
| 17 | + |
| 18 | + for variable in [ |
| 19 | + 'GITHUB_REPOSITORY', |
| 20 | + 'GITHUB_REPOSITORY_OWNER', |
| 21 | + 'GITHUB_SERVER_URL', |
| 22 | + ]: |
| 23 | + monkeypatch.delenv(variable, raising=False) |
| 24 | + |
| 25 | + |
11 | 26 | def parse_args(*args): |
12 | 27 | """Parse locale helper arguments for tests. |
13 | 28 |
|
@@ -362,13 +377,9 @@ def test_collect_source_files_scans_existing_source_directories(tmp_path): |
362 | 377 | ] |
363 | 378 |
|
364 | 379 |
|
365 | | -def test_x_extract_builds_command_and_rewrites_header(monkeypatch, tmp_path): |
| 380 | +def test_x_extract_builds_command_and_rewrites_header(clean_github_environment, monkeypatch, tmp_path): |
366 | 381 | """Verify xgettext extraction builds command arguments and rewrites headers.""" |
367 | 382 |
|
368 | | - monkeypatch.delenv('GITHUB_REPOSITORY', raising=False) |
369 | | - monkeypatch.delenv('GITHUB_REPOSITORY_OWNER', raising=False) |
370 | | - monkeypatch.delenv('GITHUB_SERVER_URL', raising=False) |
371 | | - |
372 | 383 | root_dir = str(tmp_path) |
373 | 384 | os.makedirs(os.path.join(root_dir, 'src', 'nested')) |
374 | 385 | with open(os.path.join(root_dir, 'src', 'main.cpp'), mode='w', encoding='utf-8') as file: |
@@ -426,6 +437,53 @@ def fake_check_output(args, cwd): |
426 | 437 | ) |
427 | 438 |
|
428 | 439 |
|
| 440 | +def test_x_extract_allows_empty_extraction(clean_github_environment, monkeypatch, tmp_path): |
| 441 | + """Verify extraction succeeds when xgettext has no messages to write.""" |
| 442 | + |
| 443 | + root_dir = str(tmp_path) |
| 444 | + os.makedirs(os.path.join(root_dir, 'src')) |
| 445 | + with open(os.path.join(root_dir, 'src', 'main.cpp'), mode='w', encoding='utf-8') as file: |
| 446 | + file.write('int main() { return 0; }\n') |
| 447 | + |
| 448 | + context = localize.build_context(args=parse_args('--root-dir', root_dir, '--project-name', 'Example')) |
| 449 | + calls = [] |
| 450 | + |
| 451 | + def fake_check_output(args, cwd): |
| 452 | + """Record xgettext calls without creating a template file.""" |
| 453 | + |
| 454 | + calls.append({ |
| 455 | + 'args': args, |
| 456 | + 'cwd': cwd, |
| 457 | + }) |
| 458 | + |
| 459 | + monkeypatch.setattr(localize.subprocess, 'check_output', fake_check_output) |
| 460 | + |
| 461 | + localize.x_extract(context=context) |
| 462 | + |
| 463 | + assert calls == [ |
| 464 | + { |
| 465 | + 'args': [ |
| 466 | + 'xgettext', |
| 467 | + *[f'--keyword={keyword}' for keyword in localize.DEFAULT_KEYWORDS], |
| 468 | + '--default-domain=example', |
| 469 | + f'--output={os.path.join(context.locale_dir, "example.po")}', |
| 470 | + '--language=C++', |
| 471 | + '--boost', |
| 472 | + '--from-code=utf-8', |
| 473 | + '-F', |
| 474 | + '--msgid-bugs-address=https://github.com/Example/Example', |
| 475 | + '--copyright-holder=Example', |
| 476 | + '--package-name=Example', |
| 477 | + '--package-version=v0', |
| 478 | + os.path.join('src', 'main.cpp'), |
| 479 | + ], |
| 480 | + 'cwd': root_dir, |
| 481 | + }, |
| 482 | + ] |
| 483 | + assert os.path.isdir(context.locale_dir) |
| 484 | + assert not os.path.exists(os.path.join(context.locale_dir, 'example.po')) |
| 485 | + |
| 486 | + |
429 | 487 | def test_x_extract_requires_source_files(tmp_path): |
430 | 488 | """Verify extraction fails clearly when no source files are found.""" |
431 | 489 |
|
|
0 commit comments