| 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 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 15 sys.path.insert(0, ROOT_DIR) | |
| 16 | 15 |
| 17 import subprocess2 | 16 import subprocess2 |
| 18 | 17 |
| 19 # Method could be a function | |
| 20 # pylint: disable=R0201 | |
| 21 | |
| 22 class Subprocess2Test(unittest.TestCase): | 18 class Subprocess2Test(unittest.TestCase): |
| 23 # Can be mocked in a test. | 19 # Can be mocked in a test. |
| 24 TO_SAVE = { | 20 TO_SAVE = { |
| 25 subprocess2: [ | 21 subprocess2: [ |
| 26 'Popen', 'communicate', 'call', 'check_call', 'capture', 'check_output'], | 22 'Popen', 'communicate', 'call', 'check_call', 'capture', 'check_output'], |
| 27 subprocess2.subprocess: ['Popen'], | 23 subprocess2.subprocess: ['Popen'], |
| 28 } | 24 } |
| 29 | 25 |
| 30 def setUp(self): | 26 def setUp(self): |
| 31 self.exe_path = __file__ | 27 self.exe_path = __file__ |
| (...skipping 23 matching lines...) Expand all Loading... |
| 55 | 51 |
| 56 @staticmethod | 52 @staticmethod |
| 57 def _fake_Popen(): | 53 def _fake_Popen(): |
| 58 results = {} | 54 results = {} |
| 59 class fake_Popen(object): | 55 class fake_Popen(object): |
| 60 returncode = -8 | 56 returncode = -8 |
| 61 def __init__(self, args, **kwargs): | 57 def __init__(self, args, **kwargs): |
| 62 assert not results | 58 assert not results |
| 63 results.update(kwargs) | 59 results.update(kwargs) |
| 64 results['args'] = args | 60 results['args'] = args |
| 65 def communicate(self): | 61 @staticmethod |
| 62 def communicate(): |
| 66 return None, None | 63 return None, None |
| 67 subprocess2.Popen = fake_Popen | 64 subprocess2.Popen = fake_Popen |
| 68 return results | 65 return results |
| 69 | 66 |
| 70 @staticmethod | 67 @staticmethod |
| 71 def _fake_subprocess_Popen(): | 68 def _fake_subprocess_Popen(): |
| 72 results = {} | 69 results = {} |
| 73 class fake_Popen(object): | 70 class fake_Popen(object): |
| 74 returncode = -8 | 71 returncode = -8 |
| 75 def __init__(self, args, **kwargs): | 72 def __init__(self, args, **kwargs): |
| 76 assert not results | 73 assert not results |
| 77 results.update(kwargs) | 74 results.update(kwargs) |
| 78 results['args'] = args | 75 results['args'] = args |
| 79 def communicate(self): | 76 @staticmethod |
| 77 def communicate(): |
| 80 return None, None | 78 return None, None |
| 81 subprocess2.subprocess.Popen = fake_Popen | 79 subprocess2.subprocess.Popen = fake_Popen |
| 82 return results | 80 return results |
| 83 | 81 |
| 84 def test_check_call_defaults(self): | 82 def test_check_call_defaults(self): |
| 85 results = self._fake_communicate() | 83 results = self._fake_communicate() |
| 86 self.assertEquals( | 84 self.assertEquals( |
| 87 ['stdout', 'stderr'], subprocess2.check_call_out(['foo'], a=True)) | 85 ['stdout', 'stderr'], subprocess2.check_call_out(['foo'], a=True)) |
| 88 expected = { | 86 expected = { |
| 89 'args': ['foo'], | 87 'args': ['foo'], |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 do('CCC') | 260 do('CCC') |
| 263 if options.sleep: | 261 if options.sleep: |
| 264 time.sleep(10) | 262 time.sleep(10) |
| 265 return options.return_value | 263 return options.return_value |
| 266 | 264 |
| 267 | 265 |
| 268 if __name__ == '__main__': | 266 if __name__ == '__main__': |
| 269 if len(sys.argv) > 1 and sys.argv[1] == '--child': | 267 if len(sys.argv) > 1 and sys.argv[1] == '--child': |
| 270 sys.exit(child_main(sys.argv[2:])) | 268 sys.exit(child_main(sys.argv[2:])) |
| 271 unittest.main() | 269 unittest.main() |
| OLD | NEW |