| 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 |
| 11 import sys | 11 import sys |
| 12 import time | 12 import time |
| 13 import unittest | 13 import unittest |
| 14 | 14 |
| 15 try: | 15 try: |
| 16 import fcntl | 16 import fcntl |
| 17 except ImportError: | 17 except ImportError: |
| 18 fcntl = None | 18 fcntl = None |
| 19 | 19 |
| 20 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | 20 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 21 | 21 |
| 22 import subprocess2 | 22 import subprocess2 |
| 23 | 23 |
| 24 from testing_support import auto_stub |
| 25 |
| 24 # Method could be a function | 26 # Method could be a function |
| 25 # pylint: disable=R0201 | 27 # pylint: disable=R0201 |
| 26 | 28 |
| 27 | 29 |
| 28 def convert_to_crlf(string): | 30 def convert_to_crlf(string): |
| 29 """Unconditionally convert LF to CRLF.""" | 31 """Unconditionally convert LF to CRLF.""" |
| 30 return string.replace('\n', '\r\n') | 32 return string.replace('\n', '\r\n') |
| 31 | 33 |
| 32 | 34 |
| 33 def convert_to_cr(string): | 35 def convert_to_cr(string): |
| 34 """Unconditionally convert LF to CR.""" | 36 """Unconditionally convert LF to CR.""" |
| 35 return string.replace('\n', '\r') | 37 return string.replace('\n', '\r') |
| 36 | 38 |
| 37 | 39 |
| 38 def convert_win(string): | 40 def convert_win(string): |
| 39 """Converts string to CRLF on Windows only.""" | 41 """Converts string to CRLF on Windows only.""" |
| 40 if sys.platform == 'win32': | 42 if sys.platform == 'win32': |
| 41 return string.replace('\n', '\r\n') | 43 return string.replace('\n', '\r\n') |
| 42 return string | 44 return string |
| 43 | 45 |
| 44 | 46 |
| 45 class DefaultsTest(unittest.TestCase): | 47 class DefaultsTest(auto_stub.TestCase): |
| 46 # Can be mocked in a test. | 48 # TODO(maruel): Do a reopen() on sys.__stdout__ and sys.__stderr__ so they |
| 47 TO_SAVE = { | 49 # can be trapped in the child process for better coverage. |
| 48 subprocess2: [ | 50 def _fake_communicate(self): |
| 49 'Popen', 'communicate', 'call', 'check_call', 'capture', 'check_output'], | |
| 50 subprocess2.subprocess.Popen: ['__init__', 'communicate'], | |
| 51 } | |
| 52 | |
| 53 def setUp(self): | |
| 54 self.saved = {} | |
| 55 for module, names in self.TO_SAVE.iteritems(): | |
| 56 self.saved[module] = dict( | |
| 57 (name, getattr(module, name)) for name in names) | |
| 58 # TODO(maruel): Do a reopen() on sys.__stdout__ and sys.__stderr__ so they | |
| 59 # can be trapped in the child process for better coverage. | |
| 60 | |
| 61 def tearDown(self): | |
| 62 for module, saved in self.saved.iteritems(): | |
| 63 for name, value in saved.iteritems(): | |
| 64 setattr(module, name, value) | |
| 65 | |
| 66 @staticmethod | |
| 67 def _fake_communicate(): | |
| 68 """Mocks subprocess2.communicate().""" | 51 """Mocks subprocess2.communicate().""" |
| 69 results = {} | 52 results = {} |
| 70 def fake_communicate(args, **kwargs): | 53 def fake_communicate(args, **kwargs): |
| 71 assert not results | 54 assert not results |
| 72 results.update(kwargs) | 55 results.update(kwargs) |
| 73 results['args'] = args | 56 results['args'] = args |
| 74 return ('stdout', 'stderr'), 0 | 57 return ('stdout', 'stderr'), 0 |
| 75 subprocess2.communicate = fake_communicate | 58 self.mock(subprocess2, 'communicate', fake_communicate) |
| 76 return results | 59 return results |
| 77 | 60 |
| 78 @staticmethod | 61 def _fake_Popen(self): |
| 79 def _fake_Popen(): | |
| 80 """Mocks the whole subprocess2.Popen class.""" | 62 """Mocks the whole subprocess2.Popen class.""" |
| 81 results = {} | 63 results = {} |
| 82 class fake_Popen(object): | 64 class fake_Popen(object): |
| 83 returncode = -8 | 65 returncode = -8 |
| 84 def __init__(self, args, **kwargs): | 66 def __init__(self, args, **kwargs): |
| 85 assert not results | 67 assert not results |
| 86 results.update(kwargs) | 68 results.update(kwargs) |
| 87 results['args'] = args | 69 results['args'] = args |
| 88 @staticmethod | 70 @staticmethod |
| 89 def communicate(): | 71 def communicate(): |
| 90 return None, None | 72 return None, None |
| 91 subprocess2.Popen = fake_Popen | 73 self.mock(subprocess2, 'Popen', fake_Popen) |
| 92 return results | 74 return results |
| 93 | 75 |
| 94 @staticmethod | 76 def _fake_subprocess_Popen(self): |
| 95 def _fake_subprocess_Popen(): | |
| 96 """Mocks the base class subprocess.Popen only.""" | 77 """Mocks the base class subprocess.Popen only.""" |
| 97 results = {} | 78 results = {} |
| 98 def __init__(self, args, **kwargs): | 79 def __init__(self, args, **kwargs): |
| 99 assert not results | 80 assert not results |
| 100 results.update(kwargs) | 81 results.update(kwargs) |
| 101 results['args'] = args | 82 results['args'] = args |
| 102 def communicate(): | 83 def communicate(): |
| 103 return None, None | 84 return None, None |
| 104 subprocess2.subprocess.Popen.__init__ = __init__ | 85 self.mock(subprocess2.subprocess.Popen, '__init__', __init__) |
| 105 subprocess2.subprocess.Popen.communicate = communicate | 86 self.mock(subprocess2.subprocess.Popen, 'communicate', communicate) |
| 106 return results | 87 return results |
| 107 | 88 |
| 108 def test_check_call_defaults(self): | 89 def test_check_call_defaults(self): |
| 109 results = self._fake_communicate() | 90 results = self._fake_communicate() |
| 110 self.assertEquals( | 91 self.assertEquals( |
| 111 ('stdout', 'stderr'), subprocess2.check_call_out(['foo'], a=True)) | 92 ('stdout', 'stderr'), subprocess2.check_call_out(['foo'], a=True)) |
| 112 expected = { | 93 expected = { |
| 113 'args': ['foo'], | 94 'args': ['foo'], |
| 114 'a':True, | 95 'a':True, |
| 115 } | 96 } |
| (...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 397 return options.return_value | 378 return options.return_value |
| 398 | 379 |
| 399 | 380 |
| 400 if __name__ == '__main__': | 381 if __name__ == '__main__': |
| 401 logging.basicConfig(level= | 382 logging.basicConfig(level= |
| 402 [logging.WARNING, logging.INFO, logging.DEBUG][ | 383 [logging.WARNING, logging.INFO, logging.DEBUG][ |
| 403 min(2, sys.argv.count('-v'))]) | 384 min(2, sys.argv.count('-v'))]) |
| 404 if len(sys.argv) > 1 and sys.argv[1] == '--child': | 385 if len(sys.argv) > 1 and sys.argv[1] == '--child': |
| 405 sys.exit(child_main(sys.argv[2:])) | 386 sys.exit(child_main(sys.argv[2:])) |
| 406 unittest.main() | 387 unittest.main() |
| OLD | NEW |