| 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 """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 |
| 11 import sys | 11 import sys |
| 12 import time | 12 import time |
| 13 import unittest | 13 import unittest |
| 14 | 14 |
| 15 try: | 15 try: |
| 16 import fcntl | 16 import fcntl # pylint: disable=F0401 |
| 17 except ImportError: | 17 except ImportError: |
| 18 fcntl = None | 18 fcntl = None |
| 19 | 19 |
| 20 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | 20 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 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 |
| (...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 def fn(c, e, un, subp): | 322 def fn(c, e, un, subp): |
| 323 # stderr output into stdout but stdout is not piped. | 323 # stderr output into stdout but stdout is not piped. |
| 324 proc = subp.Popen( | 324 proc = subp.Popen( |
| 325 e + ['--stderr'], stderr=STDOUT, universal_newlines=un) | 325 e + ['--stderr'], stderr=STDOUT, universal_newlines=un) |
| 326 res = proc.communicate(), proc.returncode | 326 res = proc.communicate(), proc.returncode |
| 327 self._check_res(res, None, None, 0) | 327 self._check_res(res, None, None, 0) |
| 328 self._run_test(fn) | 328 self._run_test(fn) |
| 329 | 329 |
| 330 def test_stderr(self): | 330 def test_stderr(self): |
| 331 cmd = ['expr', '1', '/', '0'] | 331 cmd = ['expr', '1', '/', '0'] |
| 332 p1 = subprocess.Popen(cmd, stderr=subprocess.PIPE) | 332 if sys.platform == 'win32': |
| 333 p2 = subprocess2.Popen(cmd, stderr=subprocess.PIPE) | 333 cmd = ['cmd.exe', '/c', 'exit', '1'] |
| 334 p1 = subprocess.Popen(cmd, stderr=subprocess.PIPE, shell=False) |
| 335 p2 = subprocess2.Popen(cmd, stderr=subprocess.PIPE, shell=False) |
| 334 r1 = p1.communicate() | 336 r1 = p1.communicate() |
| 335 r2 = p2.communicate(timeout=100) | 337 r2 = p2.communicate(timeout=100) |
| 336 self.assertEquals(r1, r2) | 338 self.assertEquals(r1, r2) |
| 337 | 339 |
| 338 | 340 |
| 339 class S2Test(BaseTestCase): | 341 class S2Test(BaseTestCase): |
| 340 # Tests that can only run in subprocess2, e.g. new functionalities. | 342 # Tests that can only run in subprocess2, e.g. new functionalities. |
| 341 # In particular, subprocess2.communicate() doesn't exist in subprocess. | 343 # In particular, subprocess2.communicate() doesn't exist in subprocess. |
| 342 def _run_test(self, function): | 344 def _run_test(self, function): |
| 343 """Runs tests in 6 combinations: | 345 """Runs tests in 6 combinations: |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 424 self._check_res(res, None, None, 0) | 426 self._check_res(res, None, None, 0) |
| 425 | 427 |
| 426 def test_stdin_void_stdout_timeout(self): | 428 def test_stdin_void_stdout_timeout(self): |
| 427 # Make sure a mix of VOID, PIPE and timeout works. | 429 # Make sure a mix of VOID, PIPE and timeout works. |
| 428 def fn(c, e, un): | 430 def fn(c, e, un): |
| 429 res = subprocess2.communicate( | 431 res = subprocess2.communicate( |
| 430 e + ['--stdout', '--read'], | 432 e + ['--stdout', '--read'], |
| 431 stdin=VOID, | 433 stdin=VOID, |
| 432 stdout=PIPE, | 434 stdout=PIPE, |
| 433 timeout=10, | 435 timeout=10, |
| 434 universal_newlines=un) | 436 universal_newlines=un, |
| 437 shell=False) |
| 435 self._check_res(res, c('A\nBB\nCCC\n'), None, 0) | 438 self._check_res(res, c('A\nBB\nCCC\n'), None, 0) |
| 436 self._run_test(fn) | 439 self._run_test(fn) |
| 437 | 440 |
| 438 def test_stdout_void(self): | 441 def test_stdout_void(self): |
| 439 def fn(c, e, un): | 442 def fn(c, e, un): |
| 440 res = subprocess2.communicate( | 443 res = subprocess2.communicate( |
| 441 e + ['--stdout', '--stderr'], | 444 e + ['--stdout', '--stderr'], |
| 442 stdout=VOID, | 445 stdout=VOID, |
| 443 stderr=PIPE, | 446 stderr=PIPE, |
| 444 universal_newlines=un) | 447 universal_newlines=un) |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 569 self.assertEquals(128*1024, len(''.join(stdout))) | 572 self.assertEquals(128*1024, len(''.join(stdout))) |
| 570 self._check_res(res, None, None, 0) | 573 self._check_res(res, None, None, 0) |
| 571 | 574 |
| 572 def test_tee_large_stdin(self): | 575 def test_tee_large_stdin(self): |
| 573 stdout = [] | 576 stdout = [] |
| 574 # Write 128kb. | 577 # Write 128kb. |
| 575 stdin = '0123456789abcdef' * (8*1024) | 578 stdin = '0123456789abcdef' * (8*1024) |
| 576 res = subprocess2.communicate( | 579 res = subprocess2.communicate( |
| 577 self.exe + ['--large', '--read'], stdin=stdin, stdout=stdout.append) | 580 self.exe + ['--large', '--read'], stdin=stdin, stdout=stdout.append) |
| 578 self.assertEquals(128*1024, len(''.join(stdout))) | 581 self.assertEquals(128*1024, len(''.join(stdout))) |
| 579 self._check_res(res, None, None, 0) | 582 # Windows return code is > 8 bits. |
| 583 returncode = len(stdin) if sys.platform == 'win32' else 0 |
| 584 self._check_res(res, None, None, returncode) |
| 580 | 585 |
| 581 def test_tee_cb_throw(self): | 586 def test_tee_cb_throw(self): |
| 582 # Having a callback throwing up should not cause side-effects. It's a bit | 587 # Having a callback throwing up should not cause side-effects. It's a bit |
| 583 # hard to measure. | 588 # hard to measure. |
| 584 class Blow(Exception): | 589 class Blow(Exception): |
| 585 pass | 590 pass |
| 586 def blow(_): | 591 def blow(_): |
| 587 raise Blow() | 592 raise Blow() |
| 588 proc = subprocess2.Popen(self.exe + ['--stdout'], stdout=blow) | 593 proc = subprocess2.Popen(self.exe + ['--stdout'], stdout=blow) |
| 589 try: | 594 try: |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 671 return options.return_value | 676 return options.return_value |
| 672 | 677 |
| 673 | 678 |
| 674 if __name__ == '__main__': | 679 if __name__ == '__main__': |
| 675 logging.basicConfig(level= | 680 logging.basicConfig(level= |
| 676 [logging.WARNING, logging.INFO, logging.DEBUG][ | 681 [logging.WARNING, logging.INFO, logging.DEBUG][ |
| 677 min(2, sys.argv.count('-v'))]) | 682 min(2, sys.argv.count('-v'))]) |
| 678 if len(sys.argv) > 1 and sys.argv[1] == '--child': | 683 if len(sys.argv) > 1 and sys.argv[1] == '--child': |
| 679 sys.exit(child_main(sys.argv[2:])) | 684 sys.exit(child_main(sys.argv[2:])) |
| 680 unittest.main() | 685 unittest.main() |
| OLD | NEW |