Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: tests/gclient_utils_test.py

Issue 2300433003: gclient_utils: provide subproces42 based CheckCallAndFilter. (Closed)
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « gclient_utils.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 # Copyright (c) 2012 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 os 6 import os
7 import StringIO 7 import StringIO
8 import sys 8 import sys
9 import threading 9 import threading
10 import time
10 11
11 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) 12 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
12 13
13 from testing_support.super_mox import SuperMoxTestBase 14 from testing_support.super_mox import SuperMoxTestBase
14 from testing_support import trial_dir 15 from testing_support import trial_dir
15 16
16 import gclient_utils 17 import gclient_utils
17 import subprocess2 18 import subprocess2
19 import subprocess42
18 20
19 21
20 class GclientUtilBase(SuperMoxTestBase): 22 class GclientUtilBase(SuperMoxTestBase):
21 def setUp(self): 23 def setUp(self):
22 super(GclientUtilBase, self).setUp() 24 super(GclientUtilBase, self).setUp()
23 gclient_utils.sys.stdout.flush = lambda: None 25 gclient_utils.sys.stdout.flush = lambda: None
24 self.mox.StubOutWithMock(subprocess2, 'Popen') 26 self.mox.StubOutWithMock(subprocess2, 'Popen')
25 self.mox.StubOutWithMock(subprocess2, 'communicate') 27 self.mox.StubOutWithMock(subprocess2, 'communicate')
28 self.mox.StubOutWithMock(subprocess42, 'Popen')
26 29
27 30
28 class CheckCallAndFilterTestCase(GclientUtilBase): 31 class CheckCallAndFilterTestCase(GclientUtilBase):
29 class ProcessIdMock(object): 32 class ProcessIdMock(object):
30 def __init__(self, test_string): 33 def __init__(self, test_string):
31 self.stdout = StringIO.StringIO(test_string) 34 self.stdout = StringIO.StringIO(test_string)
32 self.pid = 9284 35 self.pid = 9284
33 # pylint: disable=R0201 36 # pylint: disable=R0201
34 def wait(self): 37 def wait(self):
35 return 0 38 return 0
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 135
133 def testKillRaise(self): 136 def testKillRaise(self):
134 order = self._checkKillTimeout(output_block_for=1.0, kill_raises=True) 137 order = self._checkKillTimeout(output_block_for=1.0, kill_raises=True)
135 self.assertEquals(order, ['killed', 'unblock']) 138 self.assertEquals(order, ['killed', 'unblock'])
136 139
137 def testNoKill(self): 140 def testNoKill(self):
138 order = self._checkKillTimeout(output_block_for=0.0) 141 order = self._checkKillTimeout(output_block_for=0.0)
139 self.assertEquals(order, ['unblock']) 142 self.assertEquals(order, ['unblock'])
140 143
141 144
145 class CheckCallAndFilter42TestCase(GclientUtilBase):
146 class ProcessIdMock(object):
147 def __init__(self, test_string, block_for=None):
148 self._test_string = test_string
149 self._block_for = block_for
150 self._killed = threading.Event()
151 self._started = time.time()
152 self.pid = 9284
153 # pylint: disable=R0201
154 def wait(self):
155 return 0
156 def kill(self):
157 self._killed.set()
158 def duration(self):
159 return time.time() - self._started
160 def yield_any(self, maxsize, timeout):
161 for c in self._test_string:
162 yield 'stdout', c
163 if self._block_for:
164 while not self._killed.is_set():
165 left = self._started + self._block_for - time.time()
166 if left < 0:
167 break
168 # ignore timeout argument for faster test.
169 self._killed.wait(min(left, 0.01))
170 yield None, None
171
172 def _inner(self, args, test_string, block_for=None, kill_timeout=None):
173 cwd = 'bleh'
174 gclient_utils.sys.stdout.write(
175 '\n________ running \'boo foo bar\' in \'bleh\'\n')
176 for i in test_string:
177 gclient_utils.sys.stdout.write(i)
178 # pylint: disable=E1101
179 subprocess42.Popen(
180 args,
181 cwd=cwd,
182 stdout=subprocess42.PIPE,
183 stderr=subprocess42.STDOUT,
184 bufsize=0).AndReturn(self.ProcessIdMock(test_string, block_for))
185
186 os.getcwd()
187 self.mox.ReplayAll()
188 compiled_pattern = gclient_utils.re.compile(r'a(.*)b')
189 line_list = []
190 capture_list = []
191 def FilterLines(line):
192 line_list.append(line)
193 assert isinstance(line, str), type(line)
194 match = compiled_pattern.search(line)
195 if match:
196 capture_list.append(match.group(1))
197 gclient_utils.CheckCallAndFilterAndHeader(
198 args, cwd=cwd, always=True, filter_fn=FilterLines, use_v42=True,
199 kill_timeout=kill_timeout)
200 self.assertEquals(line_list, ['ahah', 'accb', 'allo', 'addb'])
201 self.assertEquals(capture_list, ['cc', 'dd'])
202
203 def testCheckCallAndFilter(self):
204 args = ['boo', 'foo', 'bar']
205 test_string = 'ahah\naccb\nallo\naddb\n'
206 self._inner(args, test_string)
207 self.checkstdout('\n________ running \'boo foo bar\' in \'bleh\'\n'
208 'ahah\naccb\nallo\naddb\n\n'
209 '________ running \'boo foo bar\' in \'bleh\'\nahah\naccb\nallo\naddb'
210 '\n')
211
212 def testNoLF(self):
213 # Exactly as testCheckCallAndFilterAndHeader without trailing \n
214 args = ['boo', 'foo', 'bar']
215 test_string = 'ahah\naccb\nallo\naddb'
216 self._inner(args, test_string)
217 self.checkstdout('\n________ running \'boo foo bar\' in \'bleh\'\n'
218 'ahah\naccb\nallo\naddb\n'
219 '________ running \'boo foo bar\' in \'bleh\'\nahah\naccb\nallo\naddb')
220
221 def testKilled(self):
222 args = ['boo', 'foo', 'bar']
223 test_string = 'ahah\naccb\nallo\naddb'
224 self._inner(args, test_string, block_for=3, kill_timeout=1)
225 self.checkstdout('\n________ running \'boo foo bar\' in \'bleh\'\n'
226 'ahah\naccb\nallo\naddb\n'
227 '________ running \'boo foo bar\' in \'bleh\'\nahah\naccb\nallo\naddb'
228 'ERROR: killing process \'"boo" "foo" "bar"\' in bleh running for 1s '
229 '(timeout: 1s)\n')
230
231
142 class SplitUrlRevisionTestCase(GclientUtilBase): 232 class SplitUrlRevisionTestCase(GclientUtilBase):
143 def testSSHUrl(self): 233 def testSSHUrl(self):
144 url = "ssh://test@example.com/test.git" 234 url = "ssh://test@example.com/test.git"
145 rev = "ac345e52dc" 235 rev = "ac345e52dc"
146 out_url, out_rev = gclient_utils.SplitUrlRevision(url) 236 out_url, out_rev = gclient_utils.SplitUrlRevision(url)
147 self.assertEquals(out_rev, None) 237 self.assertEquals(out_rev, None)
148 self.assertEquals(out_url, url) 238 self.assertEquals(out_url, url)
149 out_url, out_rev = gclient_utils.SplitUrlRevision("%s@%s" % (url, rev)) 239 out_url, out_rev = gclient_utils.SplitUrlRevision("%s@%s" % (url, rev))
150 self.assertEquals(out_rev, rev) 240 self.assertEquals(out_rev, rev)
151 self.assertEquals(out_url, url) 241 self.assertEquals(out_url, url)
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
262 import unittest 352 import unittest
263 import logging 353 import logging
264 level = logging.DEBUG if '-v' in sys.argv else logging.FATAL 354 level = logging.DEBUG if '-v' in sys.argv else logging.FATAL
265 logging.basicConfig( 355 logging.basicConfig(
266 level=level, 356 level=level,
267 format='%(asctime).19s %(levelname)s %(filename)s:' 357 format='%(asctime).19s %(levelname)s %(filename)s:'
268 '%(lineno)s %(message)s') 358 '%(lineno)s %(message)s')
269 unittest.main() 359 unittest.main()
270 360
271 # vim: ts=2:sw=2:tw=80:et: 361 # vim: ts=2:sw=2:tw=80:et:
OLDNEW
« no previous file with comments | « gclient_utils.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698