OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 import cStringIO | 6 import cStringIO |
7 import hashlib | 7 import hashlib |
8 import json | 8 import json |
9 import logging | 9 import logging |
10 import os | 10 import os |
11 import re | 11 import re |
12 import shutil | 12 import shutil |
13 import subprocess | 13 import subprocess |
14 import sys | 14 import sys |
15 import tempfile | 15 import tempfile |
16 import unittest | 16 import unittest |
17 | 17 |
18 ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) | 18 ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) |
19 VERBOSE = False | 19 VERBOSE = False |
| 20 FILENAME = os.path.basename(__file__) |
20 | 21 |
21 | 22 |
22 class CalledProcessError(subprocess.CalledProcessError): | 23 class CalledProcessError(subprocess.CalledProcessError): |
23 """Makes 2.6 version act like 2.7""" | 24 """Makes 2.6 version act like 2.7""" |
24 def __init__(self, returncode, cmd, output, cwd): | 25 def __init__(self, returncode, cmd, output, cwd): |
25 super(CalledProcessError, self).__init__(returncode, cmd) | 26 super(CalledProcessError, self).__init__(returncode, cmd) |
26 self.output = output | 27 self.output = output |
27 self.cwd = cwd | 28 self.cwd = cwd |
28 | 29 |
29 def __str__(self): | 30 def __str__(self): |
30 return super(CalledProcessError, self).__str__() + ( | 31 return super(CalledProcessError, self).__str__() + ( |
31 '\n' | 32 '\n' |
32 'cwd=%s\n%s') % (self.cwd, self.output) | 33 'cwd=%s\n%s') % (self.cwd, self.output) |
33 | 34 |
34 | 35 |
35 class Isolate(unittest.TestCase): | 36 class Isolate(unittest.TestCase): |
36 def setUp(self): | 37 def setUp(self): |
37 # The reason is that isolate_test.py --ok is run in a temporary directory | 38 # The reason is that FILENAME --ok is run in a temporary directory |
38 # without access to isolate.py | 39 # without access to isolate.py |
39 import isolate | 40 import isolate |
40 self.isolate = isolate | 41 self.isolate = isolate |
41 self.tempdir = tempfile.mkdtemp() | 42 self.tempdir = tempfile.mkdtemp() |
42 self.result = os.path.join(self.tempdir, 'result') | 43 self.result = os.path.join(self.tempdir, 'result') |
43 if VERBOSE: | 44 if VERBOSE: |
44 print | 45 print |
45 | 46 |
46 def tearDown(self): | 47 def tearDown(self): |
47 shutil.rmtree(self.tempdir) | 48 shutil.rmtree(self.tempdir) |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 expected = ('check', 'hashtable', 'remap', 'run', 'trace') | 121 expected = ('check', 'hashtable', 'remap', 'run', 'trace') |
121 self.assertEquals(expected, modes) | 122 self.assertEquals(expected, modes) |
122 self.assertEquals(expected, modes) | 123 self.assertEquals(expected, modes) |
123 for mode in modes: | 124 for mode in modes: |
124 self.assertTrue(hasattr(self, 'test_%s' % mode), mode) | 125 self.assertTrue(hasattr(self, 'test_%s' % mode), mode) |
125 self._expected_tree([]) | 126 self._expected_tree([]) |
126 | 127 |
127 def test_check(self): | 128 def test_check(self): |
128 cmd = [ | 129 cmd = [ |
129 '--mode', 'check', | 130 '--mode', 'check', |
130 'isolate_test.py', | 131 FILENAME, |
131 ] | 132 ] |
132 self._execute(cmd) | 133 self._execute(cmd) |
133 self._expected_tree(['result']) | 134 self._expected_tree(['result']) |
134 self._expected_result( | 135 self._expected_result( |
135 False, | 136 False, |
136 ['isolate_test.py'], | 137 [FILENAME], |
137 [os.path.join('.', 'isolate_test.py')], | 138 [os.path.join('.', FILENAME)], |
138 False) | 139 False) |
139 | 140 |
140 def test_check_non_existant(self): | 141 def test_check_non_existant(self): |
141 cmd = [ | 142 cmd = [ |
142 '--mode', 'check', | 143 '--mode', 'check', |
143 'NonExistentFile', | 144 'NonExistentFile', |
144 ] | 145 ] |
145 try: | 146 try: |
146 self._execute(cmd) | 147 self._execute(cmd) |
147 self.fail() | 148 self.fail() |
(...skipping 10 matching lines...) Expand all Loading... |
158 try: | 159 try: |
159 self._execute(cmd) | 160 self._execute(cmd) |
160 self.fail() | 161 self.fail() |
161 except subprocess.CalledProcessError: | 162 except subprocess.CalledProcessError: |
162 pass | 163 pass |
163 self._expected_tree([]) | 164 self._expected_tree([]) |
164 | 165 |
165 def test_check_abs_path(self): | 166 def test_check_abs_path(self): |
166 cmd = [ | 167 cmd = [ |
167 '--mode', 'check', | 168 '--mode', 'check', |
168 'isolate_test.py', | 169 FILENAME, |
169 '--', | 170 '--', |
170 os.path.join(ROOT_DIR, 'isolate_test.py'), | 171 os.path.join(ROOT_DIR, FILENAME), |
171 ] | 172 ] |
172 self._execute(cmd) | 173 self._execute(cmd) |
173 self._expected_tree(['result']) | 174 self._expected_tree(['result']) |
174 self._expected_result( | 175 self._expected_result( |
175 False, ['isolate_test.py'], ['isolate_test.py'], False) | 176 False, [FILENAME], [FILENAME], False) |
176 | 177 |
177 def test_hashtable(self): | 178 def test_hashtable(self): |
178 cmd = [ | 179 cmd = [ |
179 '--mode', 'hashtable', | 180 '--mode', 'hashtable', |
180 '--outdir', self.tempdir, | 181 '--outdir', self.tempdir, |
181 'isolate_test.py', | 182 FILENAME, |
182 os.path.join('data', 'isolate') + os.path.sep, | 183 os.path.join('data', 'isolate') + os.path.sep, |
183 ] | 184 ] |
184 self._execute(cmd) | 185 self._execute(cmd) |
185 files = [ | 186 files = [ |
186 'isolate_test.py', | 187 FILENAME, |
187 os.path.join('data', 'isolate', 'test_file1.txt'), | 188 os.path.join('data', 'isolate', 'test_file1.txt'), |
188 os.path.join('data', 'isolate', 'test_file2.txt'), | 189 os.path.join('data', 'isolate', 'test_file2.txt'), |
189 ] | 190 ] |
190 data = self._expected_result( | 191 data = self._expected_result( |
191 True, files, [os.path.join('.', 'isolate_test.py')], False) | 192 True, files, [os.path.join('.', FILENAME)], False) |
192 self._expected_tree( | 193 self._expected_tree( |
193 [f['sha-1'] for f in data['files'].itervalues()] + ['result']) | 194 [f['sha-1'] for f in data['files'].itervalues()] + ['result']) |
194 | 195 |
195 def test_remap(self): | 196 def test_remap(self): |
196 cmd = [ | 197 cmd = [ |
197 '--mode', 'remap', | 198 '--mode', 'remap', |
198 '--outdir', self.tempdir, | 199 '--outdir', self.tempdir, |
199 'isolate_test.py', | 200 FILENAME, |
200 ] | 201 ] |
201 self._execute(cmd) | 202 self._execute(cmd) |
202 self._expected_tree(['isolate_test.py', 'result']) | 203 self._expected_tree([FILENAME, 'result']) |
203 self._expected_result( | 204 self._expected_result( |
204 False, | 205 False, |
205 ['isolate_test.py'], | 206 [FILENAME], |
206 [os.path.join('.', 'isolate_test.py')], | 207 [os.path.join('.', FILENAME)], |
207 False) | 208 False) |
208 | 209 |
209 def test_run(self): | 210 def test_run(self): |
210 cmd = [ | 211 cmd = [ |
211 '--mode', 'run', | 212 '--mode', 'run', |
212 'isolate_test.py', | 213 FILENAME, |
213 '--', | 214 '--', |
214 sys.executable, 'isolate_test.py', '--ok', | 215 sys.executable, FILENAME, '--ok', |
215 ] | 216 ] |
216 self._execute(cmd) | 217 self._execute(cmd) |
217 self._expected_tree(['result']) | 218 self._expected_tree(['result']) |
218 # cmd[0] is not generated from infiles[0] so it's not using a relative path. | 219 # cmd[0] is not generated from infiles[0] so it's not using a relative path. |
219 self._expected_result( | 220 self._expected_result( |
220 False, ['isolate_test.py'], ['isolate_test.py', '--ok'], False) | 221 False, [FILENAME], [FILENAME, '--ok'], False) |
221 | 222 |
222 def test_run_fail(self): | 223 def test_run_fail(self): |
223 cmd = [ | 224 cmd = [ |
224 '--mode', 'run', | 225 '--mode', 'run', |
225 'isolate_test.py', | 226 FILENAME, |
226 '--', | 227 '--', |
227 sys.executable, 'isolate_test.py', '--fail', | 228 sys.executable, FILENAME, '--fail', |
228 ] | 229 ] |
229 try: | 230 try: |
230 self._execute(cmd) | 231 self._execute(cmd) |
231 self.fail() | 232 self.fail() |
232 except subprocess.CalledProcessError: | 233 except subprocess.CalledProcessError: |
233 pass | 234 pass |
234 self._expected_tree([]) | 235 self._expected_tree([]) |
235 | 236 |
236 def test_trace(self): | 237 def test_trace(self): |
237 cmd = [ | 238 cmd = [ |
238 '--mode', 'trace', | 239 '--mode', 'trace', |
239 'isolate_test.py', | 240 FILENAME, |
240 '--', | 241 '--', |
241 sys.executable, os.path.join(ROOT_DIR, 'isolate_test.py'), '--ok', | 242 sys.executable, os.path.join(ROOT_DIR, FILENAME), '--ok', |
242 ] | 243 ] |
243 out = self._execute(cmd, True) | 244 out = self._execute(cmd, True) |
244 expected_tree = ['result', 'result.log'] | 245 expected_tree = ['result', 'result.log'] |
245 if sys.platform == 'win32': | 246 if sys.platform == 'win32': |
246 expected_tree.append('result.log.etl') | 247 expected_tree.append('result.log.etl') |
247 self._expected_tree(expected_tree) | 248 self._expected_tree(expected_tree) |
248 # The 'result.log' log is OS-specific so we can't read it but we can read | 249 # The 'result.log' log is OS-specific so we can't read it but we can read |
249 # the gyp result. | 250 # the gyp result. |
250 # cmd[0] is not generated from infiles[0] so it's not using a relative path. | 251 # cmd[0] is not generated from infiles[0] so it's not using a relative path. |
251 self._expected_result( | 252 self._expected_result( |
252 False, ['isolate_test.py'], ['isolate_test.py', '--ok'], False) | 253 False, [FILENAME], [FILENAME, '--ok'], False) |
253 | 254 |
254 expected_value = { | 255 expected_value = { |
255 'conditions': [ | 256 'conditions': [ |
256 ['OS=="%s"' % self.isolate.trace_inputs.get_flavor(), { | 257 ['OS=="%s"' % self.isolate.trace_inputs.get_flavor(), { |
257 'variables': { | 258 'variables': { |
258 'isolate_files': [ | 259 'isolate_files': [ |
259 '<(DEPTH)/isolate_test.py', | 260 '<(DEPTH)/%s' % FILENAME, |
260 ], | 261 ], |
261 }, | 262 }, |
262 }], | 263 }], |
263 ], | 264 ], |
264 } | 265 } |
265 expected_buffer = cStringIO.StringIO() | 266 expected_buffer = cStringIO.StringIO() |
266 self.isolate.trace_inputs.pretty_print(expected_value, expected_buffer) | 267 self.isolate.trace_inputs.pretty_print(expected_value, expected_buffer) |
267 self.assertEquals(expected_buffer.getvalue(), out) | 268 self.assertEquals(expected_buffer.getvalue(), out) |
268 | 269 |
269 | 270 |
270 def main(): | 271 def main(): |
271 global VERBOSE | 272 global VERBOSE |
272 VERBOSE = '-v' in sys.argv | 273 VERBOSE = '-v' in sys.argv |
273 level = logging.DEBUG if VERBOSE else logging.ERROR | 274 level = logging.DEBUG if VERBOSE else logging.ERROR |
274 logging.basicConfig(level=level) | 275 logging.basicConfig(level=level) |
275 if len(sys.argv) == 1: | 276 if len(sys.argv) == 1: |
276 unittest.main() | 277 unittest.main() |
277 if sys.argv[1] == '--ok': | 278 if sys.argv[1] == '--ok': |
278 return 0 | 279 return 0 |
279 if sys.argv[1] == '--fail': | 280 if sys.argv[1] == '--fail': |
280 return 1 | 281 return 1 |
281 | 282 |
282 unittest.main() | 283 unittest.main() |
283 | 284 |
284 | 285 |
285 if __name__ == '__main__': | 286 if __name__ == '__main__': |
286 sys.exit(main()) | 287 sys.exit(main()) |
OLD | NEW |