Skip to content

Commit 1f24d24

Browse files
fix(localize): Allow empty xgettext extraction (#6)
1 parent c049430 commit 1f24d24

2 files changed

Lines changed: 67 additions & 5 deletions

File tree

scripts/localize.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,10 @@ def x_extract(context: LocaleContext):
509509
]
510510

511511
run_command(command=command, root_dir=context.root_dir)
512+
if not os.path.exists(pot_filepath):
513+
print(f'No gettext messages found; {pot_filepath} was not generated.')
514+
return
515+
512516
rewrite_pot_header(context=context, pot_filepath=pot_filepath)
513517

514518

tests/unit/test_localize.py

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,25 @@
44
import datetime
55
import os
66

7+
# lib imports
8+
import pytest
9+
710
# local imports
811
import scripts.localize as localize
912

1013

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+
1126
def parse_args(*args):
1227
"""Parse locale helper arguments for tests.
1328
@@ -362,13 +377,9 @@ def test_collect_source_files_scans_existing_source_directories(tmp_path):
362377
]
363378

364379

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):
366381
"""Verify xgettext extraction builds command arguments and rewrites headers."""
367382

368-
monkeypatch.delenv('GITHUB_REPOSITORY', raising=False)
369-
monkeypatch.delenv('GITHUB_REPOSITORY_OWNER', raising=False)
370-
monkeypatch.delenv('GITHUB_SERVER_URL', raising=False)
371-
372383
root_dir = str(tmp_path)
373384
os.makedirs(os.path.join(root_dir, 'src', 'nested'))
374385
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):
426437
)
427438

428439

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+
429487
def test_x_extract_requires_source_files(tmp_path):
430488
"""Verify extraction fails clearly when no source files are found."""
431489

0 commit comments

Comments
 (0)