Skip to content

Commit

Permalink
tests: improve cli test performance (mitmproxy#6542)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhils committed Dec 9, 2023
1 parent 4e4da07 commit bb709a7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
20 changes: 10 additions & 10 deletions mitmproxy/tools/console/keybindings.py
Expand Up @@ -9,9 +9,6 @@
HELP_HEIGHT = 5


keybinding_focus_change = utils_signals.SyncSignal(lambda text: None)


class KeyItem(urwid.WidgetWrap):
def __init__(self, walker, binding, focused):
self.walker, self.binding, self.focused = walker, binding, focused
Expand Down Expand Up @@ -39,7 +36,8 @@ def keypress(self, size, key):


class KeyListWalker(urwid.ListWalker):
def __init__(self, master):
def __init__(self, master, keybinding_focus_change):
self.keybinding_focus_change = keybinding_focus_change
self.master = master

self.index = 0
Expand Down Expand Up @@ -67,7 +65,7 @@ def set_focus(self, index):
binding = self.bindings[index]
self.index = index
self.focus_obj = self._get(self.index)
keybinding_focus_change.send(binding.help or "")
self.keybinding_focus_change.send(binding.help or "")
self._modified()

def get_next(self, pos):
Expand All @@ -90,9 +88,9 @@ def positions(self, reverse=False):


class KeyList(urwid.ListBox):
def __init__(self, master):
def __init__(self, master, keybinding_focus_change):
self.master = master
self.walker = KeyListWalker(master)
self.walker = KeyListWalker(master, keybinding_focus_change)
super().__init__(self.walker)

def keypress(self, size, key):
Expand All @@ -109,7 +107,7 @@ def keypress(self, size, key):


class KeyHelp(urwid.Frame):
def __init__(self, master):
def __init__(self, master, keybinding_focus_change):
self.master = master
super().__init__(self.widget(""))
self.set_active(False)
Expand All @@ -134,10 +132,12 @@ class KeyBindings(urwid.Pile, layoutwidget.LayoutWidget):
focus_position: int

def __init__(self, master):
oh = KeyHelp(master)
keybinding_focus_change = utils_signals.SyncSignal(lambda text: None)

oh = KeyHelp(master, keybinding_focus_change)
super().__init__(
[
KeyList(master),
KeyList(master, keybinding_focus_change),
(HELP_HEIGHT, oh),
]
)
Expand Down
13 changes: 13 additions & 0 deletions test/mitmproxy/tools/console/conftest.py
Expand Up @@ -4,8 +4,10 @@
import pytest

from mitmproxy import options
from mitmproxy.tools.console import signals
from mitmproxy.tools.console import window
from mitmproxy.tools.console.master import ConsoleMaster
from mitmproxy.utils.signals import _SignalMixin


def tokenize(input: str) -> list[str]:
Expand All @@ -19,6 +21,10 @@ def tokenize(input: str) -> list[str]:


class ConsoleTestMaster(ConsoleMaster):
def __init__(self, opts: options.Options) -> None:
super().__init__(opts)
self.addons.remove(self.addons.get("tlsconfig"))

def type(self, input: str) -> None:
for key in tokenize(input):
self.window.keypress(self.ui.get_cols_rows(), key)
Expand All @@ -39,6 +45,13 @@ async def console(monkeypatch) -> ConsoleTestMaster: # noqa
monkeypatch.setattr(ConsoleTestMaster, "sig_call_in", lambda *_, **__: True)
monkeypatch.setattr(sys.stdout, "isatty", lambda: True)

# extremely hacky: the console UI heavily depends on global signals
# that are unfortunately shared across tests
# Here we clear all existing signals so that we don't interact with previous instantiations.
for sig in signals.__dict__.values():
if isinstance(sig, _SignalMixin):
sig.receivers.clear()

opts = options.Options()
m = ConsoleTestMaster(opts)
opts.server = False
Expand Down

0 comments on commit bb709a7

Please sign in to comment.