Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | # Copyright (c) 2012 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 | """This module tests the command module.""" |
| 8 | |
| 9 | import argparse |
| 10 | import os |
| 11 | import sys |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 12 | |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame^] | 13 | sys.path.insert(0, os.path.abspath('%s/../..' % os.path.dirname(__file__))) |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 14 | |
| 15 | from chromite.lib import cros_test_lib |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame^] | 16 | from chromite import cros |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 17 | |
| 18 | _COMMAND_NAME = 'superAwesomeCommandOfFunness' |
| 19 | |
| 20 | |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame^] | 21 | @cros.CommandDecorator(_COMMAND_NAME) |
| 22 | class TestCommand(cros.CrosCommand): |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 23 | """A fake command.""" |
| 24 | def Run(self): |
| 25 | print 'Just testing' |
| 26 | |
| 27 | |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame^] | 28 | # pylint: disable=W0212 |
| 29 | class CommandTest(cros_test_lib.TestCase): |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 30 | """This test class tests that Commands method.""" |
| 31 | |
| 32 | def testParserSetsCrosClass(self): |
| 33 | """Tests that our parser sets cros_class correctly.""" |
| 34 | my_parser = argparse.ArgumentParser() |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame^] | 35 | cros.CrosCommand.AddParser(my_parser) |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 36 | ns = my_parser.parse_args([]) |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame^] | 37 | self.assertEqual(ns.cros_class, cros.CrosCommand) |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 38 | |
| 39 | def testCommandDecorator(self): |
| 40 | """Tests that our decorator correctly adds TestCommand to _commands.""" |
| 41 | # Note this exposes an implementation detail of _commands. |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame^] | 42 | self.assertEqual(cros._commands[_COMMAND_NAME], TestCommand) |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 43 | |
| 44 | def testBadUseOfCommandDecorator(self): |
| 45 | """Tests that our decorator correctly rejects bad test commands.""" |
| 46 | try: |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame^] | 47 | # pylint: disable=W0612 |
| 48 | @cros.CommandDecorator('bad') |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 49 | class BadTestCommand(object): |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame^] | 50 | """A command that wasn't implemented correctly.""" |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 51 | pass |
| 52 | |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame^] | 53 | except cros.InvalidCommandError: |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 54 | pass |
| 55 | else: |
| 56 | self.fail('Invalid command was accepted by the CommandDecorator') |
| 57 | |
| 58 | |
| 59 | if __name__ == '__main__': |
| 60 | cros_test_lib.main() |