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

Side by Side Diff: scripts/common/unittests/annotator_test.py

Issue 12386016: Initial implementation of annotator library. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/tools/build
Patch Set: More python 2.6 changes. Created 7 years, 9 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 | Annotate | Revision Log
« no previous file with comments | « scripts/common/annotator.py ('k') | scripts/master/chromium_step.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #!/usr/bin/env python
2 # Copyright (c) 2013 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5
6 """Unit tests for classes in annotator.py."""
7
8 import cStringIO
9 import json
10 import os
11 import sys
12 import tempfile
13 import unittest
14
15 import test_env # pylint: disable=W0611
16
17 from common import annotator
18 from common import chromium_utils
19
20
21 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
22
23
24 class FilterCapture(chromium_utils.RunCommandFilter):
25 """Captures the text and places it into an array."""
26 def __init__(self):
27 chromium_utils.RunCommandFilter.__init__(self)
28 self.text = []
29
30 def FilterLine(self, line):
31 self.text.append(line.rstrip())
32
33 def FilterDone(self, text):
34 self.text.append(text)
35
36
37 class TestAnnotationStreams(unittest.TestCase):
38 def setUp(self):
39 self.buf = cStringIO.StringIO()
40
41 def _getLines(self):
42 return self.buf.getvalue().rstrip().split('\n')
43
44 def testBasicUsage(self):
45 stream = annotator.StructuredAnnotationStream(stream=self.buf)
46 with stream.step('one') as _:
47 pass
48 with stream.step('two') as _:
49 pass
50
51 result = [
52 '@@@SEED_STEP one@@@',
53 '@@@STEP_CURSOR one@@@',
54 '@@@STEP_STARTED@@@',
55 '@@@STEP_CLOSED@@@',
56 '@@@SEED_STEP two@@@',
57 '@@@STEP_CURSOR two@@@',
58 '@@@STEP_STARTED@@@',
59 '@@@STEP_CLOSED@@@',
60 ]
61
62 self.assertEquals(result, self._getLines())
63
64 def testStepAnnotations(self):
65 stream = annotator.StructuredAnnotationStream(stream=self.buf)
66 with stream.step('one') as s:
67 s.step_warnings()
68 s.step_failure()
69 s.step_exception()
70 s.step_clear()
71 s.step_summary_clear()
72 s.step_text('hello')
73 s.step_summary_text('hello!')
74 s.step_log_line('mylog', 'test')
75 s.step_log_end('mylog')
76 s.step_log_line('myperflog', 'perf data')
77 s.step_log_end_perf('myperflog', 'dashboardname')
78 s.write_log_lines('full_log', ['line one', 'line two'])
79 s.write_log_lines('full_perf_log', ['perf line one', 'perf line two'],
80 perf='full_perf')
81
82 result = [
83 '@@@SEED_STEP one@@@',
84 '@@@STEP_CURSOR one@@@',
85 '@@@STEP_STARTED@@@',
86 '@@@STEP_WARNINGS@@@',
87 '@@@STEP_FAILURE@@@',
88 '@@@STEP_EXCEPTION@@@',
89 '@@@STEP_CLEAR@@@',
90 '@@@STEP_SUMMARY_CLEAR@@@',
91 '@@@STEP_TEXT@hello@@@',
92 '@@@STEP_SUMMARY_TEXT@hello!@@@',
93 '@@@STEP_LOG_LINE@mylog@test@@@',
94 '@@@STEP_LOG_END@mylog@@@',
95 '@@@STEP_LOG_LINE@myperflog@perf data@@@',
96 '@@@STEP_LOG_END_PERF@myperflog@dashboardname@@@',
97 '@@@STEP_LOG_LINE@full_log@line one@@@',
98 '@@@STEP_LOG_LINE@full_log@line two@@@',
99 '@@@STEP_LOG_END@full_log@@@',
100 '@@@STEP_LOG_LINE@full_perf_log@perf line one@@@',
101 '@@@STEP_LOG_LINE@full_perf_log@perf line two@@@',
102 '@@@STEP_LOG_END_PERF@full_perf_log@full_perf@@@',
103 '@@@STEP_CLOSED@@@',
104 ]
105
106 self.assertEquals(result, self._getLines())
107
108 def testSeedStep(self):
109 steps = ['one', 'two']
110 stream = annotator.StructuredAnnotationStream(seed_steps=steps,
111 stream=self.buf)
112 with stream.step('one'):
113 pass
114 with stream.step('two'):
115 pass
116
117 result = [
118 '@@@SEED_STEP one@@@',
119 '@@@SEED_STEP two@@@',
120 '@@@STEP_CURSOR one@@@',
121 '@@@STEP_STARTED@@@',
122 '@@@STEP_CLOSED@@@',
123 '@@@STEP_CURSOR two@@@',
124 '@@@STEP_STARTED@@@',
125 '@@@STEP_CLOSED@@@'
126 ]
127
128 self.assertEquals(result, self._getLines())
129
130 def testSeedStepSkip(self):
131 steps = ['one', 'two', 'three']
132 stream = annotator.StructuredAnnotationStream(seed_steps=steps,
133 stream=self.buf)
134 with stream.step('one'):
135 pass
136 with stream.step('three'):
137 pass
138 with stream.step('two'):
139 pass
140
141 result = [
142 '@@@SEED_STEP one@@@',
143 '@@@SEED_STEP two@@@',
144 '@@@SEED_STEP three@@@',
145 '@@@STEP_CURSOR one@@@',
146 '@@@STEP_STARTED@@@',
147 '@@@STEP_CLOSED@@@',
148 '@@@STEP_CURSOR three@@@',
149 '@@@STEP_STARTED@@@',
150 '@@@STEP_CLOSED@@@',
151 '@@@SEED_STEP two@@@',
152 '@@@STEP_CURSOR two@@@',
153 '@@@STEP_STARTED@@@',
154 '@@@STEP_CLOSED@@@',
155 ]
156
157 self.assertEquals(result, self._getLines())
158
159 def testException(self):
160 stream = annotator.StructuredAnnotationStream(stream=self.buf)
161
162 def dummy_func():
163 with stream.step('one'):
164 raise Exception('oh no!')
165 self.assertRaises(Exception, dummy_func)
166
167 log_string = '@@@STEP_LOG_LINE@exception'
168 exception = any(line.startswith(log_string) for line in self._getLines())
169 self.assertTrue(exception)
170
171 def testNoNesting(self):
172 stream = annotator.StructuredAnnotationStream(stream=self.buf)
173
174 def dummy_func():
175 with stream.step('one'):
176 with stream.step('two'):
177 pass
178 self.assertRaises(Exception, dummy_func)
179
180 def testProtectedStartStop(self):
181 stream = annotator.StructuredAnnotationStream(stream=self.buf)
182
183 def dummy_func():
184 with stream.step('one') as s:
185 s.step_started()
186 self.assertRaises(AttributeError, dummy_func)
187
188 def dummy_func2():
189 with stream.step('two') as s:
190 s.step_closed()
191 self.assertRaises(AttributeError, dummy_func2)
192
193 def testDupLogs(self):
194 stream = annotator.StructuredAnnotationStream(stream=self.buf)
195
196 with stream.step('one') as s:
197 lines = ['one', 'two']
198 s.write_log_lines('mylog', lines)
199 self.assertRaises(ValueError, s.write_log_lines, 'mylog', lines)
200
201 def testAdvanced(self):
202 step = annotator.AdvancedAnnotationStep(stream=self.buf)
203 stream = annotator.AdvancedAnnotationStream(stream=self.buf)
204 stream.seed_step('one')
205 stream.seed_step('two')
206 stream.step_cursor('one')
207 step.step_started()
208 stream.step_cursor('two')
209 step.step_started()
210 stream.step_cursor('one')
211 step.step_closed()
212 stream.step_cursor('two')
213 step.step_closed()
214
215 result = [
216 '@@@SEED_STEP one@@@',
217 '@@@SEED_STEP two@@@',
218 '@@@STEP_CURSOR one@@@',
219 '@@@STEP_STARTED@@@',
220 '@@@STEP_CURSOR two@@@',
221 '@@@STEP_STARTED@@@',
222 '@@@STEP_CURSOR one@@@',
223 '@@@STEP_CLOSED@@@',
224 '@@@STEP_CURSOR two@@@',
225 '@@@STEP_CLOSED@@@',
226 ]
227
228 self.assertEquals(result, self._getLines())
229
230
231 def _synthesizeCmd(args):
232 basecmd = [sys.executable, '-c']
233 basecmd.extend(args)
234 return basecmd
235
236
237 class TestExecution(unittest.TestCase):
238 def setUp(self):
239 self.capture = FilterCapture()
240 self.tempfd, self.tempfn = tempfile.mkstemp()
241 self.temp = os.fdopen(self.tempfd, 'wb')
242 self.script = os.path.join(SCRIPT_DIR, os.pardir, 'annotator.py')
243
244 def tearDown(self):
245 self.temp.close()
246 if os.path.exists(self.tempfn):
247 os.remove(self.tempfn)
248
249 def _runAnnotator(self, cmdlist):
250 json.dump(cmdlist, self.temp)
251 self.temp.close()
252 cmd = [sys.executable, self.script, self.tempfn]
253 env = os.environ.copy()
254 env['PYTHONPATH'] = os.pathsep.join(sys.path)
255 return chromium_utils.RunCommand(cmd, filter_obj=self.capture, env=env,
256 print_cmd=False)
257
258 def testSimpleExecution(self):
259 cmdlist = [{'name': 'one', 'cmd': _synthesizeCmd(['print \'hello!\''])},
260 {'name': 'two', 'cmd': _synthesizeCmd(['print \'yo!\''])}]
261
262 ret = self._runAnnotator(cmdlist)
263
264 self.assertEquals(ret, 0)
265
266 result = [
267 '@@@SEED_STEP one@@@',
268 '@@@SEED_STEP two@@@',
269 '@@@STEP_CURSOR one@@@',
270 '@@@STEP_STARTED@@@',
271 '',
272 '/usr/bin/python -c "print \'hello!\'"',
273 'hello!',
274 '@@@STEP_CLOSED@@@',
275 '@@@STEP_CURSOR two@@@',
276 '@@@STEP_STARTED@@@',
277 '',
278 '/usr/bin/python -c "print \'yo!\'"',
279 'yo!',
280 '@@@STEP_CLOSED@@@',
281 ]
282 self.assertEquals(result, self.capture.text)
283
284 def testFailBuild(self):
285 cmdlist = [{'name': 'one', 'cmd': _synthesizeCmd(['print \'hello!\''])},
286 {'name': 'two', 'cmd': _synthesizeCmd(['error'])}]
287
288 ret = self._runAnnotator(cmdlist)
289
290 self.assertTrue('@@@STEP_FAILURE@@@' in self.capture.text)
291 self.assertEquals(ret, 1)
292
293 def testStopBuild(self):
294 cmdlist = [{'name': 'one', 'cmd': _synthesizeCmd(['error'])},
295 {'name': 'two', 'cmd': _synthesizeCmd(['print \'yo!\''])}]
296
297 ret = self._runAnnotator(cmdlist)
298
299 self.assertTrue('@@@STEP_CURSOR two@@@' not in self.capture.text)
300 self.assertEquals(ret, 1)
301
302 def testException(self):
303 cmdlist = [{'name': 'one', 'cmd': ['doesn\'t exist']}]
304
305 ret = self._runAnnotator(cmdlist)
306
307 self.assertTrue('@@@STEP_EXCEPTION@@@' in self.capture.text)
308 self.assertEquals(ret, 1)
309
310 def testAlwaysRun(self):
311 cmdlist = [{'name': 'one', 'cmd': _synthesizeCmd(['error'])},
312 {'name': 'two', 'cmd': _synthesizeCmd(['print \'yo!\'']),
313 'always_run': True}]
314
315 ret = self._runAnnotator(cmdlist)
316 self.assertTrue('@@@STEP_CURSOR two@@@' in self.capture.text)
317 self.assertTrue('yo!' in self.capture.text)
318 self.assertEquals(ret, 1)
319
320 def testAlwaysRunNoDupes(self):
321 cmdlist = [{'name': 'one', 'cmd': _synthesizeCmd(['print \'yo!\'']),
322 'always_run': True},
323 {'name': 'two', 'cmd': _synthesizeCmd(['error'])},
324 {'name': 'three', 'cmd': _synthesizeCmd(['print \'hello!\'']),
325 'always_run': True}]
326
327 ret = self._runAnnotator(cmdlist)
328 self.assertEquals(self.capture.text.count('yo!'), 1)
329 self.assertTrue('hello!' in self.capture.text)
330 self.assertEquals(ret, 1)
331
332
333 if __name__ == '__main__':
334 unittest.main()
OLDNEW
« no previous file with comments | « scripts/common/annotator.py ('k') | scripts/master/chromium_step.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698