joi@chromium.org | 1eddb32 | 2012-04-03 10:15:24 +0000 | [diff] [blame] | 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 | |
newt@chromium.org | aaa243a | 2014-10-31 16:24:47 +0000 | [diff] [blame] | 9 | import codecs |
joi@chromium.org | 1eddb32 | 2012-04-03 10:15:24 +0000 | [diff] [blame] | 10 | import os |
joi@chromium.org | 1eddb32 | 2012-04-03 10:15:24 +0000 | [diff] [blame] | 11 | import sys |
| 12 | import tempfile |
| 13 | if __name__ == '__main__': |
joi@chromium.org | 98b76c0 | 2012-12-07 17:18:23 +0000 | [diff] [blame] | 14 | sys.path.append(os.path.join(os.path.dirname(__file__), '../..')) |
joi@chromium.org | 1eddb32 | 2012-04-03 10:15:24 +0000 | [diff] [blame] | 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 | |
joi@chromium.org | 247c890 | 2013-10-11 09:53:28 +0000 | [diff] [blame] | 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 |
joi@chromium.org | 247c890 | 2013-10-11 09:53:28 +0000 | [diff] [blame] | 45 | expected_dep_file = os.path.join(output_dir, 'substitute.grd.d') |
thakis@chromium.org | b5f1fff | 2014-01-08 00:58:46 +0000 | [diff] [blame] | 46 | builder.Run(DummyOpts(), ['-o', output_dir, |
| 47 | '--depdir', output_dir, |
| 48 | '--depfile', expected_dep_file]) |
| 49 | |
joi@chromium.org | 247c890 | 2013-10-11 09:53:28 +0000 | [diff] [blame] | 50 | self.failUnless(os.path.isfile(expected_dep_file)) |
| 51 | with open(expected_dep_file) as f: |
| 52 | line = f.readline() |
brettw@chromium.org | 856ff33 | 2014-07-21 20:05:21 +0000 | [diff] [blame] | 53 | (dep_output_file, deps_string) = line.split(': ') |
joi@chromium.org | 247c890 | 2013-10-11 09:53:28 +0000 | [diff] [blame] | 54 | deps = deps_string.split(' ') |
brettw@chromium.org | 856ff33 | 2014-07-21 20:05:21 +0000 | [diff] [blame] | 55 | |
| 56 | self.failUnlessEqual("resource.h", dep_output_file) |
joi@chromium.org | 247c890 | 2013-10-11 09:53:28 +0000 | [diff] [blame] | 57 | self.failUnlessEqual(1, len(deps)) |
| 58 | self.failUnlessEqual(deps[0], |
| 59 | util.PathFromRoot('grit/testdata/substitute.xmb')) |
| 60 | |
brettw@chromium.org | 856ff33 | 2014-07-21 20:05:21 +0000 | [diff] [blame] | 61 | def testAssertOutputs(self): |
| 62 | output_dir = tempfile.mkdtemp() |
| 63 | class DummyOpts(object): |
| 64 | def __init__(self): |
| 65 | self.input = util.PathFromRoot('grit/testdata/substitute.grd') |
| 66 | self.verbose = False |
| 67 | self.extra_verbose = False |
| 68 | |
| 69 | # Incomplete output file list should fail. |
| 70 | builder_fail = build.RcBuilder() |
| 71 | self.failUnlessEqual(2, |
| 72 | builder_fail.Run(DummyOpts(), [ |
| 73 | '-o', output_dir, |
| 74 | '-a', os.path.abspath( |
| 75 | os.path.join(output_dir, 'en_generated_resources.rc'))])) |
| 76 | |
| 77 | # Complete output file list should succeed. |
| 78 | builder_ok = build.RcBuilder() |
| 79 | self.failUnlessEqual(0, |
| 80 | builder_ok.Run(DummyOpts(), [ |
| 81 | '-o', output_dir, |
| 82 | '-a', os.path.abspath( |
| 83 | os.path.join(output_dir, 'en_generated_resources.rc')), |
| 84 | '-a', os.path.abspath( |
| 85 | os.path.join(output_dir, 'sv_generated_resources.rc')), |
| 86 | '-a', os.path.abspath( |
| 87 | os.path.join(output_dir, 'resource.h'))])) |
joi@chromium.org | 1eddb32 | 2012-04-03 10:15:24 +0000 | [diff] [blame] | 88 | |
newt@chromium.org | aaa243a | 2014-10-31 16:24:47 +0000 | [diff] [blame] | 89 | def _verifyWhitelistedOutput(self, |
| 90 | filename, |
| 91 | whitelisted_ids, |
| 92 | non_whitelisted_ids, |
| 93 | encoding='utf8'): |
| 94 | self.failUnless(os.path.exists(filename)) |
| 95 | whitelisted_ids_found = [] |
| 96 | non_whitelisted_ids_found = [] |
| 97 | with codecs.open(filename, encoding=encoding) as f: |
| 98 | for line in f.readlines(): |
| 99 | for whitelisted_id in whitelisted_ids: |
| 100 | if whitelisted_id in line: |
| 101 | whitelisted_ids_found.append(whitelisted_id) |
| 102 | for non_whitelisted_id in non_whitelisted_ids: |
| 103 | if non_whitelisted_id in line: |
| 104 | non_whitelisted_ids_found.append(non_whitelisted_id) |
| 105 | self.longMessage = True |
| 106 | self.assertEqual(whitelisted_ids, |
| 107 | whitelisted_ids_found, |
| 108 | '\nin file {}'.format(os.path.basename(filename))) |
| 109 | non_whitelisted_msg = ('Non-Whitelisted IDs {} found in {}' |
| 110 | .format(non_whitelisted_ids_found, os.path.basename(filename))) |
| 111 | self.assertFalse(non_whitelisted_ids_found, non_whitelisted_msg) |
| 112 | |
| 113 | def testWhitelistStrings(self): |
| 114 | output_dir = tempfile.mkdtemp() |
| 115 | builder = build.RcBuilder() |
| 116 | class DummyOpts(object): |
| 117 | def __init__(self): |
| 118 | self.input = util.PathFromRoot('grit/testdata/whitelist_strings.grd') |
| 119 | self.verbose = False |
| 120 | self.extra_verbose = False |
| 121 | whitelist_file = util.PathFromRoot('grit/testdata/whitelist.txt') |
| 122 | builder.Run(DummyOpts(), ['-o', output_dir, |
| 123 | '-w', whitelist_file]) |
| 124 | header = os.path.join(output_dir, 'whitelist_test_resources.h') |
| 125 | rc = os.path.join(output_dir, 'en_whitelist_test_strings.rc') |
| 126 | |
| 127 | whitelisted_ids = ['IDS_MESSAGE_WHITELISTED'] |
| 128 | non_whitelisted_ids = ['IDS_MESSAGE_NOT_WHITELISTED'] |
| 129 | self._verifyWhitelistedOutput( |
| 130 | header, |
| 131 | whitelisted_ids, |
| 132 | non_whitelisted_ids, |
| 133 | ) |
| 134 | self._verifyWhitelistedOutput( |
| 135 | rc, |
| 136 | whitelisted_ids, |
| 137 | non_whitelisted_ids, |
| 138 | encoding='utf16' |
| 139 | ) |
| 140 | |
| 141 | def testWhitelistResources(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_resources.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 | map_cc = os.path.join(output_dir, 'whitelist_test_resources_map.cc') |
| 154 | map_h = os.path.join(output_dir, 'whitelist_test_resources_map.h') |
| 155 | pak = os.path.join(output_dir, 'whitelist_test_resources.pak') |
| 156 | |
| 157 | # Ensure the resource map header and .pak files exist, but don't verify |
| 158 | # their content. |
| 159 | self.failUnless(os.path.exists(map_h)) |
| 160 | self.failUnless(os.path.exists(pak)) |
| 161 | |
| 162 | whitelisted_ids = [ |
| 163 | 'IDR_STRUCTURE_WHITELISTED', |
| 164 | 'IDR_STRUCTURE_IN_TRUE_IF_WHITELISTED', |
| 165 | 'IDR_INCLUDE_WHITELISTED', |
| 166 | ] |
| 167 | non_whitelisted_ids = [ |
| 168 | 'IDR_STRUCTURE_NOT_WHITELISTED', |
| 169 | 'IDR_STRUCTURE_IN_TRUE_IF_NOT_WHITELISTED', |
| 170 | 'IDR_STRUCTURE_IN_FALSE_IF_WHITELISTED', |
| 171 | 'IDR_STRUCTURE_IN_FALSE_IF_NOT_WHITELISTED', |
| 172 | 'IDR_INCLUDE_NOT_WHITELISTED', |
| 173 | ] |
| 174 | for output_file in (header, map_cc): |
| 175 | self._verifyWhitelistedOutput( |
| 176 | output_file, |
| 177 | whitelisted_ids, |
| 178 | non_whitelisted_ids, |
| 179 | ) |
| 180 | |
newt@chromium.org | c538054 | 2014-11-04 18:51:39 +0000 | [diff] [blame^] | 181 | def testOutputAllResourceDefinesTrue(self): |
| 182 | output_dir = tempfile.mkdtemp() |
| 183 | builder = build.RcBuilder() |
| 184 | class DummyOpts(object): |
| 185 | def __init__(self): |
| 186 | self.input = util.PathFromRoot('grit/testdata/whitelist_resources.grd') |
| 187 | self.verbose = False |
| 188 | self.extra_verbose = False |
| 189 | whitelist_file = util.PathFromRoot('grit/testdata/whitelist.txt') |
| 190 | builder.Run(DummyOpts(), ['-o', output_dir, |
| 191 | '-w', whitelist_file, |
| 192 | '--output-all-resource-defines',]) |
| 193 | header = os.path.join(output_dir, 'whitelist_test_resources.h') |
| 194 | map_cc = os.path.join(output_dir, 'whitelist_test_resources_map.cc') |
| 195 | |
| 196 | whitelisted_ids = [ |
| 197 | 'IDR_STRUCTURE_WHITELISTED', |
| 198 | 'IDR_STRUCTURE_NOT_WHITELISTED', |
| 199 | 'IDR_STRUCTURE_IN_TRUE_IF_WHITELISTED', |
| 200 | 'IDR_STRUCTURE_IN_TRUE_IF_NOT_WHITELISTED', |
| 201 | 'IDR_STRUCTURE_IN_FALSE_IF_WHITELISTED', |
| 202 | 'IDR_STRUCTURE_IN_FALSE_IF_NOT_WHITELISTED', |
| 203 | 'IDR_INCLUDE_WHITELISTED', |
| 204 | 'IDR_INCLUDE_NOT_WHITELISTED', |
| 205 | ] |
| 206 | non_whitelisted_ids = [] |
| 207 | for output_file in (header, map_cc): |
| 208 | self._verifyWhitelistedOutput( |
| 209 | output_file, |
| 210 | whitelisted_ids, |
| 211 | non_whitelisted_ids, |
| 212 | ) |
| 213 | |
| 214 | def testOutputAllResourceDefinesFalse(self): |
| 215 | output_dir = tempfile.mkdtemp() |
| 216 | builder = build.RcBuilder() |
| 217 | class DummyOpts(object): |
| 218 | def __init__(self): |
| 219 | self.input = util.PathFromRoot('grit/testdata/whitelist_resources.grd') |
| 220 | self.verbose = False |
| 221 | self.extra_verbose = False |
| 222 | whitelist_file = util.PathFromRoot('grit/testdata/whitelist.txt') |
| 223 | builder.Run(DummyOpts(), ['-o', output_dir, |
| 224 | '-w', whitelist_file, |
| 225 | '--no-output-all-resource-defines',]) |
| 226 | header = os.path.join(output_dir, 'whitelist_test_resources.h') |
| 227 | map_cc = os.path.join(output_dir, 'whitelist_test_resources_map.cc') |
| 228 | |
| 229 | whitelisted_ids = [ |
| 230 | 'IDR_STRUCTURE_WHITELISTED', |
| 231 | 'IDR_STRUCTURE_IN_TRUE_IF_WHITELISTED', |
| 232 | 'IDR_INCLUDE_WHITELISTED', |
| 233 | ] |
| 234 | non_whitelisted_ids = [ |
| 235 | 'IDR_STRUCTURE_NOT_WHITELISTED', |
| 236 | 'IDR_STRUCTURE_IN_TRUE_IF_NOT_WHITELISTED', |
| 237 | 'IDR_STRUCTURE_IN_FALSE_IF_WHITELISTED', |
| 238 | 'IDR_STRUCTURE_IN_FALSE_IF_NOT_WHITELISTED', |
| 239 | 'IDR_INCLUDE_NOT_WHITELISTED', |
| 240 | ] |
| 241 | for output_file in (header, map_cc): |
| 242 | self._verifyWhitelistedOutput( |
| 243 | output_file, |
| 244 | whitelisted_ids, |
| 245 | non_whitelisted_ids, |
| 246 | ) |
| 247 | |
newt@chromium.org | aaa243a | 2014-10-31 16:24:47 +0000 | [diff] [blame] | 248 | |
joi@chromium.org | 1eddb32 | 2012-04-03 10:15:24 +0000 | [diff] [blame] | 249 | if __name__ == '__main__': |
| 250 | unittest.main() |