blob: d47c17bf099636832ff8ba5861d94c95ca027477 [file] [log] [blame]
Chris Sosa90c78502012-10-05 17:07:42 -07001#!/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
9import argparse
10import os
11import sys
Chris Sosa90c78502012-10-05 17:07:42 -070012
Brian Harring984988f2012-10-10 22:53:30 -070013sys.path.insert(0, os.path.abspath('%s/../..' % os.path.dirname(__file__)))
Chris Sosa90c78502012-10-05 17:07:42 -070014
15from chromite.lib import cros_test_lib
Brian Harring984988f2012-10-10 22:53:30 -070016from chromite import cros
Chris Sosa90c78502012-10-05 17:07:42 -070017
18_COMMAND_NAME = 'superAwesomeCommandOfFunness'
19
20
Brian Harring984988f2012-10-10 22:53:30 -070021@cros.CommandDecorator(_COMMAND_NAME)
22class TestCommand(cros.CrosCommand):
Chris Sosa90c78502012-10-05 17:07:42 -070023 """A fake command."""
24 def Run(self):
25 print 'Just testing'
26
27
Brian Harring984988f2012-10-10 22:53:30 -070028# pylint: disable=W0212
29class CommandTest(cros_test_lib.TestCase):
Chris Sosa90c78502012-10-05 17:07:42 -070030 """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()
Brian Harring984988f2012-10-10 22:53:30 -070035 cros.CrosCommand.AddParser(my_parser)
Chris Sosa90c78502012-10-05 17:07:42 -070036 ns = my_parser.parse_args([])
Brian Harring984988f2012-10-10 22:53:30 -070037 self.assertEqual(ns.cros_class, cros.CrosCommand)
Chris Sosa90c78502012-10-05 17:07:42 -070038
39 def testCommandDecorator(self):
40 """Tests that our decorator correctly adds TestCommand to _commands."""
41 # Note this exposes an implementation detail of _commands.
Brian Harring984988f2012-10-10 22:53:30 -070042 self.assertEqual(cros._commands[_COMMAND_NAME], TestCommand)
Chris Sosa90c78502012-10-05 17:07:42 -070043
44 def testBadUseOfCommandDecorator(self):
45 """Tests that our decorator correctly rejects bad test commands."""
46 try:
Brian Harring984988f2012-10-10 22:53:30 -070047 # pylint: disable=W0612
48 @cros.CommandDecorator('bad')
Chris Sosa90c78502012-10-05 17:07:42 -070049 class BadTestCommand(object):
Brian Harring984988f2012-10-10 22:53:30 -070050 """A command that wasn't implemented correctly."""
Chris Sosa90c78502012-10-05 17:07:42 -070051 pass
52
Brian Harring984988f2012-10-10 22:53:30 -070053 except cros.InvalidCommandError:
Chris Sosa90c78502012-10-05 17:07:42 -070054 pass
55 else:
56 self.fail('Invalid command was accepted by the CommandDecorator')
57
58
59if __name__ == '__main__':
60 cros_test_lib.main()