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 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
|
#!/usr/bin/env python3
# Copyright (c) Facebook, Inc. and its affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
#
from typing import List, Optional
from later.unittest import TestCase
from termcolor import cprint
from nubia import argument, command, deprecated
from tests.util import TestShell
class CommandSpecTest(TestCase):
async def test_command_sync(self):
@command
def test_command() -> int:
"""
Sample Docstring
"""
return 22
shell = TestShell(commands=[test_command])
self.assertEqual(22, await shell.run_cli_line("test_shell test-command "))
async def test_command_name_spec1(self):
@command
@argument("arg", description="argument help", aliases=["i"])
async def test_command(arg: List[str]) -> int:
"""
Sample Docstring
"""
self.assertEqual(["a", "b"], arg)
cprint(arg, "green")
return 22
shell = TestShell(commands=[test_command])
self.assertEqual(
22, await shell.run_cli_line("test_shell test-command --arg a b")
)
self.assertEqual(
22, await shell.run_interactive_line('test-command arg=["a","b"]')
)
self.assertEqual(
22, await shell.run_interactive_line("test-command arg=[a, b]")
)
async def test_command_name_spec2(self):
"""
Explicitly setting the command name with underscore, we should respect
the supplied name and not auto-transform it
"""
@command("bleh_command")
@argument("arg", description="argument help", aliases=["i"])
async def test_command(arg: List[str]) -> int:
"""
Sample Docstring
"""
self.assertEqual(["a", "b"], arg)
cprint(arg, "green")
return 22
shell = TestShell(commands=[test_command])
self.assertEqual(
22, await shell.run_cli_line("test_shell bleh_command --arg a b")
)
self.assertEqual(
22, await shell.run_interactive_line('bleh_command arg=["a","b"]')
)
self.assertEqual(
22, await shell.run_interactive_line("bleh_command arg=[a, b]")
)
async def test_command_async(self):
@command
@argument("arg", description="argument help", aliases=["i"])
async def test_command(arg: List[str]) -> int:
"""
Sample Docstring
"""
self.assertEqual(["a", "b"], arg)
cprint(arg, "green")
return 22
shell = TestShell(commands=[test_command])
self.assertEqual(
22, await shell.run_cli_line("test_shell test-command --arg a b")
)
self.assertEqual(
22, await shell.run_interactive_line('test-command arg=["a","b"]')
)
async def test_command_aliases_spec(self):
"""
Testing aliases
"""
@command("bleh_command", aliases=["bleh"])
@argument("arg", description="argument help", aliases=["i"])
async def test_command(arg: List[str]) -> int:
"""
Sample Docstring
"""
self.assertEqual(["a", "b"], arg)
cprint(arg, "green")
return 22
shell = TestShell(commands=[test_command])
self.assertEqual(22, await shell.run_cli_line("test_shell bleh -i a b"))
async def test_command_find_approx_spec(self):
"""
Testing approximate command / subcommand typing
"""
@command("command_first", aliases=["first"])
@argument("arg", description="argument help", aliases=["i"])
async def test_command_1(arg: int = 22) -> int:
"""
Sample Docstring
"""
cprint(arg, "green")
return arg
@command("command_second", aliases=["second"])
@argument("arg", description="argument help", aliases=["i"])
async def test_command_2(arg: int = 23) -> int:
"""
Sample Docstring
"""
cprint(arg, "green")
return arg
shell = TestShell(commands=[test_command_1, test_command_2])
# correct command name
self.assertEqual(22, await shell.run_interactive_line("first"))
# unique prefix command name
self.assertEqual(22, await shell.run_interactive_line("f"))
# unique levenshtein command name
self.assertEqual(22, await shell.run_interactive_line("firts"))
# unique prefix + levenshtein command name
self.assertEqual(22, await shell.run_interactive_line("firs"))
# non-unique prefix command name
self.assertEqual(None, await shell.run_interactive_line("command"))
# approximate matching only works for interactive mode, not CLI
self.assertEqual(22, await shell.run_cli_line("test_shell first"))
with self.assertRaises(SystemExit):
await shell.run_cli_line("test_shell f")
with self.assertRaises(SystemExit):
await shell.run_cli_line("test_shell firts")
with self.assertRaises(SystemExit):
await shell.run_cli_line("test_shell firs")
with self.assertRaises(SystemExit):
await shell.run_cli_line("test_shell command")
async def test_no_type_works_the_same(self):
@command
@argument("arg", positional=True)
async def test_command(arg: str) -> int:
"""
Sample Docstring
"""
self.assertIsInstance(arg, str)
self.assertEqual("1", arg)
return 64 + int(arg)
shell = TestShell(commands=[test_command])
self.assertEqual(65, await shell.run_cli_line("test_shell test-command 1"))
self.assertEqual(65, await shell.run_interactive_line("test-command 1"))
self.assertEqual(65, await shell.run_interactive_line('test-command "1"'))
@command
@argument("arg")
async def test_command(arg: str) -> int:
"""
Sample Docstring
"""
self.assertIsInstance(arg, str)
self.assertEqual("1", arg)
return 64 + int(arg)
shell = TestShell(commands=[test_command])
self.assertEqual(
65, await shell.run_cli_line("test_shell test-command --arg 1")
)
self.assertEqual(
65,
await shell.run_interactive_line("test-command arg=1"),
)
self.assertEqual(
65,
await shell.run_interactive_line('test-command arg="1"'),
)
async def test_command_with_postional(self):
@command
@argument("arg1", positional=True)
@argument("arg2", positional=True)
@argument("arg3", positional=True)
async def test_command(arg1: str, arg2: str, arg3: str) -> int:
"""
Sample Docstring
"""
cprint([arg1, arg2])
self.assertEqual("1", arg1)
self.assertIsInstance(arg1, str)
self.assertEqual("2", arg2)
self.assertIsInstance(arg2, str)
self.assertEqual("nubia", arg3)
return 64 * int(arg1) + int(arg2)
shell = TestShell(commands=[test_command])
self.assertEqual(
66, await shell.run_cli_line("test_shell test-command 1 2 nubia")
)
self.assertEqual(66, await shell.run_interactive_line("test-command 1 2 nubia"))
async def test_command_with_extra_spaces(self):
@command
@argument("arg1", positional=True)
async def test_command(arg1: str) -> None:
"""
Sample Docstring
"""
self.assertEqual("1", arg1)
self.assertIsInstance(arg1, str)
return True
shell = TestShell(commands=[test_command])
self.assertTrue(await shell.run_interactive_line("test-command 1"))
self.assertTrue(await shell.run_interactive_line("test-command 1"))
self.assertTrue(await shell.run_interactive_line("test-command 1"))
self.assertTrue(await shell.run_interactive_line(" test-command 1"))
self.assertTrue(await shell.run_interactive_line(" test-command 1"))
self.assertTrue(await shell.run_interactive_line("test-command 1 "))
self.assertTrue(await shell.run_interactive_line("test-command 1 "))
self.assertTrue(await shell.run_interactive_line(" test-command 1 "))
async def test_command_with_postional_and_named_arguments(self):
@command
@argument("arg2", positional=True)
@argument("arg3", positional=True)
async def test_command(arg1: str, arg2: str, arg3: str) -> int:
"""
Sample Docstring
"""
cprint([arg1, arg2])
self.assertEqual("1", arg1)
self.assertIsInstance(arg1, str)
self.assertEqual("2", arg2)
self.assertIsInstance(arg2, str)
self.assertEqual("nubia", arg3)
return 64 * int(arg1) + int(arg2)
shell = TestShell(commands=[test_command])
self.assertEqual(
66, await shell.run_cli_line("test_shell test-command --arg1=1 2 nubia")
)
self.assertEqual(
66, await shell.run_interactive_line("test-command arg1=1 2 nubia")
)
self.assertEqual(
66, await shell.run_interactive_line("test-command arg1=1 arg2=2 nubia")
)
# Fails parsing because positionals have to be at the end
self.assertEqual(
1, await shell.run_interactive_line("test-command 2 nubia arg1=1")
)
async def test_command_with_mutex_groups(self):
@command(exclusive_arguments=["arg1", "arg2"])
@argument("arg1")
@argument("arg2")
async def test_command(arg1: str = "0", arg2: str = "0") -> int:
"""
Sample Docstring
"""
return 64 * int(arg1) + int(arg2)
shell = TestShell(commands=[test_command])
self.assertEqual(
64, await shell.run_cli_line("test_shell test-command --arg1 1")
)
self.assertEqual(
64,
await shell.run_interactive_line("test-command arg1=1"),
)
self.assertEqual(
2, await shell.run_cli_line("test_shell test-command --arg2 2")
)
self.assertEqual(
2,
await shell.run_interactive_line("test-command arg2=2"),
)
with self.assertRaises(SystemExit):
await shell.run_cli_line("test_shell test-command --arg1 1 --arg2 2")
self.assertEqual(
66,
await shell.run_interactive_line("test-command arg1=1 arg2=2"),
"We are not enforsing mutex groups on interactive",
)
async def test_command_with_mutex_groups_two_positionals(self):
msg = "We don't supporting mutex group with required arguments"
with self.assertRaises(ValueError, msg=msg):
@command(exclusive_arguments=["arg1", "arg2"])
@argument("arg1", positional=True)
@argument("arg2")
async def test_command(arg1: str, arg2: str = "lalala") -> int:
"""
Sample Docstring
"""
return -1
await TestShell(commands=[test_command]).run_async()
async def test_command_default_argument(self):
"""
Tests that calling a command from the CLI without all arguments
specified will fall back to the default arguments set in the command
definition.
"""
@command
@argument("arg", description="argument help", aliases=["i"])
async def test_command(arg: int = 22) -> int:
"""
Sample Docstring
"""
cprint(arg, "green")
return arg
shell = TestShell(commands=[test_command])
self.assertEqual(22, await shell.run_cli_line("test_shell test-command"))
self.assertEqual(22, await shell.run_interactive_line("test-command"))
async def test_command_optional_argument(self):
"""
Same as above but check for make the argument optional in Python sense.
"""
@command
@argument("arg", description="argument help", aliases=["i"])
async def test_command(arg: Optional[List[str]] = None) -> int:
"""
Sample Docstring
"""
arg = arg or ["42"]
cprint(arg, "green")
return sum(int(x) for x in arg)
shell = TestShell(commands=[test_command])
self.assertEqual(42, await shell.run_cli_line("test_shell test-command"))
self.assertEqual(42, await shell.run_interactive_line("test-command"))
self.assertEqual(0, await shell.run_cli_line("test_shell test-command --arg 0"))
self.assertEqual(
0,
await shell.run_interactive_line("test-command arg=[0]"),
)
async def test_command_one_required_one_default_argument(self):
"""
Tests that calling a command from the CLI without all arguments
specified will fall back to the default arguments set in the command
definition.
"""
@command("bleh_command")
@argument("arg1", description="argument help", aliases=["i1"])
@argument("arg2", description="argument 2 help", aliases=["i2"])
async def test_command(arg1: int, arg2: int = 1) -> int:
"""
Sample Docstring
"""
cprint(arg1, "green")
return arg1 + arg2
shell = TestShell(commands=[test_command])
self.assertEqual(
22, await shell.run_cli_line("test_shell bleh_command --arg1=21")
)
self.assertEqual(
22,
await shell.run_interactive_line("bleh_command arg1=21"),
)
async def test_command_for_blacklist_plugin_allowed(self):
@command("allowed")
async def test_command():
"""
Sample Docstring
"""
cprint("Command Executed as required", "green")
return 42
shell = TestShell(commands=[test_command])
self.assertEqual(42, await shell.run_cli_line("test_shell allowed"))
self.assertEqual(42, await shell.run_interactive_line("allowed"))
async def test_command_for_blacklist_plugin_blacklisted(self):
@command("blocked")
async def test_command():
"""
Sample Docstring
"""
cprint("Command executed, but should be blocked", "red")
return 3
shell = TestShell(commands=[test_command])
self.assertEqual(1, await shell.run_cli_line("test_shell blocked"))
self.assertEqual(1, await shell.run_interactive_line("blocked"))
async def test_command_with_negative_ints(self):
@command("minus_command")
@argument("arg1", type=int)
async def test_command(arg1):
"""
Sample Docstring
"""
self.assertEqual(type(5), type(arg1))
return 42 if arg1 == -1 else -1
shell = TestShell(commands=[test_command])
# Cli run
self.assertEqual(
42, await shell.run_cli_line("test_shell minus_command --arg1=-1")
)
# Interactive
self.assertEqual(42, await shell.run_interactive_line("minus_command arg1=-1"))
async def test_command_with_negative_floats(self):
@command("minus_command")
@argument("arg1", type=float)
async def test_command(arg1):
"""
Sample Docstring
"""
self.assertEqual(type(5.0), type(arg1))
return 42 if arg1 == -1.0 else 55
shell = TestShell(commands=[test_command])
# Cli run
self.assertEqual(
42, await shell.run_cli_line("test_shell minus_command --arg1=-1")
)
self.assertEqual(
42, await shell.run_cli_line("test_shell minus_command --arg1=-1.0")
)
# Interactive
self.assertEqual(42, await shell.run_interactive_line("minus_command arg1=-1"))
self.assertEqual(
42, await shell.run_interactive_line("minus_command arg1=-1.0")
)
async def test_command_deprecation(self):
@deprecated(superseded_by="new-command")
@command
def old_command() -> int:
"""
Sample Docstring
"""
cprint("This command is deprecated", "yellow")
return new_command()
@command
def new_command() -> int:
"""
Sample Docstring
"""
cprint("This is the future", "green")
return 42
shell = TestShell(commands=[old_command, new_command])
self.assertEqual(42, await shell.run_cli_line("test_shell old-command"))
self.assertEqual(42, await shell.run_interactive_line("old-command"))
self.assertEqual(42, await shell.run_cli_line("test_shell new-command"))
self.assertEqual(42, await shell.run_interactive_line("new-command"))
async def test_type_lifting(self):
@command
@argument("args")
async def test_command(args: List[str]) -> str:
"""
Sample Docstring
"""
return "|".join(args)
shell = TestShell(commands=[test_command])
# CLI
self.assertEqual(
"a", await shell.run_cli_line("test_shell test-command --args a")
)
self.assertEqual(
"a|b", await shell.run_cli_line("test_shell test-command --args a b")
)
# Interactive
self.assertEqual("a", await shell.run_interactive_line('test-command args="a"'))
self.assertEqual(
"a", await shell.run_interactive_line('test-command args=["a"]')
)
self.assertEqual(
"a|b", await shell.run_interactive_line('test-command args=["a", "b"]')
)
|