diff --git a/scripts/tests/test_validate_translation_files.py b/scripts/tests/test_validate_translation_files.py index 821701d5231..233f6ab9e9e 100644 --- a/scripts/tests/test_validate_translation_files.py +++ b/scripts/tests/test_validate_translation_files.py @@ -3,10 +3,11 @@ """ import os.path +import re from ..validate_translation_files import ( get_translation_files, - main, + validate_translation_files, ) SCRIPT_DIR = os.path.dirname(__file__) @@ -35,7 +36,7 @@ def test_main_on_invalid_files(capsys): Integration test for the `main` function on some invalid files. """ mock_translations_dir = os.path.join(SCRIPT_DIR, 'mock_translations_dir') - exit_code = main(mock_translations_dir) + exit_code = validate_translation_files(mock_translations_dir) out, err = capsys.readouterr() assert 'VALID:' in out, 'Valid files should be printed in stdout' @@ -44,8 +45,7 @@ def test_main_on_invalid_files(capsys): assert 'hi/LC_MESSAGES/django.po' not in out, 'Invalid file should be printed in stderr' assert 'en/LC_MESSAGES/django.po' not in out, 'Source file should not be validated' - assert 'INVALID:' in err - assert 'hi/LC_MESSAGES/django.po' in err + assert re.match(r'INVALID: .*hi/LC_MESSAGES/django.po', err) assert '\'msgstr\' is not a valid Python brace format string, unlike \'msgid\'' in err assert 'FAILURE: Some translations are invalid.' in err @@ -57,7 +57,7 @@ def test_main_on_valid_files(capsys): Integration test for the `main` function but only for the Arabic translations which is valid. """ mock_translations_dir = os.path.join(SCRIPT_DIR, 'mock_translations_dir/demo-xblock/conf/locale/ar') - exit_code = main(mock_translations_dir) + exit_code = validate_translation_files(mock_translations_dir) out, err = capsys.readouterr() assert 'VALID:' in out, 'Valid files should be printed in stdout' diff --git a/scripts/validate_translation_files.py b/scripts/validate_translation_files.py index 8d334e26656..e9d98e4f660 100644 --- a/scripts/validate_translation_files.py +++ b/scripts/validate_translation_files.py @@ -1,7 +1,12 @@ -import sys +""" +Validate translation files using GNU gettext `msgfmt` command. +""" + +import argparse import os import os.path import subprocess +import sys def get_translation_files(translation_directory): @@ -39,7 +44,9 @@ def validate_translation_file(po_file): } -def main(translations_dir='translations'): +def validate_translation_files( + translations_dir='translations', +): """ Run GNU gettext `msgfmt` and print errors to stderr. @@ -81,5 +88,14 @@ def main(translations_dir='translations'): return exit_code +def main(): # pragma: no cover + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--dir', action='store', type=str, default='translations') + args = parser.parse_args() + sys.exit(validate_translation_files( + translations_dir=args.dir, + )) + + if __name__ == '__main__': - sys.exit(main()) # pragma: no cover + main() # pragma: no cover