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.
16 class FakeMBW(mb
.MetaBuildWrapper
):
18 super(FakeMBW
, self
).__init
__()
22 self
.cross_compile
= None
25 self
.platform
= 'linux2'
26 self
.chromium_src_dir
= '/fake_src'
27 self
.default_config
= '/fake_src/tools/mb/mb_config.pyl'
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):
47 self
.cross_compile
= env
.get('GYP_CROSSCOMPILE')
48 self
.calls
.append(cmd
)
50 return self
.cmds
.pop(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
)
58 self
.err
+= sep
.join(args
) + end
60 self
.out
+= sep
.join(args
) + end
62 def TempFile(self
, mode
='w'):
63 return FakeFile(self
.files
)
65 def RemoveFile(self
, 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
:
75 class FakeFile(object):
76 def __init__(self
, files
):
77 self
.name
= '/tmp/file'
81 def write(self
, contents
):
85 self
.files
[self
.name
] = self
.buf
90 'common_dev_configs': ['gn_debug'],
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'],
101 'fake_builder': 'gyp_rel_bot',
102 'fake_gn_builder': 'gn_rel_bot',
103 'fake_gyp_builder': 'gyp_debug',
108 'gn_args': 'enable_doom_melon=true',
109 'gyp_crosscompile': True,
110 'gyp_defines': 'doom_melon=1',
113 'gn_args': 'enable_doom_melon=false',
114 'gyp_defaults': 'doom_melon=0',
116 'gyp': {'type': 'gyp'},
117 'gn': {'type': 'gn'},
119 'gn_args': 'use_goma=true goma_dir="$(goma_dir)"',
120 'gyp_defines': 'goma=1 gomadir="$(goma_dir)"',
123 'gn_args': 'is_debug=false',
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):
138 mbw
.files
.setdefault(mbw
.default_config
, TEST_CONFIG
)
140 for path
, contents
in files
.items():
141 mbw
.files
[path
] = contents
144 def check(self
, args
, mbw
=None, files
=None, out
=None, err
=None, ret
=None):
146 mbw
= self
.fake_mbw(files
)
148 actual_ret
= mbw
.args
.func()
150 self
.assertEqual(actual_ret
, ret
)
152 self
.assertEqual(mbw
.out
, out
)
154 self
.assertEqual(mbw
.err
, err
)
157 def test_clobber(self
):
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
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"]
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"]
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"]
225 mbw
= self
.fake_mbw(files
)
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
, {
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
):
252 '/tmp/swarming_targets': 'base_unittests\n',
253 '/fake_src/testing/buildbot/gn_isolate_map.pyl': (
254 "{'base_unittests': {"
255 " 'label': '//base:base_unittests',"
260 '/fake_src/out/Default/base_unittests.runtime_deps': (
264 mbw
= self
.fake_mbw(files
)
267 '--swarming-targets-file', '/tmp/swarming_targets',
268 '//out/Default'], mbw
=mbw
, ret
=0)
269 self
.assertIn('/fake_src/out/Default/base_unittests.isolate',
271 self
.assertIn('/fake_src/out/Default/base_unittests.isolated.gen.json',
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'],
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"))
308 orig_stdout
= sys
.stdout
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'])
315 sys
.stdout
= orig_stdout
318 def test_validate(self
):
319 self
.check(['validate'], ret
=0)
322 if __name__
== '__main__':