blob: 1e198afd89e47f5ef5c4d85b7cfcf22af8159b31 [file] [log] [blame]
Mike Frysingere58c0e22017-10-04 15:43:30 -04001# -*- coding: utf-8 -*-
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
David Pursellf1c27c12015-03-18 09:51:38 -07006"""Tests for the command module."""
Chris Sosa90c78502012-10-05 17:07:42 -07007
Mike Frysinger383367e2014-09-16 15:06:17 -04008from __future__ import print_function
9
Chris Sosa90c78502012-10-05 17:07:42 -070010import argparse
Alex Klein665aa072019-12-12 15:13:53 -070011import importlib
Ralph Nathan74e864d2015-05-11 12:13:53 -070012import os
Chris Sosa90c78502012-10-05 17:07:42 -070013
David Pursellf1c27c12015-03-18 09:51:38 -070014from chromite.cli import command
15from chromite.lib import commandline
Chris Sosa90c78502012-10-05 17:07:42 -070016from chromite.lib import cros_test_lib
David Pursellf1c27c12015-03-18 09:51:38 -070017from chromite.lib import partial_mock
18
Chris Sosa90c78502012-10-05 17:07:42 -070019
Don Garrett5bcf1442015-03-13 15:55:02 -070020# pylint:disable=protected-access
21
Chris Sosa90c78502012-10-05 17:07:42 -070022_COMMAND_NAME = 'superAwesomeCommandOfFunness'
23
24
David Pursellf1c27c12015-03-18 09:51:38 -070025@command.CommandDecorator(_COMMAND_NAME)
David Pursellffb90042015-03-23 09:21:41 -070026class TestCommand(command.CliCommand):
Chris Sosa90c78502012-10-05 17:07:42 -070027 """A fake command."""
28 def Run(self):
Mike Frysinger383367e2014-09-16 15:06:17 -040029 print('Just testing')
Chris Sosa90c78502012-10-05 17:07:42 -070030
31
David Pursellf80b8c52015-04-03 12:46:45 -070032class TestCommandTest(cros_test_lib.MockTestCase):
Chris Sosa90c78502012-10-05 17:07:42 -070033 """This test class tests that Commands method."""
34
David Pursellffb90042015-03-23 09:21:41 -070035 def testParserSetsCommandClass(self):
36 """Tests that our parser sets command_class correctly."""
Chris Sosa90c78502012-10-05 17:07:42 -070037 my_parser = argparse.ArgumentParser()
David Pursellffb90042015-03-23 09:21:41 -070038 command.CliCommand.AddParser(my_parser)
Chris Sosa90c78502012-10-05 17:07:42 -070039 ns = my_parser.parse_args([])
David Pursellffb90042015-03-23 09:21:41 -070040 self.assertEqual(ns.command_class, command.CliCommand)
Chris Sosa90c78502012-10-05 17:07:42 -070041
42 def testCommandDecorator(self):
43 """Tests that our decorator correctly adds TestCommand to _commands."""
44 # Note this exposes an implementation detail of _commands.
David Pursellf1c27c12015-03-18 09:51:38 -070045 self.assertEqual(command._commands[_COMMAND_NAME], TestCommand)
Chris Sosa90c78502012-10-05 17:07:42 -070046
47 def testBadUseOfCommandDecorator(self):
48 """Tests that our decorator correctly rejects bad test commands."""
49 try:
Mike Frysinger27e21b72018-07-12 14:20:21 -040050 # pylint: disable=unused-variable
David Pursellf1c27c12015-03-18 09:51:38 -070051 @command.CommandDecorator('bad')
Chris Sosa90c78502012-10-05 17:07:42 -070052 class BadTestCommand(object):
Brian Harring984988f2012-10-10 22:53:30 -070053 """A command that wasn't implemented correctly."""
Chris Sosa90c78502012-10-05 17:07:42 -070054
David Pursellf1c27c12015-03-18 09:51:38 -070055 except command.InvalidCommandError:
Chris Sosa90c78502012-10-05 17:07:42 -070056 pass
57 else:
58 self.fail('Invalid command was accepted by the CommandDecorator')
David Pursellf1c27c12015-03-18 09:51:38 -070059
David Pursellc7ba7842015-07-08 10:48:41 -070060 def testAddDeviceArgument(self):
61 """Tests CliCommand.AddDeviceArgument()."""
David Pursellf80b8c52015-04-03 12:46:45 -070062 parser = argparse.ArgumentParser()
63 command.CliCommand.AddDeviceArgument(parser)
David Pursellc7ba7842015-07-08 10:48:41 -070064 # Device should be a positional argument.
David Pursellf80b8c52015-04-03 12:46:45 -070065 parser.parse_args(['device'])
David Pursellf80b8c52015-04-03 12:46:45 -070066
David Pursellf1c27c12015-03-18 09:51:38 -070067
68class MockCommand(partial_mock.PartialMock):
David Pursellffb90042015-03-23 09:21:41 -070069 """Mock class for a generic CLI command."""
David Pursellf1c27c12015-03-18 09:51:38 -070070 ATTRS = ('Run',)
71 COMMAND = None
72 TARGET_CLASS = None
73
74 def __init__(self, args, base_args=None):
75 partial_mock.PartialMock.__init__(self)
76 self.args = args
Benjamin Gordon121a2aa2018-05-04 16:24:45 -060077 self.rc_mock = cros_test_lib.RunCommandMock()
David Pursellf1c27c12015-03-18 09:51:38 -070078 self.rc_mock.SetDefaultCmdResult()
79 parser = commandline.ArgumentParser(caching=True)
80 subparsers = parser.add_subparsers()
81 subparser = subparsers.add_parser(self.COMMAND, caching=True)
82 self.TARGET_CLASS.AddParser(subparser)
83
84 args = base_args if base_args else []
85 args += [self.COMMAND] + self.args
86 options = parser.parse_args(args)
David Pursellffb90042015-03-23 09:21:41 -070087 self.inst = options.command_class(options)
David Pursellf1c27c12015-03-18 09:51:38 -070088
89 def Run(self, inst):
90 with self.rc_mock:
91 return self.backup['Run'](inst)
92
93
94class CommandTest(cros_test_lib.MockTestCase):
95 """This test class tests that we can load modules correctly."""
96
David Pursellf1c27c12015-03-18 09:51:38 -070097 def testFindModules(self):
98 """Tests that we can return modules correctly when mocking out glob."""
99 fake_command_file = 'cros_command_test.py'
100 filtered_file = 'cros_command_unittest.py'
David Pursellf1c27c12015-03-18 09:51:38 -0700101
Mike Frysinger1a470812019-11-07 01:19:17 -0500102 self.PatchObject(os, 'listdir',
David Pursellf1c27c12015-03-18 09:51:38 -0700103 return_value=[fake_command_file, filtered_file])
104
Mike Frysinger1a470812019-11-07 01:19:17 -0500105 self.assertEqual(command.ListCommands(), {'command-test'})
David Pursellf1c27c12015-03-18 09:51:38 -0700106
107 def testLoadCommands(self):
108 """Tests import commands correctly."""
109 fake_module = 'cros_command_test'
Alex Klein665aa072019-12-12 15:13:53 -0700110 module_path = 'chromite.cli.cros.%s' % fake_module
David Pursellf1c27c12015-03-18 09:51:38 -0700111
David Pursellf1c27c12015-03-18 09:51:38 -0700112 # The code doesn't use the return value, so stub it out lazy-like.
Alex Klein665aa072019-12-12 15:13:53 -0700113 load_mock = self.PatchObject(importlib, 'import_module', return_value=None)
David Pursellf1c27c12015-03-18 09:51:38 -0700114
Mike Frysinger1a470812019-11-07 01:19:17 -0500115 command._commands['command-test'] = 123
116 self.assertEqual(command.ImportCommand('command-test'), 123)
117 command._commands.pop('command-test')
David Pursellf1c27c12015-03-18 09:51:38 -0700118
119 load_mock.assert_called_with(module_path)
120
David Pursellffb90042015-03-23 09:21:41 -0700121 def testListCrosCommands(self):
122 """Tests we get a sane `cros` list back."""
David Pursellc7ba7842015-07-08 10:48:41 -0700123 cros_commands = command.ListCommands()
David Pursellf1c27c12015-03-18 09:51:38 -0700124 # Pick some commands that are likely to not go away.
125 self.assertIn('chrome-sdk', cros_commands)
126 self.assertIn('flash', cros_commands)