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