PageRenderTime 55ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/subscription-manager-1.0.10/test/test_po_files.py

#
Python | 161 lines | 120 code | 23 blank | 18 comment | 7 complexity | 299fc473e1f1effb841a3e284d7e550a MD5 | raw file
Possible License(s): GPL-2.0
  1. import os
  2. import glob
  3. import unittest
  4. import datetime
  5. import sys
  6. import time
  7. # easy_install polib http://polib.readthedocs.org/
  8. import polib
  9. import locale
  10. import gettext
  11. _ = gettext.gettext
  12. #locale.setlocale(locale.LC_ALL, '')
  13. from stubs import MockStderr
  14. from stubs import MockStdout
  15. from subscription_manager import managercli
  16. # Localization domain:
  17. APP = "rhsm"
  18. # Directory where translations are deployed:
  19. DIR = '/usr/share/locale/'
  20. gettext.bindtextdomain(APP, DIR)
  21. gettext.textdomain(APP)
  22. po_files = glob.glob("po/*.po")
  23. langs = []
  24. for po_file in po_files:
  25. langs.append(po_file[:-3])
  26. class PotFile:
  27. def __init__(self):
  28. self.msgids = []
  29. PO_PATH = "po/"
  30. pot_file = "%s/keys.pot" % PO_PATH
  31. po = polib.pofile(pot_file)
  32. for entry in po:
  33. self.msgids.append(entry.msgid)
  34. self.msgids.sort()
  35. class TestLocale(unittest.TestCase):
  36. # see http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
  37. # http://docs.redhat.com/docs/en-US/Red_Hat_Enterprise_Linux/5/html/International_Language_Support_Guide/Red_Hat_Enterprise_Linux_International_Language_Support_Guide-Installing_and_supporting_languages.html
  38. test_locales = [
  39. # as_IN is kind of busted in RHEL6, and seemingly
  40. # very busted in 14
  41. #"as_IN", # Assamese
  42. "bn_IN", # Bengali
  43. "de_DE", # German
  44. "es_ES", # Spanish
  45. "en_US", # english us
  46. "fr_FR", # French
  47. "gu_IN", # Gujarati
  48. "hi_IN", # Hindi
  49. "it_IT", # Italian
  50. "ja_JP", # Japanese
  51. "kn_IN", # Kannada
  52. "ml_IN", # Malayalam
  53. "mr_IN", # Marathi
  54. "or_IN", # Oriya
  55. "pa_IN", # Punjabi
  56. # "ne_IN", # Nepali
  57. #"se_IN", # Sinhala
  58. #"br_IN", # Maithili
  59. "pt_BR", # Portugese
  60. "ru_RU", # Russian
  61. "si_LK", # Sri Lankan
  62. "ta_IN", # Tamil
  63. "te_IN", # telgu
  64. "zh_CN", # Chinese Simplified
  65. "zh_TW", # Chinese Traditional
  66. "ko_KR"] # korean
  67. def _setupLang(self, lang):
  68. os.environ['LANG'] = lang
  69. locale.setlocale(locale.LC_ALL, '')
  70. gettext.bindtextdomain(APP, DIR)
  71. class TestLocaleDate(TestLocale):
  72. def tearDown(self):
  73. self._setupLang("en_US")
  74. # FIXME
  75. # we work around this dynamicaly in managergui.py, but this
  76. # is here to see if we get anything new, or if the known
  77. # busted start working
  78. def test_strftime_1_1_2012(self):
  79. # yeah, this is weird. parsing the localized date format
  80. # for ja_JP and ko_KR fails in double digit months (10,11,12) even
  81. # though it seems to use a zero padded month field.
  82. # wibbly wobbly timey wimey
  83. self.known_busted = ["or_IN.UTF-8"]
  84. self.__test_strftime(datetime.date(2012, 1, 1))
  85. def test_strftime_10_30_2011(self):
  86. # zh_CN filed as https://bugzilla.redhat.com/show_bug.cgi?id=838647
  87. self.known_busted = ["zh_CN.UTF-8", "or_IN.UTF-8", "ja_JP.UTF-8", "ko_KR.UTF-8"]
  88. self.__test_strftime(datetime.date(2011, 10, 30))
  89. def __test_strftime(self, dt):
  90. for test_locale in self.test_locales:
  91. lc = "%s.UTF-8" % test_locale
  92. self._setupLang(lc)
  93. try:
  94. time.strptime(dt.strftime("%x"), "%x")
  95. except ValueError:
  96. if lc not in self.known_busted:
  97. raise
  98. continue
  99. # The above fails on f17/rhel6.3, but not f16, so don't
  100. # worry about the "no longer busted" test for now. We should
  101. # probably dump '%x' stuff anyway and just use iso8601 date
  102. # if lc in self.known_busted:
  103. # self.fail("%s used to be busted, but works now" % test_locale)
  104. # These are meant to catch bugs like bz #744536
  105. class TestUnicodeGettext(TestLocale):
  106. def setUp(self):
  107. self._setupLang("ja_JP.UTF-8")
  108. sys.stderr = MockStderr()
  109. sys.stdout = MockStdout()
  110. def tearDown(self):
  111. self._setupLang("en_US")
  112. sys.stdout = sys.__stdout__
  113. sys.stderr = sys.__stderr__
  114. def test_ja_not_serial(self):
  115. msg = _("'%s' is not a valid serial number") % "123123"
  116. unicode(managercli.to_unicode_or_bust(msg)).encode("UTF-8") + '\n'
  117. def test_systemExit(self):
  118. try:
  119. managercli.systemExit(1, _("'%s' is not a valid serial number") % "123123")
  120. except SystemExit:
  121. # tis okay, we are looking for unicode errors on the string encode
  122. pass
  123. def test_all_strings_all_langs(self):
  124. pot = PotFile()
  125. msgids = pot.msgids
  126. for lang in self.test_locales:
  127. self._setupLang(lang)
  128. for msgid in msgids:
  129. "%s" % gettext.gettext(msgid)
  130. if __name__ == "__main__":
  131. po = PotFile()