|
| 1 | +"""Tests to cover the Tools/i18n package""" |
| 2 | + |
| 3 | +import os |
| 4 | +import unittest |
| 5 | + |
| 6 | +from test.script_helper import assert_python_ok |
| 7 | +from test.test_tools import toolsdir |
| 8 | +from test.support import temp_cwd |
| 9 | + |
| 10 | +class Test_pygettext(unittest.TestCase): |
| 11 | + """Tests for the pygettext.py tool""" |
| 12 | + |
| 13 | + script = os.path.join(toolsdir,'i18n', 'pygettext.py') |
| 14 | + |
| 15 | + def get_header(self, data): |
| 16 | + """ utility: return the header of a .po file as a dictionary """ |
| 17 | + headers = {} |
| 18 | + for line in data.split('\n'): |
| 19 | + if not line or line.startswith(('#', 'msgid','msgstr')): |
| 20 | + continue |
| 21 | + line = line.strip('"') |
| 22 | + key, val = line.split(':',1) |
| 23 | + headers[key] = val.strip() |
| 24 | + return headers |
| 25 | + |
| 26 | + def test_header(self): |
| 27 | + """Make sure the required fields are in the header, according to: |
| 28 | + http://www.gnu.org/software/gettext/manual/gettext.html#Header-Entry |
| 29 | + """ |
| 30 | + with temp_cwd(None) as cwd: |
| 31 | + assert_python_ok(self.script) |
| 32 | + with open('messages.pot') as fp: |
| 33 | + data = fp.read() |
| 34 | + header = self.get_header(data) |
| 35 | + |
| 36 | + self.assertIn("Project-Id-Version", header) |
| 37 | + self.assertIn("POT-Creation-Date", header) |
| 38 | + self.assertIn("PO-Revision-Date", header) |
| 39 | + self.assertIn("Last-Translator", header) |
| 40 | + self.assertIn("Language-Team", header) |
| 41 | + self.assertIn("MIME-Version", header) |
| 42 | + self.assertIn("Content-Type", header) |
| 43 | + self.assertIn("Content-Transfer-Encoding", header) |
| 44 | + self.assertIn("Generated-By", header) |
| 45 | + |
| 46 | + # not clear if these should be required in POT (template) files |
| 47 | + #self.assertIn("Report-Msgid-Bugs-To", header) |
| 48 | + #self.assertIn("Language", header) |
| 49 | + |
| 50 | + #"Plural-Forms" is optional |
| 51 | + |
| 52 | + |
| 53 | + def test_POT_Creation_Date(self): |
| 54 | + """ Match the date format from xgettext for POT-Creation-Date """ |
| 55 | + from datetime import datetime |
| 56 | + with temp_cwd(None) as cwd: |
| 57 | + assert_python_ok(self.script) |
| 58 | + with open('messages.pot') as fp: |
| 59 | + data = fp.read() |
| 60 | + header = self.get_header(data) |
| 61 | + creationDate = header['POT-Creation-Date'] |
| 62 | + |
| 63 | + # peel off the escaped newline at the end of string |
| 64 | + if creationDate.endswith('\\n'): |
| 65 | + creationDate = creationDate[:-len('\\n')] |
| 66 | + |
| 67 | + # This will raise if the date format does not exactly match. |
| 68 | + datetime.strptime(creationDate, '%Y-%m-%d %H:%M%z') |
0 commit comments