From ceb074a8f94e4a0d33bdab3ba54803488c0ce682 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Mon, 27 May 2019 12:30:01 +0300 Subject: pybabel compile: exit with code 1 if errors were encountered Fixes #627 --- .gitignore | 3 ++- babel/messages/frontend.py | 14 ++++++++++++-- .../data/project/i18n/fi_BUGGY/LC_MESSAGES/messages.po | 5 +++++ tests/messages/test_frontend.py | 9 +++++++++ 4 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 tests/messages/data/project/i18n/fi_BUGGY/LC_MESSAGES/messages.po diff --git a/.gitignore b/.gitignore index 50e5838..2886dec 100644 --- a/.gitignore +++ b/.gitignore @@ -19,4 +19,5 @@ docs/_build test-env tests/messages/data/project/i18n/en_US tests/messages/data/project/i18n/long_messages.pot -tests/messages/data/project/i18n/temp* \ No newline at end of file +tests/messages/data/project/i18n/temp* +tests/messages/data/project/i18n/fi_BUGGY/LC_MESSAGES/*.mo diff --git a/babel/messages/frontend.py b/babel/messages/frontend.py index f673f90..9e7c68c 100644 --- a/babel/messages/frontend.py +++ b/babel/messages/frontend.py @@ -182,8 +182,13 @@ class compile_catalog(Command): 'or the base directory') def run(self): + n_errors = 0 for domain in self.domain: - self._run_domain(domain) + for catalog, errors in self._run_domain(domain).items(): + n_errors += len(errors) + if n_errors: + self.log.error('%d errors encountered.' % n_errors) + return (1 if n_errors else 0) def _run_domain(self, domain): po_files = [] @@ -219,6 +224,8 @@ class compile_catalog(Command): if not po_files: raise DistutilsOptionError('no message catalogs found') + catalogs_and_errors = {} + for idx, (locale, po_file) in enumerate(po_files): mo_file = mo_files[idx] with open(po_file, 'rb') as infile: @@ -241,7 +248,8 @@ class compile_catalog(Command): self.log.info('catalog %s is marked as fuzzy, skipping', po_file) continue - for message, errors in catalog.check(): + catalogs_and_errors[catalog] = catalog_errors = list(catalog.check()) + for message, errors in catalog_errors: for error in errors: self.log.error( 'error: %s:%d: %s', po_file, message.lineno, error @@ -252,6 +260,8 @@ class compile_catalog(Command): with open(mo_file, 'wb') as outfile: write_mo(outfile, catalog, use_fuzzy=self.use_fuzzy) + return catalogs_and_errors + class extract_messages(Command): """Message extraction command for use in ``setup.py`` scripts. diff --git a/tests/messages/data/project/i18n/fi_BUGGY/LC_MESSAGES/messages.po b/tests/messages/data/project/i18n/fi_BUGGY/LC_MESSAGES/messages.po new file mode 100644 index 0000000..0a0745b --- /dev/null +++ b/tests/messages/data/project/i18n/fi_BUGGY/LC_MESSAGES/messages.po @@ -0,0 +1,5 @@ +msgid "" +msgstr "" + +msgid "bar %(sign)s" +msgstr "tanko %(merkki)s" diff --git a/tests/messages/test_frontend.py b/tests/messages/test_frontend.py index fa0112c..deaa660 100644 --- a/tests/messages/test_frontend.py +++ b/tests/messages/test_frontend.py @@ -1383,3 +1383,12 @@ def test_extract_add_location(): assert isinstance(cmdinst, extract_messages) assert cmdinst.add_location == 'never' assert cmdinst.no_location + + +def test_extract_error_code(monkeypatch, capsys): + monkeypatch.chdir(project_dir) + cmdinst = configure_cli_command("compile --domain=messages --directory i18n --locale fi_BUGGY") + assert cmdinst.run() == 1 + out, err = capsys.readouterr() + # replace hack below for py2/py3 compatibility + assert "unknown named placeholder 'merkki'" in err.replace("u'", "'") -- cgit v1.2.1