| 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 # pylint: disable=E1101,W0403 | 6 # pylint: disable=E1101,W0403 |
| 7 | 7 |
| 8 import os |
| 8 import StringIO | 9 import StringIO |
| 9 | 10 |
| 10 # Fixes include path. | 11 # Fixes include path. |
| 11 from super_mox import SuperMoxTestBase | 12 from super_mox import SuperMoxTestBase |
| 12 import trial_dir | 13 import trial_dir |
| 13 import os | 14 |
| 14 import gclient_utils | 15 import gclient_utils |
| 16 import subprocess2 |
| 15 | 17 |
| 16 | 18 |
| 17 class GclientUtilBase(SuperMoxTestBase): | 19 class GclientUtilBase(SuperMoxTestBase): |
| 18 def setUp(self): | 20 def setUp(self): |
| 19 super(GclientUtilBase, self).setUp() | 21 super(GclientUtilBase, self).setUp() |
| 20 gclient_utils.sys.stdout.flush = lambda: None | 22 gclient_utils.sys.stdout.flush = lambda: None |
| 21 self.mox.StubOutWithMock(gclient_utils, 'Popen') | 23 self.mox.StubOutWithMock(subprocess2, 'Popen') |
| 24 self.mox.StubOutWithMock(subprocess2, 'communicate') |
| 22 | 25 |
| 23 | 26 |
| 24 class GclientUtilsUnittest(GclientUtilBase): | 27 class GclientUtilsUnittest(GclientUtilBase): |
| 25 """General gclient_utils.py tests.""" | 28 """General gclient_utils.py tests.""" |
| 26 def testMembersChanged(self): | 29 def testMembersChanged(self): |
| 27 members = [ | 30 members = [ |
| 28 'CheckCall', 'CheckCallError', 'CheckCallAndFilter', | 31 'CheckCallAndFilter', |
| 29 'CheckCallAndFilterAndHeader', 'Error', 'ExecutionQueue', 'FileRead', | 32 'CheckCallAndFilterAndHeader', 'Error', 'ExecutionQueue', 'FileRead', |
| 30 'FileWrite', 'FindFileUpwards', 'FindGclientRoot', | 33 'FileWrite', 'FindFileUpwards', 'FindGclientRoot', |
| 31 'GetGClientRootAndEntries', 'IsDateRevision', 'MakeDateRevision', | 34 'GetGClientRootAndEntries', 'IsDateRevision', 'MakeDateRevision', |
| 32 'MakeFileAutoFlush', 'MakeFileAnnotated', 'PathDifference', 'Popen', | 35 'MakeFileAutoFlush', 'MakeFileAnnotated', 'PathDifference', |
| 33 'PrintableObject', 'RemoveDirectory', 'SoftClone', 'SplitUrlRevision', | 36 'PrintableObject', 'RemoveDirectory', 'SoftClone', 'SplitUrlRevision', |
| 34 'SyntaxErrorToError', 'WorkItem', | 37 'SyntaxErrorToError', 'WorkItem', |
| 35 'errno', 'hack_subprocess', 'logging', 'os', 'Queue', 're', 'rmtree', | 38 'errno', 'logging', 'os', 'Queue', 're', 'rmtree', |
| 36 'stat', 'subprocess', 'sys','threading', 'time', | 39 'stat', 'subprocess2', 'sys','threading', 'time', |
| 37 ] | 40 ] |
| 38 # If this test fails, you should add the relevant test. | 41 # If this test fails, you should add the relevant test. |
| 39 self.compareMembers(gclient_utils, members) | 42 self.compareMembers(gclient_utils, members) |
| 40 | 43 |
| 41 | 44 |
| 42 class CheckCallTestCase(GclientUtilBase): | |
| 43 def testCheckCallSuccess(self): | |
| 44 args = ['boo', 'foo', 'bar'] | |
| 45 process = self.mox.CreateMockAnything() | |
| 46 process.returncode = 0 | |
| 47 gclient_utils.Popen( | |
| 48 args, cwd='bar', | |
| 49 stderr=None, | |
| 50 stdout=gclient_utils.subprocess.PIPE).AndReturn(process) | |
| 51 process.communicate().AndReturn(['bleh', 'foo']) | |
| 52 self.mox.ReplayAll() | |
| 53 gclient_utils.CheckCall(args, cwd='bar') | |
| 54 | |
| 55 def testCheckCallFailure(self): | |
| 56 args = ['boo', 'foo', 'bar'] | |
| 57 process = self.mox.CreateMockAnything() | |
| 58 process.returncode = 42 | |
| 59 gclient_utils.Popen( | |
| 60 args, | |
| 61 stderr=None, | |
| 62 stdout=gclient_utils.subprocess.PIPE).AndReturn(process) | |
| 63 process.communicate().AndReturn(['bleh', 'foo']) | |
| 64 self.mox.ReplayAll() | |
| 65 try: | |
| 66 gclient_utils.CheckCall(args) | |
| 67 self.fail() | |
| 68 except gclient_utils.CheckCallError, e: | |
| 69 self.assertEqual(e.command, args) | |
| 70 self.assertEqual(e.cwd, None) | |
| 71 self.assertEqual(e.returncode, 42) | |
| 72 self.assertEqual(e.stdout, 'bleh') | |
| 73 self.assertEqual(e.stderr, 'foo') | |
| 74 | |
| 75 | 45 |
| 76 class CheckCallAndFilterTestCase(GclientUtilBase): | 46 class CheckCallAndFilterTestCase(GclientUtilBase): |
| 77 class ProcessIdMock(object): | 47 class ProcessIdMock(object): |
| 78 def __init__(self, test_string): | 48 def __init__(self, test_string): |
| 79 self.stdout = StringIO.StringIO(test_string) | 49 self.stdout = StringIO.StringIO(test_string) |
| 80 def wait(self): | 50 def wait(self): |
| 81 pass | 51 pass |
| 82 | 52 |
| 83 def _inner(self, args, test_string): | 53 def _inner(self, args, test_string): |
| 84 cwd = 'bleh' | 54 cwd = 'bleh' |
| 85 gclient_utils.sys.stdout.write( | 55 gclient_utils.sys.stdout.write( |
| 86 '\n________ running \'boo foo bar\' in \'bleh\'\n') | 56 '\n________ running \'boo foo bar\' in \'bleh\'\n') |
| 87 for i in test_string: | 57 for i in test_string: |
| 88 gclient_utils.sys.stdout.write(i) | 58 gclient_utils.sys.stdout.write(i) |
| 89 gclient_utils.Popen( | 59 subprocess2.Popen( |
| 90 args, | 60 args, |
| 91 cwd=cwd, | 61 cwd=cwd, |
| 92 stdout=gclient_utils.subprocess.PIPE, | 62 stdout=subprocess2.PIPE, |
| 93 stderr=gclient_utils.subprocess.STDOUT, | 63 stderr=subprocess2.STDOUT, |
| 94 bufsize=0).AndReturn(self.ProcessIdMock(test_string)) | 64 bufsize=0).AndReturn(self.ProcessIdMock(test_string)) |
| 95 | 65 |
| 96 self.mox.ReplayAll() | 66 self.mox.ReplayAll() |
| 97 compiled_pattern = gclient_utils.re.compile(r'a(.*)b') | 67 compiled_pattern = gclient_utils.re.compile(r'a(.*)b') |
| 98 line_list = [] | 68 line_list = [] |
| 99 capture_list = [] | 69 capture_list = [] |
| 100 def FilterLines(line): | 70 def FilterLines(line): |
| 101 line_list.append(line) | 71 line_list.append(line) |
| 102 assert isinstance(line, str), type(line) | 72 assert isinstance(line, str), type(line) |
| 103 match = compiled_pattern.search(line) | 73 match = compiled_pattern.search(line) |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 os.chmod(l3, 0) | 167 os.chmod(l3, 0) |
| 198 os.chmod(l2, 0) | 168 os.chmod(l2, 0) |
| 199 os.chmod(l1, 0) | 169 os.chmod(l1, 0) |
| 200 | 170 |
| 201 | 171 |
| 202 if __name__ == '__main__': | 172 if __name__ == '__main__': |
| 203 import unittest | 173 import unittest |
| 204 unittest.main() | 174 unittest.main() |
| 205 | 175 |
| 206 # vim: ts=2:sw=2:tw=80:et: | 176 # vim: ts=2:sw=2:tw=80:et: |
| OLD | NEW |