OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 | |
6 '''Unit tests for the 'grit build' tool. | |
7 ''' | |
8 | |
9 import codecs | |
10 import os | |
11 import sys | |
12 import tempfile | |
13 if __name__ == '__main__': | |
14 sys.path.append(os.path.join(os.path.dirname(__file__), '../..')) | |
15 | |
16 import unittest | |
17 | |
18 from grit import util | |
19 from grit.tool import build | |
20 | |
21 | |
22 class BuildUnittest(unittest.TestCase): | |
23 | |
24 def testFindTranslationsWithSubstitutions(self): | |
25 # This is a regression test; we had a bug where GRIT would fail to find | |
26 # messages with substitutions e.g. "Hello [IDS_USER]" where IDS_USER is | |
27 # another <message>. | |
28 output_dir = tempfile.mkdtemp() | |
29 builder = build.RcBuilder() | |
30 class DummyOpts(object): | |
31 def __init__(self): | |
32 self.input = util.PathFromRoot('grit/testdata/substitute.grd') | |
33 self.verbose = False | |
34 self.extra_verbose = False | |
35 builder.Run(DummyOpts(), ['-o', output_dir]) | |
36 | |
37 def testGenerateDepFile(self): | |
38 output_dir = tempfile.mkdtemp() | |
39 builder = build.RcBuilder() | |
40 class DummyOpts(object): | |
41 def __init__(self): | |
42 self.input = util.PathFromRoot('grit/testdata/substitute.grd') | |
43 self.verbose = False | |
44 self.extra_verbose = False | |
45 expected_dep_file = os.path.join(output_dir, 'substitute.grd.d') | |
46 builder.Run(DummyOpts(), ['-o', output_dir, | |
47 '--depdir', output_dir, | |
48 '--depfile', expected_dep_file]) | |
49 | |
50 self.failUnless(os.path.isfile(expected_dep_file)) | |
51 with open(expected_dep_file) as f: | |
52 line = f.readline() | |
53 (dep_output_file, deps_string) = line.split(': ') | |
54 deps = deps_string.split(' ') | |
55 | |
56 self.failUnlessEqual("resource.h", dep_output_file) | |
57 self.failUnlessEqual(1, len(deps)) | |
58 self.failUnlessEqual(deps[0], | |
59 util.PathFromRoot('grit/testdata/substitute.xmb')) | |
60 | |
61 def testGenerateDepFileWithResourceIds(self): | |
62 output_dir = tempfile.mkdtemp() | |
63 builder = build.RcBuilder() | |
64 class DummyOpts(object): | |
65 def __init__(self): | |
66 self.input = util.PathFromRoot('grit/testdata/substitute_no_ids.grd') | |
67 self.verbose = False | |
68 self.extra_verbose = False | |
69 expected_dep_file = os.path.join(output_dir, 'substitute_no_ids.grd.d') | |
70 builder.Run(DummyOpts(), | |
71 ['-f', util.PathFromRoot('grit/testdata/resource_ids'), | |
72 '-o', output_dir, | |
73 '--depdir', output_dir, | |
74 '--depfile', expected_dep_file]) | |
75 | |
76 self.failUnless(os.path.isfile(expected_dep_file)) | |
77 with open(expected_dep_file) as f: | |
78 line = f.readline() | |
79 (dep_output_file, deps_string) = line.split(': ') | |
80 deps = deps_string.split(' ') | |
81 | |
82 self.failUnlessEqual("resource.h", dep_output_file) | |
83 self.failUnlessEqual(2, len(deps)) | |
84 self.failUnlessEqual(deps[0], | |
85 util.PathFromRoot('grit/testdata/substitute.xmb')) | |
86 self.failUnlessEqual(deps[1], | |
87 util.PathFromRoot('grit/testdata/resource_ids')) | |
88 | |
89 def testAssertOutputs(self): | |
90 output_dir = tempfile.mkdtemp() | |
91 class DummyOpts(object): | |
92 def __init__(self): | |
93 self.input = util.PathFromRoot('grit/testdata/substitute.grd') | |
94 self.verbose = False | |
95 self.extra_verbose = False | |
96 | |
97 # Incomplete output file list should fail. | |
98 builder_fail = build.RcBuilder() | |
99 self.failUnlessEqual(2, | |
100 builder_fail.Run(DummyOpts(), [ | |
101 '-o', output_dir, | |
102 '-a', os.path.abspath( | |
103 os.path.join(output_dir, 'en_generated_resources.rc'))])) | |
104 | |
105 # Complete output file list should succeed. | |
106 builder_ok = build.RcBuilder() | |
107 self.failUnlessEqual(0, | |
108 builder_ok.Run(DummyOpts(), [ | |
109 '-o', output_dir, | |
110 '-a', os.path.abspath( | |
111 os.path.join(output_dir, 'en_generated_resources.rc')), | |
112 '-a', os.path.abspath( | |
113 os.path.join(output_dir, 'sv_generated_resources.rc')), | |
114 '-a', os.path.abspath( | |
115 os.path.join(output_dir, 'resource.h'))])) | |
116 | |
117 def _verifyWhitelistedOutput(self, | |
118 filename, | |
119 whitelisted_ids, | |
120 non_whitelisted_ids, | |
121 encoding='utf8'): | |
122 self.failUnless(os.path.exists(filename)) | |
123 whitelisted_ids_found = [] | |
124 non_whitelisted_ids_found = [] | |
125 with codecs.open(filename, encoding=encoding) as f: | |
126 for line in f.readlines(): | |
127 for whitelisted_id in whitelisted_ids: | |
128 if whitelisted_id in line: | |
129 whitelisted_ids_found.append(whitelisted_id) | |
130 for non_whitelisted_id in non_whitelisted_ids: | |
131 if non_whitelisted_id in line: | |
132 non_whitelisted_ids_found.append(non_whitelisted_id) | |
133 self.longMessage = True | |
134 self.assertEqual(whitelisted_ids, | |
135 whitelisted_ids_found, | |
136 '\nin file {}'.format(os.path.basename(filename))) | |
137 non_whitelisted_msg = ('Non-Whitelisted IDs {} found in {}' | |
138 .format(non_whitelisted_ids_found, os.path.basename(filename))) | |
139 self.assertFalse(non_whitelisted_ids_found, non_whitelisted_msg) | |
140 | |
141 def testWhitelistStrings(self): | |
142 output_dir = tempfile.mkdtemp() | |
143 builder = build.RcBuilder() | |
144 class DummyOpts(object): | |
145 def __init__(self): | |
146 self.input = util.PathFromRoot('grit/testdata/whitelist_strings.grd') | |
147 self.verbose = False | |
148 self.extra_verbose = False | |
149 whitelist_file = util.PathFromRoot('grit/testdata/whitelist.txt') | |
150 builder.Run(DummyOpts(), ['-o', output_dir, | |
151 '-w', whitelist_file]) | |
152 header = os.path.join(output_dir, 'whitelist_test_resources.h') | |
153 rc = os.path.join(output_dir, 'en_whitelist_test_strings.rc') | |
154 | |
155 whitelisted_ids = ['IDS_MESSAGE_WHITELISTED'] | |
156 non_whitelisted_ids = ['IDS_MESSAGE_NOT_WHITELISTED'] | |
157 self._verifyWhitelistedOutput( | |
158 header, | |
159 whitelisted_ids, | |
160 non_whitelisted_ids, | |
161 ) | |
162 self._verifyWhitelistedOutput( | |
163 rc, | |
164 whitelisted_ids, | |
165 non_whitelisted_ids, | |
166 encoding='utf16' | |
167 ) | |
168 | |
169 def testWhitelistResources(self): | |
170 output_dir = tempfile.mkdtemp() | |
171 builder = build.RcBuilder() | |
172 class DummyOpts(object): | |
173 def __init__(self): | |
174 self.input = util.PathFromRoot('grit/testdata/whitelist_resources.grd') | |
175 self.verbose = False | |
176 self.extra_verbose = False | |
177 whitelist_file = util.PathFromRoot('grit/testdata/whitelist.txt') | |
178 builder.Run(DummyOpts(), ['-o', output_dir, | |
179 '-w', whitelist_file]) | |
180 header = os.path.join(output_dir, 'whitelist_test_resources.h') | |
181 map_cc = os.path.join(output_dir, 'whitelist_test_resources_map.cc') | |
182 map_h = os.path.join(output_dir, 'whitelist_test_resources_map.h') | |
183 pak = os.path.join(output_dir, 'whitelist_test_resources.pak') | |
184 | |
185 # Ensure the resource map header and .pak files exist, but don't verify | |
186 # their content. | |
187 self.failUnless(os.path.exists(map_h)) | |
188 self.failUnless(os.path.exists(pak)) | |
189 | |
190 whitelisted_ids = [ | |
191 'IDR_STRUCTURE_WHITELISTED', | |
192 'IDR_STRUCTURE_IN_TRUE_IF_WHITELISTED', | |
193 'IDR_INCLUDE_WHITELISTED', | |
194 ] | |
195 non_whitelisted_ids = [ | |
196 'IDR_STRUCTURE_NOT_WHITELISTED', | |
197 'IDR_STRUCTURE_IN_TRUE_IF_NOT_WHITELISTED', | |
198 'IDR_STRUCTURE_IN_FALSE_IF_WHITELISTED', | |
199 'IDR_STRUCTURE_IN_FALSE_IF_NOT_WHITELISTED', | |
200 'IDR_INCLUDE_NOT_WHITELISTED', | |
201 ] | |
202 for output_file in (header, map_cc): | |
203 self._verifyWhitelistedOutput( | |
204 output_file, | |
205 whitelisted_ids, | |
206 non_whitelisted_ids, | |
207 ) | |
208 | |
209 def testOutputAllResourceDefinesTrue(self): | |
210 output_dir = tempfile.mkdtemp() | |
211 builder = build.RcBuilder() | |
212 class DummyOpts(object): | |
213 def __init__(self): | |
214 self.input = util.PathFromRoot('grit/testdata/whitelist_resources.grd') | |
215 self.verbose = False | |
216 self.extra_verbose = False | |
217 whitelist_file = util.PathFromRoot('grit/testdata/whitelist.txt') | |
218 builder.Run(DummyOpts(), ['-o', output_dir, | |
219 '-w', whitelist_file, | |
220 '--output-all-resource-defines',]) | |
221 header = os.path.join(output_dir, 'whitelist_test_resources.h') | |
222 map_cc = os.path.join(output_dir, 'whitelist_test_resources_map.cc') | |
223 | |
224 whitelisted_ids = [ | |
225 'IDR_STRUCTURE_WHITELISTED', | |
226 'IDR_STRUCTURE_NOT_WHITELISTED', | |
227 'IDR_STRUCTURE_IN_TRUE_IF_WHITELISTED', | |
228 'IDR_STRUCTURE_IN_TRUE_IF_NOT_WHITELISTED', | |
229 'IDR_STRUCTURE_IN_FALSE_IF_WHITELISTED', | |
230 'IDR_STRUCTURE_IN_FALSE_IF_NOT_WHITELISTED', | |
231 'IDR_INCLUDE_WHITELISTED', | |
232 'IDR_INCLUDE_NOT_WHITELISTED', | |
233 ] | |
234 non_whitelisted_ids = [] | |
235 for output_file in (header, map_cc): | |
236 self._verifyWhitelistedOutput( | |
237 output_file, | |
238 whitelisted_ids, | |
239 non_whitelisted_ids, | |
240 ) | |
241 | |
242 def testOutputAllResourceDefinesFalse(self): | |
243 output_dir = tempfile.mkdtemp() | |
244 builder = build.RcBuilder() | |
245 class DummyOpts(object): | |
246 def __init__(self): | |
247 self.input = util.PathFromRoot('grit/testdata/whitelist_resources.grd') | |
248 self.verbose = False | |
249 self.extra_verbose = False | |
250 whitelist_file = util.PathFromRoot('grit/testdata/whitelist.txt') | |
251 builder.Run(DummyOpts(), ['-o', output_dir, | |
252 '-w', whitelist_file, | |
253 '--no-output-all-resource-defines',]) | |
254 header = os.path.join(output_dir, 'whitelist_test_resources.h') | |
255 map_cc = os.path.join(output_dir, 'whitelist_test_resources_map.cc') | |
256 | |
257 whitelisted_ids = [ | |
258 'IDR_STRUCTURE_WHITELISTED', | |
259 'IDR_STRUCTURE_IN_TRUE_IF_WHITELISTED', | |
260 'IDR_INCLUDE_WHITELISTED', | |
261 ] | |
262 non_whitelisted_ids = [ | |
263 'IDR_STRUCTURE_NOT_WHITELISTED', | |
264 'IDR_STRUCTURE_IN_TRUE_IF_NOT_WHITELISTED', | |
265 'IDR_STRUCTURE_IN_FALSE_IF_WHITELISTED', | |
266 'IDR_STRUCTURE_IN_FALSE_IF_NOT_WHITELISTED', | |
267 'IDR_INCLUDE_NOT_WHITELISTED', | |
268 ] | |
269 for output_file in (header, map_cc): | |
270 self._verifyWhitelistedOutput( | |
271 output_file, | |
272 whitelisted_ids, | |
273 non_whitelisted_ids, | |
274 ) | |
275 | |
276 def testWriteOnlyNew(self): | |
277 output_dir = tempfile.mkdtemp() | |
278 builder = build.RcBuilder() | |
279 class DummyOpts(object): | |
280 def __init__(self): | |
281 self.input = util.PathFromRoot('grit/testdata/substitute.grd') | |
282 self.verbose = False | |
283 self.extra_verbose = False | |
284 UNCHANGED = 10 | |
285 header = os.path.join(output_dir, 'resource.h') | |
286 | |
287 builder.Run(DummyOpts(), ['-o', output_dir]) | |
288 self.failUnless(os.path.exists(header)) | |
289 first_mtime = os.stat(header).st_mtime | |
290 | |
291 os.utime(header, (UNCHANGED, UNCHANGED)) | |
292 builder.Run(DummyOpts(), ['-o', output_dir, '--write-only-new', '0']) | |
293 self.failUnless(os.path.exists(header)) | |
294 second_mtime = os.stat(header).st_mtime | |
295 | |
296 os.utime(header, (UNCHANGED, UNCHANGED)) | |
297 builder.Run(DummyOpts(), ['-o', output_dir, '--write-only-new', '1']) | |
298 self.failUnless(os.path.exists(header)) | |
299 third_mtime = os.stat(header).st_mtime | |
300 | |
301 self.assertTrue(abs(second_mtime - UNCHANGED) > 5) | |
302 self.assertTrue(abs(third_mtime - UNCHANGED) < 5) | |
303 | |
304 def testGenerateDepFileWithDependOnStamp(self): | |
305 output_dir = tempfile.mkdtemp() | |
306 builder = build.RcBuilder() | |
307 class DummyOpts(object): | |
308 def __init__(self): | |
309 self.input = util.PathFromRoot('grit/testdata/substitute.grd') | |
310 self.verbose = False | |
311 self.extra_verbose = False | |
312 expected_dep_file_name = 'substitute.grd.d' | |
313 expected_stamp_file_name = expected_dep_file_name + '.stamp' | |
314 expected_dep_file = os.path.join(output_dir, expected_dep_file_name) | |
315 expected_stamp_file = os.path.join(output_dir, expected_stamp_file_name) | |
316 if os.path.isfile(expected_stamp_file): | |
317 os.remove(expected_stamp_file) | |
318 builder.Run(DummyOpts(), ['-o', output_dir, | |
319 '--depdir', output_dir, | |
320 '--depfile', expected_dep_file, | |
321 '--depend-on-stamp']) | |
322 self.failUnless(os.path.isfile(expected_stamp_file)) | |
323 first_mtime = os.stat(expected_stamp_file).st_mtime | |
324 | |
325 # Reset mtime to very old. | |
326 OLDTIME = 10 | |
327 os.utime(expected_stamp_file, (OLDTIME, OLDTIME)) | |
328 | |
329 builder.Run(DummyOpts(), ['-o', output_dir, | |
330 '--depdir', output_dir, | |
331 '--depfile', expected_dep_file, | |
332 '--depend-on-stamp']) | |
333 self.failUnless(os.path.isfile(expected_stamp_file)) | |
334 second_mtime = os.stat(expected_stamp_file).st_mtime | |
335 | |
336 # Some OS have a 2s stat resolution window, so can't do a direct comparison. | |
337 self.assertTrue((second_mtime - OLDTIME) > 5) | |
338 self.assertTrue(abs(second_mtime - first_mtime) < 5) | |
339 | |
340 self.failUnless(os.path.isfile(expected_dep_file)) | |
341 with open(expected_dep_file) as f: | |
342 line = f.readline() | |
343 (dep_output_file, deps_string) = line.split(': ') | |
344 deps = deps_string.split(' ') | |
345 | |
346 self.failUnlessEqual(expected_stamp_file_name, dep_output_file) | |
347 self.failUnlessEqual(1, len(deps)) | |
348 self.failUnlessEqual(deps[0], | |
349 util.PathFromRoot('grit/testdata/substitute.xmb')) | |
350 | |
351 | |
352 if __name__ == '__main__': | |
353 unittest.main() | |
OLD | NEW |