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

Side by Side Diff: chromite/chromite_unittest.py

Issue 6005004: WIP Chromite supporting "LEGACY" mode, as requested by Anush. (Closed) Base URL: ssh://git@gitrw.chromium.org:9222/crosutils.git@master
Patch Set: Fixes for sosa's code review comments. Created 9 years, 11 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
OLDNEW
(Empty)
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
6
7 """Unit tests for chromite.py."""
8
9 import doctest
10 import os
11 import re
12 import unittest
13
14 import chromite
15 from lib import text_menu
16 import mox
17
18
19 class _DeathException(Exception):
20 """A bogus exception used by the mock out of Die."""
21 pass
22
23
24 class TestFindCommand(unittest.TestCase):
25 """Test chromite._FindCommand."""
26 # TODO(dianders): Add a test where I override chromite._COMMAND_HANDLERS and
27 # chromite._COMMAND_STRS so that I can test more of _FindCommand().
28
29 def setUp(self):
30 """Test initialization."""
31 # Create our mox and stub out function calls used by _FindCommand()...
32 self.mox = mox.Mox()
33 self.mox.StubOutWithMock(chromite, 'Die')
34 self.mox.StubOutWithMock(chromite, 'Info')
35 self.mox.StubOutWithMock(text_menu, 'TextMenu')
36
37 def tearDown(self):
38 """Test cleanup."""
39 # Unset stubs...
40 self.mox.UnsetStubs()
41
42 def testInvalidCommand(self):
43 """Test that _FindCommand('implode') causes Die()."""
44 # Should be a call to Die. We'll have it fake a _DeathException...
45 chromite.Die(mox.IsA(basestring)).AndRaise(_DeathException)
46
47 # Run the command and verify proper mocks were called...
48 self.mox.ReplayAll()
49 self.assertRaises(_DeathException, chromite._FindCommand, 'implode')
50 self.mox.VerifyAll()
51
52 def testBlankCommandWithQuit(self):
53 """Test that _FindCommand('') shows menu, mocking quit.
54
55 This tests the case that the user quit out of the menu without choosing
56 anything.
57 """
58 # Should be one call to TextMenu, which will return None for quit.
59 text_menu.TextMenu(mox.IsA(list),
60 mox.IsA(basestring),
61 menu_width=0).AndReturn(None)
62
63 # Run the command and verify proper mocks were called...
64 self.mox.ReplayAll()
65 cmd_str = chromite._FindCommand('')
66 self.mox.VerifyAll()
67
68 self.assertEquals(cmd_str, None,
69 '_FindCommand should have returned None from TextMenu')
70
71 def testBlankCommandWithChoice0(self):
72 """Test that _FindCommand('') shows menu, mocking choice 0.
73
74 This tests the case that the user chose choice 0 for the menu.
75 """
76 # Should be one call to TextMenu, which will return item 0.
77 text_menu.TextMenu(mox.IsA(list),
78 mox.IsA(basestring),
79 menu_width=0).AndReturn(0)
80
81 # Run the command and verify proper mocks were called...
82 self.mox.ReplayAll()
83 cmd_str = chromite._FindCommand('')
84 self.mox.VerifyAll()
85
86 self.assertTrue(isinstance(cmd_str, basestring),
87 '_FindCommand should return a string')
88 self.assertTrue(bool(cmd_str),
89 '_FindCommand should not return a blank string')
90
91 def _TestExactCommand(self, cmd_to_find):
92 """Helper for tests that try to find an exact match.
93
94 Args:
95 cmd_to_find: The name of the command to find, like 'Build', ...
96 """
97 # Expect no mocks to be called...
98
99 # Run the command and verify proper mocks were called...
100 self.mox.ReplayAll()
101 cmd_str = chromite._FindCommand(cmd_to_find)
102 self.mox.VerifyAll()
103
104 self.assertEqual(cmd_str, cmd_to_find.lower(),
105 '_FindCommand("%s") should return "%s" back.' %
106 (cmd_to_find, cmd_to_find.lower()))
107
108 def testCaseSensitiveBuild(self):
109 """Test that _FindCommand('build') returns 'build'.
110
111 The command matching should be case sensitive.
112 """
113 self._TestExactCommand('build')
114
115 def testCaseInsensitiveBuild(self):
116 """Test that _FindCommand('BUiLD') returns 'build'.
117
118 The command matching should be case insensitive.
119 """
120 self._TestExactCommand('BUiLD')
121
122 def testShCommand(self):
123 """Test that _FindCommand('sh') returns 'shell'.
124
125 This serves two purposes:
126 1. Test the 'prefix' feature of _FindCommand
127 2. Validate that nobody has introduced another command that starts with
128 'sh', since it's expected that many people will use this to invoke the
129 shell.
130 """
131 # _FindCommand should give us a message that it has interpreted sh as shell.
132 chromite.Info(mox.IsA(basestring))
133
134 # Run the command and verify proper mocks were called...
135 self.mox.ReplayAll()
136 cmd_str = chromite._FindCommand('sh')
137 self.mox.VerifyAll()
138
139 self.assertEqual(cmd_str, 'shell',
140 '_FindCommand("sh") should return "shell" back.')
141
142
143 class TestFindSpec(unittest.TestCase):
144 """Test chromite._FindSpec."""
145
146 def setUp(self):
147 """Test initialization."""
148 # Create our mox and stub out function calls used by _FindSpec()...
149 self.mox = mox.Mox()
150 self.mox.StubOutWithMock(os, 'listdir')
151 self.mox.StubOutWithMock(os.path, 'isfile')
152 self.mox.StubOutWithMock(chromite, 'Die')
153 self.mox.StubOutWithMock(chromite, 'Info')
154 self.mox.StubOutWithMock(text_menu, 'TextMenu')
155
156 def tearDown(self):
157 """Test cleanup."""
158 # Unset stubs...
159 self.mox.UnsetStubs()
160
161 def testInvalidSpec(self):
162 """Test that _FindSpec('bogusSpec') causes Die()."""
163 # Pass this spec name...
164 spec_name = 'bogusSpec'
165
166 # We'll tell mox to say that these specs exist...
167 dir_list = ['x87-toadstool.spec', 'x87-luigi.SPeC', 'x88-princess.spec',
168 '_default']
169
170 # This spec doesn't represent any full path.
171 os.path.isfile(spec_name).AndReturn(False)
172
173 # This spec isn't found in our search path.
174 os.path.isfile(mox.Regex('^/.*%s.spec$' % spec_name)).MultipleTimes(
175 ).AndReturn(False)
176
177 # Give the fake directory listing...
178 os.listdir(mox.IsA(basestring)).MultipleTimes().AndReturn(dir_list)
179
180 # Should be a call to Die. We'll have it fake a _DeathException...
181 chromite.Die(mox.IsA(basestring)).AndRaise(_DeathException)
182
183 # Run the command and verify proper mocks were called...
184 self.mox.ReplayAll()
185 self.assertRaises(_DeathException, chromite._FindSpec, 'bogusSpec')
186 self.mox.VerifyAll()
187
188 def testFullPath(self):
189 """Test that _FindSpec(full_path) returns full_path.
190
191 _FindSpec is defined so that if you pass a full file path to it, it
192 should just return that. It doesn't need to have any special suffix or
193 live in a spec folder.
194 """
195 # Pass this spec name...
196 spec_name = __file__
197
198 # Just say that this is a full path...
199 os.path.isfile(spec_name).AndReturn(True)
200
201 # Run the command and verify proper mocks were called...
202 self.mox.ReplayAll()
203 path = chromite._FindSpec(spec_name)
204 self.mox.VerifyAll()
205
206 self.assertEqual(path, spec_name,
207 '_FindSpec() should just return param if full path.')
208
209 def testExactSpecName(self):
210 """Test that _FindSpec(exact_spec_name) returns the path for the spec."""
211 # We'll search for this bogus spec; we'll use mox to pretend it exists in
212 # the search path.
213 spec_name = 'y87-luigi'
214
215 # This spec doesn't represent any full path
216 os.path.isfile(spec_name).AndReturn(False)
217
218 # When we look through the search path for this spec (with .spec at
219 # the end), we will consider the spec to be found.
220 os.path.isfile(mox.Regex('^/.*%s.spec$' % spec_name)).AndReturn(True)
221
222 # Run the command and verify proper mocks were called...
223 self.mox.ReplayAll()
224 spec_path = chromite._FindSpec(spec_name)
225 self.mox.VerifyAll()
226
227 self.assertTrue(re.search('^/.*%s.spec$' % spec_name, spec_path),
228 '_FindSpec() should have returned absolute path for spec.')
229
230 def testUniqueSpecName(self):
231 """Test that _FindSpec(unique_part_name) returns the path for the spec."""
232 # We'll search for this spec. Weird capitalization on purpose to test
233 # case sensitiveness.
234 spec_name = 'ToaDSTooL'
235
236 # We'll tell mox to say that these specs exist in the first directory...
237 dir_list = ['_default',
238 'x87-luigi.spec', 'x87-toadstool.SPeC', 'x88-princess.spec']
239
240 # We expect it to find this spec.
241 expected_result = 'x87-toadstool.SPeC'
242
243 # Self-checks for test code...
244 assert dir_list == sorted(dir_list)
245
246 # This spec doesn't represent any full path.
247 os.path.isfile(spec_name).AndReturn(False)
248
249 # This spec isn't found in our search path.
250 os.path.isfile(mox.Regex('^/.*%s.spec$' % spec_name)).MultipleTimes(
251 ).AndReturn(False)
252
253 # Return our directory listing.
254 # TODO(dianders): How to make first mocked call return dir_list and
255 # subsequent return []
256 os.listdir(mox.IsA(basestring)).AndReturn(dir_list)
257
258 # Run the command and verify proper mocks were called...
259 self.mox.ReplayAll()
260 spec_path = chromite._FindSpec(spec_name)
261 self.mox.VerifyAll()
262
263 self.assertTrue(re.search('^/.*%s$' % expected_result, spec_path),
264 '_FindSpec("%s") incorrectly returned "%s".' %
265 (spec_name, spec_path))
266
267 def _TestBlankSpecName(self, menu_return):
268 """Helper for tests passing a blank spec name.
269
270 Args:
271 menu_return: This value is returned by TextMenu. Could be none or an
272 integer index into the dir_list of this function.
273 """
274 # We'll search for this spec.
275 spec_name = ''
276
277 # We'll tell mox to say that these specs exist in the first directory...
278 # ...only valid specs to make it easier to compare things.
279 dir_list = ['x87-luigi.spec', 'x87-toadstool.SPeC', 'x88-princess.spec']
280 num_specs = len(dir_list)
281
282 # Self-checks for test code...
283 assert menu_return < len(dir_list)
284 assert dir_list == sorted(dir_list)
285
286 # This spec doesn't represent any full path.
287 os.path.isfile(spec_name).AndReturn(False)
288
289 # Return our directory listing.
290 # TODO(dianders): How to make first mocked call return dir_list and
291 # subsequent return []
292 os.listdir(mox.IsA(basestring)).AndReturn(dir_list)
293
294 # We expect there to be 1 more item than we passed in, since we account
295 # for 'HOST'.
296 check_num_items_fn = lambda items: len(items) == (num_specs + 1)
297
298 # Add 1 to menu_return, since 'HOST' is first...
299 if menu_return is None:
300 adjusted_menu_return = menu_return
301 else:
302 adjusted_menu_return = menu_return + 1
303
304 # Should be one call to TextMenu, which will return menu_return.
305 text_menu.TextMenu(mox.And(mox.IsA(list), mox.Func(check_num_items_fn)),
306 mox.IsA(basestring)).AndReturn(adjusted_menu_return)
307
308 # Run the command and verify proper mocks were called...
309 self.mox.ReplayAll()
310 spec_path = chromite._FindSpec(spec_name)
311 self.mox.VerifyAll()
312
313 if menu_return is None:
314 self.assertEquals(spec_path, None,
315 '_FindSpec should have returned None from TextMenu')
316 else:
317 expected_result = dir_list[menu_return]
318 self.assertTrue(re.search('^/.*%s$' % expected_result, spec_path),
319 '_FindSpec("%s") incorrectly returned "%s".' %
320 (spec_name, spec_path))
321
322 def testBlankSpecNameWithQuit(self):
323 """Test that _FindSpec('') shows menu, mocking quit."""
324 self._TestBlankSpecName(None)
325
326 def testBlankSpecNameWithChoice0(self):
327 """Test that _FindSpec('') shows menu, mocking choice 0."""
328 self._TestBlankSpecName(0)
329
330 def testPartialSpecNameWithChoice0(self):
331 """Test that _FindSpec(non_unique_str) shows menu, mocking choice 0."""
332 # We'll search for this spec.
333 spec_name = 'x87'
334
335 # We'll tell mox to say that these specs exist in the first directory...
336 dir_list = ['_default',
337 'x87-luigi.spec', 'x87-toadstool.SPeC', 'x88-princess.spec']
338
339 # We expect 2 matches and should get back luigi as choice 0.
340 num_match = 2
341 expected_result = 'x87-luigi.spec'
342
343 # Self-checks for test code...
344 assert dir_list == sorted(dir_list)
345
346 # This spec doesn't represent any full path.
347 os.path.isfile(spec_name).AndReturn(False)
348
349 # This spec isn't found in our search path.
350 os.path.isfile(mox.Regex('^/.*%s.spec$' % spec_name)).MultipleTimes(
351 ).AndReturn(False)
352
353 # Return our directory listing.
354 # TODO(dianders): How to make first mocked call return dir_list and
355 # subsequent return []
356 os.listdir(mox.IsA(basestring)).AndReturn(dir_list)
357
358 # Should be one call to TextMenu, which will return 0.
359 text_menu.TextMenu(mox.And(mox.IsA(list),
360 mox.Func(lambda items: len(items) == num_match)),
361 mox.IsA(basestring)).AndReturn(0)
362
363 # Run the command and verify proper mocks were called...
364 self.mox.ReplayAll()
365 spec_path = chromite._FindSpec(spec_name)
366 self.mox.VerifyAll()
367
368 self.assertTrue(re.search('^/.*%s$' % expected_result, spec_path),
369 '_FindSpec("%s") incorrectly returned "%s".' %
370 (spec_name, spec_path))
371
372
373 if __name__ == '__main__':
374 doctest.testmod(chromite)
375 unittest.main()
376
OLDNEW
« chromite/chromite.py ('K') | « chromite/chromite.sh ('k') | chromite/lib/cros_build_lib.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698