OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 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 |
| 4 # found in the LICENSE file. |
| 5 |
| 6 """Unit tests for subprocess2.py.""" |
| 7 |
| 8 import optparse |
| 9 import os |
| 10 import sys |
| 11 import time |
| 12 import unittest |
| 13 |
| 14 ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 15 sys.path.insert(0, ROOT_DIR) |
| 16 |
| 17 import subprocess2 |
| 18 |
| 19 |
| 20 class Subprocess2Test(unittest.TestCase): |
| 21 # Can be mocked in a test. |
| 22 TO_SAVE = ['Popen', 'call', 'check_call', 'capture', 'check_output'] |
| 23 |
| 24 def setUp(self): |
| 25 self.exe_path = __file__ |
| 26 self.exe = [self.exe_path, '--child'] |
| 27 self.saved = dict( |
| 28 (name, getattr(subprocess2, name)) for name in self.TO_SAVE) |
| 29 |
| 30 def tearDown(self): |
| 31 for name, value in self.saved.iteritems(): |
| 32 setattr(subprocess2, name, value) |
| 33 |
| 34 @staticmethod |
| 35 def _prep(): |
| 36 results = {} |
| 37 def fake_call(args, **kwargs): |
| 38 results.update(kwargs) |
| 39 results['args'] = args |
| 40 return ['stdout', 'stderr'], 0 |
| 41 subprocess2.call = fake_call |
| 42 return results |
| 43 |
| 44 def test_check_call_defaults(self): |
| 45 results = self._prep() |
| 46 self.assertEquals( |
| 47 ['stdout', 'stderr'], subprocess2.check_call(['foo'], a=True)) |
| 48 expected = { |
| 49 'args': ['foo'], |
| 50 'a':True, |
| 51 } |
| 52 self.assertEquals(expected, results) |
| 53 |
| 54 def test_check_output_defaults(self): |
| 55 results = self._prep() |
| 56 # It's discarding 'stderr' because it assumes stderr=subprocess2.STDOUT but |
| 57 # fake_call() doesn't 'implement' that. |
| 58 self.assertEquals('stdout', subprocess2.check_output(['foo'], a=True)) |
| 59 expected = { |
| 60 'args': ['foo'], |
| 61 'a':True, |
| 62 'stdout': subprocess2.PIPE, |
| 63 'stderr': subprocess2.STDOUT, |
| 64 } |
| 65 self.assertEquals(expected, results) |
| 66 |
| 67 def test_timeout(self): |
| 68 # It'd be better to not discard stdout. |
| 69 out, returncode = subprocess2.call( |
| 70 self.exe + ['--sleep', '--stdout'], |
| 71 timeout=0.01, |
| 72 stdout=subprocess2.PIPE) |
| 73 self.assertEquals(-9, returncode) |
| 74 self.assertEquals(['', None], out) |
| 75 |
| 76 def test_void(self): |
| 77 out = subprocess2.check_output( |
| 78 self.exe + ['--stdout', '--stderr'], |
| 79 stdout=subprocess2.VOID) |
| 80 self.assertEquals(None, out) |
| 81 out = subprocess2.check_output( |
| 82 self.exe + ['--stdout', '--stderr'], |
| 83 stderr=subprocess2.VOID) |
| 84 self.assertEquals('A\nBB\nCCC\n', out) |
| 85 |
| 86 def test_check_output_throw(self): |
| 87 try: |
| 88 subprocess2.check_output(self.exe + ['--fail', '--stderr']) |
| 89 self.fail() |
| 90 except subprocess2.CalledProcessError, e: |
| 91 self.assertEquals('a\nbb\nccc\n', e.stdout) |
| 92 self.assertEquals(None, e.stderr) |
| 93 self.assertEquals(64, e.returncode) |
| 94 |
| 95 def test_check_call_throw(self): |
| 96 try: |
| 97 subprocess2.check_call(self.exe + ['--fail', '--stderr']) |
| 98 self.fail() |
| 99 except subprocess2.CalledProcessError, e: |
| 100 self.assertEquals(None, e.stdout) |
| 101 self.assertEquals(None, e.stderr) |
| 102 self.assertEquals(64, e.returncode) |
| 103 |
| 104 |
| 105 def child_main(args): |
| 106 parser = optparse.OptionParser() |
| 107 parser.add_option( |
| 108 '--fail', |
| 109 dest='return_value', |
| 110 action='store_const', |
| 111 default=0, |
| 112 const=64) |
| 113 parser.add_option('--stdout', action='store_true') |
| 114 parser.add_option('--stderr', action='store_true') |
| 115 parser.add_option('--sleep', action='store_true') |
| 116 options, args = parser.parse_args(args) |
| 117 if args: |
| 118 parser.error('Internal error') |
| 119 |
| 120 def do(string): |
| 121 if options.stdout: |
| 122 print >> sys.stdout, string.upper() |
| 123 if options.stderr: |
| 124 print >> sys.stderr, string.lower() |
| 125 |
| 126 do('A') |
| 127 do('BB') |
| 128 do('CCC') |
| 129 if options.sleep: |
| 130 time.sleep(10) |
| 131 return options.return_value |
| 132 |
| 133 |
| 134 if __name__ == '__main__': |
| 135 if len(sys.argv) > 1 and sys.argv[1] == '--child': |
| 136 sys.exit(child_main(sys.argv[2:])) |
| 137 unittest.main() |
OLD | NEW |