OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # Copyright (c) 2010 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2010 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 import StringIO | 6 import StringIO |
7 | 7 |
8 # Fixes include path. | 8 # Fixes include path. |
9 from super_mox import SuperMoxTestBase | 9 from super_mox import SuperMoxTestBase |
10 | 10 |
11 import gclient_utils | 11 import gclient_utils |
12 | 12 |
13 | 13 |
14 class GclientUtilBase(SuperMoxTestBase): | 14 class GclientUtilBase(SuperMoxTestBase): |
15 def setUp(self): | 15 def setUp(self): |
16 super(GclientUtilBase, self).setUp() | 16 super(GclientUtilBase, self).setUp() |
17 gclient_utils.sys.stdout.flush = lambda: None | 17 gclient_utils.sys.stdout.flush = lambda: None |
| 18 self.mox.StubOutWithMock(gclient_utils, 'Popen') |
18 | 19 |
19 | 20 |
20 class GclientUtilsUnittest(GclientUtilBase): | 21 class GclientUtilsUnittest(GclientUtilBase): |
21 """General gclient_utils.py tests.""" | 22 """General gclient_utils.py tests.""" |
22 def testMembersChanged(self): | 23 def testMembersChanged(self): |
23 members = [ | 24 members = [ |
24 'CheckCall', 'CheckCallError', 'Error', 'ExecutionQueue', 'FileRead', | 25 'CheckCall', 'CheckCallError', 'CheckCallAndFilter', |
| 26 'CheckCallAndFilterAndHeader', 'Error', 'ExecutionQueue', 'FileRead', |
25 'FileWrite', 'FindFileUpwards', 'FindGclientRoot', | 27 'FileWrite', 'FindFileUpwards', 'FindGclientRoot', |
26 'GetGClientRootAndEntries', 'GetNamedNodeText', | 28 'GetGClientRootAndEntries', 'GetNamedNodeText', |
27 'GetNodeNamedAttributeText', 'PathDifference', 'ParseXML', 'Popen', | 29 'GetNodeNamedAttributeText', 'PathDifference', 'ParseXML', 'Popen', |
28 'PrintableObject', 'RemoveDirectory', 'SplitUrlRevision', | 30 'PrintableObject', 'RemoveDirectory', 'SplitUrlRevision', |
29 'SubprocessCall', 'SubprocessCallAndFilter', 'SyntaxErrorToError', | 31 'SyntaxErrorToError', 'WorkItem', |
30 'WorkItem', | |
31 'errno', 'logging', 'os', 're', 'stat', 'subprocess', 'sys', | 32 'errno', 'logging', 'os', 're', 'stat', 'subprocess', 'sys', |
32 'threading', 'time', 'xml', | 33 'threading', 'time', 'xml', |
33 ] | 34 ] |
34 # If this test fails, you should add the relevant test. | 35 # If this test fails, you should add the relevant test. |
35 self.compareMembers(gclient_utils, members) | 36 self.compareMembers(gclient_utils, members) |
36 | 37 |
37 | 38 |
38 class CheckCallTestCase(GclientUtilBase): | 39 class CheckCallTestCase(GclientUtilBase): |
39 def testCheckCallSuccess(self): | 40 def testCheckCallSuccess(self): |
40 args = ['boo', 'foo', 'bar'] | 41 args = ['boo', 'foo', 'bar'] |
41 process = self.mox.CreateMockAnything() | 42 process = self.mox.CreateMockAnything() |
42 process.returncode = 0 | 43 process.returncode = 0 |
43 env = gclient_utils.os.environ.copy() | 44 gclient_utils.Popen( |
44 env['LANGUAGE'] = 'en' | |
45 gclient_utils.subprocess.Popen( | |
46 args, cwd=None, | 45 args, cwd=None, |
47 stderr=None, | 46 stderr=None, |
48 env=env, | 47 stdout=gclient_utils.subprocess.PIPE).AndReturn(process) |
49 stdout=gclient_utils.subprocess.PIPE, | |
50 shell=gclient_utils.sys.platform.startswith('win')).AndReturn(process) | |
51 process.communicate().AndReturn(['bleh', 'foo']) | 48 process.communicate().AndReturn(['bleh', 'foo']) |
52 self.mox.ReplayAll() | 49 self.mox.ReplayAll() |
53 gclient_utils.CheckCall(args) | 50 gclient_utils.CheckCall(args) |
54 | 51 |
55 def testCheckCallFailure(self): | 52 def testCheckCallFailure(self): |
56 args = ['boo', 'foo', 'bar'] | 53 args = ['boo', 'foo', 'bar'] |
57 process = self.mox.CreateMockAnything() | 54 process = self.mox.CreateMockAnything() |
58 process.returncode = 42 | 55 process.returncode = 42 |
59 env = gclient_utils.os.environ.copy() | 56 gclient_utils.Popen( |
60 env['LANGUAGE'] = 'en' | |
61 gclient_utils.subprocess.Popen( | |
62 args, cwd=None, | 57 args, cwd=None, |
63 stderr=None, | 58 stderr=None, |
64 env=env, | 59 stdout=gclient_utils.subprocess.PIPE).AndReturn(process) |
65 stdout=gclient_utils.subprocess.PIPE, | |
66 shell=gclient_utils.sys.platform.startswith('win')).AndReturn(process) | |
67 process.communicate().AndReturn(['bleh', 'foo']) | 60 process.communicate().AndReturn(['bleh', 'foo']) |
68 self.mox.ReplayAll() | 61 self.mox.ReplayAll() |
69 try: | 62 try: |
70 gclient_utils.CheckCall(args) | 63 gclient_utils.CheckCall(args) |
71 self.fail() | 64 self.fail() |
72 except gclient_utils.CheckCallError, e: | 65 except gclient_utils.CheckCallError, e: |
73 self.assertEqual(e.command, args) | 66 self.assertEqual(e.command, args) |
74 self.assertEqual(e.cwd, None) | 67 self.assertEqual(e.cwd, None) |
75 self.assertEqual(e.retcode, 42) | 68 self.assertEqual(e.retcode, 42) |
76 self.assertEqual(e.stdout, 'bleh') | 69 self.assertEqual(e.stdout, 'bleh') |
77 self.assertEqual(e.stderr, 'foo') | 70 self.assertEqual(e.stderr, 'foo') |
78 | 71 |
79 | 72 |
80 class SubprocessCallAndFilterTestCase(GclientUtilBase): | 73 class CheckCallAndFilterTestCase(GclientUtilBase): |
81 class ProcessIdMock(object): | 74 class ProcessIdMock(object): |
82 def __init__(self, test_string): | 75 def __init__(self, test_string): |
83 self.stdout = StringIO.StringIO(test_string) | 76 self.stdout = StringIO.StringIO(test_string) |
84 def wait(self): | 77 def wait(self): |
85 pass | 78 pass |
86 | 79 |
87 def _inner(self, args, test_string): | 80 def _inner(self, args, test_string): |
88 cwd = 'bleh' | 81 cwd = 'bleh' |
89 env = gclient_utils.os.environ.copy() | |
90 env['LANGUAGE'] = 'en' | |
91 gclient_utils.sys.stdout.write( | 82 gclient_utils.sys.stdout.write( |
92 '\n________ running \'boo foo bar\' in \'bleh\'\n') | 83 '\n________ running \'boo foo bar\' in \'bleh\'\n') |
93 for i in test_string: | 84 for i in test_string: |
94 gclient_utils.sys.stdout.write(i) | 85 gclient_utils.sys.stdout.write(i) |
95 gclient_utils.subprocess.Popen( | 86 gclient_utils.Popen( |
96 args, | 87 args, |
97 cwd=cwd, | 88 cwd=cwd, |
98 shell=(gclient_utils.sys.platform == 'win32'), | |
99 env=env, | |
100 stdout=gclient_utils.subprocess.PIPE, | 89 stdout=gclient_utils.subprocess.PIPE, |
101 stderr=gclient_utils.subprocess.STDOUT, | 90 stderr=gclient_utils.subprocess.STDOUT, |
102 bufsize=0).AndReturn(self.ProcessIdMock(test_string)) | 91 bufsize=0).AndReturn(self.ProcessIdMock(test_string)) |
103 | 92 |
104 self.mox.ReplayAll() | 93 self.mox.ReplayAll() |
105 compiled_pattern = gclient_utils.re.compile(r'a(.*)b') | 94 compiled_pattern = gclient_utils.re.compile(r'a(.*)b') |
106 line_list = [] | 95 line_list = [] |
107 capture_list = [] | 96 capture_list = [] |
108 def FilterLines(line): | 97 def FilterLines(line): |
109 line_list.append(line) | 98 line_list.append(line) |
| 99 assert isinstance(line, str), type(line) |
110 match = compiled_pattern.search(line) | 100 match = compiled_pattern.search(line) |
111 if match: | 101 if match: |
112 capture_list.append(match.group(1)) | 102 capture_list.append(match.group(1)) |
113 gclient_utils.SubprocessCallAndFilter( | 103 gclient_utils.CheckCallAndFilterAndHeader( |
114 args, cwd=cwd, print_messages=True, print_stdout=True, | 104 args, cwd=cwd, always=True, filter_fn=FilterLines) |
115 filter_fn=FilterLines) | |
116 self.assertEquals(line_list, ['ahah', 'accb', 'allo', 'addb']) | 105 self.assertEquals(line_list, ['ahah', 'accb', 'allo', 'addb']) |
117 self.assertEquals(capture_list, ['cc', 'dd']) | 106 self.assertEquals(capture_list, ['cc', 'dd']) |
118 | 107 |
119 def testSubprocessCallAndFilter(self): | 108 def testCheckCallAndFilter(self): |
120 args = ['boo', 'foo', 'bar'] | 109 args = ['boo', 'foo', 'bar'] |
121 test_string = 'ahah\naccb\nallo\naddb\n' | 110 test_string = 'ahah\naccb\nallo\naddb\n' |
122 self._inner(args, test_string) | 111 self._inner(args, test_string) |
123 | 112 |
124 def testNoLF(self): | 113 def testNoLF(self): |
125 # Exactly as testSubprocessCallAndFilter without trailing \n | 114 # Exactly as testCheckCallAndFilterAndHeader without trailing \n |
126 args = ['boo', 'foo', 'bar'] | 115 args = ['boo', 'foo', 'bar'] |
127 test_string = 'ahah\naccb\nallo\naddb' | 116 test_string = 'ahah\naccb\nallo\naddb' |
128 self._inner(args, test_string) | 117 self._inner(args, test_string) |
129 | 118 |
130 | 119 |
131 class SplitUrlRevisionTestCase(GclientUtilBase): | 120 class SplitUrlRevisionTestCase(GclientUtilBase): |
132 def testSSHUrl(self): | 121 def testSSHUrl(self): |
133 url = "ssh://test@example.com/test.git" | 122 url = "ssh://test@example.com/test.git" |
134 rev = "ac345e52dc" | 123 rev = "ac345e52dc" |
135 out_url, out_rev = gclient_utils.SplitUrlRevision(url) | 124 out_url, out_rev = gclient_utils.SplitUrlRevision(url) |
(...skipping 30 matching lines...) Expand all Loading... |
166 out_url, out_rev = gclient_utils.SplitUrlRevision("%s@%s" % (url, rev)) | 155 out_url, out_rev = gclient_utils.SplitUrlRevision("%s@%s" % (url, rev)) |
167 self.assertEquals(out_rev, rev) | 156 self.assertEquals(out_rev, rev) |
168 self.assertEquals(out_url, url) | 157 self.assertEquals(out_url, url) |
169 | 158 |
170 | 159 |
171 if __name__ == '__main__': | 160 if __name__ == '__main__': |
172 import unittest | 161 import unittest |
173 unittest.main() | 162 unittest.main() |
174 | 163 |
175 # vim: ts=2:sw=2:tw=80:et: | 164 # vim: ts=2:sw=2:tw=80:et: |
OLD | NEW |