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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
# --------------------------------------------------------------------------------------------
# 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, disable_color
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 TestCommandPreview(unittest.TestCase):
def setUp(self):
from knack.help_files import helps
class PreviewTestCommandLoader(CLICommandsLoader):
def load_command_table(self, args):
super().load_command_table(args)
with CommandGroup(self, '', '{}#{{}}'.format(__name__)) as g:
g.command('cmd1', 'example_handler', is_preview=True)
with CommandGroup(self, 'grp1', '{}#{{}}'.format(__name__), is_preview=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().load_arguments(command)
helps['grp1'] = """
type: group
short-summary: A group.
"""
self.cli_ctx = DummyCLI(commands_loader_cls=PreviewTestCommandLoader)
@redirect_io
def test_preview_command_group_help(self):
""" Ensure preview 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 [Preview] : A group.
Commands:
cmd1 [Preview] : Short summary here.
""".format(self.cli_ctx.name)
assert_in_multi_line(expected, actual)
@redirect_io
def test_preview_command_plain_execute(self):
""" Ensure general warning displayed when running preview command. """
self.cli_ctx.invoke('cmd1 -b b'.split())
actual = self.io.getvalue()
expected = "This command is in preview. It may be changed/removed in a future release."
self.assertIn(expected, actual)
@redirect_io
def test_preview_command_plain_execute_only_show_error(self):
""" Ensure warning is suppressed when running preview command. """
# Directly use --only-show-errors
self.cli_ctx.invoke('cmd1 -b b --only-show-errors'.split())
actual = self.io.getvalue()
self.assertNotIn("preview", actual)
# Apply --only-show-errors with config
self.cli_ctx.only_show_errors = True
self.cli_ctx.config.set_value('core', 'only_show_errors', 'True')
self.cli_ctx.invoke('cmd1 -b b'.split())
actual = self.io.getvalue()
self.assertNotIn("preview", actual)
self.cli_ctx.config.set_value('core', 'only_show_errors', '')
self.cli_ctx.only_show_errors = False
@redirect_io
@disable_color
def test_preview_command_plain_execute_no_color(self):
""" Ensure warning is displayed without color. """
self.cli_ctx.invoke('cmd1 -b b'.split())
actual = self.io.getvalue()
self.assertIn("WARNING: This command is in preview. It may be changed/removed in a future release.", actual)
@redirect_io
def test_preview_command_implicitly_execute(self):
""" Ensure general warning displayed when running command from a preview parent group. """
self.cli_ctx.invoke('grp1 cmd1 -b b'.split())
actual = self.io.getvalue()
expected = "Command group 'grp1' is in preview. It may be changed/removed in a future release."
self.assertIn(expected, actual)
@redirect_io
@disable_color
def test_preview_command_implicitly_no_color(self):
""" Ensure warning is displayed without color. """
self.cli_ctx.invoke('grp1 cmd1 -b b'.split())
actual = self.io.getvalue()
expected = "WARNING: Command group 'grp1' is in preview. It may be changed/removed in a future release."
self.assertIn(expected, actual)
class TestCommandGroupPreview(unittest.TestCase):
def setUp(self):
from knack.help_files import helps
class PreviewTestCommandLoader(CLICommandsLoader):
def load_command_table(self, args):
super().load_command_table(args)
with CommandGroup(self, 'group1', '{}#{{}}'.format(__name__), is_preview=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().load_arguments(command)
helps['group1'] = """
type: group
short-summary: A group.
"""
self.cli_ctx = DummyCLI(commands_loader_cls=PreviewTestCommandLoader)
@redirect_io
def test_preview_command_group_help_plain(self):
""" Ensure help warnings appear for preview 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 in preview. It may be changed/removed in a future release.
Commands:
cmd1 : Short summary here.
""".format(self.cli_ctx.name)
assert_in_multi_line(expected, actual)
@redirect_io
@disable_color
def test_preview_command_group_help_plain_no_color(self):
""" Ensure warning is displayed without color. """
with self.assertRaises(SystemExit):
self.cli_ctx.invoke('group1 -h'.split())
actual = self.io.getvalue()
expected = """
Group
cli group1 : A group.
WARNING: This command group is in preview. It may be changed/removed in a future release.
Commands:
cmd1 : Short summary here.
""".format(self.cli_ctx.name)
self.assertEqual(expected, actual)
@redirect_io
def test_preview_command_implicitly(self):
""" Ensure help warning displayed for command in preview because of a preview 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 in preview. It may be changed/removed in a future
release.
""".format(self.cli_ctx.name)
assert_in_multi_line(expected, actual)
class TestArgumentPreview(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 PreviewTestCommandLoader(CLICommandsLoader):
def load_command_table(self, args):
super().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_preview=True, action=LoggerAction)
super().load_arguments(command)
helps['grp1'] = """
type: group
short-summary: A group.
"""
self.cli_ctx = DummyCLI(commands_loader_cls=PreviewTestCommandLoader)
@redirect_io
def test_preview_arguments_command_help(self):
""" Ensure preview 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 [Preview] [Required] : Arg1.
Argument '--arg1' is in preview. It may be changed/removed in a future release.
""".format(self.cli_ctx.name)
assert_in_multi_line(expected, actual)
@redirect_io
@disable_color
def test_preview_arguments_command_help_no_color(self):
""" Ensure warning is displayed without color. """
with self.assertRaises(SystemExit):
self.cli_ctx.invoke('arg-test -h'.split())
actual = self.io.getvalue()
expected = """
Arguments
--arg1 [Preview] [Required] : Arg1.
WARNING: Argument '--arg1' is in preview. It may be changed/removed in a future release.
""".format(self.cli_ctx.name)
self.assertIn(expected, actual)
@redirect_io
def test_preview_arguments_execute(self):
""" Ensure deprecated arguments can be used. """
self.cli_ctx.invoke('arg-test --arg1 foo --opt1 bar'.split())
actual = self.io.getvalue()
preview_expected = "Argument '--arg1' is in preview. It may be changed/removed in a future release."
self.assertIn(preview_expected, actual)
action_expected = "Side-effect from some original action!"
self.assertIn(action_expected, actual)
@redirect_io
@disable_color
def test_preview_arguments_execute_no_color(self):
""" Ensure warning is displayed without color. """
self.cli_ctx.invoke('arg-test --arg1 foo --opt1 bar'.split())
actual = self.io.getvalue()
preview_expected = "WARNING: Argument '--arg1' is in preview. It may be changed/removed in a future release."
self.assertIn(preview_expected, actual)
action_expected = "Side-effect from some original action!"
self.assertIn(action_expected, actual)
@redirect_io
def test_preview_arguments_execute_only_show_error(self):
""" Ensure warning is suppressed when using preview arguments. """
self.cli_ctx.invoke('arg-test --arg1 foo --opt1 bar --only-show-errors'.split())
actual = self.io.getvalue()
self.assertNotIn("preview", actual)
action_expected = "Side-effect from some original action!"
self.assertIn(action_expected, actual)
if __name__ == '__main__':
unittest.main()
|