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.
14 class FakeMBW(mb
.MetaBuildWrapper
):
16 super(FakeMBW
, self
).__init
__()
22 self
.platform
= 'linux2'
23 self
.chromium_src_dir
= '/fake_src'
24 self
.default_config
= '/fake_src/tools/mb/mb_config.pyl'
26 def ExpandUser(self
, path
):
27 return '$HOME/%s' % path
29 def Exists(self
, path
):
30 return self
.files
.get(path
) is not None
32 def MaybeMakeDirectory(self
, path
):
35 def ReadFile(self
, path
):
36 return self
.files
[path
]
38 def WriteFile(self
, path
, contents
):
39 self
.files
[path
] = contents
42 self
.calls
.append(cmd
)
44 return self
.cmds
.pop(0)
47 def Print(self
, *args
, **kwargs
):
48 sep
= kwargs
.get('sep', ' ')
49 end
= kwargs
.get('end', '\n')
50 f
= kwargs
.get('file', sys
.stdout
)
52 self
.err
+= sep
.join(args
) + end
54 self
.out
+= sep
.join(args
) + end
56 def TempFile(self
, mode
='w'):
57 return FakeFile(self
.files
)
59 def RemoveFile(self
, path
):
63 class FakeFile(object):
64 def __init__(self
, files
):
65 self
.name
= '/tmp/file'
69 def write(self
, contents
):
73 self
.files
[self
.name
] = self
.buf
76 class IntegrationTest(unittest
.TestCase
):
77 def test_validate(self
):
78 # Note that this validates that the actual mb_config.pyl is valid.
79 ret
= mb
.main(['validate', '--quiet'])
80 self
.assertEqual(ret
, 0)
85 'common_dev_configs': ['gn_debug'],
87 'gyp_rel_bot': ['gyp', 'rel', 'goma'],
88 'gn_debug': ['gn', 'debug'],
89 'gn_rel_bot': ['gn', 'rel', 'goma'],
90 'private': ['gyp', 'fake_feature1'],
91 'unsupported': ['gn', 'fake_feature2'],
95 'fake_builder': 'gyp_rel_bot',
96 'fake_gn_builder': 'gn_rel_bot',
101 'gn_args': 'enable_doom_melon=true',
102 'gyp_defines': 'doom_melon=1',
105 'gn_args': 'enable_doom_melon=false',
106 'gyp_defaults': 'doom_melon=0',
108 'gyp': {'type': 'gyp'},
109 'gn': {'type': 'gn'},
111 'gn_args': 'use_goma=true goma_dir="$(goma_dir)"',
112 'gyp_defines': 'goma=1 gomadir="$(goma_dir)"',
115 'gn_args': 'is_debug=false',
116 'gyp_config': 'Release',
119 'gn_args': 'is_debug=true',
122 'private_configs': ['private'],
123 'unsupported_configs': ['unsupported'],
128 class UnitTest(unittest
.TestCase
):
129 def fake_mbw(self
, files
=None):
131 mbw
.files
.setdefault(mbw
.default_config
, TEST_CONFIG
)
133 for path
, contents
in files
.items():
134 mbw
.files
[path
] = contents
137 def check(self
, args
, mbw
=None, files
=None, out
=None, err
=None, ret
=None):
139 mbw
= self
.fake_mbw(files
)
141 actual_ret
= mbw
.args
.func()
143 self
.assertEqual(actual_ret
, ret
)
145 self
.assertEqual(mbw
.out
, out
)
147 self
.assertEqual(mbw
.err
, err
)
150 def test_gn_analyze(self
):
151 files
= {'/tmp/in.json': """{\
152 "files": ["foo/foo_unittest.cc"],
153 "targets": ["foo_unittests", "bar_unittests"]
156 mbw
= self
.fake_mbw(files
)
157 mbw
.Call
= lambda cmd
: (0, 'out/Default/foo_unittests\n', '')
159 self
.check(['analyze', '-c', 'gn_debug', '//out/Default',
160 '/tmp/in.json', '/tmp/out.json'], mbw
=mbw
, ret
=0)
161 out
= json
.loads(mbw
.files
['/tmp/out.json'])
162 self
.assertEqual(out
, {
163 'status': 'Found dependency',
164 'targets': ['foo_unittests'],
165 'build_targets': ['foo_unittests']
168 def test_gn_analyze_all(self
):
169 files
= {'/tmp/in.json': """{\
170 "files": ["foo/foo_unittest.cc"],
171 "targets": ["all", "bar_unittests"]
173 mbw
= self
.fake_mbw(files
)
174 mbw
.Call
= lambda cmd
: (0, 'out/Default/foo_unittests\n', '')
175 self
.check(['analyze', '-c', 'gn_debug', '//out/Default',
176 '/tmp/in.json', '/tmp/out.json'], mbw
=mbw
, ret
=0)
177 out
= json
.loads(mbw
.files
['/tmp/out.json'])
178 self
.assertEqual(out
, {
179 'status': 'Found dependency (all)',
182 def test_gn_analyze_missing_file(self
):
183 files
= {'/tmp/in.json': """{\
184 "files": ["foo/foo_unittest.cc"],
185 "targets": ["bar_unittests"]
187 mbw
= self
.fake_mbw(files
)
190 (1, 'The input matches no targets, configs, or files\n', ''),
191 (1, 'The input matches no targets, configs, or files\n', ''),
194 self
.check(['analyze', '-c', 'gn_debug', '//out/Default',
195 '/tmp/in.json', '/tmp/out.json'], mbw
=mbw
, ret
=0)
196 out
= json
.loads(mbw
.files
['/tmp/out.json'])
197 self
.assertEqual(out
, {
200 'status': 'No dependency',
203 def test_gn_gen(self
):
204 self
.check(['gen', '-c', 'gn_debug', '//out/Default'], ret
=0)
205 self
.check(['gen', '-c', 'gyp_rel_bot', '//out/Release'], ret
=0)
207 def test_gn_gen_fails(self
):
208 mbw
= self
.fake_mbw()
209 mbw
.Call
= lambda cmd
: (1, '', '')
210 self
.check(['gen', '-c', 'gn_debug', '//out/Default'], mbw
=mbw
, ret
=1)
212 def test_gn_gen_swarming(self
):
214 '/tmp/swarming_targets': 'base_unittests\n',
215 '/fake_src/testing/buildbot/ninja_to_gn.pyl': (
216 "{'base_unittests': '//base:base_unittests'}\n"
218 '/fake_src/out/Default/base_unittests.runtime_deps': (
222 mbw
= self
.fake_mbw(files
)
225 '--swarming-targets-file', '/tmp/swarming_targets',
226 '//out/Default'], mbw
=mbw
, ret
=0)
227 self
.assertIn('/fake_src/out/Default/base_unittests.isolate',
229 self
.assertIn('/fake_src/out/Default/base_unittests.isolated.gen.json',
232 def test_gn_lookup(self
):
233 self
.check(['lookup', '-c', 'gn_debug'], ret
=0)
235 def test_gn_lookup_goma_dir_expansion(self
):
236 self
.check(['lookup', '-c', 'gn_rel_bot', '-g', '/foo'], ret
=0,
237 out
=("/fake_src/buildtools/linux64/gn gen '<path>' "
238 "'--args=is_debug=false use_goma=true "
239 "goma_dir=\"/foo\"'\n" ))
241 def test_gyp_analyze(self
):
242 self
.check(['analyze', '-c', 'gyp_rel_bot', '//out/Release',
243 '/tmp/in.json', '/tmp/out.json'],
246 def test_gyp_gen(self
):
247 self
.check(['gen', '-c', 'gyp_rel_bot', '//out/Release'], ret
=0)
249 def test_gyp_gen_fails(self
):
250 mbw
= self
.fake_mbw()
251 mbw
.Call
= lambda cmd
: (1, '', '')
252 self
.check(['gen', '-c', 'gyp_rel_bot', '//out/Release'], mbw
=mbw
, ret
=1)
254 def test_gyp_lookup_goma_dir_expansion(self
):
255 self
.check(['lookup', '-c', 'gyp_rel_bot', '-g', '/foo'], ret
=0,
256 out
=("python build/gyp_chromium -G 'output_dir=<path>' "
257 "-G config=Release -D goma=1 -D gomadir=/foo\n"))
260 self
.assertRaises(SystemExit, self
.check
, ['-h'])
261 self
.assertRaises(SystemExit, self
.check
, ['help'])
262 self
.assertRaises(SystemExit, self
.check
, ['help', 'gen'])
264 def test_validate(self
):
265 self
.check(['validate'], ret
=0)
268 if __name__
== '__main__':