Porting tests to Py3

This commit is contained in:
Sundeep Anand 2019-06-18 13:25:07 +05:30
commit 8e5f065c53
6 changed files with 248 additions and 179 deletions

View file

@ -6,7 +6,7 @@
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# Copyright (c) 2015 Red Hat, Inc.
# Copyright (c) 2019 Red Hat, Inc.
#
# This program is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
@ -29,7 +29,7 @@ export TESTVERSION=1.0
BUILT_FILES=
FILES=$(METADATA) gettext_test.py Makefile test_data.py runtest.sh
FILES=$(METADATA) Makefile runtest.sh test_gettext.py test_i18n.py
.PHONY: all install download clean
@ -52,12 +52,11 @@ $(METADATA): Makefile
@echo "Description: Functional test for the gettext module" >> $(METADATA)
@echo "Type: Functional" >> $(METADATA)
@echo "TestTime: 5m" >> $(METADATA)
@echo "RunFor: python" >> $(METADATA)
@echo "Requires: python" >> $(METADATA)
@echo "RunFor: python3" >> $(METADATA)
@echo "Requires: python3" >> $(METADATA)
@echo "Priority: Normal" >> $(METADATA)
@echo "License: GPLv2" >> $(METADATA)
@echo "Confidential: no" >> $(METADATA)
@echo "Destructive: no" >> $(METADATA)
rhts-lint $(METADATA)

View file

@ -1,158 +0,0 @@
# -*- coding: utf-8 -*-
import subprocess
import gettext
import os
import logging
"""
Saving logs
"""
logging.basicConfig(level=logging.INFO)
test_data = {"fr_FR": '''msgstr "Bonjour le monde!"''',
"de_DE": '''msgstr "Hallo Welt!"''',
"es_ES": '''msgstr "Hola Mundo!"''',
"it_IT": '''msgstr "Ciao mondo!"''',
"pt_BR": '''msgstr "Olá Mundo!"''',
"ja_JP": '''msgstr "「こんにちは世界」"''',
"ko_KR": '''msgstr "안녕하세요!"''',
"ru_RU": '''msgstr "Привет мир!"''',
"zh_CN": '''msgstr "你好,世界!"''',
"zh_TW": '''msgstr "你好,世界!"'''}
logging.info("TEST RESULTS FOR ATOMIC HOST\n\n")
def locale():
"""
Check common files for locale support
"""
try:
locale = subprocess.Popen(["locale", "-a"], stdout=subprocess.PIPE)
stddata = locale.communicate()
if stddata:
actual = stddata[0].split('\n')[:-1]
else:
logging.error("Locale support Test: ERROR\n")
return
expected = ['en_US', 'en_US.iso88591',
'en_US.iso885915', 'en_US.utf8']
if set(expected).issubset(set(actual)):
logging.info("Locale support Test: SUCCESS\n")
else:
logging.error("Locale support Test: ERROR\n")
except OSError as e:
logging.error("Locale support Test: OSError\n")
def check_gettext():
"""
Check if gettext present
"""
try:
cmd1 = ['rpm', '-q', 'gettext']
p1 = subprocess.Popen(cmd1, stdout=subprocess.PIPE)
stddata, stderr = p1.communicate()
logging.info("gettext version: %s" % (stddata))
if 'gettext' in stddata:
logging.info("GNU Internationalized Utilities Test: SUCCESS\n")
else:
logging.error("GNU Internationalized Utilities Test: ERROR\n")
except OSError as e:
logging.error("GNU Internationalized Utilities Test: OSError\n")
def pot_creation():
"""
Creates hello.pot file using test_data.py file
"""
try:
pot_file = subprocess.Popen(
"xgettext -d 'hello' -o hello.pot test_data.py", shell=True)
pot = pot_file.communicate()
except OSError as e:
logging.error(".pot file creation failed: OSError\n")
def make_dir(locale):
path = "./locale/%s/LC_MESSAGES" % (locale)
os.makedirs(path)
def po_creation(locale):
"""
creates .po file using hello.pot file
"""
try:
po_fr = subprocess.Popen(
"msginit --no-translator -l %s -i hello.pot -o ./locale/%s/LC_MESSAGES/%s.po" % (locale, locale, locale), shell=True)
po_fr = po_fr.communicate()
except OSError as e:
logging.error(".po file creation failed: OSError\n")
def trans(locale):
"""
Updates .po file with the translations for corresponding
language
"""
if locale in test_data.keys():
data = test_data[locale]
with open('./locale/%s/LC_MESSAGES/%s.po' % (locale, locale), 'rw') as f1:
data1 = f1.readlines()
data1.pop()
data1.append(data)
for index, data in enumerate(data1):
if "Content-Type" in data:
data1[index] = '"Content-Type: text/plain; charset=UTF-8\\n"\n'
with open('./locale/%s/LC_MESSAGES/%s' % (locale, locale) + ".po", 'w') as f1:
for line in data1:
f1.write(line)
def mo_creation(locale):
"""
Creates .mo file for different locale form .po file
"""
try:
mo_fr = subprocess.Popen("msgfmt ./locale/%s/LC_MESSAGES/%s.po --output-file ./locale/%s/LC_MESSAGES/%s.mo" %
(locale, locale, locale, locale), shell=True)
mo_fr = mo_fr.communicate()
except OSError as e:
logging.error(".mo file creation failed: OSError\n")
def check_trans(locale):
"""
Verify if the output is correct for different language
"""
try:
stddata = subprocess.check_output(
'LANGUAGE=%s python test_data.py %s' % (locale, locale), shell=True)
if stddata.strip() == test_data[locale].strip('msgstr "'):
logging.info("Translation Test for %s: SUCCESS\n" % (locale))
else:
logging.error("Translation Test for %s: ERROR\n" % (locale))
except OSError as e:
logging.error("Translation Test for %s: OSERROR\n" % (locale))
def del_locale():
try:
subprocess.call(['rm', '-rf', './locale'])
except OSError as e:
logging.error("OSError\n")
if __name__ == "__main__":
locale()
check_gettext()
pot_creation()
for locale in test_data.keys():
make_dir(locale)
po_creation(locale)
trans(locale)
mo_creation(locale)
check_trans(locale)
del_locale()

