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
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)
59 'common_dev_configs': ['gn_debug'],
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'],
69 'fake_builder': 'gyp_rel_bot',
70 'fake_gn_builder': 'gn_rel_bot',
75 'gn_args': 'enable_doom_melon=true',
76 'gyp_defines': 'doom_melon=1',
79 'gn_args': 'enable_doom_melon=false',
80 'gyp_defaults': 'doom_melon=0',
82 'gyp': {'type': 'gyp'},
85 'gn_args': 'use_goma=true goma_dir="$(goma_dir)"',
86 'gyp_defines': 'goma=1 gomadir="$(goma_dir)"',
89 'gn_args': 'is_debug=false',
90 'gyp_config': 'Release',
93 'gn_args': 'is_debug=true',
96 'private_configs': ['private'],
97 'unsupported_configs': ['unsupported'],
102 class UnitTest(unittest
.TestCase
):
103 def fake_mbw(self
, files
=None):
105 mbw
.files
.setdefault(mbw
.default_config
, TEST_CONFIG
)
107 for path
, contents
in files
.items():
108 mbw
.files
[path
] = contents
111 def check(self
, args
, mbw
=None, files
=None, out
=None, err
=None, ret
=None):
113 mbw
= self
.fake_mbw(files
)
115 actual_ret
= mbw
.args
.func()
117 self
.assertEqual(actual_ret
, ret
)
119 self
.assertEqual(mbw
.out
, out
)
121 self
.assertEqual(mbw
.err
, err
)
124 def test_gn_analyze(self
):
125 files
= {'/tmp/in.json': """{\
126 "files": ["foo/foo_unittest.cc"],
127 "targets": ["foo_unittests", "bar_unittests"]
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"]
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_gn_analyze_missing_file(self
):
157 files
= {'/tmp/in.json': """{\
158 "files": ["foo/foo_unittest.cc"],
159 "targets": ["bar_unittests"]
161 mbw
= self
.fake_mbw(files
)
162 mbw
.Call
= lambda cmd
: (
163 1, 'The input matches no targets, configs, or files\n', '')
165 self
.check(['analyze', '-c', 'gn_debug', '//out/Default',
166 '/tmp/in.json', '/tmp/out.json'], mbw
=mbw
, ret
=0)
167 out
= json
.loads(mbw
.files
['/tmp/out.json'])
168 self
.assertEqual(out
, {
171 'status': 'No dependency',
174 def test_gyp_analyze(self
):
175 self
.check(['analyze', '-c', 'gyp_rel_bot', '//out/Release',
176 '/tmp/in.json', '/tmp/out.json'],
180 self
.check(['gen', '-c', 'gn_debug', '//out/Default'], ret
=0)
181 self
.check(['gen', '-c', 'gyp_rel_bot', '//out/Release'], ret
=0)
183 def test_gen_fails(self
):
184 mbw
= self
.fake_mbw()
185 mbw
.Call
= lambda cmd
: (1, '', '')
186 self
.check(['gen', '-c', 'gn_debug', '//out/Default'], mbw
=mbw
, ret
=1)
187 self
.check(['gen', '-c', 'gyp_rel_bot', '//out/Release'], mbw
=mbw
, ret
=1)
189 def test_goma_dir_expansion(self
):
190 self
.check(['lookup', '-c', 'gyp_rel_bot', '-g', '/foo'], ret
=0,
191 out
=("python build/gyp_chromium -G 'output_dir=<path>' "
192 "-G config=Release -D goma=1 -D gomadir=/foo\n"))
193 self
.check(['lookup', '-c', 'gn_rel_bot', '-g', '/foo'], ret
=0,
194 out
=("/fake_src/buildtools/linux64/gn gen '<path>' "
195 "'--args=is_debug=false use_goma=true "
196 "goma_dir=\"/foo\"'\n" ))
199 self
.assertRaises(SystemExit, self
.check
, ['-h'])
200 self
.assertRaises(SystemExit, self
.check
, ['help'])
201 self
.assertRaises(SystemExit, self
.check
, ['help', 'gen'])
203 def test_lookup(self
):
204 self
.check(['lookup', '-c', 'gn_debug'], ret
=0)
206 def test_validate(self
):
207 self
.check(['validate'], ret
=0)
210 if __name__
== '__main__':