OLD | NEW |
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 362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
373 def test_timeout_shell_throws(self): | 373 def test_timeout_shell_throws(self): |
374 def fn(c, e, un): | 374 def fn(c, e, un): |
375 try: | 375 try: |
376 # With shell=True, it needs a string. | 376 # With shell=True, it needs a string. |
377 subprocess2.communicate(' '.join(self.exe), timeout=0.01, shell=True) | 377 subprocess2.communicate(' '.join(self.exe), timeout=0.01, shell=True) |
378 self.fail() | 378 self.fail() |
379 except TypeError: | 379 except TypeError: |
380 pass | 380 pass |
381 self._run_test(fn) | 381 self._run_test(fn) |
382 | 382 |
| 383 def test_stdin(self): |
| 384 def fn(c, e, un): |
| 385 stdin = '0123456789' |
| 386 res = subprocess2.communicate( |
| 387 e + ['--read'], |
| 388 stdin=stdin, |
| 389 universal_newlines=un) |
| 390 self._check_res(res, None, None, 10) |
| 391 self._run_test(fn) |
| 392 |
| 393 def test_stdin_unicode(self): |
| 394 def fn(c, e, un): |
| 395 stdin = u'0123456789' |
| 396 res = subprocess2.communicate( |
| 397 e + ['--read'], |
| 398 stdin=stdin, |
| 399 universal_newlines=un) |
| 400 self._check_res(res, None, None, 10) |
| 401 self._run_test(fn) |
| 402 |
| 403 def test_stdin_void(self): |
| 404 res = subprocess2.communicate(self.exe + ['--read'], stdin=VOID) |
| 405 self._check_res(res, None, None, 0) |
| 406 |
| 407 def test_stdin_void_stdout_timeout(self): |
| 408 # Make sure a mix of VOID, PIPE and timeout works. |
| 409 def fn(c, e, un): |
| 410 res = subprocess2.communicate( |
| 411 e + ['--stdout', '--read'], |
| 412 stdin=VOID, |
| 413 stdout=PIPE, |
| 414 timeout=10, |
| 415 universal_newlines=un) |
| 416 self._check_res(res, c('A\nBB\nCCC\n'), None, 0) |
| 417 self._run_test(fn) |
| 418 |
383 def test_stdout_void(self): | 419 def test_stdout_void(self): |
384 def fn(c, e, un): | 420 def fn(c, e, un): |
385 res = subprocess2.communicate( | 421 res = subprocess2.communicate( |
386 e + ['--stdout', '--stderr'], | 422 e + ['--stdout', '--stderr'], |
387 stdout=VOID, | 423 stdout=VOID, |
388 stderr=PIPE, | 424 stderr=PIPE, |
389 universal_newlines=un) | 425 universal_newlines=un) |
390 self._check_res(res, None, c('a\nbb\nccc\n'), 0) | 426 self._check_res(res, None, c('a\nbb\nccc\n'), 0) |
391 self._run_test(fn) | 427 self._run_test(fn) |
392 | 428 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
428 stdout=stdout.append, | 464 stdout=stdout.append, |
429 stderr=stderr.append, | 465 stderr=stderr.append, |
430 universal_newlines=un) | 466 universal_newlines=un) |
431 self.assertEquals(c('A\nBB\nCCC\n'), ''.join(stdout)) | 467 self.assertEquals(c('A\nBB\nCCC\n'), ''.join(stdout)) |
432 self.assertEquals(c('a\nbb\nccc\n'), ''.join(stderr)) | 468 self.assertEquals(c('a\nbb\nccc\n'), ''.join(stderr)) |
433 self._check_res(res, None, None, 0) | 469 self._check_res(res, None, None, 0) |
434 self._run_test(fn) | 470 self._run_test(fn) |
435 | 471 |
436 def test_tee_stdin(self): | 472 def test_tee_stdin(self): |
437 def fn(c, e, un): | 473 def fn(c, e, un): |
| 474 # Mix of stdin input and stdout callback. |
438 stdout = [] | 475 stdout = [] |
439 stdin = '0123456789' | 476 stdin = '0123456789' |
440 res = subprocess2.communicate( | 477 res = subprocess2.communicate( |
441 e + ['--stdout', '--read'], stdin=stdin, stdout=stdout.append, | 478 e + ['--stdout', '--read'], |
| 479 stdin=stdin, |
| 480 stdout=stdout.append, |
442 universal_newlines=un) | 481 universal_newlines=un) |
443 self.assertEquals(c('A\nBB\nCCC\n'), ''.join(stdout)) | 482 self.assertEquals(c('A\nBB\nCCC\n'), ''.join(stdout)) |
444 self._check_res(res, None, None, 0) | 483 self._check_res(res, None, None, 10) |
445 self._run_test(fn) | 484 self._run_test(fn) |
446 | 485 |
447 def test_tee_throw(self): | 486 def test_tee_throw(self): |
448 def fn(c, e, un): | 487 def fn(c, e, un): |
| 488 # Make sure failure still returns stderr completely. |
449 stderr = [] | 489 stderr = [] |
450 try: | 490 try: |
451 subprocess2.check_output( | 491 subprocess2.check_output( |
452 e + ['--stderr', '--fail'], stderr=stderr.append, | 492 e + ['--stderr', '--fail'], |
| 493 stderr=stderr.append, |
453 universal_newlines=un) | 494 universal_newlines=un) |
454 self.fail() | 495 self.fail() |
455 except subprocess2.CalledProcessError, e: | 496 except subprocess2.CalledProcessError, e: |
456 self._check_exception(e, '', None, 64) | 497 self._check_exception(e, '', None, 64) |
457 self.assertEquals(c('a\nbb\nccc\n'), ''.join(stderr)) | 498 self.assertEquals(c('a\nbb\nccc\n'), ''.join(stderr)) |
458 self._run_test(fn) | 499 self._run_test(fn) |
459 | 500 |
460 def test_tee_timeout_stdout_void(self): | 501 def test_tee_timeout_stdout_void(self): |
461 def fn(c, e, un): | 502 def fn(c, e, un): |
462 stderr = [] | 503 stderr = [] |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
573 sys.stderr.write(options.eol) | 614 sys.stderr.write(options.eol) |
574 | 615 |
575 do('A') | 616 do('A') |
576 do('BB') | 617 do('BB') |
577 do('CCC') | 618 do('CCC') |
578 if options.large: | 619 if options.large: |
579 # Print 128kb. | 620 # Print 128kb. |
580 string = '0123456789abcdef' * (8*1024) | 621 string = '0123456789abcdef' * (8*1024) |
581 sys.stdout.write(string) | 622 sys.stdout.write(string) |
582 if options.read: | 623 if options.read: |
| 624 assert options.return_value is 0 |
583 try: | 625 try: |
584 while sys.stdin.read(): | 626 while sys.stdin.read(1): |
585 pass | 627 options.return_value += 1 |
586 except OSError: | 628 except OSError: |
587 pass | 629 pass |
588 if options.sleep_last: | 630 if options.sleep_last: |
589 time.sleep(10) | 631 time.sleep(10) |
590 return options.return_value | 632 return options.return_value |
591 | 633 |
592 | 634 |
593 if __name__ == '__main__': | 635 if __name__ == '__main__': |
594 logging.basicConfig(level= | 636 logging.basicConfig(level= |
595 [logging.WARNING, logging.INFO, logging.DEBUG][ | 637 [logging.WARNING, logging.INFO, logging.DEBUG][ |
596 min(2, sys.argv.count('-v'))]) | 638 min(2, sys.argv.count('-v'))]) |
597 if len(sys.argv) > 1 and sys.argv[1] == '--child': | 639 if len(sys.argv) > 1 and sys.argv[1] == '--child': |
598 sys.exit(child_main(sys.argv[2:])) | 640 sys.exit(child_main(sys.argv[2:])) |
599 unittest.main() | 641 unittest.main() |
OLD | NEW |