From 6a41122990017491222602753441de8ac438b735 Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Sun, 30 Jul 2017 02:14:44 +0200 Subject: [PATCH] make option redeclaration silent if signature is the same --- mitmproxy/addonmanager.py | 13 ++++++++++++- test/mitmproxy/test_addonmanager.py | 8 ++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/mitmproxy/addonmanager.py b/mitmproxy/addonmanager.py index 9094288b7..857ed530c 100644 --- a/mitmproxy/addonmanager.py +++ b/mitmproxy/addonmanager.py @@ -86,7 +86,18 @@ class Loader: choices: typing.Optional[typing.Sequence[str]] = None ) -> None: if name in self.master.options: - ctx.log.warn("Over-riding existing option %s" % name) + existing = self.master.options._options[name] + same_signature = ( + existing.name == name and + existing.typespec == typespec and + existing.default == default and + existing.help == help and + existing.choices == choices + ) + if same_signature: + return + else: + ctx.log.warn("Over-riding existing option %s" % name) self.master.options.add_option( name, typespec, diff --git a/test/mitmproxy/test_addonmanager.py b/test/mitmproxy/test_addonmanager.py index 5bff61d1c..67b766fa0 100644 --- a/test/mitmproxy/test_addonmanager.py +++ b/test/mitmproxy/test_addonmanager.py @@ -91,7 +91,15 @@ def test_loader(): with taddons.context() as tctx: l = addonmanager.Loader(tctx.master) l.add_option("custom_option", bool, False, "help") + assert "custom_option" in l.master.options + + # calling this again with the same signature is a no-op. l.add_option("custom_option", bool, False, "help") + assert not tctx.master.has_log("Over-riding existing option") + + # a different signature should emit a warning though. + l.add_option("custom_option", bool, True, "help") + assert tctx.master.has_log("Over-riding existing option") def cmd(a: str) -> str: return "foo"