OLD | NEW |
(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 # Should die in response to the quit. |
| 64 chromite.Die(mox.IsA(basestring)).AndRaise(_DeathException) |
| 65 |
| 66 # Run the command and verify proper mocks were called... |
| 67 self.mox.ReplayAll() |
| 68 self.assertRaises(_DeathException, chromite._FindCommand, '') |
| 69 self.mox.VerifyAll() |
| 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 os.path.isfile(mox.Regex('^/.*%s$' % expected_result)).AndReturn(True) |
| 259 |
| 260 # Run the command and verify proper mocks were called... |
| 261 self.mox.ReplayAll() |
| 262 spec_path = chromite._FindSpec(spec_name) |
| 263 self.mox.VerifyAll() |
| 264 |
| 265 self.assertTrue(re.search('^/.*%s$' % expected_result, spec_path), |
| 266 '_FindSpec("%s") incorrectly returned "%s".' % |
| 267 (spec_name, spec_path)) |
| 268 |
| 269 def _TestBlankSpecName(self, menu_return): |
| 270 """Helper for tests passing a blank spec name. |
| 271 |
| 272 Args: |
| 273 menu_return: This value is returned by TextMenu. Could be none or an |
| 274 integer index into the dir_list of this function. |
| 275 """ |
| 276 # We'll search for this spec. |
| 277 spec_name = '' |
| 278 |
| 279 # We'll tell mox to say that these specs exist in the first directory... |
| 280 # ...only valid specs to make it easier to compare things. |
| 281 dir_list = ['x87-luigi.spec', 'x87-toadstool.SPeC', 'x88-princess.spec'] |
| 282 num_specs = len(dir_list) |
| 283 |
| 284 # Self-checks for test code... |
| 285 assert menu_return < len(dir_list) |
| 286 assert dir_list == sorted(dir_list) |
| 287 |
| 288 # This spec doesn't represent any full path. |
| 289 os.path.isfile(spec_name).AndReturn(False) |
| 290 |
| 291 # Return our directory listing. |
| 292 # TODO(dianders): How to make first mocked call return dir_list and |
| 293 # subsequent return [] |
| 294 os.listdir(mox.IsA(basestring)).AndReturn(dir_list) |
| 295 |
| 296 for i in xrange(num_specs): |
| 297 os.path.isfile(mox.Regex('^/.*%s$' % dir_list[i])).AndReturn(True) |
| 298 |
| 299 # We expect there to be 1 more item than we passed in, since we account |
| 300 # for 'HOST'. |
| 301 check_num_items_fn = lambda items: len(items) == (num_specs + 1) |
| 302 |
| 303 # Add 1 to menu_return, since 'HOST' is first... |
| 304 if menu_return is None: |
| 305 adjusted_menu_return = menu_return |
| 306 else: |
| 307 adjusted_menu_return = menu_return + 1 |
| 308 |
| 309 # 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)), |
| 311 mox.IsA(basestring)).AndReturn(adjusted_menu_return) |
| 312 |
| 313 # Should die in response to the quit if directed to quit. |
| 314 if menu_return is None: |
| 315 chromite.Die(mox.IsA(basestring)).AndRaise(_DeathException) |
| 316 |
| 317 # Run the command and verify proper mocks were called... |
| 318 self.mox.ReplayAll() |
| 319 if menu_return is None: |
| 320 self.assertRaises(_DeathException, chromite._FindSpec, spec_name) |
| 321 else: |
| 322 spec_path = chromite._FindSpec(spec_name) |
| 323 self.mox.VerifyAll() |
| 324 |
| 325 if menu_return is not None: |
| 326 expected_result = dir_list[menu_return] |
| 327 self.assertTrue(re.search('^/.*%s$' % expected_result, spec_path), |
| 328 '_FindSpec("%s") incorrectly returned "%s".' % |
| 329 (spec_name, spec_path)) |
| 330 |
| 331 def testBlankSpecNameWithQuit(self): |
| 332 """Test that _FindSpec('') shows menu, mocking quit.""" |
| 333 self._TestBlankSpecName(None) |
| 334 |
| 335 def testBlankSpecNameWithChoice0(self): |
| 336 """Test that _FindSpec('') shows menu, mocking choice 0.""" |
| 337 self._TestBlankSpecName(0) |
| 338 |
| 339 def testPartialSpecNameWithChoice0(self): |
| 340 """Test that _FindSpec(non_unique_str) shows menu, mocking choice 0.""" |
| 341 # We'll search for this spec. |
| 342 spec_name = 'x87' |
| 343 |
| 344 # We'll tell mox to say that these specs exist in the first directory... |
| 345 dir_list = ['_default', |
| 346 'x87-luigi.spec', 'x87-toadstool.SPeC', 'x88-princess.spec'] |
| 347 |
| 348 # We expect 2 matches and should get back luigi as choice 0. |
| 349 matches = ['x87-luigi.spec', 'x87-toadstool.SPeC'] |
| 350 num_match = len(matches) |
| 351 expected_result = 'x87-luigi.spec' |
| 352 |
| 353 # Self-checks for test code... |
| 354 assert dir_list == sorted(dir_list) |
| 355 |
| 356 # This spec doesn't represent any full path. |
| 357 os.path.isfile(spec_name).AndReturn(False) |
| 358 |
| 359 # This spec isn't found in our search path. |
| 360 os.path.isfile(mox.Regex('^/.*%s.spec$' % spec_name)).MultipleTimes( |
| 361 ).AndReturn(False) |
| 362 |
| 363 # Return our directory listing. |
| 364 # TODO(dianders): How to make first mocked call return dir_list and |
| 365 # subsequent return [] |
| 366 os.listdir(mox.IsA(basestring)).AndReturn(dir_list) |
| 367 |
| 368 for i in xrange(num_match): |
| 369 os.path.isfile(mox.Regex('^/.*%s$' % matches[i])).AndReturn(True) |
| 370 |
| 371 # Should be one call to TextMenu, which will return 0. |
| 372 text_menu.TextMenu(mox.And(mox.IsA(list), |
| 373 mox.Func(lambda items: len(items) == num_match)), |
| 374 mox.IsA(basestring)).AndReturn(0) |
| 375 |
| 376 # Run the command and verify proper mocks were called... |
| 377 self.mox.ReplayAll() |
| 378 spec_path = chromite._FindSpec(spec_name) |
| 379 self.mox.VerifyAll() |
| 380 |
| 381 self.assertTrue(re.search('^/.*%s$' % expected_result, spec_path), |
| 382 '_FindSpec("%s") incorrectly returned "%s".' % |
| 383 (spec_name, spec_path)) |
| 384 |
| 385 |
| 386 if __name__ == '__main__': |
| 387 doctest.testmod(chromite) |
| 388 unittest.main() |
| 389 |
OLD | NEW |