| 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 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 pass | 246 pass |
| 247 | 247 |
| 248 def test_check_output_throw_stdout(self): | 248 def test_check_output_throw_stdout(self): |
| 249 def fn(c, e, un, subp): | 249 def fn(c, e, un, subp): |
| 250 if not hasattr(subp, 'check_output'): | 250 if not hasattr(subp, 'check_output'): |
| 251 return | 251 return |
| 252 try: | 252 try: |
| 253 subp.check_output( | 253 subp.check_output( |
| 254 e + ['--fail', '--stdout'], universal_newlines=un) | 254 e + ['--fail', '--stdout'], universal_newlines=un) |
| 255 self.fail() | 255 self.fail() |
| 256 except subp.CalledProcessError, e: | 256 except subp.CalledProcessError, exception: |
| 257 self._check_exception(subp, e, c('A\nBB\nCCC\n'), None, 64) | 257 self._check_exception(subp, exception, c('A\nBB\nCCC\n'), None, 64) |
| 258 self._run_test(fn) | 258 self._run_test(fn) |
| 259 | 259 |
| 260 def test_check_output_throw_no_stderr(self): | 260 def test_check_output_throw_no_stderr(self): |
| 261 def fn(c, e, un, subp): | 261 def fn(c, e, un, subp): |
| 262 if not hasattr(subp, 'check_output'): | 262 if not hasattr(subp, 'check_output'): |
| 263 return | 263 return |
| 264 try: | 264 try: |
| 265 subp.check_output( | 265 subp.check_output( |
| 266 e + ['--fail', '--stderr'], universal_newlines=un) | 266 e + ['--fail', '--stderr'], universal_newlines=un) |
| 267 self.fail() | 267 self.fail() |
| 268 except subp.CalledProcessError, e: | 268 except subp.CalledProcessError, exception: |
| 269 self._check_exception(subp, e, c(''), None, 64) | 269 self._check_exception(subp, exception, c(''), None, 64) |
| 270 self._run_test(fn) | 270 self._run_test(fn) |
| 271 | 271 |
| 272 def test_check_output_throw_stderr(self): | 272 def test_check_output_throw_stderr(self): |
| 273 def fn(c, e, un, subp): | 273 def fn(c, e, un, subp): |
| 274 if not hasattr(subp, 'check_output'): | 274 if not hasattr(subp, 'check_output'): |
| 275 return | 275 return |
| 276 try: | 276 try: |
| 277 subp.check_output( | 277 subp.check_output( |
| 278 e + ['--fail', '--stderr'], | 278 e + ['--fail', '--stderr'], |
| 279 stderr=subp.PIPE, | 279 stderr=subp.PIPE, |
| 280 universal_newlines=un) | 280 universal_newlines=un) |
| 281 self.fail() | 281 self.fail() |
| 282 except subp.CalledProcessError, e: | 282 except subp.CalledProcessError, exception: |
| 283 self._check_exception(subp, e, '', c('a\nbb\nccc\n'), 64) | 283 self._check_exception(subp, exception, '', c('a\nbb\nccc\n'), 64) |
| 284 self._run_test(fn) | 284 self._run_test(fn) |
| 285 | 285 |
| 286 def test_check_output_throw_stderr_stdout(self): | 286 def test_check_output_throw_stderr_stdout(self): |
| 287 def fn(c, e, un, subp): | 287 def fn(c, e, un, subp): |
| 288 if not hasattr(subp, 'check_output'): | 288 if not hasattr(subp, 'check_output'): |
| 289 return | 289 return |
| 290 try: | 290 try: |
| 291 subp.check_output( | 291 subp.check_output( |
| 292 e + ['--fail', '--stderr'], | 292 e + ['--fail', '--stderr'], |
| 293 stderr=subp.STDOUT, | 293 stderr=subp.STDOUT, |
| 294 universal_newlines=un) | 294 universal_newlines=un) |
| 295 self.fail() | 295 self.fail() |
| 296 except subp.CalledProcessError, e: | 296 except subp.CalledProcessError, exception: |
| 297 self._check_exception(subp, e, c('a\nbb\nccc\n'), None, 64) | 297 self._check_exception(subp, exception, c('a\nbb\nccc\n'), None, 64) |
| 298 self._run_test(fn) | 298 self._run_test(fn) |
| 299 | 299 |
| 300 def test_check_call_throw(self): | 300 def test_check_call_throw(self): |
| 301 for subp in (subprocess, subprocess2): | 301 for subp in (subprocess, subprocess2): |
| 302 try: | 302 try: |
| 303 subp.check_call(self.exe + ['--fail', '--stderr']) | 303 subp.check_call(self.exe + ['--fail', '--stderr']) |
| 304 self.fail() | 304 self.fail() |
| 305 except subp.CalledProcessError, e: | 305 except subp.CalledProcessError, exception: |
| 306 self._check_exception(subp, e, None, None, 64) | 306 self._check_exception(subp, exception, None, None, 64) |
| 307 | 307 |
| 308 def test_redirect_stderr_to_stdout_pipe(self): | 308 def test_redirect_stderr_to_stdout_pipe(self): |
| 309 def fn(c, e, un, subp): | 309 def fn(c, e, un, subp): |
| 310 # stderr output into stdout. | 310 # stderr output into stdout. |
| 311 proc = subp.Popen( | 311 proc = subp.Popen( |
| 312 e + ['--stderr'], | 312 e + ['--stderr'], |
| 313 stdout=subp.PIPE, | 313 stdout=subp.PIPE, |
| 314 stderr=subp.STDOUT, | 314 stderr=subp.STDOUT, |
| 315 universal_newlines=un) | 315 universal_newlines=un) |
| 316 res = proc.communicate(), proc.returncode | 316 res = proc.communicate(), proc.returncode |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 496 def test_tee_throw(self): | 496 def test_tee_throw(self): |
| 497 def fn(c, e, un): | 497 def fn(c, e, un): |
| 498 # Make sure failure still returns stderr completely. | 498 # Make sure failure still returns stderr completely. |
| 499 stderr = [] | 499 stderr = [] |
| 500 try: | 500 try: |
| 501 subprocess2.check_output( | 501 subprocess2.check_output( |
| 502 e + ['--stderr', '--fail'], | 502 e + ['--stderr', '--fail'], |
| 503 stderr=stderr.append, | 503 stderr=stderr.append, |
| 504 universal_newlines=un) | 504 universal_newlines=un) |
| 505 self.fail() | 505 self.fail() |
| 506 except subprocess2.CalledProcessError, e: | 506 except subprocess2.CalledProcessError, exception: |
| 507 self._check_exception(e, '', None, 64) | 507 self._check_exception(exception, '', None, 64) |
| 508 self.assertEquals(c('a\nbb\nccc\n'), ''.join(stderr)) | 508 self.assertEquals(c('a\nbb\nccc\n'), ''.join(stderr)) |
| 509 self._run_test(fn) | 509 self._run_test(fn) |
| 510 | 510 |
| 511 def test_tee_timeout_stdout_void(self): | 511 def test_tee_timeout_stdout_void(self): |
| 512 def fn(c, e, un): | 512 def fn(c, e, un): |
| 513 stderr = [] | 513 stderr = [] |
| 514 res = subprocess2.communicate( | 514 res = subprocess2.communicate( |
| 515 e + ['--stdout', '--stderr', '--fail'], | 515 e + ['--stdout', '--stderr', '--fail'], |
| 516 stdout=VOID, | 516 stdout=VOID, |
| 517 stderr=stderr.append, | 517 stderr=stderr.append, |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 642 return options.return_value | 642 return options.return_value |
| 643 | 643 |
| 644 | 644 |
| 645 if __name__ == '__main__': | 645 if __name__ == '__main__': |
| 646 logging.basicConfig(level= | 646 logging.basicConfig(level= |
| 647 [logging.WARNING, logging.INFO, logging.DEBUG][ | 647 [logging.WARNING, logging.INFO, logging.DEBUG][ |
| 648 min(2, sys.argv.count('-v'))]) | 648 min(2, sys.argv.count('-v'))]) |
| 649 if len(sys.argv) > 1 and sys.argv[1] == '--child': | 649 if len(sys.argv) > 1 and sys.argv[1] == '--child': |
| 650 sys.exit(child_main(sys.argv[2:])) | 650 sys.exit(child_main(sys.argv[2:])) |
| 651 unittest.main() | 651 unittest.main() |
| OLD | NEW |