blob: 4504bd13093623a42391910d0f2aacf2bc14b70a [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
Don Garrett5bcf1442015-03-13 15:55:02 -070014# pylint:disable=protected-access
15
Chris Sosa90c78502012-10-05 17:07:42 -070016_COMMAND_NAME = 'superAwesomeCommandOfFunness'
17
18
Brian Harring984988f2012-10-10 22:53:30 -070019@cros.CommandDecorator(_COMMAND_NAME)
20class TestCommand(cros.CrosCommand):
Chris Sosa90c78502012-10-05 17:07:42 -070021 """A fake command."""
22 def Run(self):
Mike Frysinger383367e2014-09-16 15:06:17 -040023 print('Just testing')
Chris Sosa90c78502012-10-05 17:07:42 -070024
25
Brian Harring984988f2012-10-10 22:53:30 -070026class CommandTest(cros_test_lib.TestCase):
Chris Sosa90c78502012-10-05 17:07:42 -070027 """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 Harring984988f2012-10-10 22:53:30 -070032 cros.CrosCommand.AddParser(my_parser)
Chris Sosa90c78502012-10-05 17:07:42 -070033 ns = my_parser.parse_args([])
Brian Harring984988f2012-10-10 22:53:30 -070034 self.assertEqual(ns.cros_class, cros.CrosCommand)
Chris Sosa90c78502012-10-05 17:07:42 -070035
36 def testCommandDecorator(self):
37 """Tests that our decorator correctly adds TestCommand to _commands."""
38 # Note this exposes an implementation detail of _commands.
Brian Harring984988f2012-10-10 22:53:30 -070039 self.assertEqual(cros._commands[_COMMAND_NAME], TestCommand)
Chris Sosa90c78502012-10-05 17:07:42 -070040
41 def testBadUseOfCommandDecorator(self):
42 """Tests that our decorator correctly rejects bad test commands."""
43 try:
Brian Harring984988f2012-10-10 22:53:30 -070044 # pylint: disable=W0612
45 @cros.CommandDecorator('bad')
Chris Sosa90c78502012-10-05 17:07:42 -070046 class BadTestCommand(object):
Brian Harring984988f2012-10-10 22:53:30 -070047 """A command that wasn't implemented correctly."""
Chris Sosa90c78502012-10-05 17:07:42 -070048 pass
49
Brian Harring984988f2012-10-10 22:53:30 -070050 except cros.InvalidCommandError:
Chris Sosa90c78502012-10-05 17:07:42 -070051 pass
52 else:
53 self.fail('Invalid command was accepted by the CommandDecorator')