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 |
| 12 | import unittest |
| 13 | |
| 14 | sys.path.insert(0, os.path.abspath('%s/../../..' % os.path.dirname(__file__))) |
| 15 | |
| 16 | from chromite.lib import cros_test_lib |
| 17 | from chromite.cros.commands import command |
| 18 | |
| 19 | _COMMAND_NAME = 'superAwesomeCommandOfFunness' |
| 20 | |
| 21 | |
| 22 | @command.CommandDecorator(_COMMAND_NAME) |
| 23 | class TestCommand(command.CrosCommand): |
| 24 | """A fake command.""" |
| 25 | def Run(self): |
| 26 | print 'Just testing' |
| 27 | |
| 28 | |
| 29 | class CommandTest(unittest.TestCase): |
| 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() |
| 35 | command.CrosCommand.AddParser(my_parser) |
| 36 | ns = my_parser.parse_args([]) |
| 37 | self.assertEqual(ns.cros_class, command.CrosCommand) |
| 38 | |
| 39 | def testCommandDecorator(self): |
| 40 | """Tests that our decorator correctly adds TestCommand to _commands.""" |
| 41 | # Note this exposes an implementation detail of _commands. |
| 42 | self.assertEqual(command._commands[_COMMAND_NAME], TestCommand) |
| 43 | |
| 44 | def testBadUseOfCommandDecorator(self): |
| 45 | """Tests that our decorator correctly rejects bad test commands.""" |
| 46 | try: |
| 47 | @command.CommandDecorator('bad') |
| 48 | class BadTestCommand(object): |
| 49 | """A command that wasn't implement correctly.""" |
| 50 | pass |
| 51 | |
| 52 | except command.InvalidCommandError: |
| 53 | pass |
| 54 | else: |
| 55 | self.fail('Invalid command was accepted by the CommandDecorator') |
| 56 | |
| 57 | |
| 58 | if __name__ == '__main__': |
| 59 | cros_test_lib.main() |