Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 1 | # 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 | |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame^] | 5 | """Tests for the command module.""" |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 6 | |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 7 | from __future__ import print_function |
| 8 | |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 9 | import argparse |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame^] | 10 | import glob |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 11 | |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame^] | 12 | from chromite.cli import command |
| 13 | from chromite.lib import commandline |
| 14 | from chromite.lib import cros_build_lib_unittest |
| 15 | from chromite.lib import cros_import |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 16 | from chromite.lib import cros_test_lib |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame^] | 17 | from chromite.lib import partial_mock |
| 18 | |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 19 | |
Don Garrett | 5bcf144 | 2015-03-13 15:55:02 -0700 | [diff] [blame] | 20 | # pylint:disable=protected-access |
| 21 | |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 22 | _COMMAND_NAME = 'superAwesomeCommandOfFunness' |
| 23 | |
| 24 | |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame^] | 25 | @command.CommandDecorator(_COMMAND_NAME) |
| 26 | class TestCommand(command.CrosCommand): |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 27 | """A fake command.""" |
| 28 | def Run(self): |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 29 | print('Just testing') |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 30 | |
| 31 | |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame^] | 32 | class TestCommandTest(cros_test_lib.TestCase): |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 33 | """This test class tests that Commands method.""" |
| 34 | |
| 35 | def testParserSetsCrosClass(self): |
| 36 | """Tests that our parser sets cros_class correctly.""" |
| 37 | my_parser = argparse.ArgumentParser() |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame^] | 38 | command.CrosCommand.AddParser(my_parser) |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 39 | ns = my_parser.parse_args([]) |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame^] | 40 | self.assertEqual(ns.cros_class, command.CrosCommand) |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 41 | |
| 42 | def testCommandDecorator(self): |
| 43 | """Tests that our decorator correctly adds TestCommand to _commands.""" |
| 44 | # Note this exposes an implementation detail of _commands. |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame^] | 45 | self.assertEqual(command._commands[_COMMAND_NAME], TestCommand) |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 46 | |
| 47 | def testBadUseOfCommandDecorator(self): |
| 48 | """Tests that our decorator correctly rejects bad test commands.""" |
| 49 | try: |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 50 | # pylint: disable=W0612 |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame^] | 51 | @command.CommandDecorator('bad') |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 52 | class BadTestCommand(object): |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 53 | """A command that wasn't implemented correctly.""" |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 54 | pass |
| 55 | |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame^] | 56 | except command.InvalidCommandError: |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 57 | pass |
| 58 | else: |
| 59 | self.fail('Invalid command was accepted by the CommandDecorator') |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame^] | 60 | |
| 61 | |
| 62 | class MockCommand(partial_mock.PartialMock): |
| 63 | """Mock class for a generic cros command.""" |
| 64 | ATTRS = ('Run',) |
| 65 | COMMAND = None |
| 66 | TARGET_CLASS = None |
| 67 | |
| 68 | def __init__(self, args, base_args=None): |
| 69 | partial_mock.PartialMock.__init__(self) |
| 70 | self.args = args |
| 71 | self.rc_mock = cros_build_lib_unittest.RunCommandMock() |
| 72 | self.rc_mock.SetDefaultCmdResult() |
| 73 | parser = commandline.ArgumentParser(caching=True) |
| 74 | subparsers = parser.add_subparsers() |
| 75 | subparser = subparsers.add_parser(self.COMMAND, caching=True) |
| 76 | self.TARGET_CLASS.AddParser(subparser) |
| 77 | |
| 78 | args = base_args if base_args else [] |
| 79 | args += [self.COMMAND] + self.args |
| 80 | options = parser.parse_args(args) |
| 81 | self.inst = options.cros_class(options) |
| 82 | |
| 83 | def Run(self, inst): |
| 84 | with self.rc_mock: |
| 85 | return self.backup['Run'](inst) |
| 86 | |
| 87 | |
| 88 | class CommandTest(cros_test_lib.MockTestCase): |
| 89 | """This test class tests that we can load modules correctly.""" |
| 90 | |
| 91 | # pylint: disable=W0212 |
| 92 | |
| 93 | def testFindModules(self): |
| 94 | """Tests that we can return modules correctly when mocking out glob.""" |
| 95 | fake_command_file = 'cros_command_test.py' |
| 96 | filtered_file = 'cros_command_unittest.py' |
| 97 | mydir = 'mydir' |
| 98 | |
| 99 | self.PatchObject(glob, 'glob', |
| 100 | return_value=[fake_command_file, filtered_file]) |
| 101 | |
| 102 | self.assertEqual(command._FindModules(mydir), [fake_command_file]) |
| 103 | |
| 104 | def testLoadCommands(self): |
| 105 | """Tests import commands correctly.""" |
| 106 | fake_module = 'cros_command_test' |
| 107 | fake_command_file = '%s.py' % fake_module |
| 108 | module_path = ('chromite', 'cros', 'commands', fake_module) |
| 109 | |
| 110 | self.PatchObject(command, '_FindModules', return_value=[fake_command_file]) |
| 111 | # The code doesn't use the return value, so stub it out lazy-like. |
| 112 | load_mock = self.PatchObject(cros_import, 'ImportModule', return_value=None) |
| 113 | |
| 114 | command._ImportCommands() |
| 115 | |
| 116 | load_mock.assert_called_with(module_path) |
| 117 | |
| 118 | def testListCommands(self): |
| 119 | """Tests we get a sane list back.""" |
| 120 | cros_commands = command.ListCommands() |
| 121 | # Pick some commands that are likely to not go away. |
| 122 | self.assertIn('chrome-sdk', cros_commands) |
| 123 | self.assertIn('flash', cros_commands) |