Update V8 to version 4.6.55.
[chromium-blink-merge.git] / tools / mb / mb_unittest.py
blob6723950b96ef8d2bd1e0b1f2c5da6ccbbdc0c8a4
1 # Copyright 2015 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 """Tests for mb.py."""
7 import json
8 import sys
9 import unittest
11 import mb
14 class FakeMBW(mb.MetaBuildWrapper):
15 def __init__(self):
16 super(FakeMBW, self).__init__()
17 self.files = {}
18 self.calls = []
19 self.cmds = []
20 self.out = ''
21 self.err = ''
22 self.platform = 'linux2'
23 self.chromium_src_dir = '/fake_src'
24 self.default_config = '/fake_src/tools/mb/mb_config.pyl'
26 def ExpandUser(self, path):
27 return '$HOME/%s' % path
29 def Exists(self, path):
30 return self.files.get(path) is not None
32 def MaybeMakeDirectory(self, path):
33 pass
35 def ReadFile(self, path):
36 return self.files[path]
38 def WriteFile(self, path, contents):
39 self.files[path] = contents
41 def Call(self, cmd):
42 self.calls.append(cmd)
43 if self.cmds:
44 return self.cmds.pop(0)
45 return 0, '', ''
47 def Print(self, *args, **kwargs):
48 sep = kwargs.get('sep', ' ')
49 end = kwargs.get('end', '\n')
50 f = kwargs.get('file', sys.stdout)
51 if f == sys.stderr:
52 self.err += sep.join(args) + end
53 else:
54 self.out += sep.join(args) + end
56 def TempFile(self, mode='w'):
57 return FakeFile(self.files)
59 def RemoveFile(self, path):
60 del self.files[path]
63 class FakeFile(object):
64 def __init__(self, files):
65 self.name = '/tmp/file'
66 self.buf = ''
67 self.files = files
69 def write(self, contents):
70 self.buf += contents
72 def close(self):
73 self.files[self.name] = self.buf
76 class IntegrationTest(unittest.TestCase):
77 def test_validate(self):
78 # Note that this validates that the actual mb_config.pyl is valid.
79 ret = mb.main(['validate', '--quiet'])
80 self.assertEqual(ret, 0)
83 TEST_CONFIG = """\
85 'common_dev_configs': ['gn_debug'],
86 'configs': {
87 'gyp_rel_bot': ['gyp', 'rel', 'goma'],
88 'gn_debug': ['gn', 'debug'],
89 'gn_rel_bot': ['gn', 'rel', 'goma'],
90 'private': ['gyp', 'fake_feature1'],
91 'unsupported': ['gn', 'fake_feature2'],
93 'masters': {
94 'fake_master': {
95 'fake_builder': 'gyp_rel_bot',
96 'fake_gn_builder': 'gn_rel_bot',
99 'mixins': {
100 'fake_feature1': {
101 'gn_args': 'enable_doom_melon=true',
102 'gyp_defines': 'doom_melon=1',
104 'fake_feature2': {
105 'gn_args': 'enable_doom_melon=false',
106 'gyp_defaults': 'doom_melon=0',
108 'gyp': {'type': 'gyp'},
109 'gn': {'type': 'gn'},
110 'goma': {
111 'gn_args': 'use_goma=true goma_dir="$(goma_dir)"',
112 'gyp_defines': 'goma=1 gomadir="$(goma_dir)"',
114 'rel': {
115 'gn_args': 'is_debug=false',
116 'gyp_config': 'Release',
118 'debug': {
119 'gn_args': 'is_debug=true',
122 'private_configs': ['private'],
123 'unsupported_configs': ['unsupported'],
128 class UnitTest(unittest.TestCase):
129 def fake_mbw(self, files=None):
130 mbw = FakeMBW()
131 mbw.files.setdefault(mbw.default_config, TEST_CONFIG)
132 if files:
133 for path, contents in files.items():
134 mbw.files[path] = contents
135 return mbw
137 def check(self, args, mbw=None, files=None, out=None, err=None, ret=None):
138 if not mbw:
139 mbw = self.fake_mbw(files)
140 mbw.ParseArgs(args)
141 actual_ret = mbw.args.func()
142 if ret is not None:
143 self.assertEqual(actual_ret, ret)
144 if out is not None:
145 self.assertEqual(mbw.out, out)
146 if err is not None:
147 self.assertEqual(mbw.err, err)
148 return mbw
150 def test_gn_analyze(self):
151 files = {'/tmp/in.json': """{\
152 "files": ["foo/foo_unittest.cc"],
153 "targets": ["foo_unittests", "bar_unittests"]
154 }"""}
156 mbw = self.fake_mbw(files)
157 mbw.Call = lambda cmd: (0, 'out/Default/foo_unittests\n', '')
159 self.check(['analyze', '-c', 'gn_debug', '//out/Default',
160 '/tmp/in.json', '/tmp/out.json'], mbw=mbw, ret=0)
161 out = json.loads(mbw.files['/tmp/out.json'])
162 self.assertEqual(out, {
163 'status': 'Found dependency',
164 'targets': ['foo_unittests'],
165 'build_targets': ['foo_unittests']
168 def test_gn_analyze_all(self):
169 files = {'/tmp/in.json': """{\
170 "files": ["foo/foo_unittest.cc"],
171 "targets": ["all", "bar_unittests"]
172 }"""}
173 mbw = self.fake_mbw(files)
174 mbw.Call = lambda cmd: (0, 'out/Default/foo_unittests\n', '')
175 self.check(['analyze', '-c', 'gn_debug', '//out/Default',
176 '/tmp/in.json', '/tmp/out.json'], mbw=mbw, ret=0)
177 out = json.loads(mbw.files['/tmp/out.json'])
178 self.assertEqual(out, {
179 'status': 'Found dependency (all)',
182 def test_gn_analyze_missing_file(self):
183 files = {'/tmp/in.json': """{\
184 "files": ["foo/foo_unittest.cc"],
185 "targets": ["bar_unittests"]
186 }"""}
187 mbw = self.fake_mbw(files)
188 mbw.cmds = [
189 (0, '', ''),
190 (1, 'The input matches no targets, configs, or files\n', ''),
191 (1, 'The input matches no targets, configs, or files\n', ''),
194 self.check(['analyze', '-c', 'gn_debug', '//out/Default',
195 '/tmp/in.json', '/tmp/out.json'], mbw=mbw, ret=0)
196 out = json.loads(mbw.files['/tmp/out.json'])
197 self.assertEqual(out, {
198 'build_targets': [],
199 'targets': [],
200 'status': 'No dependency',
203 def test_gn_gen(self):
204 self.check(['gen', '-c', 'gn_debug', '//out/Default'], ret=0)
205 self.check(['gen', '-c', 'gyp_rel_bot', '//out/Release'], ret=0)
207 def test_gn_gen_fails(self):
208 mbw = self.fake_mbw()
209 mbw.Call = lambda cmd: (1, '', '')
210 self.check(['gen', '-c', 'gn_debug', '//out/Default'], mbw=mbw, ret=1)
212 def test_gn_gen_swarming(self):
213 files = {
214 '/tmp/swarming_targets': 'base_unittests\n',
215 '/fake_src/testing/buildbot/ninja_to_gn.pyl': (
216 "{'base_unittests': '//base:base_unittests'}\n"
218 '/fake_src/out/Default/base_unittests.runtime_deps': (
219 "base_unittests\n"
222 mbw = self.fake_mbw(files)
223 self.check(['gen',
224 '-c', 'gn_debug',
225 '--swarming-targets-file', '/tmp/swarming_targets',
226 '//out/Default'], mbw=mbw, ret=0)
227 self.assertIn('/fake_src/out/Default/base_unittests.isolate',
228 mbw.files)
229 self.assertIn('/fake_src/out/Default/base_unittests.isolated.gen.json',
230 mbw.files)
232 def test_gn_lookup(self):
233 self.check(['lookup', '-c', 'gn_debug'], ret=0)
235 def test_gn_lookup_goma_dir_expansion(self):
236 self.check(['lookup', '-c', 'gn_rel_bot', '-g', '/foo'], ret=0,
237 out=("/fake_src/buildtools/linux64/gn gen '<path>' "
238 "'--args=is_debug=false use_goma=true "
239 "goma_dir=\"/foo\"'\n" ))
241 def test_gyp_analyze(self):
242 self.check(['analyze', '-c', 'gyp_rel_bot', '//out/Release',
243 '/tmp/in.json', '/tmp/out.json'],
244 ret=0)
246 def test_gyp_gen(self):
247 self.check(['gen', '-c', 'gyp_rel_bot', '//out/Release'], ret=0)
249 def test_gyp_gen_fails(self):
250 mbw = self.fake_mbw()
251 mbw.Call = lambda cmd: (1, '', '')
252 self.check(['gen', '-c', 'gyp_rel_bot', '//out/Release'], mbw=mbw, ret=1)
254 def test_gyp_lookup_goma_dir_expansion(self):
255 self.check(['lookup', '-c', 'gyp_rel_bot', '-g', '/foo'], ret=0,
256 out=("python build/gyp_chromium -G 'output_dir=<path>' "
257 "-G config=Release -D goma=1 -D gomadir=/foo\n"))
259 def test_help(self):
260 self.assertRaises(SystemExit, self.check, ['-h'])
261 self.assertRaises(SystemExit, self.check, ['help'])
262 self.assertRaises(SystemExit, self.check, ['help', 'gen'])
264 def test_validate(self):
265 self.check(['validate'], ret=0)
268 if __name__ == '__main__':
269 unittest.main()