net cleanup: Remove unnecessary namespace prefixes.
[chromium-blink-merge.git] / tools / mb / mb_unittest.py
blob9551377626439f1545beee7581b0388ddd7b2aad
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):
104 mbw = FakeMBW()
105 if files:
106 for path, contents in files.items():
107 mbw.files[path] = contents
108 mbw.files.setdefault(mbw.default_config, TEST_CONFIG)
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_gyp_analyze(self):
157 self.check(['analyze', '-c', 'gyp_rel_bot', '//out/Release',
158 '/tmp/in.json', '/tmp/out.json'],
159 ret=0)
161 def test_gen(self):
162 self.check(['gen', '-c', 'gn_debug', '//out/Default'], ret=0)
163 self.check(['gen', '-c', 'gyp_rel_bot', '//out/Release'], ret=0)
165 def test_goma_dir_expansion(self):
166 self.check(['lookup', '-c', 'gyp_rel_bot', '-g', '/foo'], ret=0,
167 out=("python build/gyp_chromium -G 'output_dir=<path>' "
168 "-G config=Release -D goma=1 -D gomadir=/foo\n"))
169 self.check(['lookup', '-c', 'gn_rel_bot', '-g', '/foo'], ret=0,
170 out=("/fake_src/buildtools/linux64/gn gen '<path>' "
171 "'--args=is_debug=false use_goma=true "
172 "goma_dir=\"/foo\"'\n" ))
174 def test_help(self):
175 self.assertRaises(SystemExit, self.check, ['-h'])
176 self.assertRaises(SystemExit, self.check, ['help'])
177 self.assertRaises(SystemExit, self.check, ['help', 'gen'])
179 def test_lookup(self):
180 self.check(['lookup', '-c', 'gn_debug'], ret=0)
182 def test_validate(self):
183 self.check(['validate'], ret=0)
186 if __name__ == '__main__':
187 unittest.main()