blob: f3bed29f2ed652a063385506f8db7bde1b009d8b [file] [log] [blame]
Chris Sosa90c78502012-10-05 17:07:42 -07001# 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 Frysinger383367e2014-09-16 15:06:17 -04007from __future__ import print_function
8
Chris Sosa90c78502012-10-05 17:07:42 -07009import argparse
Chris Sosa90c78502012-10-05 17:07:42 -070010
11from chromite.lib import cros_test_lib
Brian Harring984988f2012-10-10 22:53:30 -070012from chromite import cros
Chris Sosa90c78502012-10-05 17:07:42 -070013
14_COMMAND_NAME = 'superAwesomeCommandOfFunness'
15
16
Brian Harring984988f2012-10-10 22:53:30 -070017@cros.CommandDecorator(_COMMAND_NAME)
18class TestCommand(cros.CrosCommand):
Chris Sosa90c78502012-10-05 17:07:42 -070019 """A fake command."""
20 def Run(self):
Mike Frysinger383367e2014-09-16 15:06:17 -040021 print('Just testing')
Chris Sosa90c78502012-10-05 17:07:42 -070022
23
Brian Harring984988f2012-10-10 22:53:30 -070024# pylint: disable=W0212
25class CommandTest(cros_test_lib.TestCase):
Chris Sosa90c78502012-10-05 17:07:42 -070026 """This test class tests that Commands method."""
27
28 def testParserSetsCrosClass(self):
29 """Tests that our parser sets cros_class correctly."""
30 my_parser = argparse.ArgumentParser()
Brian Harring984988f2012-10-10 22:53:30 -070031 cros.CrosCommand.AddParser(my_parser)
Chris Sosa90c78502012-10-05 17:07:42 -070032 ns = my_parser.parse_args([])
Brian Harring984988f2012-10-10 22:53:30 -070033 self.assertEqual(ns.cros_class, cros.CrosCommand)
Chris Sosa90c78502012-10-05 17:07:42 -070034
35 def testCommandDecorator(self):
36 """Tests that our decorator correctly adds TestCommand to _commands."""
37 # Note this exposes an implementation detail of _commands.
Brian Harring984988f2012-10-10 22:53:30 -070038 self.assertEqual(cros._commands[_COMMAND_NAME], TestCommand)
Chris Sosa90c78502012-10-05 17:07:42 -070039
40 def testBadUseOfCommandDecorator(self):
41 """Tests that our decorator correctly rejects bad test commands."""
42 try:
Brian Harring984988f2012-10-10 22:53:30 -070043 # pylint: disable=W0612
44 @cros.CommandDecorator('bad')
Chris Sosa90c78502012-10-05 17:07:42 -070045 class BadTestCommand(object):
Brian Harring984988f2012-10-10 22:53:30 -070046 """A command that wasn't implemented correctly."""
Chris Sosa90c78502012-10-05 17:07:42 -070047 pass
48
Brian Harring984988f2012-10-10 22:53:30 -070049 except cros.InvalidCommandError:
Chris Sosa90c78502012-10-05 17:07:42 -070050 pass
51 else:
52 self.fail('Invalid command was accepted by the CommandDecorator')