1""" 2 SoftLayer.tests.CLI.environment_tests 3 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 4 5 :license: MIT, see LICENSE for more details. 6""" 7 8import click 9from unittest import mock as mock 10 11from SoftLayer.CLI import environment 12from SoftLayer import testing 13 14 15@click.command() 16def fixture_command(): 17 pass 18 19 20class EnvironmentTests(testing.TestCase): 21 22 def set_up(self): 23 self.env = environment.Environment() 24 25 def test_list_commands(self): 26 self.env.load() 27 actions = self.env.list_commands() 28 self.assertIn('virtual', actions) 29 self.assertIn('dns', actions) 30 31 def test_get_command_invalid(self): 32 cmd = self.env.get_command('invalid', 'command') 33 self.assertEqual(cmd, None) 34 35 def test_get_command(self): 36 fixture_loader = environment.ModuleLoader( 37 'tests.CLI.environment_tests', 38 'fixture_command', 39 ) 40 self.env.commands = {'fixture:run': fixture_loader} 41 command = self.env.get_command('fixture', 'run') 42 self.assertIsInstance(command, click.Command) 43 44 @mock.patch('click.prompt') 45 def test_input(self, prompt_mock): 46 r = self.env.input('input') 47 prompt_mock.assert_called_with('input', 48 default=None, 49 show_default=True) 50 self.assertEqual(prompt_mock(), r) 51 52 @mock.patch('click.prompt') 53 def test_getpass(self, prompt_mock): 54 r = self.env.getpass('input') 55 prompt_mock.assert_called_with('input', default=None, hide_input=True) 56 self.assertEqual(prompt_mock(), r) 57 58 @mock.patch('click.prompt') 59 @mock.patch('tkinter.Tk') 60 def test_getpass_issues1436(self, tk, prompt_mock): 61 prompt_mock.return_value = 'àR' 62 self.env.getpass('input') 63 prompt_mock.assert_called_with('input', default=None, hide_input=True) 64 tk.assert_called_with() 65 66 def test_resolve_alias(self): 67 self.env.aliases = {'aliasname': 'realname'} 68 r = self.env.resolve_alias('aliasname') 69 self.assertEqual(r, 'realname') 70 71 r = self.env.resolve_alias('realname') 72 self.assertEqual(r, 'realname') 73 74 @mock.patch('click.echo') 75 def test_print_unicode(self, echo): 76 output = "\u3010TEST\u3011 image" 77 # https://docs.python.org/3.6/library/exceptions.html#UnicodeError 78 echo.side_effect = [ 79 UnicodeEncodeError('utf8', output, 0, 1, "Test Exception"), 80 output 81 ] 82 self.env.fout(output) 83 self.assertEqual(2, echo.call_count) 84 85 def test_format_output_is_json(self): 86 self.env.format = 'jsonraw' 87 self.assertTrue(self.env.format_output_is_json()) 88