blob: 11bfe4ed0e5057d378afee6a0959af12b75fd622 [file] [log] [blame]
Chris Sosa90c78502012-10-05 17:07:42 -07001#!/usr/bin/python
Chris Sosa90c78502012-10-05 17:07:42 -07002# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""This module tests the command module."""
7
Mike Frysinger383367e2014-09-16 15:06:17 -04008from __future__ import print_function
9
Chris Sosa90c78502012-10-05 17:07:42 -070010import argparse
11import os
12import sys
Chris Sosa90c78502012-10-05 17:07:42 -070013
Brian Harring984988f2012-10-10 22:53:30 -070014sys.path.insert(0, os.path.abspath('%s/../..' % os.path.dirname(__file__)))
Chris Sosa90c78502012-10-05 17:07:42 -070015
16from chromite.lib import cros_test_lib
Brian Harring984988f2012-10-10 22:53:30 -070017from chromite import cros
Chris Sosa90c78502012-10-05 17:07:42 -070018
19_COMMAND_NAME = 'superAwesomeCommandOfFunness'
20
21
Brian Harring984988f2012-10-10 22:53:30 -070022@cros.CommandDecorator(_COMMAND_NAME)
23class TestCommand(cros.CrosCommand):
Chris Sosa90c78502012-10-05 17:07:42 -070024 """A fake command."""
25 def Run(self):
Mike Frysinger383367e2014-09-16 15:06:17 -040026 print('Just testing')
Chris Sosa90c78502012-10-05 17:07:42 -070027
28
Brian Harring984988f2012-10-10 22:53:30 -070029# pylint: disable=W0212
30class CommandTest(cros_test_lib.TestCase):
Chris Sosa90c78502012-10-05 17:07:42 -070031 """This test class tests that Commands method."""
32
33 def testParserSetsCrosClass(self):
34 """Tests that our parser sets cros_class correctly."""
35 my_parser = argparse.ArgumentParser()
Brian Harring984988f2012-10-10 22:53:30 -070036 cros.CrosCommand.AddParser(my_parser)
Chris Sosa90c78502012-10-05 17:07:42 -070037 ns = my_parser.parse_args([])
Brian Harring984988f2012-10-10 22:53:30 -070038 self.assertEqual(ns.cros_class, cros.CrosCommand)
Chris Sosa90c78502012-10-05 17:07:42 -070039
40 def testCommandDecorator(self):
41 """Tests that our decorator correctly adds TestCommand to _commands."""
42 # Note this exposes an implementation detail of _commands.
Brian Harring984988f2012-10-10 22:53:30 -070043 self.assertEqual(cros._commands[_COMMAND_NAME], TestCommand)
Chris Sosa90c78502012-10-05 17:07:42 -070044
45 def testBadUseOfCommandDecorator(self):
46 """Tests that our decorator correctly rejects bad test commands."""
47 try:
Brian Harring984988f2012-10-10 22:53:30 -070048 # pylint: disable=W0612
49 @cros.CommandDecorator('bad')
Chris Sosa90c78502012-10-05 17:07:42 -070050 class BadTestCommand(object):
Brian Harring984988f2012-10-10 22:53:30 -070051 """A command that wasn't implemented correctly."""
Chris Sosa90c78502012-10-05 17:07:42 -070052 pass
53
Brian Harring984988f2012-10-10 22:53:30 -070054 except cros.InvalidCommandError:
Chris Sosa90c78502012-10-05 17:07:42 -070055 pass
56 else:
57 self.fail('Invalid command was accepted by the CommandDecorator')
58
59
60if __name__ == '__main__':
61 cros_test_lib.main()