Skip to content

Commit

Permalink
Codecov
Browse files Browse the repository at this point in the history
  • Loading branch information
Anusha Shekhar authored and Anusha Shekhar committed Dec 14, 2023
1 parent cb4bb02 commit 11d6c28
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 17 deletions.
79 changes: 65 additions & 14 deletions testing/test_hookcaller.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from pluggy import PluginValidationError
from pluggy._hooks import HookCaller
from pluggy._hooks import HookImpl
from pluggy._hooks import HookimplOpts
from pluggy._tracing import saferepr

hookspec = HookspecMarker("example")
Expand Down Expand Up @@ -410,7 +411,8 @@ def hello(self, arg: object) -> None:

pm.add_hookspecs(Api)

# make sure a bad signature still raises an error when using specname
"""make sure a bad signature still raises an error when using specname"""

class Plugin:
@hookimpl(specname="hello")
def foo(self, arg: int, too, many, args) -> int:
Expand All @@ -419,8 +421,9 @@ def foo(self, arg: int, too, many, args) -> int:
with pytest.raises(PluginValidationError):
pm.register(Plugin())

# make sure check_pending still fails if specname doesn't have a
# corresponding spec. EVEN if the function name matches one.
"""make sure check_pending still fails if specname doesn't have a
corresponding spec. EVEN if the function name matches one."""

class Plugin2:
@hookimpl(specname="bar")
def hello(self, arg: int) -> int:
Expand Down Expand Up @@ -451,14 +454,62 @@ def conflict(self) -> None:
)


def test_hookcaller_repr_with_saferepr_failure(
hc: HookCaller, addmeth: AddMeth
) -> None:
@addmeth()
def he_method2() -> None:
# Intentional error to make the repr fail
raise ValueError("Intentional error in he_method2")

# Verify that HookCaller.repr with saferepr still works despite the error
expected_repr = f"<HookCaller {saferepr(hc.name)}>"
assert repr(hc) == expected_repr
def test_hook_impl_initialization() -> None:
# Mock data
plugin = "example_plugin"
plugin_name = "ExamplePlugin"

def example_function(x):
return x

Check warning on line 463 in testing/test_hookcaller.py

View check run for this annotation

Codecov / codecov/patch

testing/test_hookcaller.py#L463

Added line #L463 was not covered by tests

hook_impl_opts: HookimplOpts = {
"wrapper": False,
"hookwrapper": False,
"optionalhook": False,
"tryfirst": False,
"trylast": False,
"specname": "",
}

# Initialize HookImpl
hook_impl = HookImpl(plugin, plugin_name, example_function, hook_impl_opts)

# Verify attributes are set correctly
assert hook_impl.function == example_function
assert hook_impl.argnames == ("x",)
assert hook_impl.kwargnames == ()
assert hook_impl.plugin == plugin
assert hook_impl.opts == hook_impl_opts
assert hook_impl.plugin_name == plugin_name
assert not hook_impl.wrapper
assert not hook_impl.hookwrapper
assert not hook_impl.optionalhook
assert not hook_impl.tryfirst
assert not hook_impl.trylast


def test_hook_impl_representation() -> None:
# Mock data
plugin = "example_plugin"
plugin_name = "ExamplePlugin"

def example_function(x):
return x

Check warning on line 497 in testing/test_hookcaller.py

View check run for this annotation

Codecov / codecov/patch

testing/test_hookcaller.py#L497

Added line #L497 was not covered by tests

hook_impl_opts: HookimplOpts = {
"wrapper": False,
"hookwrapper": False,
"optionalhook": False,
"tryfirst": False,
"trylast": False,
"specname": "",
}

# Initialize HookImpl
hook_impl = HookImpl(plugin, plugin_name, example_function, hook_impl_opts)

# Verify __repr__ method
expected_repr = (
f"<HookImpl plugin_name={saferepr(plugin_name)}, " f"plugin={saferepr(plugin)}>"
)
assert repr(hook_impl) == expected_repr
3 changes: 0 additions & 3 deletions testing/test_tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,6 @@ def test_saferepr_broken_getattribute():
"""

class SomeClass:
def __getattribute__(self, attr):
raise RuntimeError

def __repr__(self):
raise RuntimeError

Expand Down

0 comments on commit 11d6c28

Please sign in to comment.