Move BrowserCloseTest.* over to unit tests.
[chromium-blink-merge.git] / tools / mb / mb_unittest.py
blob4eb79b45acb954b1193821b46f95ec3aba1a0f75
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.out = ''
20 self.err = ''
21 self.platform = 'linux2'
22 self.chromium_src_dir = '/fake_src'
23 self.default_config = '/fake_src/tools/mb/mb_config.pyl'
25 def ExpandUser(self, path):
26 return '$HOME/%s' % path
28 def Exists(self, path):
29 return self.files.get(path) is not None
31 def ReadFile(self, path):
32 return self.files[path]
34 def WriteFile(self, path, contents):
35 self.files[path] = contents
37 def Call(self, cmd):
38 self.calls.append(cmd)
39 return 0, '', ''
41 def Print(self, *args, **kwargs):
42 sep = kwargs.get('sep', ' ')
43 end = kwargs.get('end', '\n')
44 f = kwargs.get('file', sys.stdout)
45 if f == sys.stderr:
46 self.err += sep.join(args) + end
47 else:
48 self.out += sep.join(args) + end
50 def TempFile(self):
51 return FakeFile(self.files)
53 def RemoveFile(self, path):
54 del self.files[path]
57 class FakeFile(object):
58 def __init__(self, files):
59 self.name = '/tmp/file'
60 self.buf = ''
61 self.files = files
63 def write(self, contents):
64 self.buf += contents
66 def close(self):
67 self.files[self.name] = self.buf
70 class IntegrationTest(unittest.TestCase):
71 def test_validate(self):
72 # Note that this validates that the actual mb_config.pyl is valid.
73 ret = mb.main(['validate', '--quiet'])
74 self.assertEqual(ret, 0)
77 TEST_CONFIG = """\
79 'common_dev_configs': ['gn_debug'],
80 'configs': {
81 'gyp_rel_bot': ['gyp', 'rel', 'goma'],
82 'gn_debug': ['gn', 'debug'],
83 'gn_rel_bot': ['gn', 'rel', 'goma'],
84 'private': ['gyp', 'fake_feature1'],
85 'unsupported': ['gn', 'fake_feature2'],
87 'masters': {
88 'fake_master': {
89 'fake_builder': 'gyp_rel_bot',
90 'fake_gn_builder': 'gn_rel_bot',
93 'mixins': {
94 'fake_feature1': {
95 'gn_args': 'enable_doom_melon=true',
96 'gyp_defines': 'doom_melon=1',
98 'fake_feature2': {
99 'gn_args': 'enable_doom_melon=false',
100 'gyp_defaults': 'doom_melon=0',
102 'gyp': {'type': 'gyp'},
103 'gn': {'type': 'gn'},
104 'goma': {
105 'gn_args': 'use_goma=true goma_dir="$(goma_dir)"',
106 'gyp_defines': 'goma=1 gomadir="$(goma_dir)"',
108 'rel': {
109 'gn_args': 'is_debug=false',
110 'gyp_config': 'Release',
112 'debug': {
113 'gn_args': 'is_debug=true',
116 'private_configs': ['private'],
117 'unsupported_configs': ['unsupported'],
122 class UnitTest(unittest.TestCase):
123 def fake_mbw(self, files=None):
124 mbw = FakeMBW()
125 mbw.files.setdefault(mbw.default_config, TEST_CONFIG)
126 if files:
127 for path, contents in files.items():
128 mbw.files[path] = contents
129 return mbw
131 def check(self, args, mbw=None, files=None, out=None, err=None, ret=None):
132 if not mbw:
133 mbw = self.fake_mbw(files)
134 mbw.ParseArgs(args)
135 actual_ret = mbw.args.func()
136 if ret is not None:
137 self.assertEqual(actual_ret, ret)
138 if out is not None:
139 self.assertEqual(mbw.out, out)
140 if err is not None:
141 self.assertEqual(mbw.err, err)
142 return mbw
144 def test_gn_analyze(self):
145 files = {'/tmp/in.json': """{\
146 "files": ["foo/foo_unittest.cc"],
147 "targets": ["foo_unittests", "bar_unittests"]
148 }"""}
150 mbw = self.fake_mbw(files)
151 mbw.Call = lambda cmd: (0, 'out/Default/foo_unittests\n', '')
153 self.check(['analyze', '-c', 'gn_debug', '//out/Default',
154 '/tmp/in.json', '/tmp/out.json'], mbw=mbw, ret=0)
155 out = json.loads(mbw.files['/tmp/out.json'])
156 self.assertEqual(out, {
157 'status': 'Found dependency',
158 'targets': ['foo_unittests'],
159 'build_targets': ['foo_unittests']
162 def test_gn_analyze_all(self):
163 files = {'/tmp/in.json': """{\
164 "files": ["foo/foo_unittest.cc"],
165 "targets": ["all", "bar_unittests"]
166 }"""}
167 mbw = self.fake_mbw(files)
168 mbw.Call = lambda cmd: (0, 'out/Default/foo_unittests\n', '')
170 self.check(['analyze', '-c', 'gn_debug', '//out/Default',
171 '/tmp/in.json', '/tmp/out.json'], mbw=mbw, ret=0)
172 out = json.loads(mbw.files['/tmp/out.json'])
173 self.assertEqual(out, {
174 'status': 'Found dependency (all)',
177 def test_gn_analyze_missing_file(self):
178 files = {'/tmp/in.json': """{\
179 "files": ["foo/foo_unittest.cc"],
180 "targets": ["bar_unittests"]
181 }"""}
182 mbw = self.fake_mbw(files)
183 mbw.Call = lambda cmd: (
184 1, 'The input matches no targets, configs, or files\n', '')
186 self.check(['analyze', '-c', 'gn_debug', '//out/Default',
187 '/tmp/in.json', '/tmp/out.json'], mbw=mbw, ret=0)
188 out = json.loads(mbw.files['/tmp/out.json'])
189 self.assertEqual(out, {
190 'build_targets': [],
191 'targets': [],
192 'status': 'No dependency',
195 def test_gyp_analyze(self):
196 self.check(['analyze', '-c', 'gyp_rel_bot', '//out/Release',
197 '/tmp/in.json', '/tmp/out.json'],
198 ret=0)
200 def test_gen(self):
201 self.check(['gen', '-c', 'gn_debug', '//out/Default'], ret=0)
202 self.check(['gen', '-c', 'gyp_rel_bot', '//out/Release'], ret=0)
204 def test_gen_fails(self):
205 mbw = self.fake_mbw()
206 mbw.Call = lambda cmd: (1, '', '')
207 self.check(['gen', '-c', 'gn_debug', '//out/Default'], mbw=mbw, ret=1)
208 self.check(['gen', '-c', 'gyp_rel_bot', '//out/Release'], mbw=mbw, ret=1)
210 def test_goma_dir_expansion(self):
211 self.check(['lookup', '-c', 'gyp_rel_bot', '-g', '/foo'], ret=0,
212 out=("python build/gyp_chromium -G 'output_dir=<path>' "
213 "-G config=Release -D goma=1 -D gomadir=/foo\n"))
214 self.check(['lookup', '-c', 'gn_rel_bot', '-g', '/foo'], ret=0,
215 out=("/fake_src/buildtools/linux64/gn gen '<path>' "
216 "'--args=is_debug=false use_goma=true "
217 "goma_dir=\"/foo\"'\n" ))
219 def test_help(self):
220 self.assertRaises(SystemExit, self.check, ['-h'])
221 self.assertRaises(SystemExit, self.check, ['help'])
222 self.assertRaises(SystemExit, self.check, ['help', 'gen'])
224 def test_lookup(self):
225 self.check(['lookup', '-c', 'gn_debug'], ret=0)
227 def test_validate(self):
228 self.check(['validate'], ret=0)
231 if __name__ == '__main__':
232 unittest.main()