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

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