From be50f3f4810c70e0050a17cefee6a69bdc76e271 Mon Sep 17 00:00:00 2001 From: kira0204 Date: Tue, 6 Mar 2018 05:44:05 +0530 Subject: [PATCH 1/5] wrong additions test-for-2850 few fixes mock testing Typo error --- mitmproxy/command.py | 11 ++++++++--- test/mitmproxy/test_command.py | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/mitmproxy/command.py b/mitmproxy/command.py index 451415765..114e882d8 100644 --- a/mitmproxy/command.py +++ b/mitmproxy/command.py @@ -1,5 +1,5 @@ """ - This module manges and invokes typed commands. + This module manages and invokes typed commands. """ import inspect import types @@ -131,8 +131,13 @@ class CommandManager(mitmproxy.types._CommandBase): for i in dir(addon): if not i.startswith("__"): o = getattr(addon, i) - if hasattr(o, "command_path"): - self.add(o.command_path, o) + 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) def add(self, path: str, func: typing.Callable): self.commands[path] = Command(self, path, func) diff --git a/test/mitmproxy/test_command.py b/test/mitmproxy/test_command.py index e2b807532..ffbb20afd 100644 --- a/test/mitmproxy/test_command.py +++ b/test/mitmproxy/test_command.py @@ -1,4 +1,5 @@ import typing +from unittest import mock from mitmproxy import command from mitmproxy import flow from mitmproxy import exceptions @@ -309,6 +310,19 @@ class TDec: pass +def test_collect_commands(): + """ + This tests for the error thrown by hasattr() + """ + with mock.patch("mitmproxy.command.hasattr") as mock_hasattr: + mock_hasattr.return_value = False + with taddons.context() as tctx: + mock_hasattr.side_effect = OSError + c = command.CommandManager(tctx.master) + a = TDec() + c.collect_commands(a) + + def test_decorator(): with taddons.context() as tctx: c = command.CommandManager(tctx.master) From 57197c3e6cad1c1ef16e3538c0032ce94e239553 Mon Sep 17 00:00:00 2001 From: kira0204 Date: Wed, 7 Mar 2018 06:34:16 +0530 Subject: [PATCH 2/5] using asserts --- test/mitmproxy/test_command.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/mitmproxy/test_command.py b/test/mitmproxy/test_command.py index ffbb20afd..6b8bc7b08 100644 --- a/test/mitmproxy/test_command.py +++ b/test/mitmproxy/test_command.py @@ -321,6 +321,9 @@ def test_collect_commands(): c = command.CommandManager(tctx.master) a = TDec() c.collect_commands(a) + assert not "cmd1" in c.commands + assert not "cmd2" in c.commands + assert not "empty" in c.commands def test_decorator(): From 167e01acdfac792412b48fd2b30e7bd85b028cda Mon Sep 17 00:00:00 2001 From: kira0204 Date: Wed, 7 Mar 2018 16:12:12 +0530 Subject: [PATCH 3/5] fixing lint --- test/mitmproxy/test_command.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/mitmproxy/test_command.py b/test/mitmproxy/test_command.py index 6b8bc7b08..0c8d7208c 100644 --- a/test/mitmproxy/test_command.py +++ b/test/mitmproxy/test_command.py @@ -321,9 +321,9 @@ def test_collect_commands(): c = command.CommandManager(tctx.master) a = TDec() c.collect_commands(a) - assert not "cmd1" in c.commands - assert not "cmd2" in c.commands - assert not "empty" in c.commands + assert "cmd1" not in c.commands + assert "cmd2" not in c.commands + assert "empty" not in c.commands def test_decorator(): From 5dcc3b4ff832f817fc621d9116f1b399eb2ff137 Mon Sep 17 00:00:00 2001 From: kira0204 Date: Mon, 12 Mar 2018 00:28:43 +0530 Subject: [PATCH 4/5] Testing using addon --- test/mitmproxy/test_command.py | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/test/mitmproxy/test_command.py b/test/mitmproxy/test_command.py index 0c8d7208c..324985784 100644 --- a/test/mitmproxy/test_command.py +++ b/test/mitmproxy/test_command.py @@ -1,5 +1,4 @@ import typing -from unittest import mock from mitmproxy import command from mitmproxy import flow from mitmproxy import exceptions @@ -310,20 +309,27 @@ class TDec: pass +class TAttr: + def __getattr__(self, item): + raise IOError + + +class TCmds(TAttr): + def __init__(self): + self.TAttr = TAttr() + + def test_collect_commands(): """ This tests for the error thrown by hasattr() """ - with mock.patch("mitmproxy.command.hasattr") as mock_hasattr: - mock_hasattr.return_value = False - with taddons.context() as tctx: - mock_hasattr.side_effect = OSError - c = command.CommandManager(tctx.master) - a = TDec() - c.collect_commands(a) - assert "cmd1" not in c.commands - assert "cmd2" not in c.commands - assert "empty" not in c.commands + with taddons.context() as tctx: + c = command.CommandManager(tctx.master) + a = TCmds() + c.collect_commands(a) + assert "cmd1" not in c.commands + assert "cmd2" not in c.commands + assert "empty" not in c.commands def test_decorator(): From 8aad2d63cfaf30204464fcfee4ecf6f159a8f731 Mon Sep 17 00:00:00 2001 From: kira0204 Date: Mon, 12 Mar 2018 09:13:29 +0530 Subject: [PATCH 5/5] adding command --- test/mitmproxy/test_command.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/test/mitmproxy/test_command.py b/test/mitmproxy/test_command.py index 324985784..3d0a43f88 100644 --- a/test/mitmproxy/test_command.py +++ b/test/mitmproxy/test_command.py @@ -318,6 +318,10 @@ class TCmds(TAttr): def __init__(self): self.TAttr = TAttr() + @command.command("empty") + def empty(self) -> None: + pass + def test_collect_commands(): """ @@ -327,9 +331,7 @@ def test_collect_commands(): c = command.CommandManager(tctx.master) a = TCmds() c.collect_commands(a) - assert "cmd1" not in c.commands - assert "cmd2" not in c.commands - assert "empty" not in c.commands + assert "empty" in c.commands def test_decorator():