Chromium Code Reviews| 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 optparse | 8 import optparse |
| 9 import os | 9 import os |
| 10 import sys | 10 import sys |
| 11 import time | 11 import time |
| 12 import unittest | 12 import unittest |
| 13 | 13 |
| 14 ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | 14 ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 15 sys.path.insert(0, ROOT_DIR) | 15 sys.path.insert(0, ROOT_DIR) |
| 16 | 16 |
| 17 import subprocess2 | 17 import subprocess2 |
| 18 | 18 |
| 19 # Method could be a function | |
| 20 # pylint: disable=R0201 | |
| 19 | 21 |
| 20 class Subprocess2Test(unittest.TestCase): | 22 class Subprocess2Test(unittest.TestCase): |
| 21 # Can be mocked in a test. | 23 # Can be mocked in a test. |
| 22 TO_SAVE = ['Popen', 'call', 'check_call', 'capture', 'check_output'] | 24 TO_SAVE = { |
| 25 subprocess2: ['Popen', 'call', 'check_call', 'capture', 'check_output'], | |
|
Dirk Pranke
2011/04/05 21:18:49
Don't need to change this in this patch, but, do y
| |
| 26 subprocess2.subprocess: ['Popen'], | |
| 27 } | |
| 23 | 28 |
| 24 def setUp(self): | 29 def setUp(self): |
| 25 self.exe_path = __file__ | 30 self.exe_path = __file__ |
| 26 self.exe = [self.exe_path, '--child'] | 31 self.exe = [self.exe_path, '--child'] |
| 27 self.saved = dict( | 32 self.saved = {} |
| 28 (name, getattr(subprocess2, name)) for name in self.TO_SAVE) | 33 for module, names in self.TO_SAVE.iteritems(): |
| 34 self.saved[module] = dict( | |
| 35 (name, getattr(module, name)) for name in names) | |
| 29 | 36 |
| 30 def tearDown(self): | 37 def tearDown(self): |
| 31 for name, value in self.saved.iteritems(): | 38 for module, saved in self.saved.iteritems(): |
| 32 setattr(subprocess2, name, value) | 39 for name, value in saved.iteritems(): |
| 40 setattr(module, name, value) | |
| 33 | 41 |
| 34 @staticmethod | 42 @staticmethod |
| 35 def _prep(): | 43 def _fake_call(): |
| 36 results = {} | 44 results = {} |
| 37 def fake_call(args, **kwargs): | 45 def fake_call(args, **kwargs): |
| 46 assert not results | |
| 38 results.update(kwargs) | 47 results.update(kwargs) |
| 39 results['args'] = args | 48 results['args'] = args |
| 40 return ['stdout', 'stderr'], 0 | 49 return ['stdout', 'stderr'], 0 |
| 41 subprocess2.call = fake_call | 50 subprocess2.call = fake_call |
| 42 return results | 51 return results |
| 43 | 52 |
| 53 @staticmethod | |
| 54 def _fake_Popen(): | |
| 55 results = {} | |
| 56 class fake_Popen(object): | |
| 57 returncode = -8 | |
| 58 def __init__(self, args, **kwargs): | |
| 59 assert not results | |
| 60 results.update(kwargs) | |
| 61 results['args'] = args | |
| 62 def communicate(self): | |
| 63 return None, None | |
| 64 subprocess2.Popen = fake_Popen | |
| 65 return results | |
| 66 | |
| 67 @staticmethod | |
| 68 def _fake_subprocess_Popen(): | |
| 69 results = {} | |
| 70 class fake_Popen(object): | |
| 71 returncode = -8 | |
| 72 def __init__(self, args, **kwargs): | |
| 73 assert not results | |
| 74 results.update(kwargs) | |
| 75 results['args'] = args | |
| 76 def communicate(self): | |
| 77 return None, None | |
| 78 subprocess2.subprocess.Popen = fake_Popen | |
| 79 return results | |
| 80 | |
| 44 def test_check_call_defaults(self): | 81 def test_check_call_defaults(self): |
| 45 results = self._prep() | 82 results = self._fake_call() |
| 46 self.assertEquals( | 83 self.assertEquals( |
| 47 ['stdout', 'stderr'], subprocess2.check_call(['foo'], a=True)) | 84 ['stdout', 'stderr'], subprocess2.check_call(['foo'], a=True)) |
| 48 expected = { | 85 expected = { |
| 49 'args': ['foo'], | 86 'args': ['foo'], |
| 50 'a':True, | 87 'a':True, |
| 51 } | 88 } |
| 52 self.assertEquals(expected, results) | 89 self.assertEquals(expected, results) |
| 53 | 90 |
| 91 def test_call_defaults(self): | |
| 92 results = self._fake_Popen() | |
| 93 self.assertEquals(((None, None), -8), subprocess2.call(['foo'], a=True)) | |
| 94 expected = { | |
| 95 'args': ['foo'], | |
| 96 'a': True, | |
| 97 } | |
| 98 self.assertEquals(expected, results) | |
| 99 | |
| 100 def test_Popen_defaults(self): | |
| 101 results = self._fake_subprocess_Popen() | |
| 102 proc = subprocess2.Popen(['foo'], a=True) | |
| 103 self.assertEquals(-8, proc.returncode) | |
| 104 env = os.environ.copy() | |
| 105 env['LANG'] = 'en_US.UTF-8' | |
| 106 expected = { | |
| 107 'args': ['foo'], | |
| 108 'a': True, | |
| 109 'shell': bool(sys.platform=='win32'), | |
| 110 'env': env, | |
| 111 } | |
| 112 self.assertEquals(expected, results) | |
| 113 | |
| 54 def test_check_output_defaults(self): | 114 def test_check_output_defaults(self): |
| 55 results = self._prep() | 115 results = self._fake_call() |
| 56 # It's discarding 'stderr' because it assumes stderr=subprocess2.STDOUT but | 116 # It's discarding 'stderr' because it assumes stderr=subprocess2.STDOUT but |
| 57 # fake_call() doesn't 'implement' that. | 117 # fake_call() doesn't 'implement' that. |
| 58 self.assertEquals('stdout', subprocess2.check_output(['foo'], a=True)) | 118 self.assertEquals('stdout', subprocess2.check_output(['foo'], a=True)) |
| 59 expected = { | 119 expected = { |
| 60 'args': ['foo'], | 120 'args': ['foo'], |
| 61 'a':True, | 121 'a':True, |
| 62 'stdout': subprocess2.PIPE, | 122 'stdout': subprocess2.PIPE, |
| 63 'stderr': subprocess2.STDOUT, | 123 'stderr': subprocess2.STDOUT, |
| 64 } | 124 } |
| 65 self.assertEquals(expected, results) | 125 self.assertEquals(expected, results) |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 128 do('CCC') | 188 do('CCC') |
| 129 if options.sleep: | 189 if options.sleep: |
| 130 time.sleep(10) | 190 time.sleep(10) |
| 131 return options.return_value | 191 return options.return_value |
| 132 | 192 |
| 133 | 193 |
| 134 if __name__ == '__main__': | 194 if __name__ == '__main__': |
| 135 if len(sys.argv) > 1 and sys.argv[1] == '--child': | 195 if len(sys.argv) > 1 and sys.argv[1] == '--child': |
| 136 sys.exit(child_main(sys.argv[2:])) | 196 sys.exit(child_main(sys.argv[2:])) |
| 137 unittest.main() | 197 unittest.main() |
| OLD | NEW |