| 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 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 | 21 |
| 22 class Subprocess2Test(unittest.TestCase): | 22 class Subprocess2Test(unittest.TestCase): |
| 23 # Can be mocked in a test. | 23 # Can be mocked in a test. |
| 24 TO_SAVE = { | 24 TO_SAVE = { |
| 25 subprocess2: ['Popen', 'call', 'check_call', 'capture', 'check_output'], | 25 subprocess2: ['Popen', 'call', 'check_call', 'capture', 'check_output'], |
| 26 subprocess2.subprocess: ['Popen'], | 26 subprocess2.subprocess: ['Popen'], |
| 27 } | 27 } |
| 28 | 28 |
| 29 def setUp(self): | 29 def setUp(self): |
| 30 self.exe_path = __file__ | 30 self.exe_path = __file__ |
| 31 self.exe = [self.exe_path, '--child'] | 31 self.exe = [sys.executable, self.exe_path, '--child'] |
| 32 self.saved = {} | 32 self.saved = {} |
| 33 for module, names in self.TO_SAVE.iteritems(): | 33 for module, names in self.TO_SAVE.iteritems(): |
| 34 self.saved[module] = dict( | 34 self.saved[module] = dict( |
| 35 (name, getattr(module, name)) for name in names) | 35 (name, getattr(module, name)) for name in names) |
| 36 | 36 |
| 37 def tearDown(self): | 37 def tearDown(self): |
| 38 for module, saved in self.saved.iteritems(): | 38 for module, saved in self.saved.iteritems(): |
| 39 for name, value in saved.iteritems(): | 39 for name, value in saved.iteritems(): |
| 40 setattr(module, name, value) | 40 setattr(module, name, value) |
| 41 | 41 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 expected = { | 94 expected = { |
| 95 'args': ['foo'], | 95 'args': ['foo'], |
| 96 'a': True, | 96 'a': True, |
| 97 } | 97 } |
| 98 self.assertEquals(expected, results) | 98 self.assertEquals(expected, results) |
| 99 | 99 |
| 100 def test_Popen_defaults(self): | 100 def test_Popen_defaults(self): |
| 101 results = self._fake_subprocess_Popen() | 101 results = self._fake_subprocess_Popen() |
| 102 proc = subprocess2.Popen(['foo'], a=True) | 102 proc = subprocess2.Popen(['foo'], a=True) |
| 103 self.assertEquals(-8, proc.returncode) | 103 self.assertEquals(-8, proc.returncode) |
| 104 env = os.environ.copy() | |
| 105 env['LANG'] = 'en_US.UTF-8' | |
| 106 expected = { | 104 expected = { |
| 107 'args': ['foo'], | 105 'args': ['foo'], |
| 108 'a': True, | 106 'a': True, |
| 109 'shell': bool(sys.platform=='win32'), | 107 'shell': bool(sys.platform=='win32'), |
| 110 'env': env, | |
| 111 } | 108 } |
| 109 if sys.platform != 'win32': |
| 110 env = os.environ.copy() |
| 111 is_english = lambda name: env.get(name, 'en').startswith('en') |
| 112 if not is_english('LANG'): |
| 113 env['LANG'] = 'en_US.UTF-8' |
| 114 expected['env'] = env |
| 115 if not is_english('LANGUAGE'): |
| 116 env['LANGUAGE'] = 'en_US.UTF-8' |
| 117 expected['env'] = env |
| 112 self.assertEquals(expected, results) | 118 self.assertEquals(expected, results) |
| 113 | 119 |
| 114 def test_check_output_defaults(self): | 120 def test_check_output_defaults(self): |
| 115 results = self._fake_call() | 121 results = self._fake_call() |
| 116 # It's discarding 'stderr' because it assumes stderr=subprocess2.STDOUT but | 122 # It's discarding 'stderr' because it assumes stderr=subprocess2.STDOUT but |
| 117 # fake_call() doesn't 'implement' that. | 123 # fake_call() doesn't 'implement' that. |
| 118 self.assertEquals('stdout', subprocess2.check_output(['foo'], a=True)) | 124 self.assertEquals('stdout', subprocess2.check_output(['foo'], a=True)) |
| 119 expected = { | 125 expected = { |
| 120 'args': ['foo'], | 126 'args': ['foo'], |
| 121 'a':True, | 127 'a':True, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 133 self.assertEquals(-9, returncode) | 139 self.assertEquals(-9, returncode) |
| 134 self.assertEquals(['', None], out) | 140 self.assertEquals(['', None], out) |
| 135 | 141 |
| 136 def test_void(self): | 142 def test_void(self): |
| 137 out = subprocess2.check_output( | 143 out = subprocess2.check_output( |
| 138 self.exe + ['--stdout', '--stderr'], | 144 self.exe + ['--stdout', '--stderr'], |
| 139 stdout=subprocess2.VOID) | 145 stdout=subprocess2.VOID) |
| 140 self.assertEquals(None, out) | 146 self.assertEquals(None, out) |
| 141 out = subprocess2.check_output( | 147 out = subprocess2.check_output( |
| 142 self.exe + ['--stdout', '--stderr'], | 148 self.exe + ['--stdout', '--stderr'], |
| 149 universal_newlines=True, |
| 143 stderr=subprocess2.VOID) | 150 stderr=subprocess2.VOID) |
| 144 self.assertEquals('A\nBB\nCCC\n', out) | 151 self.assertEquals('A\nBB\nCCC\n', out) |
| 145 | 152 |
| 146 def test_check_output_throw(self): | 153 def test_check_output_throw(self): |
| 147 try: | 154 try: |
| 148 subprocess2.check_output(self.exe + ['--fail', '--stderr']) | 155 subprocess2.check_output( |
| 156 self.exe + ['--fail', '--stderr'], universal_newlines=True) |
| 149 self.fail() | 157 self.fail() |
| 150 except subprocess2.CalledProcessError, e: | 158 except subprocess2.CalledProcessError, e: |
| 151 self.assertEquals('a\nbb\nccc\n', e.stdout) | 159 self.assertEquals('a\nbb\nccc\n', e.stdout) |
| 152 self.assertEquals(None, e.stderr) | 160 self.assertEquals(None, e.stderr) |
| 153 self.assertEquals(64, e.returncode) | 161 self.assertEquals(64, e.returncode) |
| 154 | 162 |
| 155 def test_check_call_throw(self): | 163 def test_check_call_throw(self): |
| 156 try: | 164 try: |
| 157 subprocess2.check_call(self.exe + ['--fail', '--stderr']) | 165 subprocess2.check_call(self.exe + ['--fail', '--stderr']) |
| 158 self.fail() | 166 self.fail() |
| (...skipping 29 matching lines...) Expand all Loading... |
| 188 do('CCC') | 196 do('CCC') |
| 189 if options.sleep: | 197 if options.sleep: |
| 190 time.sleep(10) | 198 time.sleep(10) |
| 191 return options.return_value | 199 return options.return_value |
| 192 | 200 |
| 193 | 201 |
| 194 if __name__ == '__main__': | 202 if __name__ == '__main__': |
| 195 if len(sys.argv) > 1 and sys.argv[1] == '--child': | 203 if len(sys.argv) > 1 and sys.argv[1] == '--child': |
| 196 sys.exit(child_main(sys.argv[2:])) | 204 sys.exit(child_main(sys.argv[2:])) |
| 197 unittest.main() | 205 unittest.main() |
| OLD | NEW |