blob: 4364eadeb31d7f5eaaeb3d84c0e993ca4fd26172 [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
12import unittest
13
14sys.path.insert(0, os.path.abspath('%s/../../..' % os.path.dirname(__file__)))
15
16from chromite.lib import cros_test_lib
17from chromite.cros.commands import command
18
19_COMMAND_NAME = 'superAwesomeCommandOfFunness'
20
21
22@command.CommandDecorator(_COMMAND_NAME)
23class TestCommand(command.CrosCommand):
24 """A fake command."""
25 def Run(self):
26 print 'Just testing'
27
28
29class CommandTest(unittest.TestCase):
30 """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()
35 command.CrosCommand.AddParser(my_parser)
36 ns = my_parser.parse_args([])
37 self.assertEqual(ns.cros_class, command.CrosCommand)
38
39 def testCommandDecorator(self):
40 """Tests that our decorator correctly adds TestCommand to _commands."""
41 # Note this exposes an implementation detail of _commands.
42 self.assertEqual(command._commands[_COMMAND_NAME], TestCommand)
43
44 def testBadUseOfCommandDecorator(self):
45 """Tests that our decorator correctly rejects bad test commands."""
46 try:
47 @command.CommandDecorator('bad')
48 class BadTestCommand(object):
49 """A command that wasn't implement correctly."""
50 pass
51
52 except command.InvalidCommandError:
53 pass
54 else:
55 self.fail('Invalid command was accepted by the CommandDecorator')
56
57
58if __name__ == '__main__':
59 cros_test_lib.main()