| 1 |
from unittest.mock import Mock |
| 2 |
|
| 3 |
from _util import support |
| 4 |
from pytest import raises, skip |
| 5 |
|
| 6 |
from invoke import Call, Collection, Config, Context, Task, task |
| 7 |
from invoke import FilesystemLoader as Loader |
| 8 |
from invoke.parser.parser import ParseResult |
| 9 |
|
| 10 |
# |
| 11 |
# NOTE: Most Task tests use @task as it's the primary interface and is a very |
| 12 |
# thin wrapper around Task itself. This way we don't have to write 2x tests for |
| 13 |
# both Task and @task. Meh :) |
| 14 |
# |
| 15 |
|
| 16 |
|
| 17 |
def _func(c): |
| 18 |
pass |
| 19 |
|
| 20 |
|
| 21 |
class task_: |
| 22 |
"@task" |
| 23 |
|
| 24 |
def _load(self, name): |
| 25 |
mod, _ = self.loader.load(name) |
| 26 |
return Collection.from_module(mod) |
| 27 |
|
| 28 |
def setup_method(self): |
| 29 |
self.loader = Loader(start=support) |
| 30 |
self.vanilla = self._load("decorators") |
| 31 |
|
| 32 |
def allows_access_to_wrapped_object(self): |
| 33 |
def lolcats(c): |
| 34 |
pass |
| 35 |
|
| 36 |
assert task(lolcats).body == lolcats |
| 37 |
|
| 38 |
def allows_alias_specification(self): |
| 39 |
assert self.vanilla["foo"] == self.vanilla["bar"] |
| 40 |
|
| 41 |
def allows_multiple_aliases(self): |
| 42 |
assert self.vanilla["foo"] == self.vanilla["otherbar"] |
| 43 |
|
| 44 |
def allows_default_specification(self): |
| 45 |
assert self.vanilla[""] == self.vanilla["biz"] |
| 46 |
|
| 47 |
def has_autoprint_option(self): |
| 48 |
ap = self._load("autoprint") |
| 49 |
assert ap["nope"].autoprint is False |
| 50 |
assert ap["yup"].autoprint is True |
| 51 |
|
| 52 |
def raises_ValueError_on_multiple_defaults(self): |
| 53 |
with raises(ValueError): |
| 54 |
self._load("decorator_multi_default") |
| 55 |
|
| 56 |
def sets_arg_kind(self): |
| 57 |
skip() |
| 58 |
|
| 59 |
def sets_which_args_are_optional(self): |
| 60 |
assert self.vanilla["optional_values"].optional == ("myopt",) |
| 61 |
|
| 62 |
def allows_annotating_args_as_positional(self): |
| 63 |
assert self.vanilla["one_positional"].positional == ["pos"] |
| 64 |
assert self.vanilla["two_positionals"].positional == ["pos1", "pos2"] |
| 65 |
|
| 66 |
def allows_annotating_args_as_iterable(self): |
| 67 |
assert self.vanilla["iterable_values"].iterable == ["mylist"] |
| 68 |
|
| 69 |
def allows_annotating_args_as_incrementable(self): |
| 70 |
arg = self.vanilla["incrementable_values"] |
| 71 |
assert arg.incrementable == ["verbose"] |
| 72 |
|
| 73 |
def when_positional_arg_missing_all_non_default_args_are_positional(self): |
| 74 |
arg = self.vanilla["implicit_positionals"] |
| 75 |
assert arg.positional == ["pos1", "pos2"] |
| 76 |
|
| 77 |
def context_arguments_should_not_appear_in_implicit_positional_list(self): |
| 78 |
@task |
| 79 |
def mytask(c): |
| 80 |
pass |
| 81 |
|
| 82 |
assert len(mytask.positional) == 0 |
| 83 |
|
| 84 |
def pre_tasks_stored_directly(self): |
| 85 |
@task |
| 86 |
def whatever(c): |
| 87 |
pass |
| 88 |
|
| 89 |
@task(pre=[whatever]) |
| 90 |
def func(c): |
| 91 |
pass |
| 92 |
|
| 93 |
assert func.pre == [whatever] |
| 94 |
|
| 95 |
def allows_star_args_as_shortcut_for_pre(self): |
| 96 |
@task |
| 97 |
def pre1(c): |
| 98 |
pass |
| 99 |
|
| 100 |
@task |
| 101 |
def pre2(c): |
| 102 |
pass |
| 103 |
|
| 104 |
@task(pre1, pre2) |
| 105 |
def func(c): |
| 106 |
pass |
| 107 |
|
| 108 |
assert func.pre == (pre1, pre2) |
| 109 |
|
| 110 |
def disallows_ambiguity_between_star_args_and_pre_kwarg(self): |
| 111 |
@task |
| 112 |
def pre1(c): |
| 113 |
pass |
| 114 |
|
| 115 |
@task |
| 116 |
def pre2(c): |
| 117 |
pass |
| 118 |
|
| 119 |
with raises(TypeError): |
| 120 |
|
| 121 |
@task(pre1, pre=[pre2]) |
| 122 |
def func(c): |
| 123 |
pass |
| 124 |
|
| 125 |
def sets_name(self): |
| 126 |
@task(name="foo") |
| 127 |
def bar(c): |
| 128 |
pass |
| 129 |
|
| 130 |
assert bar.name == "foo" |
| 131 |
|
| 132 |
def returns_Task_instances_by_default(self): |
| 133 |
@task |
| 134 |
def mytask(c): |
| 135 |
pass |
| 136 |
|
| 137 |
assert isinstance(mytask, Task) |
| 138 |
|
| 139 |
def klass_kwarg_allows_overriding_class_used(self): |
| 140 |
class MyTask(Task): |
| 141 |
pass |
| 142 |
|
| 143 |
@task(klass=MyTask) |
| 144 |
def mytask(c): |
| 145 |
pass |
| 146 |
|
| 147 |
assert isinstance(mytask, MyTask) |
| 148 |
|
| 149 |
def klass_kwarg_works_for_subclassers_without_kwargs(self): |
| 150 |
# I.e. the previous test doesn't catch this particular use case |
| 151 |
class MyTask(Task): |
| 152 |
pass |
| 153 |
|
| 154 |
def uses_MyTask(*args, **kwargs): |
| 155 |
kwargs.setdefault("klass", MyTask) |
| 156 |
return task(*args, **kwargs) |
| 157 |
|
| 158 |
@uses_MyTask |
| 159 |
def mytask(c): |
| 160 |
pass |
| 161 |
|
| 162 |
assert isinstance(mytask, MyTask) |
| 163 |
|
| 164 |
def unknown_kwargs_get_mad_at_Task_level(self): |
| 165 |
# NOTE: this was previously untested behavior. We actually just |
| 166 |
# modified HOW TypeError gets raised (Task constructor, implicitly, vs |
| 167 |
# explicitly in @task itself) but the end result is the same for anyone |
| 168 |
# not trying to be stringly typed based on exception message. |
| 169 |
with raises(TypeError): |
| 170 |
|
| 171 |
@task(whatever="man") |
| 172 |
def mytask(c): |
| 173 |
pass |
| 174 |
|
| 175 |
|
| 176 |
class Task_: |
| 177 |
def has_useful_repr(self): |
| 178 |
i = repr(Task(_func)) |
| 179 |
assert "_func" in i, "'func' not found in {!r}".format(i) |
| 180 |
e = repr(Task(_func, name="funky")) |
| 181 |
assert "funky" in e, "'funky' not found in {!r}".format(e) |
| 182 |
assert "_func" not in e, "'_func' unexpectedly seen in {!r}".format(e) |
| 183 |
|
| 184 |
def equality_testing(self): |
| 185 |
t1 = Task(_func, name="foo") |
| 186 |
t2 = Task(_func, name="foo") |
| 187 |
assert t1 == t2 |
| 188 |
t3 = Task(_func, name="bar") |
| 189 |
assert t1 != t3 |
| 190 |
|
| 191 |
def equality_testing_false_for_non_task_objects(self): |
| 192 |
t = Task(_func, name="foo") |
| 193 |
# No name attribute at all |
| 194 |
assert t != object() |
| 195 |
|
| 196 |
# Name attr, but not a Task |
| 197 |
class Named: |
| 198 |
name = "foo" |
| 199 |
|
| 200 |
assert t != Named() |
| 201 |
|
| 202 |
class function_like_behavior: |
| 203 |
# Things that help them eg show up in autodoc easier |
| 204 |
def inherits_module_from_body(self): |
| 205 |
mytask = Task(_func, name="funky") |
| 206 |
assert mytask.__module__ is _func.__module__ |
| 207 |
|
| 208 |
class attributes: |
| 209 |
def has_default_flag(self): |
| 210 |
assert Task(_func).is_default is False |
| 211 |
|
| 212 |
def name_defaults_to_body_name(self): |
| 213 |
assert Task(_func).name == "_func" |
| 214 |
|
| 215 |
def can_override_name(self): |
| 216 |
assert Task(_func, name="foo").name == "foo" |
| 217 |
|
| 218 |
class callability: |
| 219 |
def setup_method(self): |
| 220 |
@task |
| 221 |
def foo(c): |
| 222 |
"My docstring" |
| 223 |
return 5 |
| 224 |
|
| 225 |
self.task = foo |
| 226 |
|
| 227 |
def dunder_call_wraps_body_call(self): |
| 228 |
context = Context() |
| 229 |
assert self.task(context) == 5 |
| 230 |
|
| 231 |
def errors_if_first_arg_not_Context(self): |
| 232 |
@task |
| 233 |
def mytask(c): |
| 234 |
pass |
| 235 |
|
| 236 |
with raises(TypeError): |
| 237 |
mytask(5) |
| 238 |
|
| 239 |
def errors_if_no_first_arg_at_all(self): |
| 240 |
with raises(TypeError): |
| 241 |
|
| 242 |
@task |
| 243 |
def mytask(): |
| 244 |
pass |
| 245 |
|
| 246 |
def tracks_times_called(self): |
| 247 |
context = Context() |
| 248 |
assert self.task.called is False |
| 249 |
self.task(context) |
| 250 |
assert self.task.called is True |
| 251 |
assert self.task.times_called == 1 |
| 252 |
self.task(context) |
| 253 |
assert self.task.times_called == 2 |
| 254 |
|
| 255 |
def wraps_body_docstring(self): |
| 256 |
assert self.task.__doc__ == "My docstring" |
| 257 |
|
| 258 |
def wraps_body_name(self): |
| 259 |
assert self.task.__name__ == "foo" |
| 260 |
|
| 261 |
class get_arguments: |
| 262 |
def setup_method(self): |
| 263 |
@task(positional=["arg_3", "arg1"], optional=["arg1"]) |
| 264 |
def mytask(c, arg1, arg2=False, arg_3=5): |
| 265 |
pass |
| 266 |
|
| 267 |
self.task = mytask |
| 268 |
self.args = self.task.get_arguments() |
| 269 |
self.argdict = self._arglist_to_dict(self.args) |
| 270 |
|
| 271 |
def _arglist_to_dict(self, arglist): |
| 272 |
# This kinda duplicates Context.add_arg(x) for x in arglist :( |
| 273 |
ret = {} |
| 274 |
for arg in arglist: |
| 275 |
for name in arg.names: |
| 276 |
ret[name] = arg |
| 277 |
return ret |
| 278 |
|
| 279 |
def _task_to_dict(self, task): |
| 280 |
return self._arglist_to_dict(task.get_arguments()) |
| 281 |
|
| 282 |
def positional_args_come_first(self): |
| 283 |
assert self.args[0].name == "arg_3" |
| 284 |
assert self.args[1].name == "arg1" |
| 285 |
assert self.args[2].name == "arg2" |
| 286 |
|
| 287 |
def kinds_are_preserved(self): |
| 288 |
# Remember that the default 'kind' is a string. |
| 289 |
assert [x.kind for x in self.args] == [int, str, bool] |
| 290 |
|
| 291 |
def positional_flag_is_preserved(self): |
| 292 |
assert [x.positional for x in self.args] == [True, True, False] |
| 293 |
|
| 294 |
def optional_flag_is_preserved(self): |
| 295 |
assert [x.optional for x in self.args] == [False, True, False] |
| 296 |
|
| 297 |
def optional_prevents_bool_defaults_from_affecting_kind(self): |
| 298 |
# Re #416. See notes in the function under test for rationale. |
| 299 |
@task(optional=["myarg"]) |
| 300 |
def mytask(c, myarg=False): |
| 301 |
pass |
| 302 |
|
| 303 |
arg = mytask.get_arguments()[0] |
| 304 |
assert arg.kind is str # not bool! |
| 305 |
|
| 306 |
def optional_plus_nonbool_default_does_not_override_kind(self): |
| 307 |
@task(optional=["myarg"]) |
| 308 |
def mytask(c, myarg=17): |
| 309 |
pass |
| 310 |
|
| 311 |
arg = mytask.get_arguments()[0] |
| 312 |
assert arg.kind is int # not str! |
| 313 |
|
| 314 |
def turns_function_signature_into_Arguments(self): |
| 315 |
assert len(self.args), 3 == str(self.args) |
| 316 |
assert "arg2" in self.argdict |
| 317 |
|
| 318 |
def shortflags_created_by_default(self): |
| 319 |
assert "a" in self.argdict |
| 320 |
assert self.argdict["a"] is self.argdict["arg1"] |
| 321 |
|
| 322 |
def shortflags_dont_care_about_positionals(self): |
| 323 |
"Positionalness doesn't impact whether shortflags are made" |
| 324 |
for short, long_ in (("a", "arg1"), ("r", "arg2"), ("g", "arg-3")): |
| 325 |
assert self.argdict[short] is self.argdict[long_] |
| 326 |
|
| 327 |
def autocreated_short_flags_can_be_disabled(self): |
| 328 |
@task(auto_shortflags=False) |
| 329 |
def mytask(c, arg): |
| 330 |
pass |
| 331 |
|
| 332 |
args = self._task_to_dict(mytask) |
| 333 |
assert "a" not in args |
| 334 |
assert "arg" in args |
| 335 |
|
| 336 |
def autocreated_shortflags_dont_collide(self): |
| 337 |
"auto-created short flags don't collide" |
| 338 |
|
| 339 |
@task |
| 340 |
def mytask(c, arg1, arg2, barg): |
| 341 |
pass |
| 342 |
|
| 343 |
args = self._task_to_dict(mytask) |
| 344 |
assert "a" in args |
| 345 |
assert args["a"] is args["arg1"] |
| 346 |
assert "r" in args |
| 347 |
assert args["r"] is args["arg2"] |
| 348 |
assert "b" in args |
| 349 |
assert args["b"] is args["barg"] |
| 350 |
|
| 351 |
def early_auto_shortflags_shouldnt_lock_out_real_shortflags(self): |
| 352 |
# I.e. "task --foo -f" => --foo should NOT get to pick '-f' for its |
| 353 |
# shortflag or '-f' is totally fucked. |
| 354 |
@task |
| 355 |
def mytask(c, longarg, l): # noqa |
| 356 |
pass |
| 357 |
|
| 358 |
args = self._task_to_dict(mytask) |
| 359 |
assert "longarg" in args |
| 360 |
assert "o" in args |
| 361 |
assert args["o"] is args["longarg"] |
| 362 |
assert "l" in args |
| 363 |
|
| 364 |
def context_arguments_are_not_returned(self): |
| 365 |
@task |
| 366 |
def mytask(c): |
| 367 |
pass |
| 368 |
|
| 369 |
assert len(mytask.get_arguments()) == 0 |
| 370 |
|
| 371 |
def underscores_become_dashes(self): |
| 372 |
@task |
| 373 |
def mytask(c, longer_arg): |
| 374 |
pass |
| 375 |
|
| 376 |
arg = mytask.get_arguments()[0] |
| 377 |
assert arg.names == ("longer-arg", "l") |
| 378 |
assert arg.attr_name == "longer_arg" |
| 379 |
assert arg.name == "longer_arg" |
| 380 |
|
| 381 |
class help: |
| 382 |
def setup_method(self): |
| 383 |
@task( |
| 384 |
help={ |
| 385 |
"simple": "key", |
| 386 |
"with_underscores": "yup", |
| 387 |
"with-dashes": "also yup", |
| 388 |
} |
| 389 |
) |
| 390 |
def mytask(c, simple, with_underscores, with_dashes): |
| 391 |
pass |
| 392 |
|
| 393 |
self.help = { |
| 394 |
arg.name: arg.help for arg in mytask.get_arguments() |
| 395 |
} |
| 396 |
|
| 397 |
def base_case(self): |
| 398 |
assert self.help["simple"] == "key" |
| 399 |
|
| 400 |
def underscored_name_via_underscores(self): |
| 401 |
assert self.help["with_underscores"] == "yup" |
| 402 |
|
| 403 |
def underscored_name_via_dashes(self): |
| 404 |
assert self.help["with_dashes"] == "also yup" |
| 405 |
|
| 406 |
def raises_ValueError_on_keys_not_found_in_task_args(self): |
| 407 |
@task(help={"non-existing-param": "Help text"}) |
| 408 |
def no_parameters(c): |
| 409 |
pass |
| 410 |
|
| 411 |
err = r"field was set.*don't exist:.*'non-existing-param'*" |
| 412 |
with raises(ValueError, match=err): |
| 413 |
no_parameters.get_arguments() |
| 414 |
|
| 415 |
def no_ValueError_on_unfound_keys_when_configured_otherwise(self): |
| 416 |
@task( |
| 417 |
help={"non-existing-param": "Help text", "param": "ganoes"} |
| 418 |
) |
| 419 |
def malazan(c, param): |
| 420 |
pass |
| 421 |
|
| 422 |
arg_dict = { |
| 423 |
arg.name: arg.help |
| 424 |
for arg in malazan.get_arguments(ignore_unknown_help=True) |
| 425 |
} |
| 426 |
assert "non_existing_param" not in arg_dict |
| 427 |
assert arg_dict["param"] == "ganoes" |
| 428 |
|
| 429 |
def arg_value_is_copied_to_avoid_state_bleed_when_shared(self): |
| 430 |
# TODO: the 'real' solve here is to make sharing common |
| 431 |
# arguments between tasks a first class citizen; there's |
| 432 |
# tickets for that. |
| 433 |
shared_help = {"shared": "help"} |
| 434 |
|
| 435 |
@task(help=shared_help) |
| 436 |
def first(c, shared): |
| 437 |
pass |
| 438 |
|
| 439 |
@task(help=shared_help) |
| 440 |
def second(c, shared): |
| 441 |
pass |
| 442 |
|
| 443 |
# Running get_arguments() is required to trigger bug or lack |
| 444 |
# thereof, but also lets us do a nice safety check after. |
| 445 |
helps = [x.get_arguments()[0].help for x in (first, second)] |
| 446 |
assert helps == ["help", "help"] |
| 447 |
|
| 448 |
|
| 449 |
# Dummy task for Call tests |
| 450 |
_ = object() |
| 451 |
|
| 452 |
|
| 453 |
class Call_: |
| 454 |
def setup_method(self): |
| 455 |
self.task = Task(Mock(__name__="mytask")) |
| 456 |
|
| 457 |
class init: |
| 458 |
class task: |
| 459 |
def is_required(self): |
| 460 |
with raises(TypeError): |
| 461 |
Call() |
| 462 |
|
| 463 |
def is_first_posarg(self): |
| 464 |
assert Call(_).task is _ |
| 465 |
|
| 466 |
class called_as: |
| 467 |
def defaults_to_None(self): |
| 468 |
assert Call(_).called_as is None |
| 469 |
|
| 470 |
def may_be_given(self): |
| 471 |
assert Call(_, called_as="foo").called_as == "foo" |
| 472 |
|
| 473 |
class args: |
| 474 |
def defaults_to_empty_tuple(self): |
| 475 |
assert Call(_).args == tuple() |
| 476 |
|
| 477 |
def may_be_given(self): |
| 478 |
assert Call(_, args=(1, 2, 3)).args == (1, 2, 3) |
| 479 |
|
| 480 |
class kwargs: |
| 481 |
def defaults_to_empty_dict(self): |
| 482 |
assert Call(_).kwargs == dict() |
| 483 |
|
| 484 |
def may_be_given(self): |
| 485 |
assert Call(_, kwargs={"foo": "bar"}).kwargs == {"foo": "bar"} |
| 486 |
|
| 487 |
class stringrep: |
| 488 |
"__str__" |
| 489 |
|
| 490 |
def includes_task_name(self): |
| 491 |
call = Call(self.task) |
| 492 |
assert str(call) == "<Call 'mytask', args: (), kwargs: {}>" |
| 493 |
|
| 494 |
def works_for_subclasses(self): |
| 495 |
class MyCall(Call): |
| 496 |
pass |
| 497 |
|
| 498 |
call = MyCall(self.task) |
| 499 |
assert "<MyCall" in str(call) |
| 500 |
|
| 501 |
def includes_args_and_kwargs(self): |
| 502 |
call = Call( |
| 503 |
self.task, |
| 504 |
args=("posarg1", "posarg2"), |
| 505 |
# Single-key dict to avoid dict ordering issues |
| 506 |
kwargs={"kwarg1": "val1"}, |
| 507 |
) |
| 508 |
expected = "<Call 'mytask', args: ('posarg1', 'posarg2'), kwargs: {'kwarg1': 'val1'}>" # noqa |
| 509 |
assert str(call) == expected |
| 510 |
|
| 511 |
def includes_aka_if_explicit_name_given(self): |
| 512 |
call = Call(self.task, called_as="notmytask") |
| 513 |
expected = "<Call 'mytask' (called as: 'notmytask'), args: (), kwargs: {}>" # noqa |
| 514 |
assert str(call) == expected |
| 515 |
|
| 516 |
def skips_aka_if_explicit_name_same_as_task_name(self): |
| 517 |
call = Call(self.task, called_as="mytask") |
| 518 |
assert str(call) == "<Call 'mytask', args: (), kwargs: {}>" |
| 519 |
|
| 520 |
class make_context: |
| 521 |
def requires_config_and_core_parse_result_arguments(self): |
| 522 |
# Neither |
| 523 |
with raises(TypeError): |
| 524 |
Call(_).make_context() |
| 525 |
# No core_parse_result |
| 526 |
with raises(TypeError): |
| 527 |
Call(_).make_context(config=Config()) |
| 528 |
|
| 529 |
def creates_a_new_Context_from_given_config_and_core_parse_result( |
| 530 |
self, |
| 531 |
): |
| 532 |
conf = Config(defaults={"foo": "bar"}) |
| 533 |
parse_result = ParseResult() |
| 534 |
parse_result.remainder = "stuff!" |
| 535 |
c = Call(_).make_context( |
| 536 |
config=conf, core_parse_result=parse_result |
| 537 |
) |
| 538 |
assert isinstance(c, Context) |
| 539 |
assert c.foo == "bar" |
| 540 |
assert c.remainder == "stuff!" |
| 541 |
|
| 542 |
class clone: |
| 543 |
def returns_new_but_equivalent_object(self): |
| 544 |
orig = Call(self.task) |
| 545 |
clone = orig.clone() |
| 546 |
assert clone is not orig |
| 547 |
assert clone == orig |
| 548 |
|
| 549 |
def can_clone_into_a_subclass(self): |
| 550 |
orig = Call(self.task) |
| 551 |
|
| 552 |
class MyCall(Call): |
| 553 |
pass |
| 554 |
|
| 555 |
clone = orig.clone(into=MyCall) |
| 556 |
assert clone == orig |
| 557 |
assert isinstance(clone, MyCall) |
| 558 |
|
| 559 |
def can_be_given_extra_kwargs_to_clone_with(self): |
| 560 |
orig = Call(self.task) |
| 561 |
|
| 562 |
class MyCall(Call): |
| 563 |
def __init__(self, *args, **kwargs): |
| 564 |
self.hooray = kwargs.pop("hooray") |
| 565 |
super().__init__(*args, **kwargs) |
| 566 |
|
| 567 |
clone = orig.clone(into=MyCall, with_={"hooray": "woo"}) |
| 568 |
assert clone.hooray == "woo" |