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