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
__()
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
38 self
.calls
.append(cmd
)
41 def Print(self
, *args
, **kwargs
):
42 sep
= kwargs
.get('sep', ' ')
43 end
= kwargs
.get('end', '\n')
44 f
= kwargs
.get('file', sys
.stdout
)
46 self
.err
+= sep
.join(args
) + end
48 self
.out
+= sep
.join(args
) + end
51 return FakeFile(self
.files
)
53 def RemoveFile(self
, path
):
57 class FakeFile(object):
58 def __init__(self
, files
):
59 self
.name
= '/tmp/file'
63 def write(self
, contents
):
67 self
.files
[self
.name
] = self
.buf
70 class IntegrationTest(unittest
.TestCase
):
71 def test_validate(self
):
72 # Note that this validates that the actual mb_config.pyl is valid.
73 ret
= mb
.main(['validate', '--quiet'])
74 self
.assertEqual(ret
, 0)
79 'common_dev_configs': ['gn_debug'],
81 'gyp_rel_bot': ['gyp', 'rel', 'goma'],
82 'gn_debug': ['gn', 'debug'],
83 'gn_rel_bot': ['gn', 'rel', 'goma'],
84 'private': ['gyp', 'fake_feature1'],
85 'unsupported': ['gn', 'fake_feature2'],
89 'fake_builder': 'gyp_rel_bot',
90 'fake_gn_builder': 'gn_rel_bot',
95 'gn_args': 'enable_doom_melon=true',
96 'gyp_defines': 'doom_melon=1',
99 'gn_args': 'enable_doom_melon=false',
100 'gyp_defaults': 'doom_melon=0',
102 'gyp': {'type': 'gyp'},
103 'gn': {'type': 'gn'},
105 'gn_args': 'use_goma=true goma_dir="$(goma_dir)"',
106 'gyp_defines': 'goma=1 gomadir="$(goma_dir)"',
109 'gn_args': 'is_debug=false',
110 'gyp_config': 'Release',
113 'gn_args': 'is_debug=true',
116 'private_configs': ['private'],
117 'unsupported_configs': ['unsupported'],
122 class UnitTest(unittest
.TestCase
):
123 def fake_mbw(self
, files
=None):
125 mbw
.files
.setdefault(mbw
.default_config
, TEST_CONFIG
)
127 for path
, contents
in files
.items():
128 mbw
.files
[path
] = contents
131 def check(self
, args
, mbw
=None, files
=None, out
=None, err
=None, ret
=None):
133 mbw
= self
.fake_mbw(files
)
135 actual_ret
= mbw
.args
.func()
137 self
.assertEqual(actual_ret
, ret
)
139 self
.assertEqual(mbw
.out
, out
)
141 self
.assertEqual(mbw
.err
, err
)
144 def test_gn_analyze(self
):
145 files
= {'/tmp/in.json': """{\
146 "files": ["foo/foo_unittest.cc"],
147 "targets": ["foo_unittests", "bar_unittests"]
150 mbw
= self
.fake_mbw(files
)
151 mbw
.Call
= lambda cmd
: (0, 'out/Default/foo_unittests\n', '')
153 self
.check(['analyze', '-c', 'gn_debug', '//out/Default',
154 '/tmp/in.json', '/tmp/out.json'], mbw
=mbw
, ret
=0)
155 out
= json
.loads(mbw
.files
['/tmp/out.json'])
156 self
.assertEqual(out
, {
157 'status': 'Found dependency',
158 'targets': ['foo_unittests'],
159 'build_targets': ['foo_unittests']
162 def test_gn_analyze_all(self
):
163 files
= {'/tmp/in.json': """{\
164 "files": ["foo/foo_unittest.cc"],
165 "targets": ["all", "bar_unittests"]
167 mbw
= self
.fake_mbw(files
)
168 mbw
.Call
= lambda cmd
: (0, 'out/Default/foo_unittests\n', '')
170 self
.check(['analyze', '-c', 'gn_debug', '//out/Default',
171 '/tmp/in.json', '/tmp/out.json'], mbw
=mbw
, ret
=0)
172 out
= json
.loads(mbw
.files
['/tmp/out.json'])
173 self
.assertEqual(out
, {
174 'status': 'Found dependency (all)',
177 def test_gn_analyze_missing_file(self
):
178 files
= {'/tmp/in.json': """{\
179 "files": ["foo/foo_unittest.cc"],
180 "targets": ["bar_unittests"]
182 mbw
= self
.fake_mbw(files
)
183 mbw
.Call
= lambda cmd
: (
184 1, 'The input matches no targets, configs, or files\n', '')
186 self
.check(['analyze', '-c', 'gn_debug', '//out/Default',
187 '/tmp/in.json', '/tmp/out.json'], mbw
=mbw
, ret
=0)
188 out
= json
.loads(mbw
.files
['/tmp/out.json'])
189 self
.assertEqual(out
, {
192 'status': 'No dependency',
195 def test_gyp_analyze(self
):
196 self
.check(['analyze', '-c', 'gyp_rel_bot', '//out/Release',
197 '/tmp/in.json', '/tmp/out.json'],
201 self
.check(['gen', '-c', 'gn_debug', '//out/Default'], ret
=0)
202 self
.check(['gen', '-c', 'gyp_rel_bot', '//out/Release'], ret
=0)
204 def test_gen_fails(self
):
205 mbw
= self
.fake_mbw()
206 mbw
.Call
= lambda cmd
: (1, '', '')
207 self
.check(['gen', '-c', 'gn_debug', '//out/Default'], mbw
=mbw
, ret
=1)
208 self
.check(['gen', '-c', 'gyp_rel_bot', '//out/Release'], mbw
=mbw
, ret
=1)
210 def test_goma_dir_expansion(self
):
211 self
.check(['lookup', '-c', 'gyp_rel_bot', '-g', '/foo'], ret
=0,
212 out
=("python build/gyp_chromium -G 'output_dir=<path>' "
213 "-G config=Release -D goma=1 -D gomadir=/foo\n"))
214 self
.check(['lookup', '-c', 'gn_rel_bot', '-g', '/foo'], ret
=0,
215 out
=("/fake_src/buildtools/linux64/gn gen '<path>' "
216 "'--args=is_debug=false use_goma=true "
217 "goma_dir=\"/foo\"'\n" ))
220 self
.assertRaises(SystemExit, self
.check
, ['-h'])
221 self
.assertRaises(SystemExit, self
.check
, ['help'])
222 self
.assertRaises(SystemExit, self
.check
, ['help', 'gen'])
224 def test_lookup(self
):
225 self
.check(['lookup', '-c', 'gn_debug'], ret
=0)
227 def test_validate(self
):
228 self
.check(['validate'], ret
=0)
231 if __name__
== '__main__':