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 |
(...skipping 19 matching lines...) Expand all Loading... |
30 'WorkItem', | 30 'WorkItem', |
31 'errno', 'logging', 'os', 're', 'stat', 'subprocess', 'sys', | 31 'errno', 'logging', 'os', 're', 'stat', 'subprocess', 'sys', |
32 'threading', 'time', 'xml', | 32 'threading', 'time', 'xml', |
33 ] | 33 ] |
34 # If this test fails, you should add the relevant test. | 34 # If this test fails, you should add the relevant test. |
35 self.compareMembers(gclient_utils, members) | 35 self.compareMembers(gclient_utils, members) |
36 | 36 |
37 | 37 |
38 class CheckCallTestCase(GclientUtilBase): | 38 class CheckCallTestCase(GclientUtilBase): |
39 def testCheckCallSuccess(self): | 39 def testCheckCallSuccess(self): |
40 command = ['boo', 'foo', 'bar'] | 40 args = ['boo', 'foo', 'bar'] |
41 process = self.mox.CreateMockAnything() | 41 process = self.mox.CreateMockAnything() |
42 process.returncode = 0 | 42 process.returncode = 0 |
43 env = gclient_utils.os.environ.copy() | 43 env = gclient_utils.os.environ.copy() |
44 env['LANGUAGE'] = 'en' | 44 env['LANGUAGE'] = 'en' |
45 gclient_utils.subprocess.Popen( | 45 gclient_utils.subprocess.Popen( |
46 command, cwd=None, | 46 args, cwd=None, |
47 stderr=None, | 47 stderr=None, |
48 env=env, | 48 env=env, |
49 stdout=gclient_utils.subprocess.PIPE, | 49 stdout=gclient_utils.subprocess.PIPE, |
50 shell=gclient_utils.sys.platform.startswith('win')).AndReturn(process) | 50 shell=gclient_utils.sys.platform.startswith('win')).AndReturn(process) |
51 process.communicate().AndReturn(['bleh', 'foo']) | 51 process.communicate().AndReturn(['bleh', 'foo']) |
52 self.mox.ReplayAll() | 52 self.mox.ReplayAll() |
53 gclient_utils.CheckCall(command) | 53 gclient_utils.CheckCall(args) |
54 | 54 |
55 def testCheckCallFailure(self): | 55 def testCheckCallFailure(self): |
56 command = ['boo', 'foo', 'bar'] | 56 args = ['boo', 'foo', 'bar'] |
57 process = self.mox.CreateMockAnything() | 57 process = self.mox.CreateMockAnything() |
58 process.returncode = 42 | 58 process.returncode = 42 |
59 env = gclient_utils.os.environ.copy() | 59 env = gclient_utils.os.environ.copy() |
60 env['LANGUAGE'] = 'en' | 60 env['LANGUAGE'] = 'en' |
61 gclient_utils.subprocess.Popen( | 61 gclient_utils.subprocess.Popen( |
62 command, cwd=None, | 62 args, cwd=None, |
63 stderr=None, | 63 stderr=None, |
64 env=env, | 64 env=env, |
65 stdout=gclient_utils.subprocess.PIPE, | 65 stdout=gclient_utils.subprocess.PIPE, |
66 shell=gclient_utils.sys.platform.startswith('win')).AndReturn(process) | 66 shell=gclient_utils.sys.platform.startswith('win')).AndReturn(process) |
67 process.communicate().AndReturn(['bleh', 'foo']) | 67 process.communicate().AndReturn(['bleh', 'foo']) |
68 self.mox.ReplayAll() | 68 self.mox.ReplayAll() |
69 try: | 69 try: |
70 gclient_utils.CheckCall(command) | 70 gclient_utils.CheckCall(args) |
71 self.fail() | 71 self.fail() |
72 except gclient_utils.CheckCallError, e: | 72 except gclient_utils.CheckCallError, e: |
73 self.assertEqual(e.command, command) | 73 self.assertEqual(e.command, args) |
74 self.assertEqual(e.cwd, None) | 74 self.assertEqual(e.cwd, None) |
75 self.assertEqual(e.retcode, 42) | 75 self.assertEqual(e.retcode, 42) |
76 self.assertEqual(e.stdout, 'bleh') | 76 self.assertEqual(e.stdout, 'bleh') |
77 self.assertEqual(e.stderr, 'foo') | 77 self.assertEqual(e.stderr, 'foo') |
78 | 78 |
79 | 79 |
80 class SubprocessCallAndFilterTestCase(GclientUtilBase): | 80 class SubprocessCallAndFilterTestCase(GclientUtilBase): |
81 class ProcessIdMock(object): | 81 class ProcessIdMock(object): |
82 def __init__(self, test_string): | 82 def __init__(self, test_string): |
83 self.stdout = StringIO.StringIO(test_string) | 83 self.stdout = StringIO.StringIO(test_string) |
84 def wait(self): | 84 def wait(self): |
85 pass | 85 pass |
86 | 86 |
87 def _inner(self, command, test_string): | 87 def _inner(self, args, test_string): |
88 in_directory = 'bleh' | 88 cwd = 'bleh' |
89 env = gclient_utils.os.environ.copy() | 89 env = gclient_utils.os.environ.copy() |
90 env['LANGUAGE'] = 'en' | 90 env['LANGUAGE'] = 'en' |
91 gclient_utils.sys.stdout.write( | 91 gclient_utils.sys.stdout.write( |
92 '\n________ running \'boo foo bar\' in \'bleh\'\n') | 92 '\n________ running \'boo foo bar\' in \'bleh\'\n') |
93 for i in test_string: | 93 for i in test_string: |
94 gclient_utils.sys.stdout.write(i) | 94 gclient_utils.sys.stdout.write(i) |
95 gclient_utils.subprocess.Popen( | 95 gclient_utils.subprocess.Popen( |
96 command, | 96 args, |
97 cwd=in_directory, | 97 cwd=cwd, |
98 shell=(gclient_utils.sys.platform == 'win32'), | 98 shell=(gclient_utils.sys.platform == 'win32'), |
99 env=env, | 99 env=env, |
100 stdout=gclient_utils.subprocess.PIPE, | 100 stdout=gclient_utils.subprocess.PIPE, |
101 stderr=gclient_utils.subprocess.STDOUT, | 101 stderr=gclient_utils.subprocess.STDOUT, |
102 bufsize=0).AndReturn(self.ProcessIdMock(test_string)) | 102 bufsize=0).AndReturn(self.ProcessIdMock(test_string)) |
103 | 103 |
104 self.mox.ReplayAll() | 104 self.mox.ReplayAll() |
105 compiled_pattern = gclient_utils.re.compile(r'a(.*)b') | 105 compiled_pattern = gclient_utils.re.compile(r'a(.*)b') |
106 line_list = [] | 106 line_list = [] |
107 capture_list = [] | 107 capture_list = [] |
108 def FilterLines(line): | 108 def FilterLines(line): |
109 line_list.append(line) | 109 line_list.append(line) |
110 match = compiled_pattern.search(line) | 110 match = compiled_pattern.search(line) |
111 if match: | 111 if match: |
112 capture_list.append(match.group(1)) | 112 capture_list.append(match.group(1)) |
113 gclient_utils.SubprocessCallAndFilter( | 113 gclient_utils.SubprocessCallAndFilter( |
114 command, in_directory, True, True, None, FilterLines) | 114 args, cwd=cwd, print_messages=True, print_stdout=True, |
| 115 filter_fn=FilterLines) |
115 self.assertEquals(line_list, ['ahah', 'accb', 'allo', 'addb']) | 116 self.assertEquals(line_list, ['ahah', 'accb', 'allo', 'addb']) |
116 self.assertEquals(capture_list, ['cc', 'dd']) | 117 self.assertEquals(capture_list, ['cc', 'dd']) |
117 | 118 |
118 def testSubprocessCallAndFilter(self): | 119 def testSubprocessCallAndFilter(self): |
119 command = ['boo', 'foo', 'bar'] | 120 args = ['boo', 'foo', 'bar'] |
120 test_string = 'ahah\naccb\nallo\naddb\n' | 121 test_string = 'ahah\naccb\nallo\naddb\n' |
121 self._inner(command, test_string) | 122 self._inner(args, test_string) |
122 | 123 |
123 def testNoLF(self): | 124 def testNoLF(self): |
124 # Exactly as testSubprocessCallAndFilter without trailing \n | 125 # Exactly as testSubprocessCallAndFilter without trailing \n |
125 command = ['boo', 'foo', 'bar'] | 126 args = ['boo', 'foo', 'bar'] |
126 test_string = 'ahah\naccb\nallo\naddb' | 127 test_string = 'ahah\naccb\nallo\naddb' |
127 self._inner(command, test_string) | 128 self._inner(args, test_string) |
128 | 129 |
129 | 130 |
130 class SplitUrlRevisionTestCase(GclientUtilBase): | 131 class SplitUrlRevisionTestCase(GclientUtilBase): |
131 def testSSHUrl(self): | 132 def testSSHUrl(self): |
132 url = "ssh://test@example.com/test.git" | 133 url = "ssh://test@example.com/test.git" |
133 rev = "ac345e52dc" | 134 rev = "ac345e52dc" |
134 out_url, out_rev = gclient_utils.SplitUrlRevision(url) | 135 out_url, out_rev = gclient_utils.SplitUrlRevision(url) |
135 self.assertEquals(out_rev, None) | 136 self.assertEquals(out_rev, None) |
136 self.assertEquals(out_url, url) | 137 self.assertEquals(out_url, url) |
137 out_url, out_rev = gclient_utils.SplitUrlRevision("%s@%s" % (url, rev)) | 138 out_url, out_rev = gclient_utils.SplitUrlRevision("%s@%s" % (url, rev)) |
(...skipping 27 matching lines...) Expand all Loading... |
165 out_url, out_rev = gclient_utils.SplitUrlRevision("%s@%s" % (url, rev)) | 166 out_url, out_rev = gclient_utils.SplitUrlRevision("%s@%s" % (url, rev)) |
166 self.assertEquals(out_rev, rev) | 167 self.assertEquals(out_rev, rev) |
167 self.assertEquals(out_url, url) | 168 self.assertEquals(out_url, url) |
168 | 169 |
169 | 170 |
170 if __name__ == '__main__': | 171 if __name__ == '__main__': |
171 import unittest | 172 import unittest |
172 unittest.main() | 173 unittest.main() |
173 | 174 |
174 # vim: ts=2:sw=2:tw=80:et: | 175 # vim: ts=2:sw=2:tw=80:et: |
OLD | NEW |