Blame translation-canary/translation_canary/translated/test_usability.py

Packit b040ce
# Check a .po file for basic usability
Packit b040ce
#
Packit b040ce
# This will test that the file is well-formed and that the Plural-Forms value
Packit b040ce
# is parseable
Packit b040ce
#
Packit b040ce
# Copyright (C) 2015  Red Hat, Inc.
Packit b040ce
#
Packit b040ce
# This copyrighted material is made available to anyone wishing to use,
Packit b040ce
# modify, copy, or redistribute it subject to the terms and conditions of
Packit b040ce
# the GNU Lesser General Public License v.2, or (at your option) any later
Packit b040ce
# version. This program is distributed in the hope that it will be useful,
Packit b040ce
# but WITHOUT ANY WARRANTY expressed or implied, including the implied
Packit b040ce
# warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
Packit b040ce
# the GNU Lesser General Public License for more details.  You should have
Packit b040ce
# received a copy of the GNU Lesser General Public License along with this
Packit b040ce
# program; if not, write to the Free Software Foundation, Inc., 51 Franklin
Packit b040ce
# Street, Fifth Floor, Boston, MA 02110-1301, USA.  Any Red Hat trademarks
Packit b040ce
# that are incorporated in the source code or documentation are not subject
Packit b040ce
# to the GNU Lesser General Public License and may only be used or
Packit b040ce
# replicated with the express permission of Red Hat, Inc.
Packit b040ce
#
Packit b040ce
# Red Hat Author(s): David Shea <dshea@redhat.com>
Packit b040ce
Packit b040ce
import gettext
Packit b040ce
import tempfile
Packit b040ce
import polib
Packit b040ce
import subprocess
Packit b040ce
Packit b040ce
def test_usability(pofile):
Packit b040ce
    # Use polib to write a mofile
Packit b040ce
    with tempfile.NamedTemporaryFile(mode="w+b") as mofile:
Packit b040ce
        pofile = polib.pofile(pofile)
Packit b040ce
        pofile.save_as_mofile(mofile.name)
Packit b040ce
Packit b040ce
        # Try to open it
Packit b040ce
        _t = gettext.GNUTranslations(fp=mofile)
Packit b040ce
Packit b040ce
def test_msgfmt(pofile):
Packit b040ce
    # Check that the .po file can actually be compiled
Packit b040ce
    with tempfile.NamedTemporaryFile(mode="w+b", suffix=".mo") as mofile:
Packit b040ce
        try:
Packit b040ce
            # Ignore the output on success
Packit b040ce
            subprocess.check_output(["msgfmt", "-c", "--verbose", "-o", mofile.name, pofile],
Packit b040ce
                                    stderr=subprocess.STDOUT, universal_newlines=True)
Packit b040ce
        except subprocess.CalledProcessError as e:
Packit b040ce
            raise AssertionError("Unable to compile %s: %s" % (pofile, e.output))