Index: tests/subprocess2_test.py |
diff --git a/tests/subprocess2_test.py b/tests/subprocess2_test.py |
index 1f92cdf6895a1e29b02f3de50e6011d1919d79f4..6eadbcfd3c28acf1ed0bb47d13ee171eab600924 100755 |
--- a/tests/subprocess2_test.py |
+++ b/tests/subprocess2_test.py |
@@ -77,7 +77,7 @@ class DefaultsTest(auto_stub.TestCase): |
results.update(kwargs) |
results['args'] = args |
@staticmethod |
- def communicate(): |
+ def communicate(input=None, timeout=None): # pylint: disable=W0622 |
return None, None |
self.mock(subprocess2, 'Popen', fake_Popen) |
return results |
@@ -335,6 +335,12 @@ class S2Test(BaseTestCase): |
self.assertEquals(stderr, err) |
self.assertEquals(returncode, code) |
+ def _check_exception(self, e, stdout, stderr, returncode): |
+ """On exception, look if the exception members are set correctly.""" |
+ self.assertEquals(returncode, e.returncode) |
+ self.assertEquals(stdout, e.stdout) |
+ self.assertEquals(stderr, e.stderr) |
+ |
def test_timeout(self): |
# timeout doesn't exist in subprocess. |
def fn(c, e, un): |
@@ -405,6 +411,123 @@ class S2Test(BaseTestCase): |
self._check_res(res, None, None, 0) |
self._run_test(fn) |
+ def test_check_output_tee_stderr(self): |
+ def fn(c, e, un): |
+ stderr = [] |
+ res = subprocess2.communicate( |
+ e + ['--stderr'], stderr=stderr.append, universal_newlines=un) |
+ self.assertEquals(c('a\nbb\nccc\n'), ''.join(stderr)) |
+ self._check_res(res, None, None, 0) |
+ self._run_test(fn) |
+ |
+ def test_check_output_tee_stdout_stderr(self): |
+ def fn(c, e, un): |
+ stdout = [] |
+ stderr = [] |
+ res = subprocess2.communicate( |
+ e + ['--stdout', '--stderr'], |
+ stdout=stdout.append, |
+ stderr=stderr.append, |
+ universal_newlines=un) |
+ self.assertEquals(c('A\nBB\nCCC\n'), ''.join(stdout)) |
+ self.assertEquals(c('a\nbb\nccc\n'), ''.join(stderr)) |
+ self._check_res(res, None, None, 0) |
+ self._run_test(fn) |
+ |
+ def test_check_output_tee_stdin(self): |
+ def fn(c, e, un): |
+ stdout = [] |
+ stdin = '0123456789' |
+ res = subprocess2.communicate( |
+ e + ['--stdout', '--read'], stdin=stdin, stdout=stdout.append, |
+ universal_newlines=un) |
+ self.assertEquals(c('A\nBB\nCCC\n'), ''.join(stdout)) |
+ self._check_res(res, None, None, 0) |
+ self._run_test(fn) |
+ |
+ def test_check_output_tee_throw(self): |
+ def fn(c, e, un): |
+ stderr = [] |
+ try: |
+ subprocess2.check_output( |
+ e + ['--stderr', '--fail'], stderr=stderr.append, |
+ universal_newlines=un) |
+ self.fail() |
+ except subprocess2.CalledProcessError, e: |
+ self._check_exception(e, '', None, 64) |
+ self.assertEquals(c('a\nbb\nccc\n'), ''.join(stderr)) |
+ self._run_test(fn) |
+ |
+ def test_tee_timeout_stdout_void(self): |
+ def fn(c, e, un): |
+ stderr = [] |
+ res = subprocess2.communicate( |
+ e + ['--stdout', '--stderr', '--fail'], |
+ stdout=VOID, |
+ stderr=stderr.append, |
+ timeout=10, |
+ universal_newlines=un) |
+ self._check_res(res, None, None, 64) |
+ self.assertEquals(c('a\nbb\nccc\n'), ''.join(stderr)) |
+ self._run_test(fn) |
+ |
+ def test_tee_timeout_stderr_void(self): |
+ def fn(c, e, un): |
+ stdout = [] |
+ res = subprocess2.communicate( |
+ e + ['--stdout', '--stderr', '--fail'], |
+ stdout=stdout.append, |
+ stderr=VOID, |
+ timeout=10, |
+ universal_newlines=un) |
+ self._check_res(res, None, None, 64) |
+ self.assertEquals(c('A\nBB\nCCC\n'), ''.join(stdout)) |
+ self._run_test(fn) |
+ |
+ def test_tee_timeout_stderr_stdout(self): |
+ def fn(c, e, un): |
+ stdout = [] |
+ res = subprocess2.communicate( |
+ e + ['--stdout', '--stderr', '--fail'], |
+ stdout=stdout.append, |
+ stderr=STDOUT, |
+ timeout=10, |
+ universal_newlines=un) |
+ self._check_res(res, None, None, 64) |
+ # Ordering is wrong due to buffering. |
+ self.assertEquals(c('a\nbb\nccc\nA\nBB\nCCC\n'), ''.join(stdout)) |
+ self._run_test(fn) |
+ |
+ def test_tee_large(self): |
+ stdout = [] |
+ # Read 128kb. On my workstation it takes >2s. Welcome to 2011. |
+ res = subprocess2.communicate(self.exe + ['--large'], stdout=stdout.append) |
+ self.assertEquals(128*1024, len(''.join(stdout))) |
+ self._check_res(res, None, None, 0) |
+ |
+ def test_tee_large_stdin(self): |
+ stdout = [] |
+ # Write 128kb. |
+ stdin = '0123456789abcdef' * (8*1024) |
+ res = subprocess2.communicate( |
+ self.exe + ['--large', '--read'], stdin=stdin, stdout=stdout.append) |
+ self.assertEquals(128*1024, len(''.join(stdout))) |
+ self._check_res(res, None, None, 0) |
+ |
+ def test_tee_throw(self): |
+ # Having a callback throwing up should not cause side-effects. It's a bit |
+ # hard to measure. |
+ class Blow(Exception): |
+ pass |
+ def blow(_): |
+ raise Blow() |
+ proc = subprocess2.Popen(self.exe + ['--stdout'], stdout=blow) |
+ try: |
+ proc.communicate() |
+ self.fail() |
+ except Blow: |
+ self.assertNotEquals(0, proc.returncode) |
+ |
def child_main(args): |
if sys.platform == 'win32': |