1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
"""
SoftLayer.tests.CLI.environment_tests
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
:license: MIT, see LICENSE for more details.
"""
import click
from unittest import mock as mock
from SoftLayer.CLI import environment
from SoftLayer.CLI import formatting
from SoftLayer import testing
@click.command()
def fixture_command():
pass
class EnvironmentTests(testing.TestCase):
def set_up(self):
self.env = environment.Environment()
def test_list_commands(self):
self.env.load()
actions = self.env.list_commands()
self.assertIn('virtual', actions)
self.assertIn('dns', actions)
def test_get_command_invalid(self):
cmd = self.env.get_command('invalid', 'command')
self.assertEqual(cmd, None)
def test_get_command(self):
fixture_loader = environment.ModuleLoader(
'tests.CLI.environment_tests',
'fixture_command',
)
self.env.commands = {'fixture:run': fixture_loader}
command = self.env.get_command('fixture', 'run')
self.assertIsInstance(command, click.Command)
@mock.patch('click.prompt')
def test_input(self, prompt_mock):
r = self.env.input('input')
prompt_mock.assert_called_with('input', default=None, show_default=True)
self.assertEqual(prompt_mock(), r)
@mock.patch('click.prompt')
def test_getpass(self, prompt_mock):
r = self.env.getpass('input')
prompt_mock.assert_called_with('input', default=None, hide_input=True)
self.assertEqual(prompt_mock(), r)
@mock.patch('click.prompt')
@mock.patch('tkinter.Tk')
def test_getpass_issues1436(self, tk, prompt_mock):
prompt_mock.return_value = 'àR'
self.env.getpass('input')
prompt_mock.assert_called_with('input', default=None, hide_input=True)
tk.assert_called_with()
def test_resolve_alias(self):
self.env.aliases = {'aliasname': 'realname'}
r = self.env.resolve_alias('aliasname')
self.assertEqual(r, 'realname')
r = self.env.resolve_alias('realname')
self.assertEqual(r, 'realname')
def test_format_output_is_json(self):
self.env.format = 'jsonraw'
self.assertTrue(self.env.format_output_is_json())
@mock.patch('rich.console.Console.print')
def test_multiple_tables(self, console):
tables = []
table1 = formatting.Table(["First", "Second"])
table1.add_row(["1", 2])
table2 = formatting.Table(["T2-1", "T2-2"])
table2.add_row(["zzzzzzz", "123123123"])
tables.append(table1)
tables.append(table2)
self.env.out(tables)
self.assertEqual(2, len(console.call_args_list))
|