| 1 | # Check a .po file for basic usability |
| 2 | # |
| 3 | # This will test that the file is well-formed and that the Plural-Forms value |
| 4 | # is parseable |
| 5 | # |
| 6 | # Copyright (C) 2015 Red Hat, Inc. |
| 7 | # |
| 8 | # This copyrighted material is made available to anyone wishing to use, |
| 9 | # modify, copy, or redistribute it subject to the terms and conditions of |
| 10 | # the GNU Lesser General Public License v.2, or (at your option) any later |
| 11 | # version. This program is distributed in the hope that it will be useful, |
| 12 | # but WITHOUT ANY WARRANTY expressed or implied, including the implied |
| 13 | # warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 14 | # the GNU Lesser General Public License for more details. You should have |
| 15 | # received a copy of the GNU Lesser General Public License along with this |
| 16 | # program; if not, write to the Free Software Foundation, Inc., 51 Franklin |
| 17 | # Street, Fifth Floor, Boston, MA 02110-1301, USA. Any Red Hat trademarks |
| 18 | # that are incorporated in the source code or documentation are not subject |
| 19 | # to the GNU Lesser General Public License and may only be used or |
| 20 | # replicated with the express permission of Red Hat, Inc. |
| 21 | # |
| 22 | # Red Hat Author(s): David Shea <[email protected]> |
| 23 | |
| 24 | import gettext |
| 25 | import tempfile |
| 26 | import polib |
| 27 | import subprocess |
| 28 | |
| 29 | def test_usability(pofile): |
| 30 | # Use polib to write a mofile |
| 31 | with tempfile.NamedTemporaryFile(mode="w+b") as mofile: |
| 32 | pofile = polib.pofile(pofile) |
| 33 | pofile.save_as_mofile(mofile.name) |
| 34 | |
| 35 | # Try to open it |
| 36 | _t = gettext.GNUTranslations(fp=mofile) |
| 37 | |
| 38 | def test_msgfmt(pofile): |
| 39 | # Check that the .po file can actually be compiled |
| 40 | with tempfile.NamedTemporaryFile(mode="w+b", suffix=".mo") as mofile: |
| 41 | try: |
| 42 | # Ignore the output on success |
| 43 | subprocess.check_output(["msgfmt", "-c", "--verbose", "-o", mofile.name, pofile], |
| 44 | stderr=subprocess.STDOUT, universal_newlines=True) |
| 45 | except subprocess.CalledProcessError as e: |
| 46 | raise AssertionError("Unable to compile %s: %s" % (pofile, e.output)) from e |