OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # | 2 # |
3 # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. | 3 # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 | 6 |
7 """Unit tests for main.py.""" | 7 """Unit tests for main.py.""" |
8 | 8 |
9 import doctest | 9 import doctest |
10 import os | 10 import os |
11 import re | 11 import re |
12 import unittest | 12 import unittest |
13 | 13 |
| 14 import chromite.lib.cros_build_lib as cros_lib |
14 from chromite.lib import text_menu | 15 from chromite.lib import text_menu |
15 from chromite.shell import main | 16 from chromite.shell import main |
| 17 from chromite.shell import utils |
16 import mox | 18 import mox |
17 | 19 |
18 | 20 |
19 class _DeathException(Exception): | 21 class _DeathException(Exception): |
20 """A bogus exception used by the mock out of Die.""" | 22 """A bogus exception used by the mock out of cros_lib.Die.""" |
21 pass | 23 pass |
22 | 24 |
23 | 25 |
24 class TestFindCommand(unittest.TestCase): | 26 class TestFindCommand(unittest.TestCase): |
25 """Test main._FindCommand.""" | 27 """Test main._FindCommand.""" |
26 # TODO(dianders): Add a test where I override main._COMMAND_HANDLERS | 28 # TODO(dianders): Add a test where I override main._COMMAND_HANDLERS |
27 # and main._COMMAND_STRS so that I can test more of _FindCommand(). | 29 # and main._COMMAND_STRS so that I can test more of _FindCommand(). |
28 | 30 |
29 def setUp(self): | 31 def setUp(self): |
30 """Test initialization.""" | 32 """Test initialization.""" |
31 # Create our mox and stub out function calls used by _FindCommand()... | 33 # Create our mox and stub out function calls used by _FindCommand()... |
32 self.mox = mox.Mox() | 34 self.mox = mox.Mox() |
33 self.mox.StubOutWithMock(main, 'Die') | 35 self.mox.StubOutWithMock(cros_lib, 'Die') |
34 self.mox.StubOutWithMock(main, 'Info') | 36 self.mox.StubOutWithMock(cros_lib, 'Info') |
35 self.mox.StubOutWithMock(text_menu, 'TextMenu') | 37 self.mox.StubOutWithMock(text_menu, 'TextMenu') |
36 | 38 |
37 def tearDown(self): | 39 def tearDown(self): |
38 """Test cleanup.""" | 40 """Test cleanup.""" |
39 # Unset stubs... | 41 # Unset stubs... |
40 self.mox.UnsetStubs() | 42 self.mox.UnsetStubs() |
41 | 43 |
42 def testInvalidCommand(self): | 44 def testInvalidCommand(self): |
43 """Test that _FindCommand('implode') causes Die().""" | 45 """Test that _FindCommand('implode') causes cros_lib.Die().""" |
44 # Should be a call to Die. We'll have it fake a _DeathException... | 46 # Should be a call to cros_lib.Die. We'll have it fake a _DeathException... |
45 main.Die(mox.IsA(basestring)).AndRaise(_DeathException) | 47 cros_lib.Die(mox.IsA(basestring)).AndRaise(_DeathException) |
46 | 48 |
47 # Run the command and verify proper mocks were called... | 49 # Run the command and verify proper mocks were called... |
48 self.mox.ReplayAll() | 50 self.mox.ReplayAll() |
49 self.assertRaises(_DeathException, main._FindCommand, 'implode') | 51 self.assertRaises(_DeathException, main._FindCommand, 'implode') |
50 self.mox.VerifyAll() | 52 self.mox.VerifyAll() |
51 | 53 |
52 def testBlankCommandWithQuit(self): | 54 def testBlankCommandWithQuit(self): |
53 """Test that _FindCommand('') shows menu, mocking quit. | 55 """Test that _FindCommand('') shows menu, mocking quit. |
54 | 56 |
55 This tests the case that the user quit out of the menu without choosing | 57 This tests the case that the user quit out of the menu without choosing |
56 anything. | 58 anything. |
57 """ | 59 """ |
58 # Should be one call to TextMenu, which will return None for quit. | 60 # Should be one call to TextMenu, which will return None for quit. |
59 text_menu.TextMenu(mox.IsA(list), | 61 text_menu.TextMenu(mox.IsA(list), |
60 mox.IsA(basestring), | 62 mox.IsA(basestring), |
61 menu_width=0).AndReturn(None) | 63 menu_width=0).AndReturn(None) |
62 | 64 |
63 # Should die in response to the quit. | 65 # Should die in response to the quit. |
64 main.Die(mox.IsA(basestring)).AndRaise(_DeathException) | 66 cros_lib.Die(mox.IsA(basestring)).AndRaise(_DeathException) |
65 | 67 |
66 # Run the command and verify proper mocks were called... | 68 # Run the command and verify proper mocks were called... |
67 self.mox.ReplayAll() | 69 self.mox.ReplayAll() |
68 self.assertRaises(_DeathException, main._FindCommand, '') | 70 self.assertRaises(_DeathException, main._FindCommand, '') |
69 self.mox.VerifyAll() | 71 self.mox.VerifyAll() |
70 | 72 |
71 def testBlankCommandWithChoice0(self): | 73 def testBlankCommandWithChoice0(self): |
72 """Test that _FindCommand('') shows menu, mocking choice 0. | 74 """Test that _FindCommand('') shows menu, mocking choice 0. |
73 | 75 |
74 This tests the case that the user chose choice 0 for the menu. | 76 This tests the case that the user chose choice 0 for the menu. |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 def testShCommand(self): | 124 def testShCommand(self): |
123 """Test that _FindCommand('sh') returns 'shell'. | 125 """Test that _FindCommand('sh') returns 'shell'. |
124 | 126 |
125 This serves two purposes: | 127 This serves two purposes: |
126 1. Test the 'prefix' feature of _FindCommand | 128 1. Test the 'prefix' feature of _FindCommand |
127 2. Validate that nobody has introduced another command that starts with | 129 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 | 130 'sh', since it's expected that many people will use this to invoke the |
129 shell. | 131 shell. |
130 """ | 132 """ |
131 # _FindCommand should give us a message that it has interpreted sh as shell. | 133 # _FindCommand should give us a message that it has interpreted sh as shell. |
132 main.Info(mox.IsA(basestring)) | 134 cros_lib.Info(mox.IsA(basestring)) |
133 | 135 |
134 # Run the command and verify proper mocks were called... | 136 # Run the command and verify proper mocks were called... |
135 self.mox.ReplayAll() | 137 self.mox.ReplayAll() |
136 cmd_str = main._FindCommand('sh') | 138 cmd_str = main._FindCommand('sh') |
137 self.mox.VerifyAll() | 139 self.mox.VerifyAll() |
138 | 140 |
139 self.assertEqual(cmd_str, 'shell', | 141 self.assertEqual(cmd_str, 'shell', |
140 '_FindCommand("sh") should return "shell" back.') | 142 '_FindCommand("sh") should return "shell" back.') |
141 | 143 |
142 | 144 |
143 class TestFindSpec(unittest.TestCase): | 145 class TestFindSpec(unittest.TestCase): |
144 """Test main._FindSpec.""" | 146 """Test utils.FindSpec.""" |
145 | 147 |
146 def setUp(self): | 148 def setUp(self): |
147 """Test initialization.""" | 149 """Test initialization.""" |
148 # Create our mox and stub out function calls used by _FindSpec()... | 150 # Create our mox and stub out function calls used by _FindSpec()... |
149 self.mox = mox.Mox() | 151 self.mox = mox.Mox() |
150 self.mox.StubOutWithMock(os, 'listdir') | 152 self.mox.StubOutWithMock(os, 'listdir') |
151 self.mox.StubOutWithMock(os.path, 'isfile') | 153 self.mox.StubOutWithMock(os.path, 'isfile') |
152 self.mox.StubOutWithMock(main, 'Die') | 154 self.mox.StubOutWithMock(cros_lib, 'Die') |
153 self.mox.StubOutWithMock(main, 'Info') | 155 self.mox.StubOutWithMock(cros_lib, 'Info') |
154 self.mox.StubOutWithMock(text_menu, 'TextMenu') | 156 self.mox.StubOutWithMock(text_menu, 'TextMenu') |
155 | 157 |
156 def tearDown(self): | 158 def tearDown(self): |
157 """Test cleanup.""" | 159 """Test cleanup.""" |
158 # Unset stubs... | 160 # Unset stubs... |
159 self.mox.UnsetStubs() | 161 self.mox.UnsetStubs() |
160 | 162 |
161 def testInvalidSpec(self): | 163 def testInvalidSpec(self): |
162 """Test that _FindSpec('bogusSpec') causes Die().""" | 164 """Test that _FindSpec('bogusSpec') causes cros_lib.Die().""" |
163 # Pass this spec name... | 165 # Pass this spec name... |
164 spec_name = 'bogusSpec' | 166 spec_name = 'bogusSpec' |
165 | 167 |
166 # We'll tell mox to say that these specs exist... | 168 # We'll tell mox to say that these specs exist... |
167 dir_list = ['x87-toadstool.spec', 'x87-luigi.SPeC', 'x88-princess.spec', | 169 dir_list = ['x87-toadstool.spec', 'x87-luigi.SPeC', 'x88-princess.spec', |
168 '_default'] | 170 '_default'] |
169 | 171 |
170 # This spec doesn't represent any full path. | 172 # This spec doesn't represent any full path. |
171 os.path.isfile(spec_name).AndReturn(False) | 173 os.path.isfile(spec_name).AndReturn(False) |
172 | 174 |
173 # This spec isn't found in our search path. | 175 # This spec isn't found in our search path. |
174 os.path.isfile(mox.Regex('^/.*%s.spec$' % spec_name)).MultipleTimes( | 176 os.path.isfile(mox.Regex('^/.*%s.spec$' % spec_name)).MultipleTimes( |
175 ).AndReturn(False) | 177 ).AndReturn(False) |
176 | 178 |
177 # Give the fake directory listing... | 179 # Give the fake directory listing... |
178 os.listdir(mox.IsA(basestring)).MultipleTimes().AndReturn(dir_list) | 180 os.listdir(mox.IsA(basestring)).MultipleTimes().AndReturn(dir_list) |
179 | 181 |
180 # Should be a call to Die. We'll have it fake a _DeathException... | 182 # Should be a call to cros_lib.Die. We'll have it fake a _DeathException... |
181 main.Die(mox.IsA(basestring)).AndRaise(_DeathException) | 183 cros_lib.Die(mox.IsA(basestring)).AndRaise(_DeathException) |
182 | 184 |
183 # Run the command and verify proper mocks were called... | 185 # Run the command and verify proper mocks were called... |
184 self.mox.ReplayAll() | 186 self.mox.ReplayAll() |
185 self.assertRaises(_DeathException, main._FindSpec, 'bogusSpec') | 187 self.assertRaises(_DeathException, utils.FindSpec, 'bogusSpec') |
186 self.mox.VerifyAll() | 188 self.mox.VerifyAll() |
187 | 189 |
188 def testFullPath(self): | 190 def testFullPath(self): |
189 """Test that _FindSpec(full_path) returns full_path. | 191 """Test that _FindSpec(full_path) returns full_path. |
190 | 192 |
191 _FindSpec is defined so that if you pass a full file path to it, it | 193 _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 | 194 should just return that. It doesn't need to have any special suffix or |
193 live in a spec folder. | 195 live in a spec folder. |
194 """ | 196 """ |
195 # Pass this spec name... | 197 # Pass this spec name... |
196 spec_name = __file__ | 198 spec_name = __file__ |
197 | 199 |
198 # Just say that this is a full path... | 200 # Just say that this is a full path... |
199 os.path.isfile(spec_name).AndReturn(True) | 201 os.path.isfile(spec_name).AndReturn(True) |
200 | 202 |
201 # Run the command and verify proper mocks were called... | 203 # Run the command and verify proper mocks were called... |
202 self.mox.ReplayAll() | 204 self.mox.ReplayAll() |
203 path = main._FindSpec(spec_name) | 205 path = utils.FindSpec(spec_name) |
204 self.mox.VerifyAll() | 206 self.mox.VerifyAll() |
205 | 207 |
206 self.assertEqual(path, spec_name, | 208 self.assertEqual(path, spec_name, |
207 '_FindSpec() should just return param if full path.') | 209 '_FindSpec() should just return param if full path.') |
208 | 210 |
209 def testExactSpecName(self): | 211 def testExactSpecName(self): |
210 """Test that _FindSpec(exact_spec_name) returns the path for the spec.""" | 212 """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 | 213 # We'll search for this bogus spec; we'll use mox to pretend it exists in |
212 # the search path. | 214 # the search path. |
213 spec_name = 'y87-luigi' | 215 spec_name = 'y87-luigi' |
214 | 216 |
215 # This spec doesn't represent any full path | 217 # This spec doesn't represent any full path |
216 os.path.isfile(spec_name).AndReturn(False) | 218 os.path.isfile(spec_name).AndReturn(False) |
217 | 219 |
218 # When we look through the search path for this spec (with .spec at | 220 # When we look through the search path for this spec (with .spec at |
219 # the end), we will consider the spec to be found. | 221 # the end), we will consider the spec to be found. |
220 os.path.isfile(mox.Regex('^/.*%s.spec$' % spec_name)).AndReturn(True) | 222 os.path.isfile(mox.Regex('^/.*%s.spec$' % spec_name)).AndReturn(True) |
221 | 223 |
222 # Run the command and verify proper mocks were called... | 224 # Run the command and verify proper mocks were called... |
223 self.mox.ReplayAll() | 225 self.mox.ReplayAll() |
224 spec_path = main._FindSpec(spec_name) | 226 spec_path = utils.FindSpec(spec_name) |
225 self.mox.VerifyAll() | 227 self.mox.VerifyAll() |
226 | 228 |
227 self.assertTrue(re.search('^/.*%s.spec$' % spec_name, spec_path), | 229 self.assertTrue(re.search('^/.*%s.spec$' % spec_name, spec_path), |
228 '_FindSpec() should have returned absolute path for spec.') | 230 '_FindSpec() should have returned absolute path for spec.') |
229 | 231 |
230 def testUniqueSpecName(self): | 232 def testUniqueSpecName(self): |
231 """Test that _FindSpec(unique_part_name) returns the path for the spec.""" | 233 """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 | 234 # We'll search for this spec. Weird capitalization on purpose to test |
233 # case sensitiveness. | 235 # case sensitiveness. |
234 spec_name = 'ToaDSTooL' | 236 spec_name = 'ToaDSTooL' |
(...skipping 17 matching lines...) Expand all Loading... |
252 | 254 |
253 # Return our directory listing. | 255 # Return our directory listing. |
254 # TODO(dianders): How to make first mocked call return dir_list and | 256 # TODO(dianders): How to make first mocked call return dir_list and |
255 # subsequent return [] | 257 # subsequent return [] |
256 os.listdir(mox.IsA(basestring)).AndReturn(dir_list) | 258 os.listdir(mox.IsA(basestring)).AndReturn(dir_list) |
257 | 259 |
258 os.path.isfile(mox.Regex('^/.*%s$' % expected_result)).AndReturn(True) | 260 os.path.isfile(mox.Regex('^/.*%s$' % expected_result)).AndReturn(True) |
259 | 261 |
260 # Run the command and verify proper mocks were called... | 262 # Run the command and verify proper mocks were called... |
261 self.mox.ReplayAll() | 263 self.mox.ReplayAll() |
262 spec_path = main._FindSpec(spec_name) | 264 spec_path = utils.FindSpec(spec_name) |
263 self.mox.VerifyAll() | 265 self.mox.VerifyAll() |
264 | 266 |
265 self.assertTrue(re.search('^/.*%s$' % expected_result, spec_path), | 267 self.assertTrue(re.search('^/.*%s$' % expected_result, spec_path), |
266 '_FindSpec("%s") incorrectly returned "%s".' % | 268 '_FindSpec("%s") incorrectly returned "%s".' % |
267 (spec_name, spec_path)) | 269 (spec_name, spec_path)) |
268 | 270 |
269 def _TestBlankSpecName(self, menu_return): | 271 def _TestBlankSpecName(self, menu_return): |
270 """Helper for tests passing a blank spec name. | 272 """Helper for tests passing a blank spec name. |
271 | 273 |
272 Args: | 274 Args: |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
305 adjusted_menu_return = menu_return | 307 adjusted_menu_return = menu_return |
306 else: | 308 else: |
307 adjusted_menu_return = menu_return + 1 | 309 adjusted_menu_return = menu_return + 1 |
308 | 310 |
309 # Should be one call to TextMenu, which will return menu_return. | 311 # Should be one call to TextMenu, which will return menu_return. |
310 text_menu.TextMenu(mox.And(mox.IsA(list), mox.Func(check_num_items_fn)), | 312 text_menu.TextMenu(mox.And(mox.IsA(list), mox.Func(check_num_items_fn)), |
311 mox.IsA(basestring)).AndReturn(adjusted_menu_return) | 313 mox.IsA(basestring)).AndReturn(adjusted_menu_return) |
312 | 314 |
313 # Should die in response to the quit if directed to quit. | 315 # Should die in response to the quit if directed to quit. |
314 if menu_return is None: | 316 if menu_return is None: |
315 main.Die(mox.IsA(basestring)).AndRaise(_DeathException) | 317 cros_lib.Die(mox.IsA(basestring)).AndRaise(_DeathException) |
316 | 318 |
317 # Run the command and verify proper mocks were called... | 319 # Run the command and verify proper mocks were called... |
318 self.mox.ReplayAll() | 320 self.mox.ReplayAll() |
319 if menu_return is None: | 321 if menu_return is None: |
320 self.assertRaises(_DeathException, main._FindSpec, spec_name) | 322 self.assertRaises(_DeathException, utils.FindSpec, spec_name) |
321 else: | 323 else: |
322 spec_path = main._FindSpec(spec_name) | 324 spec_path = utils.FindSpec(spec_name) |
323 self.mox.VerifyAll() | 325 self.mox.VerifyAll() |
324 | 326 |
325 if menu_return is not None: | 327 if menu_return is not None: |
326 expected_result = dir_list[menu_return] | 328 expected_result = dir_list[menu_return] |
327 self.assertTrue(re.search('^/.*%s$' % expected_result, spec_path), | 329 self.assertTrue(re.search('^/.*%s$' % expected_result, spec_path), |
328 '_FindSpec("%s") incorrectly returned "%s".' % | 330 '_FindSpec("%s") incorrectly returned "%s".' % |
329 (spec_name, spec_path)) | 331 (spec_name, spec_path)) |
330 | 332 |
331 def testBlankSpecNameWithQuit(self): | 333 def testBlankSpecNameWithQuit(self): |
332 """Test that _FindSpec('') shows menu, mocking quit.""" | 334 """Test that _FindSpec('') shows menu, mocking quit.""" |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
368 for i in xrange(num_match): | 370 for i in xrange(num_match): |
369 os.path.isfile(mox.Regex('^/.*%s$' % matches[i])).AndReturn(True) | 371 os.path.isfile(mox.Regex('^/.*%s$' % matches[i])).AndReturn(True) |
370 | 372 |
371 # Should be one call to TextMenu, which will return 0. | 373 # Should be one call to TextMenu, which will return 0. |
372 text_menu.TextMenu(mox.And(mox.IsA(list), | 374 text_menu.TextMenu(mox.And(mox.IsA(list), |
373 mox.Func(lambda items: len(items) == num_match)), | 375 mox.Func(lambda items: len(items) == num_match)), |
374 mox.IsA(basestring)).AndReturn(0) | 376 mox.IsA(basestring)).AndReturn(0) |
375 | 377 |
376 # Run the command and verify proper mocks were called... | 378 # Run the command and verify proper mocks were called... |
377 self.mox.ReplayAll() | 379 self.mox.ReplayAll() |
378 spec_path = main._FindSpec(spec_name) | 380 spec_path = utils.FindSpec(spec_name) |
379 self.mox.VerifyAll() | 381 self.mox.VerifyAll() |
380 | 382 |
381 self.assertTrue(re.search('^/.*%s$' % expected_result, spec_path), | 383 self.assertTrue(re.search('^/.*%s$' % expected_result, spec_path), |
382 '_FindSpec("%s") incorrectly returned "%s".' % | 384 '_FindSpec("%s") incorrectly returned "%s".' % |
383 (spec_name, spec_path)) | 385 (spec_name, spec_path)) |
384 | 386 |
385 | 387 |
386 if __name__ == '__main__': | 388 if __name__ == '__main__': |
387 doctest.testmod(main) | 389 doctest.testmod(main) |
388 unittest.main() | 390 unittest.main() |
OLD | NEW |