Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / native_client_sdk / src / build_tools / tests / build_artifacts_test.py
blobb40339ff7eafc90ae6b8f4dc28618652aede7537
1 #!/usr/bin/env python
2 # Copyright (c) 2014 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.
6 import os
7 import ntpath
8 import posixpath
9 import sys
10 import collections
11 import unittest
13 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
14 BUILD_TOOLS_DIR = os.path.dirname(SCRIPT_DIR)
15 CHROME_SRC = os.path.dirname(os.path.dirname(os.path.dirname(BUILD_TOOLS_DIR)))
16 MOCK_DIR = os.path.join(CHROME_SRC, 'third_party', 'pymock')
18 # For the mock library
19 sys.path.append(MOCK_DIR)
20 from mock import call, patch, Mock
22 sys.path.append(BUILD_TOOLS_DIR)
23 import build_artifacts
26 class BasePosixTestCase(unittest.TestCase):
27 def setUp(self):
28 self.addCleanup(patch.stopall)
29 patch('build_artifacts.PLATFORM', 'posix').start()
30 patch('build_artifacts.BUILD_ARCHIVE_DIR', '/archive_dir/').start()
31 patch('os.path.join', posixpath.join).start()
34 class PosixTestCase(BasePosixTestCase):
35 def setUp(self):
36 BasePosixTestCase.setUp(self)
38 def testGetToolchainNaClLib(self):
39 tests = [
40 (('newlib', 'x86_32'), 'foo/x86_64-nacl/lib32'),
41 (('newlib', 'x86_64'), 'foo/x86_64-nacl/lib'),
42 (('newlib', 'arm'), 'foo/arm-nacl/lib'),
43 (('glibc', 'x86_32'), 'foo/x86_64-nacl/lib32'),
44 (('glibc', 'x86_64'), 'foo/x86_64-nacl/lib'),
45 (('bionic', 'arm'), 'foo/arm-nacl/lib'),
46 (('pnacl', None), 'foo/le32-nacl/lib'),
49 for test in tests:
50 self.assertEqual(
51 build_artifacts.GetToolchainNaClLib(test[0][0], 'foo', test[0][1]),
52 test[1])
54 def testGetGypBuiltLib(self):
55 tests = [
56 (('newlib', 'x86_32'), 'foo/Release/gen/tc_newlib/lib32'),
57 (('newlib', 'x86_64'), 'foo/Release/gen/tc_newlib/lib64'),
58 (('newlib', 'arm'), 'foo/Release/gen/tc_newlib/libarm'),
59 (('glibc', 'x86_32'), 'foo/Release/gen/tc_glibc/lib32'),
60 (('glibc', 'x86_64'), 'foo/Release/gen/tc_glibc/lib64'),
61 (('pnacl', None), 'foo/Release/gen/tc_pnacl_newlib/lib')
64 for test in tests:
65 self.assertEqual(
66 build_artifacts.GetGypBuiltLib('foo', test[0][0], test[0][1]),
67 test[1])
69 def testGetGypToolchainLib(self):
70 tests = [
71 (('newlib', 'x86_32'),
72 'foo/Release/gen/sdk/posix_x86/nacl_x86_newlib/x86_64-nacl/lib32'),
73 (('newlib', 'x86_64'),
74 'foo/Release/gen/sdk/posix_x86/nacl_x86_newlib/x86_64-nacl/lib'),
75 (('newlib', 'arm'),
76 'foo/Release/gen/sdk/posix_x86/nacl_arm_newlib/arm-nacl/lib'),
77 (('glibc', 'x86_32'),
78 'foo/Release/gen/sdk/posix_x86/nacl_x86_glibc/x86_64-nacl/lib32'),
79 (('glibc', 'x86_64'),
80 'foo/Release/gen/sdk/posix_x86/nacl_x86_glibc/x86_64-nacl/lib'),
81 # Bionic uses the newlib toolchain lib directory
82 (('bionic', 'arm'),
83 'foo/Release/gen/sdk/posix_x86/nacl_arm_newlib/arm-nacl/lib'),
84 (('pnacl', None),
85 'foo/Release/gen/sdk/posix_x86/pnacl_newlib/le32-nacl/lib'),
88 for test in tests:
89 self.assertEqual(
90 build_artifacts.GetGypToolchainLib('foo', test[0][0], test[0][1]),
91 test[1])
93 @patch('build_artifacts.all_archives', ['foo.tar.bz2', 'bar.tar.bz2'])
94 @patch('build_version.ChromeMajorVersion', Mock(return_value='40'))
95 @patch('build_version.ChromeRevision', Mock(return_value='302630'))
96 @patch('build_version.ChromeCommitPosition', Mock(return_value=
97 '1492c3d296476fe12cafecabba6ebabe-refs/heads/master@{#302630}'))
98 @patch('buildbot_common.Archive')
99 def testUploadArchives(self, archive_mock):
100 build_artifacts.UploadArchives()
101 cwd = '/archive_dir/'
102 bucket_path = 'native-client-sdk/archives/40-302630-1492c3d29'
103 archive_mock.assert_has_calls([
104 call('foo.tar.bz2', bucket_path, cwd=cwd, step_link=False),
105 call('foo.tar.bz2.sha1', bucket_path, cwd=cwd, step_link=False),
106 call('bar.tar.bz2', bucket_path, cwd=cwd, step_link=False),
107 call('bar.tar.bz2.sha1', bucket_path, cwd=cwd, step_link=False)
111 class GypNinjaPosixTestCase(BasePosixTestCase):
112 def setUp(self):
113 BasePosixTestCase.setUp(self)
114 patch('sys.executable', 'python').start()
115 patch('build_artifacts.SRC_DIR', 'src_dir').start()
116 patch('os.environ', {}).start()
117 self.run_mock = patch('buildbot_common.Run').start()
118 self.options_mock = patch('build_artifacts.options').start()
119 self.options_mock.mac_sdk = False
120 self.options_mock.no_arm_trusted = False
121 self.gyp_defines_base = ['nacl_allow_thin_archives=0']
123 def testSimple(self):
124 build_artifacts.GypNinjaBuild(
125 None, 'gyp.py', 'foo.gyp', 'target', 'out_dir')
126 self.run_mock.assert_has_calls([
127 call(['python', 'gyp.py', 'foo.gyp', '--depth=.', '-G',
128 'output_dir=out_dir'],
129 cwd='src_dir',
130 env={'GYP_GENERATORS': 'ninja',
131 'GYP_DEFINES': ' '.join(self.gyp_defines_base)}),
132 call(['ninja', '-C', 'out_dir/Release', 'target'], cwd='src_dir')
135 def testTargetArch(self):
136 build_artifacts.GypNinjaBuild(
137 'x64', 'gyp.py', 'foo.gyp', 'target', 'out_dir')
138 self.run_mock.assert_has_calls([
139 call(['python', 'gyp.py', 'foo.gyp', '--depth=.', '-G',
140 'output_dir=out_dir'],
141 cwd='src_dir',
142 env={
143 'GYP_GENERATORS': 'ninja',
144 'GYP_DEFINES': ' '.join(self.gyp_defines_base +
145 ['target_arch=x64']),
147 call(['ninja', '-C', 'out_dir/Release', 'target'], cwd='src_dir')
150 def testMultipleTargets(self):
151 build_artifacts.GypNinjaBuild(
152 None, 'gyp.py', 'foo.gyp', ['target1', 'target2'], 'out_dir')
153 self.run_mock.assert_has_calls([
154 call(['python', 'gyp.py', 'foo.gyp', '--depth=.', '-G',
155 'output_dir=out_dir'],
156 cwd='src_dir',
157 env={'GYP_GENERATORS': 'ninja',
158 'GYP_DEFINES': ' '.join(self.gyp_defines_base)}),
159 call(['ninja', '-C', 'out_dir/Release', 'target1', 'target2'],
160 cwd='src_dir')
163 def testMacSdk(self):
164 build_artifacts.PLATFORM = 'mac'
165 self.options_mock.mac_sdk = '10.6'
166 build_artifacts.GypNinjaBuild(
167 None, 'gyp.py', 'foo.gyp', 'target', 'out_dir')
168 self.run_mock.assert_has_calls([
169 call(['python', 'gyp.py', 'foo.gyp', '--depth=.', '-G',
170 'output_dir=out_dir'],
171 cwd='src_dir',
172 env={
173 'GYP_GENERATORS': 'ninja',
174 'GYP_DEFINES': ' '.join(self.gyp_defines_base +
175 ['mac_sdk=10.6', 'clang=1']),
177 call(['ninja', '-C', 'out_dir/Release', 'target'], cwd='src_dir')
180 def testArmLinux(self):
181 build_artifacts.PLATFORM = 'linux'
182 build_artifacts.GypNinjaBuild(
183 'arm', 'gyp.py', 'foo.gyp', 'target', 'out_dir')
184 self.run_mock.assert_has_calls([
185 call(['python', 'gyp.py', 'foo.gyp', '--depth=.', '-G',
186 'output_dir=out_dir'],
187 cwd='src_dir',
188 env={
189 'GYP_CROSSCOMPILE': '1',
190 'GYP_GENERATORS': 'ninja',
191 'GYP_DEFINES': ' '.join(self.gyp_defines_base +
192 ['target_arch=arm']),
194 call(['ninja', '-C', 'out_dir/Release', 'target'], cwd='src_dir')
197 def testNoArmTrusted(self):
198 build_artifacts.PLATFORM = 'linux'
199 self.options_mock.no_arm_trusted = True
200 build_artifacts.GypNinjaBuild(
201 'arm', 'gyp.py', 'foo.gyp', 'target', 'out_dir')
202 self.run_mock.assert_has_calls([
203 call(['python', 'gyp.py', 'foo.gyp', '--depth=.', '-G',
204 'output_dir=out_dir'],
205 cwd='src_dir',
206 env={
207 'GYP_CROSSCOMPILE': '1',
208 'GYP_GENERATORS': 'ninja',
209 'GYP_DEFINES': ' '.join(self.gyp_defines_base +
210 ['target_arch=arm',
211 'disable_cross_trusted=1']),
213 call(['ninja', '-C', 'out_dir/Release', 'target'], cwd='src_dir')
217 class ArchivePosixTestCase(BasePosixTestCase):
218 def setUp(self):
219 BasePosixTestCase.setUp(self)
220 self.makedir_mock = patch('buildbot_common.MakeDir').start()
221 self.copyfile_mock = patch('buildbot_common.CopyFile').start()
222 self.copydir_mock = patch('buildbot_common.CopyDir').start()
223 self.isdir_mock = patch('os.path.isdir').start()
224 patch('os.path.exists', Mock(return_value=False)).start()
226 def dummy_isdir(path):
227 if path == '/archive_dir/posix_foo':
228 return True
229 return False
230 self.isdir_mock.side_effect = dummy_isdir
232 self.archive = build_artifacts.Archive('foo')
234 def testInit(self):
235 self.assertEqual(self.archive.name, 'posix_foo')
236 self.assertEqual(self.archive.archive_name, 'posix_foo.tar.bz2')
237 self.assertEqual(self.archive.archive_path,
238 '/archive_dir/posix_foo.tar.bz2')
239 self.assertEqual(self.archive.dirname, '/archive_dir/posix_foo')
240 self.makedir_mock.assert_called_once_with('/archive_dir/posix_foo')
242 @patch('glob.glob', Mock(side_effect=lambda x: [x]))
243 def testCopySimple(self):
244 self.archive.Copy('/copy_from', ['file1', 'file2'])
245 self.assertEqual(self.copydir_mock.call_count, 0)
246 self.copyfile_mock.assert_has_calls([
247 call('/copy_from/file1', '/archive_dir/posix_foo/file1'),
248 call('/copy_from/file2', '/archive_dir/posix_foo/file2')])
250 @patch('glob.glob')
251 def testCopyGlob(self, glob_mock):
252 glob_mock.return_value = ['/copy_from/foo', '/copy_from/bar']
253 self.archive.Copy('/copy_from', [('*', '')])
254 glob_mock.assert_called_once_with('/copy_from/*')
255 self.assertEqual(self.copydir_mock.call_count, 0)
256 self.copyfile_mock.assert_has_calls([
257 call('/copy_from/foo', '/archive_dir/posix_foo/'),
258 call('/copy_from/bar', '/archive_dir/posix_foo/')])
260 @patch('glob.glob', Mock(side_effect=lambda x: [x]))
261 def testCopyRename(self):
262 self.archive.Copy('/copy_from', [('file1', 'file1_renamed')])
263 self.assertEqual(self.copydir_mock.call_count, 0)
264 self.copyfile_mock.assert_called_once_with(
265 '/copy_from/file1', '/archive_dir/posix_foo/file1_renamed')
267 @patch('glob.glob', Mock(side_effect=lambda x: [x]))
268 def testCopyNewDir(self):
269 self.archive.Copy('/copy_from', [('file1', 'todir/')])
270 self.assertEqual(self.copydir_mock.call_count, 0)
271 self.copyfile_mock.assert_called_once_with(
272 '/copy_from/file1', '/archive_dir/posix_foo/todir/file1')
274 @patch('glob.glob', Mock(side_effect=lambda x: [x]))
275 def testCopyDir(self):
276 self.isdir_mock.side_effect = lambda _: True
277 self.archive.Copy('/copy_from', ['dirname'])
278 self.assertEqual(self.copyfile_mock.call_count, 0)
279 self.copydir_mock.assert_called_once_with(
280 '/copy_from/dirname', '/archive_dir/posix_foo/dirname')
283 class WinTestCase(unittest.TestCase):
284 def setUp(self):
285 patch('build_artifacts.PLATFORM', 'win').start()
286 patch('build_artifacts.BUILD_ARCHIVE_DIR', 'c:\\archive_dir\\').start()
287 patch('os.path.join', ntpath.join).start()
289 def tearDown(self):
290 patch.stopall()
292 @patch('os.path.exists', Mock(return_value=False))
293 @patch('buildbot_common.MakeDir')
294 def testArchiveInit(self, makedir_mock):
295 archive = build_artifacts.Archive('foo')
296 self.assertEqual(archive.name, 'win_foo')
297 self.assertEqual(archive.archive_name, 'win_foo.tar.bz2')
298 self.assertEqual(archive.archive_path, r'c:\archive_dir\win_foo.tar.bz2')
299 self.assertEqual(archive.dirname, r'c:\archive_dir\win_foo')
300 makedir_mock.assert_called_once_with(r'c:\archive_dir\win_foo')
303 if __name__ == '__main__':
304 unittest.main()