View file

@ -36,13 +36,13 @@ rlJournalStart
rlPhaseStartSetup
rlAssertRpm $PACKAGE
rlRun "TmpDir=\`mktemp -d\`" 0 "Creating tmp directory"
rlRun "cp test_data.py gettext_test.py $TmpDir"
rlRun "cp test_i18n.py test_gettext.py $TmpDir"
rlRun "pushd $TmpDir"
rlPhaseEnd
rlPhaseStartTest
rlLog "Run gettext_test.py"
rlRun "python gettext_test.py"
rlLog "Run test_gettext.py"
rlRun "python3 test_gettext.py"
rlPhaseEnd
rlPhaseStartCleanup

View file

@ -1,13 +0,0 @@
import gettext
import sys
def hello(locale):
# Set up message catalog access
t = gettext.translation('%s' % (locale), 'locale', fallback=True)
_ = t.ugettext
s = _(u'Hello World!').encode('utf-8')
print s
hello(sys.argv[1])

View file

@ -0,0 +1,227 @@
# -*- coding: utf-8 -*-
import logging
import os
import subprocess
# Saving logs
logging.basicConfig(level=logging.INFO)
DECORATE_STR = "************************************"
logging.info("TEST RESULTS FOR GETTEXT\n{0}".format(DECORATE_STR))
# CONSTANTS
PACKAGE_TO_TEST = "gettext"
DOMAIN_TO_BIND = "testdomain"
PYTHON_INTERPRETER = "python3"
TEST_I18N_FILE = "test_i18n.py"
LOG_INFO_PASS = "[ PASS ]"
LOG_INFO_FAIL = "[ FAIL ]"
LOG_INFO_OS_ERROR = "[ OS Error ]"
# Test Data
TEST_DATA = {"fr_FR": '''msgstr "Bonjour le monde!"''',
"de_DE": '''msgstr "Hallo Welt!"''',
"es_ES": '''msgstr "Hola Mundo!"''',
"it_IT": '''msgstr "Ciao mondo!"''',
"pt_BR": '''msgstr "Olá Mundo!"''',
"ja_JP": '''msgstr "「こんにちは世界」"''',
"ko_KR": '''msgstr "안녕하세요!"''',
"ru_RU": '''msgstr "Привет мир!"''',
"zh_CN": '''msgstr "你好,世界!"''',
"zh_TW": '''msgstr "你好,世界!"'''}
def test_locale():
"""
Check common files for locale support
"""
subject = "Locale Support Test"
try:
all_locales = subprocess.Popen(["locale", "-a"], stdout=subprocess.PIPE)
all_locales_data = all_locales.communicate()
if all_locales_data:
actual = all_locales_data[0].decode().split('\n')
else:
logging.error("{0}: ERROR\n".format(subject))
return
expected_locales = ['en_US', 'en_US.iso88591',
'en_US.iso885915', 'en_US.utf8']
if set(expected_locales).issubset(set(actual)):
logging.info("{0}: {1}\n".format(subject, LOG_INFO_PASS))
else:
logging.error("{0}: {1}\n".format(subject, LOG_INFO_FAIL))
except OSError as e:
logging.error("{0}: {1}\n".format(subject, LOG_INFO_OS_ERROR))
def test_gettext():
"""
Check if gettext is present
"""
subject = "GNU Internationalization Utilities Test"
try:
cmd_check_gettext = ['rpm', '-q', PACKAGE_TO_TEST]
p1 = subprocess.Popen(cmd_check_gettext, stdout=subprocess.PIPE)
std_data, stderr = p1.communicate()
std_data = std_data.decode()
logging.info("Found {0} NVR: {1}".format(PACKAGE_TO_TEST, std_data))
if PACKAGE_TO_TEST in std_data:
logging.info("{0}: {1}\n".format(subject, LOG_INFO_PASS))
else:
logging.error("{0}: {1}\n".format(subject, LOG_INFO_FAIL))
except OSError as e:
logging.error("{0}: {1}\n".format(subject, LOG_INFO_OS_ERROR))
def test_pot_creation():
"""
Creates hello.pot file using test_i18n.py file
"""
subject = "POT file Creation Test"
try:
pot_file = subprocess.Popen(
"xgettext -d '{0}' -o {1}.pot {2}".format(DOMAIN_TO_BIND,
DOMAIN_TO_BIND,
TEST_I18N_FILE),
shell=True
)
pot_file.communicate()
except OSError as e:
logging.error("{0}: {1}\n".format(subject, LOG_INFO_OS_ERROR))
else:
logging.info("{0}: {1}\n".format(subject, LOG_INFO_PASS))
def make_dir(locale_dir):
path = "./locale/{0}/LC_MESSAGES".format(locale_dir)
os.makedirs(path)
def create_po_files(active_locale):
"""
creates .po file using POT file
"""
subject = "PO file Creation"
try:
cmd_po_files = subprocess.Popen(
"msginit --no-translator -l {0} -i {1}.pot -o ./locale/{2}/LC_MESSAGES/{3}.po".format(
active_locale, DOMAIN_TO_BIND, active_locale, active_locale), shell=True
)
cmd_po_files.communicate()
except OSError as e:
logging.error("{0} failed: {1}\n".format(subject, LOG_INFO_OS_ERROR))
else:
logging.info("{0} Succeed.".format(subject))
def translate(active_locale):
"""
Updates .po file with the translations for respective language
"""
data = TEST_DATA.get(active_locale)
if not data:
return
with open('./locale/{0}/LC_MESSAGES/{1}.po'.format(active_locale,
active_locale),
'r', encoding='latin-1') as f1:
data1 = f1.readlines()
data1.pop()
data1.append(data)
for index, data in enumerate(data1):
if "Content-Type" in data:
data1[index] = '"Content-Type: text/plain; charset=UTF-8\\n"\n'
with open('./locale/{0}/LC_MESSAGES/{1}'.format(active_locale,
active_locale) + ".po", 'w') as f2:
for line in data1:
f2.write(line)
def create_mo_files(active_locale):
"""
Creates .mo file for different locale form .po file
"""
subject = "MO file Creation"
try:
mo_files = subprocess.Popen(
"msgfmt ./locale/{0}/LC_MESSAGES/{1}.po --output-file ./locale/{2}/LC_MESSAGES/{3}.mo".format(
active_locale, active_locale, active_locale, active_locale
), shell=True
)
mo_files.communicate()
except OSError as e:
logging.error("{0} failed: {1}\n".format(subject, LOG_INFO_OS_ERROR))
else:
logging.info("{0} Succeed.".format(subject))
def test_translations(active_locale):
"""
Verify if the output is correct for different language
"""
subject = "Translation Test"
try:
cmd_translation_test = subprocess.check_output(
'LANGUAGE={0} {1} {2} {3}'.format(active_locale,
PYTHON_INTERPRETER,
TEST_I18N_FILE,
active_locale),
encoding='UTF-8', shell=True
)
if cmd_translation_test.strip() == TEST_DATA[active_locale].strip('msgstr "'):
logging.info("{0} for {1}: {2}\n".format(subject, active_locale, LOG_INFO_PASS))
else:
logging.error("{0} for {1}: {2}\n".format(subject, active_locale, LOG_INFO_FAIL))
except OSError as e:
logging.error("{0} for {1}: {2}\n".format(subject, active_locale, LOG_INFO_OS_ERROR))
def tear_off():
try:
subprocess.call(['rm', '-rf', './locale', '{0}.pot'.format(DOMAIN_TO_BIND)])
except OSError as e:
logging.error("OSError\n")
if __name__ == "__main__":
"""
Gettext Tests
"""
# Prepare tests
env_tests = [test_locale,
test_gettext,
test_pot_creation]
translation_tests = [make_dir,
create_po_files,
translate,
create_mo_files,
test_translations]
# Execute tests
[env_test() for env_test in env_tests]
for locale in TEST_DATA.keys():
[test(locale) for test in translation_tests]
tear_off()

View file

@ -0,0 +1,14 @@
import gettext
import sys
def print_message(locale):
# Set up message catalog access
t = gettext.translation('%s' % locale, 'locale', fallback=True)
_ = t.gettext
s = _('Hello World!')
print(s)
print_message(sys.argv[1])