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'
29 def ExpandUser(self
, path
):
30 return '$HOME/%s' % path
32 def Exists(self
, path
):
33 return self
.files
.get(path
) is not None
35 def MaybeMakeDirectory(self
, path
):
38 def ReadFile(self
, path
):
39 return self
.files
[path
]
41 def WriteFile(self
, path
, contents
):
42 self
.files
[path
] = contents
44 def Call(self
, cmd
, env
=None):
46 self
.cross_compile
= env
.get('GYP_CROSSCOMPILE')
47 self
.calls
.append(cmd
)
49 return self
.cmds
.pop(0)
52 def Print(self
, *args
, **kwargs
):
53 sep
= kwargs
.get('sep', ' ')
54 end
= kwargs
.get('end', '\n')
55 f
= kwargs
.get('file', sys
.stdout
)
57 self
.err
+= sep
.join(args
) + end
59 self
.out
+= sep
.join(args
) + end
61 def TempFile(self
, mode
='w'):
62 return FakeFile(self
.files
)
64 def RemoveFile(self
, path
):
68 class FakeFile(object):
69 def __init__(self
, files
):
70 self
.name
= '/tmp/file'
74 def write(self
, contents
):
78 self
.files
[self
.name
] = self
.buf
83 'common_dev_configs': ['gn_debug'],
85 'gyp_rel_bot': ['gyp', 'rel', 'goma'],
86 'gn_debug': ['gn', 'debug'],
87 'gn_rel_bot': ['gn', 'rel', 'goma'],
88 'private': ['gyp', 'rel', 'fake_feature1'],
89 'unsupported': ['gn', 'fake_feature2'],
93 'fake_builder': 'gyp_rel_bot',
94 'fake_gn_builder': 'gn_rel_bot',
99 'gn_args': 'enable_doom_melon=true',
100 'gyp_crosscompile': True,
101 'gyp_defines': 'doom_melon=1',
104 'gn_args': 'enable_doom_melon=false',
105 'gyp_defaults': 'doom_melon=0',
107 'gyp': {'type': 'gyp'},
108 'gn': {'type': 'gn'},
110 'gn_args': 'use_goma=true goma_dir="$(goma_dir)"',
111 'gyp_defines': 'goma=1 gomadir="$(goma_dir)"',
114 'gn_args': 'is_debug=false',
115 'gyp_config': 'Release',
118 'gn_args': 'is_debug=true',
121 'private_configs': ['private'],
122 'unsupported_configs': ['unsupported'],
127 class UnitTest(unittest
.TestCase
):
128 def fake_mbw(self
, files
=None):
130 mbw
.files
.setdefault(mbw
.default_config
, TEST_CONFIG
)
132 for path
, contents
in files
.items():
133 mbw
.files
[path
] = contents
136 def check(self
, args
, mbw
=None, files
=None, out
=None, err
=None, ret
=None):
138 mbw
= self
.fake_mbw(files
)
140 actual_ret
= mbw
.args
.func()
142 self
.assertEqual(actual_ret
, ret
)
144 self
.assertEqual(mbw
.out
, out
)
146 self
.assertEqual(mbw
.err
, err
)
149 def test_gn_analyze(self
):
150 files
= {'/tmp/in.json': """{\
151 "files": ["foo/foo_unittest.cc"],
152 "targets": ["foo_unittests", "bar_unittests"]
155 mbw
= self
.fake_mbw(files
)
156 mbw
.Call
= lambda cmd
, env
=None: (0, 'out/Default/foo_unittests\n', '')
158 self
.check(['analyze', '-c', 'gn_debug', '//out/Default',
159 '/tmp/in.json', '/tmp/out.json'], mbw
=mbw
, ret
=0)
160 out
= json
.loads(mbw
.files
['/tmp/out.json'])
161 self
.assertEqual(out
, {
162 'status': 'Found dependency',
163 'targets': ['foo_unittests'],
164 'build_targets': ['foo_unittests']
167 def test_gn_analyze_all(self
):
168 files
= {'/tmp/in.json': """{\
169 "files": ["foo/foo_unittest.cc"],
170 "targets": ["all", "bar_unittests"]
172 mbw
= self
.fake_mbw(files
)
173 mbw
.Call
= lambda cmd
, env
=None: (0, 'out/Default/foo_unittests\n', '')
174 self
.check(['analyze', '-c', 'gn_debug', '//out/Default',
175 '/tmp/in.json', '/tmp/out.json'], mbw
=mbw
, ret
=0)
176 out
= json
.loads(mbw
.files
['/tmp/out.json'])
177 self
.assertEqual(out
, {
178 'status': 'Found dependency (all)',
181 def test_gn_analyze_missing_file(self
):
182 files
= {'/tmp/in.json': """{\
183 "files": ["foo/foo_unittest.cc"],
184 "targets": ["bar_unittests"]
186 mbw
= self
.fake_mbw(files
)
189 (1, 'The input matches no targets, configs, or files\n', ''),
190 (1, 'The input matches no targets, configs, or files\n', ''),
193 self
.check(['analyze', '-c', 'gn_debug', '//out/Default',
194 '/tmp/in.json', '/tmp/out.json'], mbw
=mbw
, ret
=0)
195 out
= json
.loads(mbw
.files
['/tmp/out.json'])
196 self
.assertEqual(out
, {
199 'status': 'No dependency',
202 def test_gn_gen(self
):
203 self
.check(['gen', '-c', 'gn_debug', '//out/Default'], ret
=0)
204 self
.check(['gen', '-c', 'gyp_rel_bot', '//out/Release'], ret
=0)
206 def test_gn_gen_fails(self
):
207 mbw
= self
.fake_mbw()
208 mbw
.Call
= lambda cmd
, env
=None: (1, '', '')
209 self
.check(['gen', '-c', 'gn_debug', '//out/Default'], mbw
=mbw
, ret
=1)
211 def test_gn_gen_swarming(self
):
213 '/tmp/swarming_targets': 'base_unittests\n',
214 '/fake_src/testing/buildbot/gn_isolate_map.pyl': (
215 "{'base_unittests': {"
216 " 'label': '//base:base_unittests',"
221 '/fake_src/out/Default/base_unittests.runtime_deps': (
225 mbw
= self
.fake_mbw(files
)
228 '--swarming-targets-file', '/tmp/swarming_targets',
229 '//out/Default'], mbw
=mbw
, ret
=0)
230 self
.assertIn('/fake_src/out/Default/base_unittests.isolate',
232 self
.assertIn('/fake_src/out/Default/base_unittests.isolated.gen.json',
235 def test_gn_lookup(self
):
236 self
.check(['lookup', '-c', 'gn_debug'], ret
=0)
238 def test_gn_lookup_goma_dir_expansion(self
):
239 self
.check(['lookup', '-c', 'gn_rel_bot', '-g', '/foo'], ret
=0,
240 out
=("/fake_src/buildtools/linux64/gn gen '<path>' "
241 "'--args=is_debug=false use_goma=true "
242 "goma_dir=\"/foo\"'\n" ))
244 def test_gyp_analyze(self
):
245 mbw
= self
.check(['analyze', '-c', 'gyp_rel_bot', '//out/Release',
246 '/tmp/in.json', '/tmp/out.json'],
248 self
.assertIn('analyzer', mbw
.calls
[0])
250 def test_gyp_crosscompile(self
):
251 mbw
= self
.fake_mbw()
252 self
.check(['gen', '-c', 'private', '//out/Release'], mbw
=mbw
)
253 self
.assertTrue(mbw
.cross_compile
)
255 def test_gyp_gen(self
):
256 self
.check(['gen', '-c', 'gyp_rel_bot', '//out/Release'], ret
=0)
258 def test_gyp_gen_fails(self
):
259 mbw
= self
.fake_mbw()
260 mbw
.Call
= lambda cmd
, env
=None: (1, '', '')
261 self
.check(['gen', '-c', 'gyp_rel_bot', '//out/Release'], mbw
=mbw
, ret
=1)
263 def test_gyp_lookup_goma_dir_expansion(self
):
264 self
.check(['lookup', '-c', 'gyp_rel_bot', '-g', '/foo'], ret
=0,
265 out
=("python build/gyp_chromium -G 'output_dir=<path>' "
266 "-G config=Release -D goma=1 -D gomadir=/foo\n"))
269 orig_stdout
= sys
.stdout
271 sys
.stdout
= StringIO
.StringIO()
272 self
.assertRaises(SystemExit, self
.check
, ['-h'])
273 self
.assertRaises(SystemExit, self
.check
, ['help'])
274 self
.assertRaises(SystemExit, self
.check
, ['help', 'gen'])
276 sys
.stdout
= orig_stdout
279 def test_validate(self
):
280 self
.check(['validate'], ret
=0)
283 if __name__
== '__main__':