Revert of Revert of Some webview documentation polish fixes. (patchset #1 id:1 of...
[chromium-blink-merge.git] / tools / mb / mb_unittest.py
blobac822ab49746e9c3da6475ce1985cb7793f44502
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 class IntegrationTest(unittest.TestCase):
51 def test_validate(self):
52 # Note that this validates that the actual mb_config.pyl is valid.
53 ret = mb.main(['validate', '--quiet'])
54 self.assertEqual(ret, 0)
57 TEST_CONFIG = """\
59 'common_dev_configs': ['gn_debug'],
60 'configs': {
61 'gyp_rel_bot': ['gyp', 'rel', 'goma'],
62 'gn_debug': ['gn', 'debug'],
63 'gn_rel_bot': ['gn', 'rel', 'goma'],
64 'private': ['gyp', 'fake_feature1'],
65 'unsupported': ['gn', 'fake_feature2'],
67 'masters': {
68 'fake_master': {
69 'fake_builder': 'gyp_rel_bot',
70 'fake_gn_builder': 'gn_rel_bot',
73 'mixins': {
74 'fake_feature1': {
75 'gn_args': 'enable_doom_melon=true',
76 'gyp_defines': 'doom_melon=1',
78 'fake_feature2': {
79 'gn_args': 'enable_doom_melon=false',
80 'gyp_defaults': 'doom_melon=0',
82 'gyp': {'type': 'gyp'},
83 'gn': {'type': 'gn'},
84 'goma': {
85 'gn_args': 'use_goma=true goma_dir="$(goma_dir)"',
86 'gyp_defines': 'goma=1 gomadir="$(goma_dir)"',
88 'rel': {
89 'gn_args': 'is_debug=false',
90 'gyp_config': 'Release',
92 'debug': {
93 'gn_args': 'is_debug=true',
96 'private_configs': ['private'],
97 'unsupported_configs': ['unsupported'],
99 """
102 class UnitTest(unittest.TestCase):
103 def fake_mbw(self, files=None):
104 mbw = FakeMBW()
105 mbw.files.setdefault(mbw.default_config, TEST_CONFIG)
106 if files:
107 for path, contents in files.items():
108 mbw.files[path] = contents
109 return mbw
111 def check(self, args, mbw=None, files=None, out=None, err=None, ret=None):
112 if not mbw:
113 mbw = self.fake_mbw(files)
114 mbw.ParseArgs(args)
115 actual_ret = mbw.args.func()
116 if ret is not None:
117 self.assertEqual(actual_ret, ret)
118 if out is not None:
119 self.assertEqual(mbw.out, out)
120 if err is not None:
121 self.assertEqual(mbw.err, err)
122 return mbw
124 def test_gn_analyze(self):
125 files = {'/tmp/in.json': """{\
126 "files": ["foo/foo_unittest.cc"],
127 "targets": ["foo_unittests", "bar_unittests"]
128 }"""}
129 mbw = self.fake_mbw(files)
130 mbw.Call = lambda cmd: (0, 'out/Default/foo_unittests\n', '')
132 self.check(['analyze', '-c', 'gn_debug', '//out/Default',
133 '/tmp/in.json', '/tmp/out.json'], mbw=mbw, ret=0)
134 out = json.loads(mbw.files['/tmp/out.json'])
135 self.assertEqual(out, {
136 'status': 'Found dependency',
137 'targets': ['foo_unittests'],
138 'build_targets': ['foo_unittests']
141 def test_gn_analyze_all(self):
142 files = {'/tmp/in.json': """{\
143 "files": ["foo/foo_unittest.cc"],
144 "targets": ["all", "bar_unittests"]
145 }"""}
146 mbw = self.fake_mbw(files)
147 mbw.Call = lambda cmd: (0, 'out/Default/foo_unittests\n', '')
149 self.check(['analyze', '-c', 'gn_debug', '//out/Default',
150 '/tmp/in.json', '/tmp/out.json'], mbw=mbw, ret=0)
151 out = json.loads(mbw.files['/tmp/out.json'])
152 self.assertEqual(out, {
153 'status': 'Found dependency (all)',
156 def test_gn_analyze_missing_file(self):
157 files = {'/tmp/in.json': """{\
158 "files": ["foo/foo_unittest.cc"],
159 "targets": ["bar_unittests"]
160 }"""}
161 mbw = self.fake_mbw(files)
162 mbw.Call = lambda cmd: (
163 1, 'The input matches no targets, configs, or files\n', '')
165 self.check(['analyze', '-c', 'gn_debug', '//out/Default',
166 '/tmp/in.json', '/tmp/out.json'], mbw=mbw, ret=0)
167 out = json.loads(mbw.files['/tmp/out.json'])
168 self.assertEqual(out, {
169 'build_targets': [],
170 'targets': [],
171 'status': 'No dependency',
174 def test_gyp_analyze(self):
175 self.check(['analyze', '-c', 'gyp_rel_bot', '//out/Release',
176 '/tmp/in.json', '/tmp/out.json'],
177 ret=0)
179 def test_gen(self):
180 self.check(['gen', '-c', 'gn_debug', '//out/Default'], ret=0)
181 self.check(['gen', '-c', 'gyp_rel_bot', '//out/Release'], ret=0)
183 def test_gen_fails(self):
184 mbw = self.fake_mbw()
185 mbw.Call = lambda cmd: (1, '', '')
186 self.check(['gen', '-c', 'gn_debug', '//out/Default'], mbw=mbw, ret=1)
187 self.check(['gen', '-c', 'gyp_rel_bot', '//out/Release'], mbw=mbw, ret=1)
189 def test_goma_dir_expansion(self):
190 self.check(['lookup', '-c', 'gyp_rel_bot', '-g', '/foo'], ret=0,
191 out=("python build/gyp_chromium -G 'output_dir=<path>' "
192 "-G config=Release -D goma=1 -D gomadir=/foo\n"))
193 self.check(['lookup', '-c', 'gn_rel_bot', '-g', '/foo'], ret=0,
194 out=("/fake_src/buildtools/linux64/gn gen '<path>' "
195 "'--args=is_debug=false use_goma=true "
196 "goma_dir=\"/foo\"'\n" ))
198 def test_help(self):
199 self.assertRaises(SystemExit, self.check, ['-h'])
200 self.assertRaises(SystemExit, self.check, ['help'])
201 self.assertRaises(SystemExit, self.check, ['help', 'gen'])
203 def test_lookup(self):
204 self.check(['lookup', '-c', 'gn_debug'], ret=0)
206 def test_validate(self):
207 self.check(['validate'], ret=0)
210 if __name__ == '__main__':
211 unittest.main()