Debian Codegraph @

po/test/po_test.py

raw
1 #
2 # Gramps - a GTK+/GNOME based genealogy program
3 #
4 # Copyright (C) 2007 Donald N. Allingham
5 # Copyright (C) 2010 Jerome Rapinat
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with this program; if not, see <https://www.gnu.org/licenses/>.
19 #
20
21 """Unittest for testing POTFILES.in and Makefile contents"""
22
23 # PYTHONPATH=/../gramps python po_test.py
24
25 import unittest
26 import os
27 import glob
28 from test import test_util
29
30 test_util.path_append_parent()
31
32
33 def get_potfile(filename):
34 with open(filename, "r") as fp:
35 retvals = []
36 for line in fp:
37 if line and line[0] != "#":
38 retvals.append(line.strip())
39 return retvals
40
41
42 # POTFILES.skip
43
44 excluded_files = get_potfile("../POTFILES.skip")
45
46
47 class TestPOT(unittest.TestCase):
48 potfiles = get_potfile("../POTFILES.in")
49 count = 1
50
51 def __init__(self, method_name, dir, file, searches):
52 method_name = method_name % self.count
53 TestPOT.count += 1
54 self.__dict__[method_name] = lambda: self.helper(dir, file, searches)
55 unittest.TestCase.__init__(self, method_name)
56
57 def helper(self, dir, file, searches):
58 realpath = dir + "/" + file
59 pathfile = realpath[3:]
60 if os.path.exists(realpath):
61 with open(realpath, "r") as fp:
62 lines = fp.read()
63 found = False
64 for search in searches:
65 if search in lines:
66 found = True
67 if found:
68 self.assertFalse(
69 pathfile[3:] not in excluded_files
70 and pathfile[3:] not in self.potfiles,
71 "'%s' is not in POTFILES.in" % (pathfile[3:],),
72 )
73 else:
74 self.assertTrue(True, "'%s' doesn't contain '%s'" % (file, search))
75 else:
76 self.assertTrue(True, "'%s' doesn't exist" % file)
77
78
79 class TestMake(unittest.TestCase):
80 count = 1
81
82 def __init__(self, method_name, dir, file):
83 method_name = method_name % self.count
84 TestMake.count += 1
85 self.__dict__[method_name] = lambda: self.helper(dir, file)
86 unittest.TestCase.__init__(self, method_name)
87
88 def helper(self, dir, file):
89 realpath = dir + "/" + file
90 pathfile = realpath[3:]
91 path, filename = realpath.rsplit("/", 1)
92 makefile = path + "/Makefile.in"
93 if pathfile[3:] in excluded_files:
94 self.assertTrue(True, "exclude '%s'" % pathfile)
95 elif os.path.exists(makefile):
96 with open(makefile, "r") as fp:
97 lines = fp.read()
98 self.assertTrue(
99 filename in lines, "'%s' not in %s/Makefile.in" % (filename, path)
100 )
101 else:
102 self.assertTrue(True, "no makefile in '%s'" % dir)
103
104
105 class TestGetText(unittest.TestCase):
106 count = 1
107
108 def __init__(self, method_name, pofile, searches):
109 method_name = method_name % self.count
110 TestGetText.count += 1
111 self.__dict__[method_name] = lambda: self.helper(pofile, searches)
112 unittest.TestCase.__init__(self, method_name)
113
114 def helper(self, pofile, searches):
115 if not os.path.exists("../../" + pofile):
116 self.assertTrue(False, "'%s' is in POTFILES.in and does not exist" % pofile)
117 with open("../../" + pofile, "r") as fp:
118 lines = fp.read()
119 found = False
120 for search in searches:
121 found = (search in lines) or found
122 self.assertTrue(
123 found,
124 "'%s' is in POTFILES.in but does not contain '%s'" % (pofile, searches),
125 )
126
127
128 class TestDups(unittest.TestCase):
129 potfiles = get_potfile("../POTFILES.in")
130 count = 1
131
132 def __init__(self, method_name, potfile):
133 method_name = method_name % self.count
134 TestPOT.count += 1
135 self.__dict__[method_name] = lambda: self.helper(potfile)
136 unittest.TestCase.__init__(self, method_name)
137
138 def helper(self, potfile):
139 self.assertTrue(
140 self.potfiles.count(potfile) == 1,
141 "'%s' is in POTFILE.in more than once." % potfile,
142 )
143
144
145 def suite1():
146 """
147 Suite of tests designed to see if that if one of a set of phrases
148 is in a file, then that file better be in POTFILES.in.
149 """
150 suite = unittest.TestSuite()
151 for dir, subdir, files in os.walk("../../src"):
152 for file in files:
153 if glob.fnmatch.fnmatch(file, "*.py"):
154 suite.addTest(
155 TestPOT(
156 "test_pot_py_%04d",
157 dir,
158 file,
159 ["import gettext", "import sgettext"],
160 )
161 )
162 elif glob.fnmatch.fnmatch(file, "*.glade"):
163 suite.addTest(
164 TestPOT("test_pot_glade_%04d", dir, file, ['translatable="yes"'])
165 )
166 if glob.fnmatch.fnmatch(file, "*.py"):
167 suite.addTest(TestMake("test_make_py_%04d", dir, file))
168 elif glob.fnmatch.fnmatch(file, "*.glade"):
169 suite.addTest(TestMake("test_make_glade_%04d", dir, file))
170 return suite
171
172
173 def suite2():
174 """
175 Suite of tests that check for each file in POTFILES.in, then it
176 should have an import gettext or sgettext.
177 """
178 suite = unittest.TestSuite()
179 potfiles = get_potfile("../POTFILES.in")
180 for potfile in potfiles:
181 # do not call gettext
182 if glob.fnmatch.fnmatch(potfile, "*.gpr.py"):
183 continue
184 # special case
185 if glob.fnmatch.fnmatch(potfile, "src/plugins/lib/libnarrate.py"):
186 continue
187 if glob.fnmatch.fnmatch(potfile, "*.py"):
188 suite.addTest(
189 TestGetText(
190 "test_gettext_py_%04d",
191 potfile,
192 ["import gettext", "from gramps.gen.ggettext", "import sgettext"],
193 )
194 )
195 elif glob.fnmatch.fnmatch(potfile, "*.glade"):
196 suite.addTest(
197 TestGetText("test_gettext_glade_%04d", potfile, ['translatable="yes"'])
198 )
199 return suite
200
201
202 def suite3():
203 """
204 Looks for duplicates in POTFILES.in.
205 """
206 suite = unittest.TestSuite()
207 for potfile in set(get_potfile("../POTFILES.in")):
208 if potfile:
209 suite.addTest(TestDups("test_dups_%04d", potfile))
210 return suite
211
212
213 if __name__ == "__main__":
214 unittest.TextTestRunner().run(suite1())
215 unittest.TextTestRunner().run(suite2())
216 unittest.TextTestRunner().run(suite3())