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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
import unittest
from unittest import mock
import sys
import argparse
from knack.arguments import ArgumentsContext
from knack.commands import CLICommandsLoader, CommandGroup
from tests.util import DummyCLI, redirect_io, assert_in_multi_line
def example_handler(arg1, arg2=None, arg3=None):
""" Short summary here. Long summary here. Still long summary. """
pass
def example_arg_handler(arg1, opt1, arg2=None, opt2=None, arg3=None,
opt3=None, arg4=None, opt4=None, arg5=None, opt5=None):
pass
class TestCommandExperimental(unittest.TestCase):
def setUp(self):
from knack.help_files import helps
class ExperimentalTestCommandLoader(CLICommandsLoader):
def load_command_table(self, args):
super(ExperimentalTestCommandLoader, self).load_command_table(args)
with CommandGroup(self, '', '{}#{{}}'.format(__name__)) as g:
g.command('cmd1', 'example_handler', is_experimental=True)
with CommandGroup(self, 'grp1', '{}#{{}}'.format(__name__), is_experimental=True) as g:
g.command('cmd1', 'example_handler')
return self.command_table
def load_arguments(self, command):
with ArgumentsContext(self, '') as c:
c.argument('arg1', options_list=['--arg', '-a'], required=False, type=int, choices=[1, 2, 3])
c.argument('arg2', options_list=['-b'], required=True, choices=['a', 'b', 'c'])
super(ExperimentalTestCommandLoader, self).load_arguments(command)
helps['grp1'] = """
type: group
short-summary: A group.
"""
self.cli_ctx = DummyCLI(commands_loader_cls=ExperimentalTestCommandLoader)
@redirect_io
def test_experimental_command_implicitly_execute(self):
""" Ensure general warning displayed when running command from an experimental parent group. """
self.cli_ctx.invoke('grp1 cmd1 -b b'.split())
actual = self.io.getvalue()
expected = "Command group 'grp1' is experimental and under development."
self.assertIn(expected, actual)
@redirect_io
def test_experimental_command_group_help(self):
""" Ensure experimental commands appear correctly in group help view. """
with self.assertRaises(SystemExit):
self.cli_ctx.invoke('-h'.split())
actual = self.io.getvalue()
expected = """
Group
{}
Subgroups:
grp1 [Experimental] : A group.
Commands:
cmd1 [Experimental] : Short summary here.
""".format(self.cli_ctx.name)
assert_in_multi_line(expected, actual)
@redirect_io
def test_experimental_command_plain_execute(self):
""" Ensure general warning displayed when running experimental command. """
self.cli_ctx.invoke('cmd1 -b b'.split())
actual = self.io.getvalue()
expected = "This command is experimental and under development."
self.assertIn(expected, actual)
class TestCommandGroupExperimental(unittest.TestCase):
def setUp(self):
from knack.help_files import helps
class ExperimentalTestCommandLoader(CLICommandsLoader):
def load_command_table(self, args):
super(ExperimentalTestCommandLoader, self).load_command_table(args)
with CommandGroup(self, 'group1', '{}#{{}}'.format(__name__), is_experimental=True) as g:
g.command('cmd1', 'example_handler')
return self.command_table
def load_arguments(self, command):
with ArgumentsContext(self, '') as c:
c.argument('arg1', options_list=['--arg', '-a'], required=False, type=int, choices=[1, 2, 3])
c.argument('arg2', options_list=['-b'], required=True, choices=['a', 'b', 'c'])
super(ExperimentalTestCommandLoader, self).load_arguments(command)
helps['group1'] = """
type: group
short-summary: A group.
"""
self.cli_ctx = DummyCLI(commands_loader_cls=ExperimentalTestCommandLoader)
@redirect_io
def test_experimental_command_group_help_plain(self):
""" Ensure help warnings appear for experimental command group help. """
with self.assertRaises(SystemExit):
self.cli_ctx.invoke('group1 -h'.split())
actual = self.io.getvalue()
expected = """
Group
cli group1 : A group.
This command group is experimental and under development.
Commands:
cmd1 : Short summary here.
""".format(self.cli_ctx.name)
assert_in_multi_line(expected, actual)
@redirect_io
def test_experimental_command_implicitly(self):
""" Ensure help warning displayed for command in experimental because of a experimental parent group. """
with self.assertRaises(SystemExit):
self.cli_ctx.invoke('group1 cmd1 -h'.split())
actual = self.io.getvalue()
expected = """
Command
{} group1 cmd1 : Short summary here.
Long summary here. Still long summary.
Command group 'group1' is experimental and under development.
""".format(self.cli_ctx.name)
assert_in_multi_line(expected, actual)
class TestArgumentExperimental(unittest.TestCase):
def setUp(self):
from knack.help_files import helps
class LoggerAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
print("Side-effect from some original action!", file=sys.stderr)
class ExperimentalTestCommandLoader(CLICommandsLoader):
def load_command_table(self, args):
super(ExperimentalTestCommandLoader, self).load_command_table(args)
with CommandGroup(self, '', '{}#{{}}'.format(__name__)) as g:
g.command('arg-test', 'example_arg_handler')
return self.command_table
def load_arguments(self, command):
with ArgumentsContext(self, 'arg-test') as c:
c.argument('arg1', help='Arg1', is_experimental=True, action=LoggerAction)
super(ExperimentalTestCommandLoader, self).load_arguments(command)
helps['grp1'] = """
type: group
short-summary: A group.
"""
self.cli_ctx = DummyCLI(commands_loader_cls=ExperimentalTestCommandLoader)
@redirect_io
def test_experimental_arguments_command_help(self):
""" Ensure experimental arguments appear correctly in command help view. """
with self.assertRaises(SystemExit):
self.cli_ctx.invoke('arg-test -h'.split())
actual = self.io.getvalue()
expected = """
Arguments
--arg1 [Experimental] [Required] : Arg1.
Argument '--arg1' is experimental and under development.
""".format(self.cli_ctx.name)
assert_in_multi_line(expected, actual)
@redirect_io
def test_experimental_arguments_execute(self):
""" Ensure deprecated arguments can be used. """
self.cli_ctx.invoke('arg-test --arg1 foo --opt1 bar'.split())
actual = self.io.getvalue()
experimental_expected = "Argument '--arg1' is experimental and under development."
self.assertIn(experimental_expected, actual)
action_expected = "Side-effect from some original action!"
self.assertIn(action_expected, actual)
if __name__ == '__main__':
unittest.main()
|