[Proximity Auth] Port the UnlockManager class.
[chromium-blink-merge.git] / tools / mb / mb_unittest.py
blob942defb9f98cc8ca6fdb03d726d0e846f2e15832
1 #!/usr/bin/python
2 # Copyright 2015 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.
6 """Tests for mb.py."""
8 import json
9 import StringIO
10 import sys
11 import unittest
13 import mb
16 class FakeMBW(mb.MetaBuildWrapper):
17 def __init__(self):
18 super(FakeMBW, self).__init__()
19 self.files = {}
20 self.calls = []
21 self.cmds = []
22 self.cross_compile = None
23 self.out = ''
24 self.err = ''
25 self.platform = 'linux2'
26 self.chromium_src_dir = '/fake_src'
27 self.default_config = '/fake_src/tools/mb/mb_config.pyl'
29 def ExpandUser(self, path):
30 return '$HOME/%s' % path
32 def Exists(self, path):
33 return self.files.get(path) is not None
35 def MaybeMakeDirectory(self, path):
36 pass
38 def ReadFile(self, path):
39 return self.files[path]
41 def WriteFile(self, path, contents):
42 self.files[path] = contents
44 def Call(self, cmd, env=None):
45 if env:
46 self.cross_compile = env.get('GYP_CROSSCOMPILE')
47 self.calls.append(cmd)
48 if self.cmds:
49 return self.cmds.pop(0)
50 return 0, '', ''
52 def Print(self, *args, **kwargs):
53 sep = kwargs.get('sep', ' ')
54 end = kwargs.get('end', '\n')
55 f = kwargs.get('file', sys.stdout)
56 if f == sys.stderr:
57 self.err += sep.join(args) + end
58 else:
59 self.out += sep.join(args) + end
61 def TempFile(self, mode='w'):
62 return FakeFile(self.files)
64 def RemoveFile(self, path):
65 del self.files[path]
68 class FakeFile(object):
69 def __init__(self, files):
70 self.name = '/tmp/file'
71 self.buf = ''
72 self.files = files
74 def write(self, contents):
75 self.buf += contents
77 def close(self):
78 self.files[self.name] = self.buf
81 TEST_CONFIG = """\
83 'common_dev_configs': ['gn_debug'],
84 'configs': {
85 'gyp_rel_bot': ['gyp', 'rel', 'goma'],
86 'gn_debug': ['gn', 'debug'],
87 'gn_rel_bot': ['gn', 'rel', 'goma'],
88 'private': ['gyp', 'rel', 'fake_feature1'],
89 'unsupported': ['gn', 'fake_feature2'],
91 'masters': {
92 'fake_master': {
93 'fake_builder': 'gyp_rel_bot',
94 'fake_gn_builder': 'gn_rel_bot',
97 'mixins': {
98 'fake_feature1': {
99 'gn_args': 'enable_doom_melon=true',
100 'gyp_crosscompile': True,
101 'gyp_defines': 'doom_melon=1',
103 'fake_feature2': {
104 'gn_args': 'enable_doom_melon=false',
105 'gyp_defaults': 'doom_melon=0',
107 'gyp': {'type': 'gyp'},
108 'gn': {'type': 'gn'},
109 'goma': {
110 'gn_args': 'use_goma=true goma_dir="$(goma_dir)"',
111 'gyp_defines': 'goma=1 gomadir="$(goma_dir)"',
113 'rel': {
114 'gn_args': 'is_debug=false',
115 'gyp_config': 'Release',
117 'debug': {
118 'gn_args': 'is_debug=true',
121 'private_configs': ['private'],
122 'unsupported_configs': ['unsupported'],
127 class UnitTest(unittest.TestCase):
128 def fake_mbw(self, files=None):
129 mbw = FakeMBW()
130 mbw.files.setdefault(mbw.default_config, TEST_CONFIG)
131 if files:
132 for path, contents in files.items():
133 mbw.files[path] = contents
134 return mbw
136 def check(self, args, mbw=None, files=None, out=None, err=None, ret=None):
137 if not mbw:
138 mbw = self.fake_mbw(files)
139 mbw.ParseArgs(args)
140 actual_ret = mbw.args.func()
141 if ret is not None:
142 self.assertEqual(actual_ret, ret)
143 if out is not None:
144 self.assertEqual(mbw.out, out)
145 if err is not None:
146 self.assertEqual(mbw.err, err)
147 return mbw
149 def test_gn_analyze(self):
150 files = {'/tmp/in.json': """{\
151 "files": ["foo/foo_unittest.cc"],
152 "targets": ["foo_unittests", "bar_unittests"]
153 }"""}
155 mbw = self.fake_mbw(files)
156 mbw.Call = lambda cmd, env=None: (0, 'out/Default/foo_unittests\n', '')
158 self.check(['analyze', '-c', 'gn_debug', '//out/Default',
159 '/tmp/in.json', '/tmp/out.json'], mbw=mbw, ret=0)
160 out = json.loads(mbw.files['/tmp/out.json'])
161 self.assertEqual(out, {
162 'status': 'Found dependency',
163 'targets': ['foo_unittests'],
164 'build_targets': ['foo_unittests']
167 def test_gn_analyze_all(self):
168 files = {'/tmp/in.json': """{\
169 "files": ["foo/foo_unittest.cc"],
170 "targets": ["all", "bar_unittests"]
171 }"""}
172 mbw = self.fake_mbw(files)
173 mbw.Call = lambda cmd, env=None: (0, 'out/Default/foo_unittests\n', '')
174 self.check(['analyze', '-c', 'gn_debug', '//out/Default',
175 '/tmp/in.json', '/tmp/out.json'], mbw=mbw, ret=0)
176 out = json.loads(mbw.files['/tmp/out.json'])
177 self.assertEqual(out, {
178 'status': 'Found dependency (all)',
181 def test_gn_analyze_missing_file(self):
182 files = {'/tmp/in.json': """{\
183 "files": ["foo/foo_unittest.cc"],
184 "targets": ["bar_unittests"]
185 }"""}
186 mbw = self.fake_mbw(files)
187 mbw.cmds = [
188 (0, '', ''),
189 (1, 'The input matches no targets, configs, or files\n', ''),
190 (1, 'The input matches no targets, configs, or files\n', ''),
193 self.check(['analyze', '-c', 'gn_debug', '//out/Default',
194 '/tmp/in.json', '/tmp/out.json'], mbw=mbw, ret=0)
195 out = json.loads(mbw.files['/tmp/out.json'])
196 self.assertEqual(out, {
197 'build_targets': [],
198 'targets': [],
199 'status': 'No dependency',
202 def test_gn_gen(self):
203 self.check(['gen', '-c', 'gn_debug', '//out/Default'], ret=0)
204 self.check(['gen', '-c', 'gyp_rel_bot', '//out/Release'], ret=0)
206 def test_gn_gen_fails(self):
207 mbw = self.fake_mbw()
208 mbw.Call = lambda cmd, env=None: (1, '', '')
209 self.check(['gen', '-c', 'gn_debug', '//out/Default'], mbw=mbw, ret=1)
211 def test_gn_gen_swarming(self):
212 files = {
213 '/tmp/swarming_targets': 'base_unittests\n',
214 '/fake_src/testing/buildbot/gn_isolate_map.pyl': (
215 "{'base_unittests': {"
216 " 'label': '//base:base_unittests',"
217 " 'type': 'raw',"
218 " 'args': [],"
219 "}}\n"
221 '/fake_src/out/Default/base_unittests.runtime_deps': (
222 "base_unittests\n"
225 mbw = self.fake_mbw(files)
226 self.check(['gen',
227 '-c', 'gn_debug',
228 '--swarming-targets-file', '/tmp/swarming_targets',
229 '//out/Default'], mbw=mbw, ret=0)
230 self.assertIn('/fake_src/out/Default/base_unittests.isolate',
231 mbw.files)
232 self.assertIn('/fake_src/out/Default/base_unittests.isolated.gen.json',
233 mbw.files)
235 def test_gn_lookup(self):
236 self.check(['lookup', '-c', 'gn_debug'], ret=0)
238 def test_gn_lookup_goma_dir_expansion(self):
239 self.check(['lookup', '-c', 'gn_rel_bot', '-g', '/foo'], ret=0,
240 out=("/fake_src/buildtools/linux64/gn gen '<path>' "
241 "'--args=is_debug=false use_goma=true "
242 "goma_dir=\"/foo\"'\n" ))
244 def test_gyp_analyze(self):
245 mbw = self.check(['analyze', '-c', 'gyp_rel_bot', '//out/Release',
246 '/tmp/in.json', '/tmp/out.json'],
247 ret=0)
248 self.assertIn('analyzer', mbw.calls[0])
250 def test_gyp_crosscompile(self):
251 mbw = self.fake_mbw()
252 self.check(['gen', '-c', 'private', '//out/Release'], mbw=mbw)
253 self.assertTrue(mbw.cross_compile)
255 def test_gyp_gen(self):
256 self.check(['gen', '-c', 'gyp_rel_bot', '//out/Release'], ret=0)
258 def test_gyp_gen_fails(self):
259 mbw = self.fake_mbw()
260 mbw.Call = lambda cmd, env=None: (1, '', '')
261 self.check(['gen', '-c', 'gyp_rel_bot', '//out/Release'], mbw=mbw, ret=1)
263 def test_gyp_lookup_goma_dir_expansion(self):
264 self.check(['lookup', '-c', 'gyp_rel_bot', '-g', '/foo'], ret=0,
265 out=("python build/gyp_chromium -G 'output_dir=<path>' "
266 "-G config=Release -D goma=1 -D gomadir=/foo\n"))
268 def test_help(self):
269 orig_stdout = sys.stdout
270 try:
271 sys.stdout = StringIO.StringIO()
272 self.assertRaises(SystemExit, self.check, ['-h'])
273 self.assertRaises(SystemExit, self.check, ['help'])
274 self.assertRaises(SystemExit, self.check, ['help', 'gen'])
275 finally:
276 sys.stdout = orig_stdout
279 def test_validate(self):
280 self.check(['validate'], ret=0)
283 if __name__ == '__main__':
284 unittest.main()