|
1 | 1 | import pytest
|
2 | 2 | import types
|
3 |
| -from pluggy import (PluginValidationError, |
4 |
| - HookCallError, HookimplMarker, HookspecMarker) |
| 3 | +import sys |
| 4 | +from pluggy import ( |
| 5 | + PluginManager, PluginValidationError, |
| 6 | + HookCallError, HookimplMarker, HookspecMarker |
| 7 | +) |
5 | 8 |
|
6 | 9 |
|
7 | 10 | hookspec = HookspecMarker("example")
|
@@ -418,3 +421,148 @@ def m(self, x):
|
418 | 421 | pm.register(p1)
|
419 | 422 | with pytest.deprecated_call():
|
420 | 423 | pm.hook.m.call_historic(kwargs=dict(x=10), proc=lambda res: res)
|
| 424 | + |
| 425 | + |
| 426 | +def test_load_setuptools_instantiation(monkeypatch, pm): |
| 427 | + pkg_resources = pytest.importorskip("pkg_resources") |
| 428 | + |
| 429 | + def my_iter(name): |
| 430 | + assert name == "hello" |
| 431 | + |
| 432 | + class EntryPoint(object): |
| 433 | + name = "myname" |
| 434 | + dist = None |
| 435 | + |
| 436 | + def load(self): |
| 437 | + class PseudoPlugin(object): |
| 438 | + x = 42 |
| 439 | + return PseudoPlugin() |
| 440 | + |
| 441 | + return iter([EntryPoint()]) |
| 442 | + |
| 443 | + monkeypatch.setattr(pkg_resources, 'iter_entry_points', my_iter) |
| 444 | + num = pm.load_setuptools_entrypoints("hello") |
| 445 | + assert num == 1 |
| 446 | + plugin = pm.get_plugin("myname") |
| 447 | + assert plugin.x == 42 |
| 448 | + assert pm.list_plugin_distinfo() == [(plugin, None)] |
| 449 | + |
| 450 | + |
| 451 | +def test_load_setuptools_not_installed(monkeypatch, pm): |
| 452 | + monkeypatch.setitem( |
| 453 | + sys.modules, 'pkg_resources', |
| 454 | + types.ModuleType("pkg_resources")) |
| 455 | + |
| 456 | + with pytest.raises(ImportError): |
| 457 | + pm.load_setuptools_entrypoints("qwe") |
| 458 | + |
| 459 | + |
| 460 | +def test_add_tracefuncs(he_pm): |
| 461 | + out = [] |
| 462 | + |
| 463 | + class api1(object): |
| 464 | + @hookimpl |
| 465 | + def he_method1(self): |
| 466 | + out.append("he_method1-api1") |
| 467 | + |
| 468 | + class api2(object): |
| 469 | + @hookimpl |
| 470 | + def he_method1(self): |
| 471 | + out.append("he_method1-api2") |
| 472 | + |
| 473 | + he_pm.register(api1()) |
| 474 | + he_pm.register(api2()) |
| 475 | + |
| 476 | + def before(hook_name, hook_impls, kwargs): |
| 477 | + out.append((hook_name, list(hook_impls), kwargs)) |
| 478 | + |
| 479 | + def after(outcome, hook_name, hook_impls, kwargs): |
| 480 | + out.append((outcome, hook_name, list(hook_impls), kwargs)) |
| 481 | + |
| 482 | + undo = he_pm.add_hookcall_monitoring(before, after) |
| 483 | + |
| 484 | + he_pm.hook.he_method1(arg=1) |
| 485 | + assert len(out) == 4 |
| 486 | + assert out[0][0] == "he_method1" |
| 487 | + assert len(out[0][1]) == 2 |
| 488 | + assert isinstance(out[0][2], dict) |
| 489 | + assert out[1] == "he_method1-api2" |
| 490 | + assert out[2] == "he_method1-api1" |
| 491 | + assert len(out[3]) == 4 |
| 492 | + assert out[3][1] == out[0][0] |
| 493 | + |
| 494 | + undo() |
| 495 | + he_pm.hook.he_method1(arg=1) |
| 496 | + assert len(out) == 4 + 2 |
| 497 | + |
| 498 | + |
| 499 | +def test_hook_tracing(he_pm): |
| 500 | + saveindent = [] |
| 501 | + |
| 502 | + class api1(object): |
| 503 | + @hookimpl |
| 504 | + def he_method1(self): |
| 505 | + saveindent.append(he_pm.trace.root.indent) |
| 506 | + |
| 507 | + class api2(object): |
| 508 | + @hookimpl |
| 509 | + def he_method1(self): |
| 510 | + saveindent.append(he_pm.trace.root.indent) |
| 511 | + raise ValueError() |
| 512 | + |
| 513 | + he_pm.register(api1()) |
| 514 | + out = [] |
| 515 | + he_pm.trace.root.setwriter(out.append) |
| 516 | + undo = he_pm.enable_tracing() |
| 517 | + try: |
| 518 | + indent = he_pm.trace.root.indent |
| 519 | + he_pm.hook.he_method1(arg=1) |
| 520 | + assert indent == he_pm.trace.root.indent |
| 521 | + assert len(out) == 2 |
| 522 | + assert 'he_method1' in out[0] |
| 523 | + assert 'finish' in out[1] |
| 524 | + |
| 525 | + out[:] = [] |
| 526 | + he_pm.register(api2()) |
| 527 | + |
| 528 | + with pytest.raises(ValueError): |
| 529 | + he_pm.hook.he_method1(arg=1) |
| 530 | + assert he_pm.trace.root.indent == indent |
| 531 | + assert saveindent[0] > indent |
| 532 | + finally: |
| 533 | + undo() |
| 534 | + |
| 535 | + |
| 536 | +@pytest.mark.parametrize('include_hookspec', [True, False]) |
| 537 | +def test_prefix_hookimpl(include_hookspec): |
| 538 | + with pytest.deprecated_call(): |
| 539 | + pm = PluginManager(hookspec.project_name, "hello_") |
| 540 | + |
| 541 | + if include_hookspec: |
| 542 | + class HookSpec(object): |
| 543 | + @hookspec |
| 544 | + def hello_myhook(self, arg1): |
| 545 | + """ add to arg1 """ |
| 546 | + |
| 547 | + pm.add_hookspecs(HookSpec) |
| 548 | + |
| 549 | + class Plugin(object): |
| 550 | + def hello_myhook(self, arg1): |
| 551 | + return arg1 + 1 |
| 552 | + |
| 553 | + with pytest.deprecated_call(): |
| 554 | + pm.register(Plugin()) |
| 555 | + pm.register(Plugin()) |
| 556 | + results = pm.hook.hello_myhook(arg1=17) |
| 557 | + assert results == [18, 18] |
| 558 | + |
| 559 | + |
| 560 | +def test_prefix_hookimpl_dontmatch_module(): |
| 561 | + with pytest.deprecated_call(): |
| 562 | + pm = PluginManager(hookspec.project_name, "hello_") |
| 563 | + |
| 564 | + class BadPlugin(object): |
| 565 | + hello_module = __import__('email') |
| 566 | + |
| 567 | + pm.register(BadPlugin()) |
| 568 | + pm.check_pending() |
0 commit comments