Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(130)

Side by Side Diff: tests/subprocess2_test.py

Issue 8750003: Add Popen.shell, add more subprocess2 tests and make it more compact. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/tools/depot_tools
Patch Set: rebase against HEAD Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « subprocess2.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. 2 # Copyright (c) 2011 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 """Unit tests for subprocess2.py.""" 6 """Unit tests for subprocess2.py."""
7 7
8 import logging 8 import logging
9 import optparse 9 import optparse
10 import os 10 import os
(...skipping 10 matching lines...) Expand all
21 21
22 import subprocess 22 import subprocess
23 import subprocess2 23 import subprocess2
24 24
25 from testing_support import auto_stub 25 from testing_support import auto_stub
26 26
27 # Method could be a function 27 # Method could be a function
28 # pylint: disable=R0201 28 # pylint: disable=R0201
29 29
30 30
31 # Create aliases for subprocess2 specific tests. They shouldn't be used for
32 # regression tests.
33 TIMED_OUT = subprocess2.TIMED_OUT
34 VOID = subprocess2.VOID
35 PIPE = subprocess2.PIPE
36 STDOUT = subprocess2.STDOUT
37
38
31 def convert_to_crlf(string): 39 def convert_to_crlf(string):
32 """Unconditionally convert LF to CRLF.""" 40 """Unconditionally convert LF to CRLF."""
33 return string.replace('\n', '\r\n') 41 return string.replace('\n', '\r\n')
34 42
35 43
36 def convert_to_cr(string): 44 def convert_to_cr(string):
37 """Unconditionally convert LF to CR.""" 45 """Unconditionally convert LF to CR."""
38 return string.replace('\n', '\r') 46 return string.replace('\n', '\r')
39 47
40 48
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 """ 203 """
196 noop = lambda x: x 204 noop = lambda x: x
197 for subp in (subprocess, subprocess2): 205 for subp in (subprocess, subprocess2):
198 function(noop, self.exe, False, subp) 206 function(noop, self.exe, False, subp)
199 function(convert_to_cr, self.exe + ['--cr'], False, subp) 207 function(convert_to_cr, self.exe + ['--cr'], False, subp)
200 function(convert_to_crlf, self.exe + ['--crlf'], False, subp) 208 function(convert_to_crlf, self.exe + ['--crlf'], False, subp)
201 function(noop, self.exe, True, subp) 209 function(noop, self.exe, True, subp)
202 function(noop, self.exe + ['--cr'], True, subp) 210 function(noop, self.exe + ['--cr'], True, subp)
203 function(noop, self.exe + ['--crlf'], True, subp) 211 function(noop, self.exe + ['--crlf'], True, subp)
204 212
205 def _check_pipes(self, subp, e, stdout, stderr): 213 def _check_exception(self, subp, e, stdout, stderr, returncode):
206 """On exception, look if the exception members are set correctly.""" 214 """On exception, look if the exception members are set correctly."""
215 self.assertEquals(returncode, e.returncode)
207 if subp is subprocess: 216 if subp is subprocess:
208 # subprocess never save the output. 217 # subprocess never save the output.
209 self.assertFalse(hasattr(e, 'stdout')) 218 self.assertFalse(hasattr(e, 'stdout'))
210 self.assertFalse(hasattr(e, 'stderr')) 219 self.assertFalse(hasattr(e, 'stderr'))
211 elif subp is subprocess2: 220 elif subp is subprocess2:
212 self.assertEquals(stdout, e.stdout) 221 self.assertEquals(stdout, e.stdout)
213 self.assertEquals(stderr, e.stderr) 222 self.assertEquals(stderr, e.stderr)
214 else: 223 else:
215 self.fail() 224 self.fail()
216 225
(...skipping 15 matching lines...) Expand all
232 241
233 def test_check_output_throw_stdout(self): 242 def test_check_output_throw_stdout(self):
234 def fn(c, e, un, subp): 243 def fn(c, e, un, subp):
235 if not hasattr(subp, 'check_output'): 244 if not hasattr(subp, 'check_output'):
236 return 245 return
237 try: 246 try:
238 subp.check_output( 247 subp.check_output(
239 e + ['--fail', '--stdout'], universal_newlines=un) 248 e + ['--fail', '--stdout'], universal_newlines=un)
240 self.fail() 249 self.fail()
241 except subp.CalledProcessError, e: 250 except subp.CalledProcessError, e:
242 self._check_pipes(subp, e, c('A\nBB\nCCC\n'), None) 251 self._check_exception(subp, e, c('A\nBB\nCCC\n'), None, 64)
243 self.assertEquals(64, e.returncode)
244 self._run_test(fn) 252 self._run_test(fn)
245 253
246 def test_check_output_throw_no_stderr(self): 254 def test_check_output_throw_no_stderr(self):
247 def fn(c, e, un, subp): 255 def fn(c, e, un, subp):
248 if not hasattr(subp, 'check_output'): 256 if not hasattr(subp, 'check_output'):
249 return 257 return
250 try: 258 try:
251 subp.check_output( 259 subp.check_output(
252 e + ['--fail', '--stderr'], universal_newlines=un) 260 e + ['--fail', '--stderr'], universal_newlines=un)
253 self.fail() 261 self.fail()
254 except subp.CalledProcessError, e: 262 except subp.CalledProcessError, e:
255 self._check_pipes(subp, e, c(''), None) 263 self._check_exception(subp, e, c(''), None, 64)
256 self.assertEquals(64, e.returncode)
257 self._run_test(fn) 264 self._run_test(fn)
258 265
259 def test_check_output_throw_stderr(self): 266 def test_check_output_throw_stderr(self):
260 def fn(c, e, un, subp): 267 def fn(c, e, un, subp):
261 if not hasattr(subp, 'check_output'): 268 if not hasattr(subp, 'check_output'):
262 return 269 return
263 try: 270 try:
264 subp.check_output( 271 subp.check_output(
265 e + ['--fail', '--stderr'], 272 e + ['--fail', '--stderr'],
266 stderr=subp.PIPE, 273 stderr=subp.PIPE,
267 universal_newlines=un) 274 universal_newlines=un)
268 self.fail() 275 self.fail()
269 except subp.CalledProcessError, e: 276 except subp.CalledProcessError, e:
270 self._check_pipes(subp, e, '', c('a\nbb\nccc\n')) 277 self._check_exception(subp, e, '', c('a\nbb\nccc\n'), 64)
271 self.assertEquals(64, e.returncode)
272 self._run_test(fn) 278 self._run_test(fn)
273 279
274 def test_check_output_throw_stderr_stdout(self): 280 def test_check_output_throw_stderr_stdout(self):
275 def fn(c, e, un, subp): 281 def fn(c, e, un, subp):
276 if not hasattr(subp, 'check_output'): 282 if not hasattr(subp, 'check_output'):
277 return 283 return
278 try: 284 try:
279 subp.check_output( 285 subp.check_output(
280 e + ['--fail', '--stderr'], 286 e + ['--fail', '--stderr'],
281 stderr=subp.STDOUT, 287 stderr=subp.STDOUT,
282 universal_newlines=un) 288 universal_newlines=un)
283 self.fail() 289 self.fail()
284 except subp.CalledProcessError, e: 290 except subp.CalledProcessError, e:
285 self._check_pipes(subp, e, c('a\nbb\nccc\n'), None) 291 self._check_exception(subp, e, c('a\nbb\nccc\n'), None, 64)
286 self.assertEquals(64, e.returncode)
287 self._run_test(fn) 292 self._run_test(fn)
288 293
289 def test_check_call_throw(self): 294 def test_check_call_throw(self):
290 for subp in (subprocess, subprocess2): 295 for subp in (subprocess, subprocess2):
291 try: 296 try:
292 subp.check_call(self.exe + ['--fail', '--stderr']) 297 subp.check_call(self.exe + ['--fail', '--stderr'])
293 self.fail() 298 self.fail()
294 except subp.CalledProcessError, e: 299 except subp.CalledProcessError, e:
295 self._check_pipes(subp, e, None, None) 300 self._check_exception(subp, e, None, None, 64)
296 self.assertEquals(64, e.returncode)
297 301
298 302
299 class S2Test(BaseTestCase): 303 class S2Test(BaseTestCase):
300 # Tests that can only run in subprocess2, e.g. new functionalities. 304 # Tests that can only run in subprocess2, e.g. new functionalities.
301 # In particular, subprocess2.communicate() doesn't exist in subprocess. 305 # In particular, subprocess2.communicate() doesn't exist in subprocess.
302 def _run_test(self, function): 306 def _run_test(self, function):
303 """Runs tests in 6 combinations: 307 """Runs tests in 6 combinations:
304 - LF output with universal_newlines=False 308 - LF output with universal_newlines=False
305 - CR output with universal_newlines=False 309 - CR output with universal_newlines=False
306 - CRLF output with universal_newlines=False 310 - CRLF output with universal_newlines=False
307 - LF output with universal_newlines=True 311 - LF output with universal_newlines=True
308 - CR output with universal_newlines=True 312 - CR output with universal_newlines=True
309 - CRLF output with universal_newlines=True 313 - CRLF output with universal_newlines=True
310 314
311 First |function| argument is the convertion for the origianl expected LF 315 First |function| argument is the convertion for the origianl expected LF
312 string to the right EOL. 316 string to the right EOL.
313 Second |function| argument is the executable and initial flag to run, to 317 Second |function| argument is the executable and initial flag to run, to
314 control what EOL is used by the child process. 318 control what EOL is used by the child process.
315 Third |function| argument is universal_newlines value. 319 Third |function| argument is universal_newlines value.
316 """ 320 """
317 noop = lambda x: x 321 noop = lambda x: x
318 function(noop, self.exe, False) 322 function(noop, self.exe, False)
319 function(convert_to_cr, self.exe + ['--cr'], False) 323 function(convert_to_cr, self.exe + ['--cr'], False)
320 function(convert_to_crlf, self.exe + ['--crlf'], False) 324 function(convert_to_crlf, self.exe + ['--crlf'], False)
321 function(noop, self.exe, True) 325 function(noop, self.exe, True)
322 function(noop, self.exe + ['--cr'], True) 326 function(noop, self.exe + ['--cr'], True)
323 function(noop, self.exe + ['--crlf'], True) 327 function(noop, self.exe + ['--crlf'], True)
324 328
329 def _check_res(self, res, stdout, stderr, returncode):
330 (out, err), code = res
331 self.assertEquals(stdout, out)
332 self.assertEquals(stderr, err)
333 self.assertEquals(returncode, code)
334
325 def test_timeout(self): 335 def test_timeout(self):
326 # timeout doesn't exist in subprocess. 336 # timeout doesn't exist in subprocess.
327 def fn(c, e, un): 337 def fn(c, e, un):
328 out, returncode = subprocess2.communicate( 338 res = subprocess2.communicate(
329 self.exe + ['--sleep_first', '--stdout'], 339 self.exe + ['--sleep_first', '--stdout'],
330 timeout=0.01, 340 timeout=0.01,
331 stdout=subprocess2.PIPE, 341 stdout=PIPE,
332 shell=False) 342 shell=False)
333 self.assertEquals(subprocess2.TIMED_OUT, returncode) 343 self._check_res(res, '', None, TIMED_OUT)
334 self.assertEquals(('', None), out) 344 self._run_test(fn)
345
346 def test_timeout_shell_throws(self):
347 def fn(c, e, un):
348 try:
349 # With shell=True, it needs a string.
350 subprocess2.communicate(' '.join(self.exe), timeout=0.01, shell=True)
351 self.fail()
352 except TypeError:
353 pass
335 self._run_test(fn) 354 self._run_test(fn)
336 355
337 def test_stdout_void(self): 356 def test_stdout_void(self):
338 def fn(c, e, un): 357 def fn(c, e, un):
339 (out, err), code = subprocess2.communicate( 358 res = subprocess2.communicate(
340 e + ['--stdout', '--stderr'], 359 e + ['--stdout', '--stderr'],
341 stdout=subprocess2.VOID, 360 stdout=VOID,
342 stderr=subprocess2.PIPE, 361 stderr=PIPE,
343 universal_newlines=un) 362 universal_newlines=un)
344 self.assertEquals(None, out) 363 self._check_res(res, None, c('a\nbb\nccc\n'), 0)
345 self.assertEquals(c('a\nbb\nccc\n'), err)
346 self.assertEquals(0, code)
347 self._run_test(fn) 364 self._run_test(fn)
348 365
349 def test_stderr_void(self): 366 def test_stderr_void(self):
350 def fn(c, e, un): 367 def fn(c, e, un):
351 (out, err), code = subprocess2.communicate( 368 res = subprocess2.communicate(
352 e + ['--stdout', '--stderr'], 369 e + ['--stdout', '--stderr'],
353 stdout=subprocess2.PIPE, 370 stdout=PIPE,
354 stderr=subprocess2.VOID, 371 stderr=VOID,
355 universal_newlines=un) 372 universal_newlines=un)
356 self.assertEquals(c('A\nBB\nCCC\n'), out) 373 self._check_res(res, c('A\nBB\nCCC\n'), None, 0)
357 self.assertEquals(None, err) 374 self._run_test(fn)
358 self.assertEquals(0, code) 375
376 def test_stdout_void_stderr_redirect(self):
377 def fn(c, e, un):
378 res = subprocess2.communicate(
379 e + ['--stdout', '--stderr'],
380 stdout=VOID,
381 stderr=STDOUT,
382 universal_newlines=un)
383 self._check_res(res, None, None, 0)
359 self._run_test(fn) 384 self._run_test(fn)
360 385
361 def test_check_output_redirect_stderr_to_stdout_pipe(self): 386 def test_check_output_redirect_stderr_to_stdout_pipe(self):
362 def fn(c, e, un): 387 def fn(c, e, un):
363 (out, err), code = subprocess2.communicate( 388 # stderr output into stdout.
389 res = subprocess2.communicate(
364 e + ['--stderr'], 390 e + ['--stderr'],
365 stdout=subprocess2.PIPE, 391 stdout=PIPE,
366 stderr=subprocess2.STDOUT, 392 stderr=STDOUT,
367 universal_newlines=un) 393 universal_newlines=un)
368 # stderr output into stdout. 394 self._check_res(res, c('a\nbb\nccc\n'), None, 0)
369 self.assertEquals(c('a\nbb\nccc\n'), out)
370 self.assertEquals(None, err)
371 self.assertEquals(0, code)
372 self._run_test(fn) 395 self._run_test(fn)
373 396
374 def test_check_output_redirect_stderr_to_stdout(self): 397 def test_check_output_redirect_stderr_to_stdout(self):
375 def fn(c, e, un): 398 def fn(c, e, un):
376 (out, err), code = subprocess2.communicate(
377 e + ['--stderr'],
378 stderr=subprocess2.STDOUT,
379 universal_newlines=un)
380 # stderr output into stdout but stdout is not piped. 399 # stderr output into stdout but stdout is not piped.
381 self.assertEquals(None, out) 400 res = subprocess2.communicate(
382 self.assertEquals(None, err) 401 e + ['--stderr'], stderr=STDOUT, universal_newlines=un)
383 self.assertEquals(0, code) 402 self._check_res(res, None, None, 0)
384 self._run_test(fn) 403 self._run_test(fn)
385 404
386 405
387 def child_main(args): 406 def child_main(args):
388 if sys.platform == 'win32': 407 if sys.platform == 'win32':
389 # Annoying, make sure the output is not translated on Windows. 408 # Annoying, make sure the output is not translated on Windows.
390 # pylint: disable=E1101,F0401 409 # pylint: disable=E1101,F0401
391 import msvcrt 410 import msvcrt
392 msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) 411 msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
393 msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY) 412 msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY)
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
441 return options.return_value 460 return options.return_value
442 461
443 462
444 if __name__ == '__main__': 463 if __name__ == '__main__':
445 logging.basicConfig(level= 464 logging.basicConfig(level=
446 [logging.WARNING, logging.INFO, logging.DEBUG][ 465 [logging.WARNING, logging.INFO, logging.DEBUG][
447 min(2, sys.argv.count('-v'))]) 466 min(2, sys.argv.count('-v'))])
448 if len(sys.argv) > 1 and sys.argv[1] == '--child': 467 if len(sys.argv) > 1 and sys.argv[1] == '--child':
449 sys.exit(child_main(sys.argv[2:])) 468 sys.exit(child_main(sys.argv[2:]))
450 unittest.main() 469 unittest.main()
OLDNEW
« no previous file with comments | « subprocess2.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698