| 1 |
import operator |
| 2 |
from functools import reduce |
| 3 |
|
| 4 |
from pytest import raises |
| 5 |
|
| 6 |
from invoke.collection import Collection |
| 7 |
from invoke.tasks import task, Task |
| 8 |
|
| 9 |
from _util import load, support_path |
| 10 |
|
| 11 |
|
| 12 |
@task |
| 13 |
def _mytask(c): |
| 14 |
print("woo!") |
| 15 |
|
| 16 |
|
| 17 |
def _func(c): |
| 18 |
pass |
| 19 |
|
| 20 |
|
| 21 |
class Collection_: |
| 22 |
class init: |
| 23 |
"__init__" |
| 24 |
|
| 25 |
def can_accept_task_varargs(self): |
| 26 |
"can accept tasks as *args" |
| 27 |
|
| 28 |
@task |
| 29 |
def task1(c): |
| 30 |
pass |
| 31 |
|
| 32 |
@task |
| 33 |
def task2(c): |
| 34 |
pass |
| 35 |
|
| 36 |
c = Collection(task1, task2) |
| 37 |
assert "task1" in c |
| 38 |
assert "task2" in c |
| 39 |
|
| 40 |
def can_accept_collections_as_varargs_too(self): |
| 41 |
sub = Collection("sub") |
| 42 |
ns = Collection(sub) |
| 43 |
assert ns.collections["sub"] == sub |
| 44 |
|
| 45 |
def kwargs_act_as_name_args_for_given_objects(self): |
| 46 |
sub = Collection() |
| 47 |
|
| 48 |
@task |
| 49 |
def task1(c): |
| 50 |
pass |
| 51 |
|
| 52 |
ns = Collection(loltask=task1, notsub=sub) |
| 53 |
assert ns["loltask"] == task1 |
| 54 |
assert ns.collections["notsub"] == sub |
| 55 |
|
| 56 |
def initial_string_arg_acts_as_name(self): |
| 57 |
sub = Collection("sub") |
| 58 |
ns = Collection(sub) |
| 59 |
assert ns.collections["sub"] == sub |
| 60 |
|
| 61 |
def initial_string_arg_meshes_with_varargs_and_kwargs(self): |
| 62 |
@task |
| 63 |
def task1(c): |
| 64 |
pass |
| 65 |
|
| 66 |
@task |
| 67 |
def task2(c): |
| 68 |
pass |
| 69 |
|
| 70 |
sub = Collection("sub") |
| 71 |
ns = Collection("root", task1, sub, sometask=task2) |
| 72 |
for x, y in ( |
| 73 |
(ns.name, "root"), |
| 74 |
(ns["task1"], task1), |
| 75 |
(ns.collections["sub"], sub), |
| 76 |
(ns["sometask"], task2), |
| 77 |
): |
| 78 |
assert x == y |
| 79 |
|
| 80 |
def accepts_load_path_kwarg(self): |
| 81 |
assert Collection().loaded_from is None |
| 82 |
assert Collection(loaded_from="a/path").loaded_from == "a/path" |
| 83 |
|
| 84 |
def accepts_auto_dash_names_kwarg(self): |
| 85 |
assert Collection().auto_dash_names is True |
| 86 |
assert Collection(auto_dash_names=False).auto_dash_names is False |
| 87 |
|
| 88 |
class useful_special_methods: |
| 89 |
def _meh(self): |
| 90 |
@task |
| 91 |
def task1(c): |
| 92 |
pass |
| 93 |
|
| 94 |
@task |
| 95 |
def task2(c): |
| 96 |
pass |
| 97 |
|
| 98 |
@task |
| 99 |
def task3(c): |
| 100 |
pass |
| 101 |
|
| 102 |
submeh = Collection("submeh", task3) |
| 103 |
return Collection("meh", task1, task2, submeh) |
| 104 |
|
| 105 |
def setup_method(self): |
| 106 |
self.c = self._meh() |
| 107 |
|
| 108 |
def repr_(self): |
| 109 |
"__repr__" |
| 110 |
expected = "<Collection 'meh': task1, task2, submeh...>" |
| 111 |
assert expected == repr(self.c) |
| 112 |
|
| 113 |
def equality_consists_of_name_tasks_and_collections(self): |
| 114 |
# Truly equal |
| 115 |
assert self.c == self._meh() |
| 116 |
# Same contents, different name == not equal |
| 117 |
diffname = self._meh() |
| 118 |
diffname.name = "notmeh" |
| 119 |
assert diffname != self.c |
| 120 |
# And a sanity check that we didn't forget __ne__...cuz that |
| 121 |
# definitely happened at one point |
| 122 |
assert not diffname == self.c |
| 123 |
# Same name, same tasks, different collections == not equal |
| 124 |
diffcols = self._meh() |
| 125 |
del diffcols.collections["submeh"] |
| 126 |
assert diffcols != self.c |
| 127 |
# Same name, different tasks, same collections == not equal |
| 128 |
difftasks = self._meh() |
| 129 |
del difftasks.tasks["task1"] |
| 130 |
assert difftasks != self.c |
| 131 |
|
| 132 |
def boolean_is_equivalent_to_tasks_and_or_collections(self): |
| 133 |
# No tasks or colls? Empty/false |
| 134 |
assert not Collection() |
| 135 |
|
| 136 |
# Tasks but no colls? True |
| 137 |
@task |
| 138 |
def foo(c): |
| 139 |
pass |
| 140 |
|
| 141 |
assert Collection(foo) |
| 142 |
# Colls but no tasks: True |
| 143 |
assert Collection(foo=Collection(foo)) |
| 144 |
# TODO: whether a tree that is not "empty" but has nothing BUT |
| 145 |
# other empty collections in it, should be true or false, is kinda |
| 146 |
# questionable - but since it would result in no usable task names, |
| 147 |
# let's say it's False. (Plus this lets us just use .task_names as |
| 148 |
# the shorthand impl...) |
| 149 |
assert not Collection(foo=Collection()) |
| 150 |
|
| 151 |
class from_module: |
| 152 |
def setup_method(self): |
| 153 |
self.c = Collection.from_module(load("integration")) |
| 154 |
|
| 155 |
class parameters: |
| 156 |
def setup_method(self): |
| 157 |
self.mod = load("integration") |
| 158 |
self.from_module = Collection.from_module |
| 159 |
|
| 160 |
def name_override(self): |
| 161 |
assert self.from_module(self.mod).name == "integration" |
| 162 |
override = self.from_module(self.mod, name="not-integration") |
| 163 |
assert override.name == "not-integration" |
| 164 |
|
| 165 |
def inline_configuration(self): |
| 166 |
# No configuration given, none gotten |
| 167 |
assert self.from_module(self.mod).configuration() == {} |
| 168 |
# Config kwarg given is reflected when config obtained |
| 169 |
coll = self.from_module(self.mod, config={"foo": "bar"}) |
| 170 |
assert coll.configuration() == {"foo": "bar"} |
| 171 |
|
| 172 |
def name_and_config_simultaneously(self): |
| 173 |
# Test w/ posargs to enforce ordering, just for safety. |
| 174 |
c = self.from_module(self.mod, "the name", {"the": "config"}) |
| 175 |
assert c.name == "the name" |
| 176 |
assert c.configuration() == {"the": "config"} |
| 177 |
|
| 178 |
def auto_dash_names_passed_to_constructor(self): |
| 179 |
# Sanity |
| 180 |
assert self.from_module(self.mod).auto_dash_names is True |
| 181 |
# Test |
| 182 |
coll = self.from_module(self.mod, auto_dash_names=False) |
| 183 |
assert coll.auto_dash_names is False |
| 184 |
|
| 185 |
def adds_tasks(self): |
| 186 |
assert "print-foo" in self.c |
| 187 |
|
| 188 |
def derives_collection_name_from_module_name(self): |
| 189 |
assert self.c.name == "integration" |
| 190 |
|
| 191 |
def copies_docstring_from_module(self): |
| 192 |
expected = "A semi-integration-test style fixture spanning multiple feature examples." # noqa |
| 193 |
# Checking the first line is sufficient. |
| 194 |
assert self.c.__doc__.strip().split("\n")[0] == expected |
| 195 |
|
| 196 |
def works_great_with_subclassing(self): |
| 197 |
class MyCollection(Collection): |
| 198 |
pass |
| 199 |
|
| 200 |
c = MyCollection.from_module(load("integration")) |
| 201 |
assert isinstance(c, MyCollection) |
| 202 |
|
| 203 |
def submodule_names_are_stripped_to_last_chunk(self): |
| 204 |
with support_path(): |
| 205 |
from package import module |
| 206 |
c = Collection.from_module(module) |
| 207 |
assert module.__name__ == "package.module" |
| 208 |
assert c.name == "module" |
| 209 |
assert "mytask" in c # Sanity |
| 210 |
|
| 211 |
def honors_explicit_collections(self): |
| 212 |
coll = Collection.from_module(load("explicit_root")) |
| 213 |
assert "top-level" in coll.tasks |
| 214 |
assert "sub-level" in coll.collections |
| 215 |
# The real key test |
| 216 |
assert "sub-task" not in coll.tasks |
| 217 |
|
| 218 |
def allows_tasks_with_explicit_names_to_override_bound_name(self): |
| 219 |
coll = Collection.from_module(load("subcollection_task_name")) |
| 220 |
assert "explicit-name" in coll.tasks # not 'implicit_name' |
| 221 |
|
| 222 |
def returns_unique_Collection_objects_for_same_input_module(self): |
| 223 |
# Ignoring self.c for now, just in case it changes later. |
| 224 |
# First, a module with no root NS |
| 225 |
mod = load("integration") |
| 226 |
c1 = Collection.from_module(mod) |
| 227 |
c2 = Collection.from_module(mod) |
| 228 |
assert c1 is not c2 |
| 229 |
# Now one *with* a root NS (which was previously buggy) |
| 230 |
mod2 = load("explicit_root") |
| 231 |
c3 = Collection.from_module(mod2) |
| 232 |
c4 = Collection.from_module(mod2) |
| 233 |
assert c3 is not c4 |
| 234 |
|
| 235 |
class explicit_root_ns: |
| 236 |
def setup_method(self): |
| 237 |
mod = load("explicit_root") |
| 238 |
mod.ns.configure( |
| 239 |
{ |
| 240 |
"key": "builtin", |
| 241 |
"otherkey": "yup", |
| 242 |
"subconfig": {"mykey": "myvalue"}, |
| 243 |
} |
| 244 |
) |
| 245 |
mod.ns.name = "builtin_name" |
| 246 |
self.unchanged = Collection.from_module(mod) |
| 247 |
self.changed = Collection.from_module( |
| 248 |
mod, |
| 249 |
name="override_name", |
| 250 |
config={ |
| 251 |
"key": "override", |
| 252 |
"subconfig": {"myotherkey": "myothervalue"}, |
| 253 |
}, |
| 254 |
) |
| 255 |
|
| 256 |
def inline_config_with_root_namespaces_overrides_builtin(self): |
| 257 |
assert self.unchanged.configuration()["key"] == "builtin" |
| 258 |
assert self.changed.configuration()["key"] == "override" |
| 259 |
|
| 260 |
def inline_config_overrides_via_merge_not_replacement(self): |
| 261 |
assert "otherkey" in self.changed.configuration() |
| 262 |
|
| 263 |
def config_override_merges_recursively(self): |
| 264 |
subconfig = self.changed.configuration()["subconfig"] |
| 265 |
assert subconfig["mykey"] == "myvalue" |
| 266 |
|
| 267 |
def inline_name_overrides_root_namespace_object_name(self): |
| 268 |
assert self.unchanged.name == "builtin-name" |
| 269 |
assert self.changed.name == "override-name" |
| 270 |
|
| 271 |
def root_namespace_object_name_overrides_module_name(self): |
| 272 |
# Duplicates part of previous test for explicitness' sake. |
| 273 |
# I.e. proves that the name doesn't end up 'explicit_root'. |
| 274 |
assert self.unchanged.name == "builtin-name" |
| 275 |
|
| 276 |
def docstring_still_copied_from_module(self): |
| 277 |
expected = "EXPLICIT LYRICS" |
| 278 |
assert self.unchanged.__doc__.strip() == expected |
| 279 |
assert self.changed.__doc__.strip() == expected |
| 280 |
|
| 281 |
class add_task: |
| 282 |
def setup_method(self): |
| 283 |
self.c = Collection() |
| 284 |
|
| 285 |
def associates_given_callable_with_given_name(self): |
| 286 |
self.c.add_task(_mytask, "foo") |
| 287 |
assert self.c["foo"] == _mytask |
| 288 |
|
| 289 |
def uses_function_name_as_implicit_name(self): |
| 290 |
self.c.add_task(_mytask) |
| 291 |
assert "_mytask" in self.c |
| 292 |
|
| 293 |
def prefers_name_kwarg_over_task_name_attr(self): |
| 294 |
self.c.add_task(Task(_func, name="notfunc"), name="yesfunc") |
| 295 |
assert "yesfunc" in self.c |
| 296 |
assert "notfunc" not in self.c |
| 297 |
|
| 298 |
def prefers_task_name_attr_over_function_name(self): |
| 299 |
self.c.add_task(Task(_func, name="notfunc")) |
| 300 |
assert "notfunc" in self.c |
| 301 |
assert "_func" not in self.c |
| 302 |
|
| 303 |
def raises_ValueError_if_no_name_found(self): |
| 304 |
# Can't use a lambda here as they are technically real functions. |
| 305 |
class Callable: |
| 306 |
def __call__(self, ctx): |
| 307 |
pass |
| 308 |
|
| 309 |
with raises(ValueError): |
| 310 |
self.c.add_task(Task(Callable())) |
| 311 |
|
| 312 |
def raises_ValueError_on_multiple_defaults(self): |
| 313 |
t1 = Task(_func, default=True) |
| 314 |
t2 = Task(_func, default=True) |
| 315 |
self.c.add_task(t1, "foo") |
| 316 |
with raises( |
| 317 |
ValueError, |
| 318 |
match=r"'bar' cannot be the default because 'foo' already is!", |
| 319 |
): |
| 320 |
self.c.add_task(t2, "bar") |
| 321 |
|
| 322 |
def raises_ValueError_if_task_added_mirrors_subcollection_name(self): |
| 323 |
self.c.add_collection(Collection("sub")) |
| 324 |
with raises(ValueError): |
| 325 |
self.c.add_task(_mytask, "sub") |
| 326 |
|
| 327 |
def allows_specifying_task_defaultness(self): |
| 328 |
self.c.add_task(_mytask, default=True) |
| 329 |
assert self.c.default == "_mytask" |
| 330 |
|
| 331 |
def specifying_default_False_overrides_task_setting(self): |
| 332 |
@task(default=True) |
| 333 |
def its_me(c): |
| 334 |
pass |
| 335 |
|
| 336 |
self.c.add_task(its_me, default=False) |
| 337 |
assert self.c.default is None |
| 338 |
|
| 339 |
def allows_specifying_aliases(self): |
| 340 |
self.c.add_task(_mytask, aliases=("task1", "task2")) |
| 341 |
assert self.c["_mytask"] is self.c["task1"] is self.c["task2"] |
| 342 |
|
| 343 |
def aliases_are_merged(self): |
| 344 |
@task(aliases=("foo", "bar")) |
| 345 |
def biz(c): |
| 346 |
pass |
| 347 |
|
| 348 |
# NOTE: using tuple above and list below to ensure no type problems |
| 349 |
self.c.add_task(biz, aliases=["baz", "boz"]) |
| 350 |
for x in ("foo", "bar", "biz", "baz", "boz"): |
| 351 |
assert self.c[x] is self.c["biz"] |
| 352 |
|
| 353 |
class add_collection: |
| 354 |
def setup_method(self): |
| 355 |
self.c = Collection() |
| 356 |
|
| 357 |
def adds_collection_as_subcollection_of_self(self): |
| 358 |
c2 = Collection("foo") |
| 359 |
self.c.add_collection(c2) |
| 360 |
assert "foo" in self.c.collections |
| 361 |
|
| 362 |
def can_take_module_objects(self): |
| 363 |
self.c.add_collection(load("integration")) |
| 364 |
assert "integration" in self.c.collections |
| 365 |
|
| 366 |
def allows_specifying_defaultness(self): |
| 367 |
collection = Collection("foo") |
| 368 |
self.c.add_collection(collection, default=True) |
| 369 |
assert self.c.default == collection.name |
| 370 |
|
| 371 |
def raises_ValueError_if_collection_without_name(self): |
| 372 |
# Aka non-root collections must either have an explicit name given |
| 373 |
# via kwarg, have a name attribute set, or be a module with |
| 374 |
# __name__ defined. |
| 375 |
root = Collection() |
| 376 |
sub = Collection() |
| 377 |
with raises(ValueError): |
| 378 |
root.add_collection(sub) |
| 379 |
|
| 380 |
def raises_ValueError_if_collection_named_same_as_task(self): |
| 381 |
self.c.add_task(_mytask, "sub") |
| 382 |
with raises(ValueError): |
| 383 |
self.c.add_collection(Collection("sub")) |
| 384 |
|
| 385 |
def raises_ValueError_on_multiple_defaults(self): |
| 386 |
t1 = Task(_func, default=True) |
| 387 |
self.c.add_task(t1, "foo") |
| 388 |
collection = Collection("bar") |
| 389 |
with raises( |
| 390 |
ValueError, |
| 391 |
match=r"'bar' cannot be the default because 'foo' already is!", |
| 392 |
): |
| 393 |
self.c.add_collection(collection, default=True) |
| 394 |
|
| 395 |
class getitem: |
| 396 |
"__getitem__" |
| 397 |
|
| 398 |
def setup_method(self): |
| 399 |
self.c = Collection() |
| 400 |
|
| 401 |
def finds_own_tasks_by_name(self): |
| 402 |
# TODO: duplicates an add_task test above, fix? |
| 403 |
self.c.add_task(_mytask, "foo") |
| 404 |
assert self.c["foo"] is _mytask |
| 405 |
|
| 406 |
def finds_subcollection_tasks_by_dotted_name(self): |
| 407 |
sub = Collection("sub") |
| 408 |
sub.add_task(_mytask) |
| 409 |
self.c.add_collection(sub) |
| 410 |
assert self.c["sub._mytask"] is _mytask |
| 411 |
|
| 412 |
def honors_aliases_in_own_tasks(self): |
| 413 |
task = Task(_func, aliases=["bar"]) |
| 414 |
self.c.add_task(task, "foo") |
| 415 |
assert self.c["bar"] is task |
| 416 |
|
| 417 |
def honors_subcollection_task_aliases(self): |
| 418 |
self.c.add_collection(load("decorators")) |
| 419 |
assert "decorators.bar" in self.c |
| 420 |
|
| 421 |
def honors_own_default_task_with_no_args(self): |
| 422 |
task = Task(_func, default=True) |
| 423 |
self.c.add_task(task) |
| 424 |
assert self.c[""] is task |
| 425 |
|
| 426 |
def honors_own_default_subcollection(self): |
| 427 |
task = Task(_func, default=True) |
| 428 |
sub = Collection("sub") |
| 429 |
sub.add_task(task, default=True) |
| 430 |
self.c.add_collection(sub, default=True) |
| 431 |
assert self.c[""] is task |
| 432 |
|
| 433 |
def honors_subcollection_default_tasks_on_subcollection_name(self): |
| 434 |
sub = Collection.from_module(load("decorators")) |
| 435 |
self.c.add_collection(sub) |
| 436 |
# Sanity |
| 437 |
assert self.c["decorators.biz"] is sub["biz"] |
| 438 |
# Real test |
| 439 |
assert self.c["decorators"] is self.c["decorators.biz"] |
| 440 |
|
| 441 |
def raises_ValueError_for_no_name_and_no_default(self): |
| 442 |
with raises(ValueError): |
| 443 |
self.c[""] |
| 444 |
|
| 445 |
def ValueError_for_empty_subcol_task_name_and_no_default(self): |
| 446 |
self.c.add_collection(Collection("whatever")) |
| 447 |
with raises(ValueError): |
| 448 |
self.c["whatever"] |
| 449 |
|
| 450 |
class to_contexts: |
| 451 |
def setup_method(self): |
| 452 |
@task |
| 453 |
def mytask(c, text, boolean=False, number=5): |
| 454 |
print(text) |
| 455 |
|
| 456 |
@task(aliases=["mytask27"]) |
| 457 |
def mytask2(c): |
| 458 |
pass |
| 459 |
|
| 460 |
@task(aliases=["othertask"], default=True) |
| 461 |
def subtask(c): |
| 462 |
pass |
| 463 |
|
| 464 |
sub = Collection("sub", subtask) |
| 465 |
self.c = Collection(mytask, mytask2, sub) |
| 466 |
self.contexts = self.c.to_contexts() |
| 467 |
alias_tups = [list(x.aliases) for x in self.contexts] |
| 468 |
self.aliases = reduce(operator.add, alias_tups, []) |
| 469 |
# Focus on 'mytask' as it has the more interesting sig |
| 470 |
self.context = [x for x in self.contexts if x.name == "mytask"][0] |
| 471 |
|
| 472 |
def returns_iterable_of_Contexts_corresponding_to_tasks(self): |
| 473 |
assert self.context.name == "mytask" |
| 474 |
assert len(self.contexts) == 3 |
| 475 |
|
| 476 |
class auto_dash_names: |
| 477 |
def context_names_automatically_become_dashed(self): |
| 478 |
@task |
| 479 |
def my_task(c): |
| 480 |
pass |
| 481 |
|
| 482 |
contexts = Collection(my_task).to_contexts() |
| 483 |
assert contexts[0].name == "my-task" |
| 484 |
|
| 485 |
def percolates_to_subcollection_tasks(self): |
| 486 |
@task |
| 487 |
def outer_task(c): |
| 488 |
pass |
| 489 |
|
| 490 |
@task |
| 491 |
def inner_task(c): |
| 492 |
pass |
| 493 |
|
| 494 |
coll = Collection(outer_task, inner=Collection(inner_task)) |
| 495 |
contexts = coll.to_contexts() |
| 496 |
expected = {"outer-task", "inner.inner-task"} |
| 497 |
assert {x.name for x in contexts} == expected |
| 498 |
|
| 499 |
def percolates_to_subcollection_names(self): |
| 500 |
@task |
| 501 |
def my_task(c): |
| 502 |
pass |
| 503 |
|
| 504 |
coll = Collection(inner_coll=Collection(my_task)) |
| 505 |
contexts = coll.to_contexts() |
| 506 |
assert contexts[0].name == "inner-coll.my-task" |
| 507 |
|
| 508 |
def aliases_are_dashed_too(self): |
| 509 |
@task(aliases=["hi_im_underscored"]) |
| 510 |
def whatever(c): |
| 511 |
pass |
| 512 |
|
| 513 |
contexts = Collection(whatever).to_contexts() |
| 514 |
assert "hi-im-underscored" in contexts[0].aliases |
| 515 |
|
| 516 |
def leading_and_trailing_underscores_are_not_affected(self): |
| 517 |
@task |
| 518 |
def _what_evers_(c): |
| 519 |
pass |
| 520 |
|
| 521 |
@task |
| 522 |
def _inner_cooler_(c): |
| 523 |
pass |
| 524 |
|
| 525 |
inner = Collection("inner", _inner_cooler_) |
| 526 |
contexts = Collection(_what_evers_, inner).to_contexts() |
| 527 |
expected = {"_what-evers_", "inner._inner-cooler_"} |
| 528 |
assert {x.name for x in contexts} == expected |
| 529 |
|
| 530 |
def _nested_underscores(self, auto_dash_names=None): |
| 531 |
@task(aliases=["other_name"]) |
| 532 |
def my_task(c): |
| 533 |
pass |
| 534 |
|
| 535 |
@task(aliases=["other_inner"]) |
| 536 |
def inner_task(c): |
| 537 |
pass |
| 538 |
|
| 539 |
# NOTE: explicitly not giving kwarg to subcollection; this |
| 540 |
# tests that the top-level namespace performs the inverse |
| 541 |
# transformation when necessary. |
| 542 |
sub = Collection("inner_coll", inner_task) |
| 543 |
return Collection( |
| 544 |
my_task, sub, auto_dash_names=auto_dash_names |
| 545 |
) |
| 546 |
|
| 547 |
def honors_init_setting_on_topmost_namespace(self): |
| 548 |
coll = self._nested_underscores(auto_dash_names=False) |
| 549 |
contexts = coll.to_contexts() |
| 550 |
names = ["my_task", "inner_coll.inner_task"] |
| 551 |
aliases = [["other_name"], ["inner_coll.other_inner"]] |
| 552 |
assert sorted(x.name for x in contexts) == sorted(names) |
| 553 |
assert sorted(x.aliases for x in contexts) == sorted(aliases) |
| 554 |
|
| 555 |
def transforms_are_applied_to_explicit_module_namespaces(self): |
| 556 |
# Symptom when bug present: Collection.to_contexts() dies |
| 557 |
# because it iterates over .task_names (transformed) and then |
| 558 |
# tries to use results to access __getitem__ (no auto |
| 559 |
# transform...because in all other situations, task structure |
| 560 |
# keys are already transformed; but this wasn't the case for |
| 561 |
# from_module() with explicit 'ns' objects!) |
| 562 |
namespace = self._nested_underscores() |
| 563 |
|
| 564 |
class FakeModule: |
| 565 |
__name__ = "my_module" |
| 566 |
ns = namespace |
| 567 |
|
| 568 |
coll = Collection.from_module( |
| 569 |
FakeModule(), auto_dash_names=False |
| 570 |
) |
| 571 |
# NOTE: underscores, not dashes |
| 572 |
expected = {"my_task", "inner_coll.inner_task"} |
| 573 |
assert {x.name for x in coll.to_contexts()} == expected |
| 574 |
|
| 575 |
def allows_flaglike_access_via_flags(self): |
| 576 |
assert "--text" in self.context.flags |
| 577 |
|
| 578 |
def positional_arglist_preserves_order_given(self): |
| 579 |
@task(positional=("second", "first")) |
| 580 |
def mytask(c, first, second, third): |
| 581 |
pass |
| 582 |
|
| 583 |
coll = Collection() |
| 584 |
coll.add_task(mytask) |
| 585 |
c = coll.to_contexts()[0] |
| 586 |
expected = [c.args["second"], c.args["first"]] |
| 587 |
assert c.positional_args == expected |
| 588 |
|
| 589 |
def exposes_namespaced_task_names(self): |
| 590 |
assert "sub.subtask" in [x.name for x in self.contexts] |
| 591 |
|
| 592 |
def exposes_namespaced_task_aliases(self): |
| 593 |
assert "sub.othertask" in self.aliases |
| 594 |
|
| 595 |
def exposes_subcollection_default_tasks(self): |
| 596 |
assert "sub" in self.aliases |
| 597 |
|
| 598 |
def exposes_aliases(self): |
| 599 |
assert "mytask27" in self.aliases |
| 600 |
|
| 601 |
class task_names: |
| 602 |
def setup_method(self): |
| 603 |
self.c = Collection.from_module(load("explicit_root")) |
| 604 |
|
| 605 |
def returns_all_task_names_including_subtasks(self): |
| 606 |
names = set(self.c.task_names.keys()) |
| 607 |
assert names == {"top-level", "sub-level.sub-task"} |
| 608 |
|
| 609 |
def includes_aliases_and_defaults_as_values(self): |
| 610 |
names = self.c.task_names |
| 611 |
assert names["top-level"] == ["other-top"] |
| 612 |
subtask_names = names["sub-level.sub-task"] |
| 613 |
assert subtask_names == ["sub-level.other-sub", "sub-level"] |
| 614 |
|
| 615 |
class configuration: |
| 616 |
"Configuration methods" |
| 617 |
|
| 618 |
def setup_method(self): |
| 619 |
self.root = Collection() |
| 620 |
self.task = Task(_func, name="task") |
| 621 |
|
| 622 |
def basic_set_and_get(self): |
| 623 |
self.root.configure({"foo": "bar"}) |
| 624 |
assert self.root.configuration() == {"foo": "bar"} |
| 625 |
|
| 626 |
def configure_performs_merging(self): |
| 627 |
self.root.configure({"foo": "bar"}) |
| 628 |
assert self.root.configuration()["foo"] == "bar" |
| 629 |
self.root.configure({"biz": "baz"}) |
| 630 |
assert set(self.root.configuration().keys()), {"foo" == "biz"} |
| 631 |
|
| 632 |
def configure_merging_is_recursive_for_nested_dicts(self): |
| 633 |
self.root.configure({"foo": "bar", "biz": {"baz": "boz"}}) |
| 634 |
self.root.configure({"biz": {"otherbaz": "otherboz"}}) |
| 635 |
c = self.root.configuration() |
| 636 |
assert c["biz"]["baz"] == "boz" |
| 637 |
assert c["biz"]["otherbaz"] == "otherboz" |
| 638 |
|
| 639 |
def configure_allows_overwriting(self): |
| 640 |
self.root.configure({"foo": "one"}) |
| 641 |
assert self.root.configuration()["foo"] == "one" |
| 642 |
self.root.configure({"foo": "two"}) |
| 643 |
assert self.root.configuration()["foo"] == "two" |
| 644 |
|
| 645 |
def call_returns_dict(self): |
| 646 |
assert self.root.configuration() == {} |
| 647 |
self.root.configure({"foo": "bar"}) |
| 648 |
assert self.root.configuration() == {"foo": "bar"} |
| 649 |
|
| 650 |
def access_merges_from_subcollections(self): |
| 651 |
inner = Collection("inner", self.task) |
| 652 |
inner.configure({"foo": "bar"}) |
| 653 |
self.root.configure({"biz": "baz"}) |
| 654 |
# With no inner collection |
| 655 |
assert set(self.root.configuration().keys()) == {"biz"} |
| 656 |
# With inner collection |
| 657 |
self.root.add_collection(inner) |
| 658 |
keys = set(self.root.configuration("inner.task").keys()) |
| 659 |
assert keys == {"foo", "biz"} |
| 660 |
|
| 661 |
def parents_overwrite_children_in_path(self): |
| 662 |
inner = Collection("inner", self.task) |
| 663 |
inner.configure({"foo": "inner"}) |
| 664 |
self.root.add_collection(inner) |
| 665 |
# Before updating root collection's config, reflects inner |
| 666 |
assert self.root.configuration("inner.task")["foo"] == "inner" |
| 667 |
self.root.configure({"foo": "outer"}) |
| 668 |
# After, reflects outer (since that now overrides) |
| 669 |
assert self.root.configuration("inner.task")["foo"] == "outer" |
| 670 |
|
| 671 |
def sibling_subcollections_ignored(self): |
| 672 |
inner = Collection("inner", self.task) |
| 673 |
inner.configure({"foo": "hi there"}) |
| 674 |
inner2 = Collection("inner2", Task(_func, name="task2")) |
| 675 |
inner2.configure({"foo": "nope"}) |
| 676 |
root = Collection(inner, inner2) |
| 677 |
assert root.configuration("inner.task")["foo"] == "hi there" |
| 678 |
assert root.configuration("inner2.task2")["foo"] == "nope" |
| 679 |
|
| 680 |
def subcollection_paths_may_be_dotted(self): |
| 681 |
leaf = Collection("leaf", self.task) |
| 682 |
leaf.configure({"key": "leaf-value"}) |
| 683 |
middle = Collection("middle", leaf) |
| 684 |
root = Collection("root", middle) |
| 685 |
config = root.configuration("middle.leaf.task") |
| 686 |
assert config == {"key": "leaf-value"} |
| 687 |
|
| 688 |
def invalid_subcollection_paths_result_in_KeyError(self): |
| 689 |
# Straight up invalid |
| 690 |
with raises(KeyError): |
| 691 |
Collection("meh").configuration("nope.task") |
| 692 |
# Exists but wrong level (should be 'root.task', not just |
| 693 |
# 'task') |
| 694 |
inner = Collection("inner", self.task) |
| 695 |
with raises(KeyError): |
| 696 |
Collection("root", inner).configuration("task") |
| 697 |
|
| 698 |
def keys_dont_have_to_exist_in_full_path(self): |
| 699 |
# Kinda duplicates earlier stuff; meh |
| 700 |
# Key only stored on leaf |
| 701 |
leaf = Collection("leaf", self.task) |
| 702 |
leaf.configure({"key": "leaf-value"}) |
| 703 |
middle = Collection("middle", leaf) |
| 704 |
root = Collection("root", middle) |
| 705 |
config = root.configuration("middle.leaf.task") |
| 706 |
assert config == {"key": "leaf-value"} |
| 707 |
# Key stored on mid + leaf but not root |
| 708 |
middle.configure({"key": "whoa"}) |
| 709 |
assert root.configuration("middle.leaf.task") == {"key": "whoa"} |
| 710 |
|
| 711 |
class subcollection_from_path: |
| 712 |
def top_level_path(self): |
| 713 |
collection = Collection.from_module(load("tree")) |
| 714 |
build = collection.collections["build"] |
| 715 |
assert collection.subcollection_from_path("build") is build |
| 716 |
|
| 717 |
def nested_path(self): |
| 718 |
collection = Collection.from_module(load("tree")) |
| 719 |
docs = collection.collections["build"].collections["docs"] |
| 720 |
assert collection.subcollection_from_path("build.docs") is docs |
| 721 |
|
| 722 |
def invalid_path(self): |
| 723 |
# This is really just testing Lexicon/dict behavior but w/e, good |
| 724 |
# to be explicit, esp if we ever want this to become Exit or |
| 725 |
# another custom exception. (For now most/all callers manually |
| 726 |
# catch KeyError and raise Exit just to keep most Exit use high up |
| 727 |
# in the stack...) |
| 728 |
with raises(KeyError): |
| 729 |
collection = Collection.from_module(load("tree")) |
| 730 |
collection.subcollection_from_path("lol.whatever.man") |
| 731 |
|
| 732 |
class serialized: |
| 733 |
def empty_collection(self): |
| 734 |
expected = dict( |
| 735 |
name=None, help=None, tasks=[], default=None, collections=[] |
| 736 |
) |
| 737 |
assert expected == Collection().serialized() |
| 738 |
|
| 739 |
def empty_named_collection(self): |
| 740 |
expected = dict( |
| 741 |
name="foo", help=None, tasks=[], default=None, collections=[] |
| 742 |
) |
| 743 |
assert expected == Collection("foo").serialized() |
| 744 |
|
| 745 |
def empty_named_docstringed_collection(self): |
| 746 |
expected = dict( |
| 747 |
name="foo", |
| 748 |
help="Hi doc", |
| 749 |
tasks=[], |
| 750 |
default=None, |
| 751 |
collections=[], |
| 752 |
) |
| 753 |
coll = Collection("foo") |
| 754 |
coll.__doc__ = "Hi doc" |
| 755 |
assert expected == coll.serialized() |
| 756 |
|
| 757 |
def name_docstring_default_and_tasks(self): |
| 758 |
expected = dict( |
| 759 |
name="deploy", |
| 760 |
help="How to deploy our code and configs.", |
| 761 |
tasks=[ |
| 762 |
dict( |
| 763 |
name="db", |
| 764 |
help="Deploy to our database servers.", |
| 765 |
aliases=["db-servers"], |
| 766 |
), |
| 767 |
dict( |
| 768 |
name="everywhere", |
| 769 |
help="Deploy to all targets.", |
| 770 |
aliases=[], |
| 771 |
), |
| 772 |
dict( |
| 773 |
name="web", |
| 774 |
help="Update and bounce the webservers.", |
| 775 |
aliases=[], |
| 776 |
), |
| 777 |
], |
| 778 |
default="everywhere", |
| 779 |
collections=[], |
| 780 |
) |
| 781 |
with support_path(): |
| 782 |
from tree import deploy |
| 783 |
|
| 784 |
coll = Collection.from_module(deploy) |
| 785 |
assert expected == coll.serialized() |
| 786 |
|
| 787 |
def name_docstring_default_tasks_and_collections(self): |
| 788 |
docs = dict( |
| 789 |
name="docs", |
| 790 |
help="Tasks for managing Sphinx docs.", |
| 791 |
tasks=[ |
| 792 |
dict( |
| 793 |
name="all", help="Build all doc formats.", aliases=[] |
| 794 |
), |
| 795 |
dict( |
| 796 |
name="html", help="Build HTML output only.", aliases=[] |
| 797 |
), |
| 798 |
dict( |
| 799 |
name="pdf", help="Build PDF output only.", aliases=[] |
| 800 |
), |
| 801 |
], |
| 802 |
default="all", |
| 803 |
collections=[], |
| 804 |
) |
| 805 |
python = dict( |
| 806 |
name="python", |
| 807 |
help="PyPI/etc distribution artifacts.", |
| 808 |
tasks=[ |
| 809 |
dict( |
| 810 |
name="all", |
| 811 |
help="Build all Python packages.", |
| 812 |
aliases=[], |
| 813 |
), |
| 814 |
dict( |
| 815 |
name="sdist", |
| 816 |
help="Build classic style tar.gz.", |
| 817 |
aliases=[], |
| 818 |
), |
| 819 |
dict(name="wheel", help="Build a wheel.", aliases=[]), |
| 820 |
], |
| 821 |
default="all", |
| 822 |
collections=[], |
| 823 |
) |
| 824 |
expected = dict( |
| 825 |
name="build", |
| 826 |
help="Tasks for compiling static code and assets.", |
| 827 |
tasks=[ |
| 828 |
dict( |
| 829 |
name="all", |
| 830 |
help="Build all necessary artifacts.", |
| 831 |
aliases=["everything"], |
| 832 |
), |
| 833 |
dict( |
| 834 |
name="c-ext", |
| 835 |
help="Build our internal C extension.", |
| 836 |
aliases=["ext"], |
| 837 |
), |
| 838 |
dict(name="zap", help="A silly way to clean.", aliases=[]), |
| 839 |
], |
| 840 |
default="all", |
| 841 |
collections=[docs, python], |
| 842 |
) |
| 843 |
with support_path(): |
| 844 |
from tree import build |
| 845 |
|
| 846 |
coll = Collection.from_module(build) |
| 847 |
assert expected == coll.serialized() |
| 848 |
|
| 849 |
def unnamed_subcollections(self): |
| 850 |
subcoll = Collection() |
| 851 |
named_subcoll = Collection("hello") |
| 852 |
# We're binding to name 'subcoll', but subcoll itself has no .name |
| 853 |
# attribute/value, which is what's being tested. When bug present, |
| 854 |
# that fact will cause serialized() to die on sorted() when |
| 855 |
# comparing to named_subcoll (which has a string name). |
| 856 |
root = Collection(named_subcoll, subcoll=subcoll) |
| 857 |
expected = dict( |
| 858 |
name=None, |
| 859 |
default=None, |
| 860 |
help=None, |
| 861 |
tasks=[], |
| 862 |
collections=[ |
| 863 |
# Expect anonymous first since we sort them as if their |
| 864 |
# name was the empty string. |
| 865 |
dict( |
| 866 |
tasks=[], |
| 867 |
collections=[], |
| 868 |
name=None, |
| 869 |
default=None, |
| 870 |
help=None, |
| 871 |
), |
| 872 |
dict( |
| 873 |
tasks=[], |
| 874 |
collections=[], |
| 875 |
name="hello", |
| 876 |
default=None, |
| 877 |
help=None, |
| 878 |
), |
| 879 |
], |
| 880 |
) |
| 881 |
assert expected == root.serialized() |