Merge pull request #2956 from kira0204/test-for-2850

Test for #2850
This commit is contained in:
Maximilian Hils 2018-03-15 00:29:52 +01:00 committed by GitHub
commit 2001184b6c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 3 deletions

View File

@ -1,5 +1,5 @@
""" """
This module manges and invokes typed commands. This module manages and invokes typed commands.
""" """
import inspect import inspect
import types import types
@ -131,7 +131,12 @@ class CommandManager(mitmproxy.types._CommandBase):
for i in dir(addon): for i in dir(addon):
if not i.startswith("__"): if not i.startswith("__"):
o = getattr(addon, i) o = getattr(addon, i)
if hasattr(o, "command_path"): try:
is_command = hasattr(o, "command_path")
except Exception:
pass # hasattr may raise if o implements __getattr__.
else:
if is_command:
self.add(o.command_path, o) self.add(o.command_path, o)
def add(self, path: str, func: typing.Callable): def add(self, path: str, func: typing.Callable):

View File

@ -309,6 +309,31 @@ class TDec:
pass pass
class TAttr:
def __getattr__(self, item):
raise IOError
class TCmds(TAttr):
def __init__(self):
self.TAttr = TAttr()
@command.command("empty")
def empty(self) -> None:
pass
def test_collect_commands():
"""
This tests for the error thrown by hasattr()
"""
with taddons.context() as tctx:
c = command.CommandManager(tctx.master)
a = TCmds()
c.collect_commands(a)
assert "empty" in c.commands
def test_decorator(): def test_decorator():
with taddons.context() as tctx: with taddons.context() as tctx:
c = command.CommandManager(tctx.master) c = command.CommandManager(tctx.master)