Remove the 'gyp_config' concept from MB.
[chromium-blink-merge.git] / tools / mb / mb_unittest.py
bloba6f2789f59777995d10234700d30e64f842f2188
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'
28 self.rmdirs = []
30 def ExpandUser(self, path):
31 return '$HOME/%s' % path
33 def Exists(self, path):
34 return self.files.get(path) is not None
36 def MaybeMakeDirectory(self, path):
37 self.files[path] = True
39 def ReadFile(self, path):
40 return self.files[path]
42 def WriteFile(self, path, contents, force_verbose=False):
43 self.files[path] = contents
45 def Call(self, cmd, env=None):
46 if env:
47 self.cross_compile = env.get('GYP_CROSSCOMPILE')
48 self.calls.append(cmd)
49 if self.cmds:
50 return self.cmds.pop(0)
51 return 0, '', ''
53 def Print(self, *args, **kwargs):
54 sep = kwargs.get('sep', ' ')
55 end = kwargs.get('end', '\n')
56 f = kwargs.get('file', sys.stdout)
57 if f == sys.stderr:
58 self.err += sep.join(args) + end
59 else:
60 self.out += sep.join(args) + end
62 def TempFile(self, mode='w'):
63 return FakeFile(self.files)
65 def RemoveFile(self, path):
66 del self.files[path]
68 def RemoveDirectory(self, path):
69 self.rmdirs.append(path)
70 files_to_delete = [f for f in self.files if f.startswith(path)]
71 for f in files_to_delete:
72 self.files[f] = None
75 class FakeFile(object):
76 def __init__(self, files):
77 self.name = '/tmp/file'
78 self.buf = ''
79 self.files = files
81 def write(self, contents):
82 self.buf += contents
84 def close(self):
85 self.files[self.name] = self.buf
88 TEST_CONFIG = """\
90 'common_dev_configs': ['gn_debug'],
91 'configs': {
92 'gyp_rel_bot': ['gyp', 'rel', 'goma'],
93 'gn_debug': ['gn', 'debug'],
94 'gyp_debug': ['gyp', 'debug'],
95 'gn_rel_bot': ['gn', 'rel', 'goma'],
96 'private': ['gyp', 'rel', 'fake_feature1'],
97 'unsupported': ['gn', 'fake_feature2'],
99 'masters': {
100 'fake_master': {
101 'fake_builder': 'gyp_rel_bot',
102 'fake_gn_builder': 'gn_rel_bot',
103 'fake_gyp_builder': 'gyp_debug',
106 'mixins': {
107 'fake_feature1': {
108 'gn_args': 'enable_doom_melon=true',
109 'gyp_crosscompile': True,
110 'gyp_defines': 'doom_melon=1',
112 'fake_feature2': {
113 'gn_args': 'enable_doom_melon=false',
114 'gyp_defaults': 'doom_melon=0',
116 'gyp': {'type': 'gyp'},
117 'gn': {'type': 'gn'},
118 'goma': {
119 'gn_args': 'use_goma=true goma_dir="$(goma_dir)"',
120 'gyp_defines': 'goma=1 gomadir="$(goma_dir)"',
122 'rel': {
123 'gn_args': 'is_debug=false',
125 'debug': {
126 'gn_args': 'is_debug=true',
129 'private_configs': ['private'],
130 'unsupported_configs': ['unsupported'],
135 class UnitTest(unittest.TestCase):
136 def fake_mbw(self, files=None):
137 mbw = FakeMBW()
138 mbw.files.setdefault(mbw.default_config, TEST_CONFIG)
139 if files:
140 for path, contents in files.items():
141 mbw.files[path] = contents
142 return mbw
144 def check(self, args, mbw=None, files=None, out=None, err=None, ret=None):
145 if not mbw:
146 mbw = self.fake_mbw(files)
147 mbw.ParseArgs(args)
148 actual_ret = mbw.args.func()
149 if ret is not None:
150 self.assertEqual(actual_ret, ret)
151 if out is not None:
152 self.assertEqual(mbw.out, out)
153 if err is not None:
154 self.assertEqual(mbw.err, err)
155 return mbw
157 def test_clobber(self):
158 files = {
159 '/fake_src/out/Debug': None,
160 '/fake_src/out/Debug/mb_type': None,
162 mbw = self.fake_mbw(files)
164 # The first time we run this, the build dir doesn't exist, so no clobber.
165 self.check(['gen', '-c', 'gn_debug', '//out/Debug'], mbw=mbw, ret=0)
166 self.assertEqual(mbw.rmdirs, [])
167 self.assertTrue(mbw.files['/fake_src/out/Debug/mb_type'], 'gn')
169 # The second time we run this, the build dir exists and matches, so no
170 # clobber.
171 self.check(['gen', '-c', 'gn_debug', '//out/Debug'], mbw=mbw, ret=0)
172 self.assertEqual(mbw.rmdirs, [])
173 self.assertEqual(mbw.files['/fake_src/out/Debug/mb_type'], 'gn')
175 # Now we switch build types; this should result in a clobber.
176 self.check(['gen', '-c', 'gyp_debug', '//out/Debug'], mbw=mbw, ret=0)
177 self.assertEqual(mbw.rmdirs, ['/fake_src/out/Debug'])
178 self.assertEqual(mbw.files['/fake_src/out/Debug/mb_type'], 'gyp')
180 # Now we delete mb_type; this checks the case where the build dir
181 # exists but wasn't populated by mb; this should also result in a clobber.
182 del mbw.files['/fake_src/out/Debug/mb_type']
183 self.check(['gen', '-c', 'gyp_debug', '//out/Debug'], mbw=mbw, ret=0)
184 self.assertEqual(mbw.rmdirs,
185 ['/fake_src/out/Debug', '/fake_src/out/Debug'])
186 self.assertEqual(mbw.files['/fake_src/out/Debug/mb_type'], 'gyp')
188 def test_gn_analyze(self):
189 files = {'/tmp/in.json': """{\
190 "files": ["foo/foo_unittest.cc"],
191 "targets": ["foo_unittests", "bar_unittests"]
192 }"""}
194 mbw = self.fake_mbw(files)
195 mbw.Call = lambda cmd, env=None: (0, 'out/Default/foo_unittests\n', '')
197 self.check(['analyze', '-c', 'gn_debug', '//out/Default',
198 '/tmp/in.json', '/tmp/out.json'], mbw=mbw, ret=0)
199 out = json.loads(mbw.files['/tmp/out.json'])
200 self.assertEqual(out, {
201 'status': 'Found dependency',
202 'targets': ['foo_unittests'],
203 'build_targets': ['foo_unittests']
206 def test_gn_analyze_all(self):
207 files = {'/tmp/in.json': """{\
208 "files": ["foo/foo_unittest.cc"],
209 "targets": ["all", "bar_unittests"]
210 }"""}
211 mbw = self.fake_mbw(files)
212 mbw.Call = lambda cmd, env=None: (0, 'out/Default/foo_unittests\n', '')
213 self.check(['analyze', '-c', 'gn_debug', '//out/Default',
214 '/tmp/in.json', '/tmp/out.json'], mbw=mbw, ret=0)
215 out = json.loads(mbw.files['/tmp/out.json'])
216 self.assertEqual(out, {
217 'status': 'Found dependency (all)',
220 def test_gn_analyze_missing_file(self):
221 files = {'/tmp/in.json': """{\
222 "files": ["foo/foo_unittest.cc"],
223 "targets": ["bar_unittests"]
224 }"""}
225 mbw = self.fake_mbw(files)
226 mbw.cmds = [
227 (0, '', ''),
228 (1, 'The input matches no targets, configs, or files\n', ''),
229 (1, 'The input matches no targets, configs, or files\n', ''),
232 self.check(['analyze', '-c', 'gn_debug', '//out/Default',
233 '/tmp/in.json', '/tmp/out.json'], mbw=mbw, ret=0)
234 out = json.loads(mbw.files['/tmp/out.json'])
235 self.assertEqual(out, {
236 'build_targets': [],
237 'targets': [],
238 'status': 'No dependency',
241 def test_gn_gen(self):
242 self.check(['gen', '-c', 'gn_debug', '//out/Default'], ret=0)
243 self.check(['gen', '-c', 'gyp_rel_bot', '//out/Release'], ret=0)
245 def test_gn_gen_fails(self):
246 mbw = self.fake_mbw()
247 mbw.Call = lambda cmd, env=None: (1, '', '')
248 self.check(['gen', '-c', 'gn_debug', '//out/Default'], mbw=mbw, ret=1)
250 def test_gn_gen_swarming(self):
251 files = {
252 '/tmp/swarming_targets': 'base_unittests\n',
253 '/fake_src/testing/buildbot/gn_isolate_map.pyl': (
254 "{'base_unittests': {"
255 " 'label': '//base:base_unittests',"
256 " 'type': 'raw',"
257 " 'args': [],"
258 "}}\n"
260 '/fake_src/out/Default/base_unittests.runtime_deps': (
261 "base_unittests\n"
264 mbw = self.fake_mbw(files)
265 self.check(['gen',
266 '-c', 'gn_debug',
267 '--swarming-targets-file', '/tmp/swarming_targets',
268 '//out/Default'], mbw=mbw, ret=0)
269 self.assertIn('/fake_src/out/Default/base_unittests.isolate',
270 mbw.files)
271 self.assertIn('/fake_src/out/Default/base_unittests.isolated.gen.json',
272 mbw.files)
274 def test_gn_lookup(self):
275 self.check(['lookup', '-c', 'gn_debug'], ret=0)
277 def test_gn_lookup_goma_dir_expansion(self):
278 self.check(['lookup', '-c', 'gn_rel_bot', '-g', '/foo'], ret=0,
279 out=("/fake_src/buildtools/linux64/gn gen '<path>' "
280 "'--args=is_debug=false use_goma=true "
281 "goma_dir=\"/foo\"'\n" ))
283 def test_gyp_analyze(self):
284 mbw = self.check(['analyze', '-c', 'gyp_rel_bot', '//out/Release',
285 '/tmp/in.json', '/tmp/out.json'],
286 ret=0)
287 self.assertIn('analyzer', mbw.calls[0])
289 def test_gyp_crosscompile(self):
290 mbw = self.fake_mbw()
291 self.check(['gen', '-c', 'private', '//out/Release'], mbw=mbw)
292 self.assertTrue(mbw.cross_compile)
294 def test_gyp_gen(self):
295 self.check(['gen', '-c', 'gyp_rel_bot', '//out/Release'], ret=0)
297 def test_gyp_gen_fails(self):
298 mbw = self.fake_mbw()
299 mbw.Call = lambda cmd, env=None: (1, '', '')
300 self.check(['gen', '-c', 'gyp_rel_bot', '//out/Release'], mbw=mbw, ret=1)
302 def test_gyp_lookup_goma_dir_expansion(self):
303 self.check(['lookup', '-c', 'gyp_rel_bot', '-g', '/foo'], ret=0,
304 out=("python build/gyp_chromium -G 'output_dir=<path>' "
305 "-D goma=1 -D gomadir=/foo\n"))
307 def test_help(self):
308 orig_stdout = sys.stdout
309 try:
310 sys.stdout = StringIO.StringIO()
311 self.assertRaises(SystemExit, self.check, ['-h'])
312 self.assertRaises(SystemExit, self.check, ['help'])
313 self.assertRaises(SystemExit, self.check, ['help', 'gen'])
314 finally:
315 sys.stdout = orig_stdout
318 def test_validate(self):
319 self.check(['validate'], ret=0)
322 if __name__ == '__main__':
323 unittest.main()