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 optparse |
10 import os | 11 import os |
11 import re | 12 import re |
12 import unittest | 13 import unittest |
13 | 14 |
14 import chromite.lib.cros_build_lib as cros_lib | 15 import chromite.lib.cros_build_lib as cros_lib |
15 from chromite.lib import text_menu | 16 from chromite.lib import text_menu |
16 from chromite.shell import main | 17 from chromite.shell import main |
17 from chromite.shell import utils | 18 from chromite.shell import utils |
18 import mox | 19 import mox |
19 | 20 |
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 | 393 |
393 # Run the command and verify proper mocks were called... | 394 # Run the command and verify proper mocks were called... |
394 self.mox.ReplayAll() | 395 self.mox.ReplayAll() |
395 spec_path = utils.FindSpec(spec_name) | 396 spec_path = utils.FindSpec(spec_name) |
396 self.mox.VerifyAll() | 397 self.mox.VerifyAll() |
397 | 398 |
398 self.assertTrue(re.search('^/.*%s$' % expected_result, spec_path), | 399 self.assertTrue(re.search('^/.*%s$' % expected_result, spec_path), |
399 '_FindSpec("%s") incorrectly returned "%s".' % | 400 '_FindSpec("%s") incorrectly returned "%s".' % |
400 (spec_name, spec_path)) | 401 (spec_name, spec_path)) |
401 | 402 |
| 403 class TestParseArguments(unittest.TestCase): |
| 404 """Test utils.FindSpec.""" |
| 405 |
| 406 def setUp(self): |
| 407 """Test initialization.""" |
| 408 self.parser = optparse.OptionParser() |
| 409 |
| 410 # Verbose defaults to full for now, just to keep people acclimatized to |
| 411 # vast amounts of comforting output. |
| 412 self.parser.add_option('-v', dest='verbose', default=3, type='int', |
| 413 help='Control verbosity: 0=silent, 1=progress, 3=full') |
| 414 self.parser.add_option('-q', action='store_const', dest='verbose', const=0, |
| 415 help='Be quieter (sets verbosity to 1)') |
| 416 |
| 417 def testEmpty(self): |
| 418 options, cmd, sub = main._ParseArguments(self.parser, |
| 419 []) |
| 420 self.assertEqual(options.verbose, 3) |
| 421 self.assertEqual(cmd, '') |
| 422 self.assertEqual(sub, []) |
| 423 |
| 424 def testBadOption(self): |
| 425 self.assertRaises(SystemExit, main._ParseArguments, self.parser, |
| 426 ['chromite', '--bad']) |
| 427 self.assertRaises(SystemExit, main._ParseArguments, self.parser, |
| 428 ['chromite', '--bad', 'build']) |
| 429 |
| 430 def testSubcmd(self): |
| 431 options, cmd, sub = main._ParseArguments(self.parser, |
| 432 ['chromite', 'build']) |
| 433 self.assertEqual(options.verbose, 3) |
| 434 self.assertEqual(cmd, 'build') |
| 435 self.assertEqual(sub, []) |
| 436 |
| 437 def testSubcmdQuiet(self): |
| 438 options, cmd, sub = main._ParseArguments(self.parser, |
| 439 ['chromite', '-q', 'build']) |
| 440 self.assertEqual(options.verbose, 0) |
| 441 self.assertEqual(cmd, 'build') |
| 442 self.assertEqual(sub, []) |
| 443 |
| 444 def testSubcmdVerbose2(self): |
| 445 options, cmd, sub = main._ParseArguments(self.parser, |
| 446 ['chromite', '-v2', 'build']) |
| 447 self.assertEqual(options.verbose, 2) |
| 448 self.assertEqual(cmd, 'build') |
| 449 self.assertEqual(sub, []) |
| 450 |
| 451 def testSubcmdVerbose4(self): |
| 452 options, cmd, sub = main._ParseArguments(self.parser, |
| 453 ['chromite', '-v', '4', 'build']) |
| 454 self.assertEqual(options.verbose, 4) |
| 455 self.assertEqual(cmd, 'build') |
| 456 self.assertEqual(sub, []) |
| 457 |
| 458 def testSubcmdArgs(self): |
| 459 options, cmd, sub = main._ParseArguments(self.parser, |
| 460 ['chromite', '-v', '4', 'build', 'seaboard', '--clean']) |
| 461 self.assertEqual(options.verbose, 4) |
| 462 self.assertEqual(cmd, 'build') |
| 463 self.assertEqual(sub, ['seaboard', '--clean']) |
402 | 464 |
403 if __name__ == '__main__': | 465 if __name__ == '__main__': |
404 doctest.testmod(main) | 466 doctest.testmod(main) |
405 unittest.main() | 467 unittest.main() |
OLD | NEW |