From e3305bf51317614b386c95da4c468e1581191d29 Mon Sep 17 00:00:00 2001 From: Andreas Jaeger Date: Sun, 24 Apr 2016 10:40:47 +0200 Subject: [PATCH] Babel Integration tests With Babel 2.2 and then with Babel 2.3.2 and 2.3.3 releases, the OpenStack infra scripts broke and uncovered bugs in the upstream Babel release. The infra scripts run in post and periodic queues, so failures here are not directly visible. With Babel 2.3.x invalid strings were send to the translation server, and we had to recover old content to fix this. Let's add some Babel integration tests that can be run whenever the upper-constraints file changes so that any new Babel release will be at least minimally tested. The shell script babel-test.sh runs a simple extraction of strings to translate from a simple input file. It uses the same commands that the infra scripts run and handles the two different cases of extraction of the normal strings and extraction of the non-standard log-level strings. The script then compares the new content with previously created good content. Once this change is in, we will run this test whenever upper-constraints.txt file changes. Needed-By: I15fa6a706323615d6072d8acaa859fea9bf35883 Change-Id: Iab7844ba748ecd84ec07ad4280b2317bd626fe9a --- babel-test/babel-input.py | 31 +++++++++++++++++++++++ babel-test/expected-log-error.pot | 28 +++++++++++++++++++++ babel-test/expected.pot | 29 +++++++++++++++++++++ tools/babel-test.sh | 42 +++++++++++++++++++++++++++++++ tox.ini | 6 +++++ 5 files changed, 136 insertions(+) create mode 100644 babel-test/babel-input.py create mode 100644 babel-test/expected-log-error.pot create mode 100644 babel-test/expected.pot create mode 100755 tools/babel-test.sh diff --git a/babel-test/babel-input.py b/babel-test/babel-input.py new file mode 100644 index 0000000000..ece057a3e0 --- /dev/null +++ b/babel-test/babel-input.py @@ -0,0 +1,31 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + + +"""Test input for Babel""" + + +from oslo.i18n import _ +from oslo.i18n import _LE +from oslo_log import log as logging + +LOG = logging.getLogger(__name__) + + +def just_testing(): + """Just some random commands for Babel to extract strings from""" + + LOG.exception(_LE("LE translated string1")) + LOG.exception(_LE("LE translated string2")) + print(_("Normal translated string1")) + # Translators: Comment for string2 + print(_("Normal translated string2")) diff --git a/babel-test/expected-log-error.pot b/babel-test/expected-log-error.pot new file mode 100644 index 0000000000..5a7395b7cf --- /dev/null +++ b/babel-test/expected-log-error.pot @@ -0,0 +1,28 @@ +# Translations template for requirements. +# Copyright (C) 2016 ORGANIZATION +# This file is distributed under the same license as the requirements +# project. +# FIRST AUTHOR , 2016. +# +#, fuzzy +msgid "" +msgstr "" +"Project-Id-Version: requirements 1\n" +"Report-Msgid-Bugs-To: https://bugs.launchpad.net/openstack-i18n/\n" +"POT-Creation-Date: 2016-04-24 09:28+0200\n" +"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" +"Last-Translator: FULL NAME \n" +"Language-Team: LANGUAGE \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=utf-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Generated-By: Babel 1.3\n" + +#: babel-test/babel-input.py:24 +msgid "LE translated string1" +msgstr "" + +#: babel-test/babel-input.py:25 +msgid "LE translated string2" +msgstr "" + diff --git a/babel-test/expected.pot b/babel-test/expected.pot new file mode 100644 index 0000000000..1b58faba2f --- /dev/null +++ b/babel-test/expected.pot @@ -0,0 +1,29 @@ +# Translations template for requirements. +# Copyright (C) 2016 ORGANIZATION +# This file is distributed under the same license as the requirements +# project. +# FIRST AUTHOR , 2016. +# +#, fuzzy +msgid "" +msgstr "" +"Project-Id-Version: requirements 1\n" +"Report-Msgid-Bugs-To: https://bugs.launchpad.net/openstack-i18n/\n" +"POT-Creation-Date: 2016-04-24 09:28+0200\n" +"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" +"Last-Translator: FULL NAME \n" +"Language-Team: LANGUAGE \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=utf-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Generated-By: Babel 1.3\n" + +#: babel-test/babel-input.py:26 +msgid "Normal translated string1" +msgstr "" + +#. Translators: Comment for string2 +#: babel-test/babel-input.py:28 +msgid "Normal translated string2" +msgstr "" + diff --git a/tools/babel-test.sh b/tools/babel-test.sh new file mode 100755 index 0000000000..45eccfe53e --- /dev/null +++ b/tools/babel-test.sh @@ -0,0 +1,42 @@ +#!/bin/bash -xe + +pybabel extract \ + --add-comments Translators: \ + --msgid-bugs-address="https://bugs.launchpad.net/openstack-i18n/" \ + --project=requirements --version=1 \ + -k "_C:1c,2" -k "_P:1,2" \ + -o babel-test/test.pot babel-test + +pybabel extract --no-default-keywords \ + --add-comments Translators: \ + --msgid-bugs-address="https://bugs.launchpad.net/openstack-i18n/" \ + --project=requirements --version=1 \ + -k "_LE" \ + -o babel-test/test-log-error.pot babel-test + +# Entries to ignore +REGEX="(POT-Creation-Date|Generated-By|Copyright (C) |FIRST AUTHOR )" + +function diff_files { + local expected=$1 + local testfile=$2 + local extra + + # grep fails if there's no content - which is fine here. + set +e + extra=$(diff -u0 $expected $testfile | \ + egrep -v "$REGEX" |egrep -c "^([-+][^-+#])") + set -e + + if [ $extra -ne 0 ] ; then + echo "Generation of test.pot failed." + echo "Extra content is:" + diff -u0 $expected $testfile | egrep -v "$REGEX" + exit 1 + fi +} + +diff_files babel-test/expected.pot babel-test/test.pot +diff_files babel-test/expected-log-error.pot babel-test/test-log-error.pot + +echo "Everything fine" diff --git a/tox.ini b/tox.ini index 2d4e715fab..927bf89ff8 100644 --- a/tox.ini +++ b/tox.ini @@ -29,6 +29,12 @@ commands = flake8 [testenv:docs] commands = python setup.py build_sphinx +[testenv:babel] +# Use the local upper-constraints.txt file +deps = Babel +install_command = pip install -c upper-constraints.txt {opts} {packages} +commands = tools/babel-test.sh + [flake8] ignore = H803 exclude = .venv,.git,.tox,dist,doc,*egg,build