From 198c7b19a3c2777c064aeba54e16b3f0b78ba143 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Fri, 15 Dec 2017 17:12:44 +1300 Subject: [PATCH] commander: test++ --- .../tools/console/commander/commander.py | 6 +-- .../mitmproxy/tools/console/test_commander.py | 54 ++++++++++++++++--- 2 files changed, 51 insertions(+), 9 deletions(-) diff --git a/mitmproxy/tools/console/commander/commander.py b/mitmproxy/tools/console/commander/commander.py index bef43a7e4..5fc7dd128 100644 --- a/mitmproxy/tools/console/commander/commander.py +++ b/mitmproxy/tools/console/commander/commander.py @@ -10,7 +10,7 @@ import mitmproxy.master import mitmproxy.command -class Completer: +class Completer: # pragma: no cover @abc.abstractmethod def cycle(self) -> str: pass @@ -52,7 +52,7 @@ def pathOptions(start: str) -> typing.Sequence[str]: prefix = os.path.dirname(start) prefix = prefix or "./" for f in files: - display = os.path.normpath(os.path.join(prefix, os.path.basename(f))) + display = os.path.join(prefix, os.path.normpath(os.path.basename(f))) if os.path.isdir(f): display += "/" ret.append(display) @@ -157,9 +157,9 @@ class CommandEdit(urwid.WidgetWrap): leader = ": " def __init__(self, master: mitmproxy.master.Master, text: str) -> None: + super().__init__(urwid.Text(self.leader)) self.master = master self.cbuf = CommandBuffer(master, text) - self._w = urwid.Text(self.leader) self.update() def keypress(self, size, key): diff --git a/test/mitmproxy/tools/console/test_commander.py b/test/mitmproxy/tools/console/test_commander.py index e8974869a..823af06d2 100644 --- a/test/mitmproxy/tools/console/test_commander.py +++ b/test/mitmproxy/tools/console/test_commander.py @@ -1,18 +1,36 @@ import os +import contextlib from mitmproxy.tools.console.commander import commander from mitmproxy.test import taddons from mitmproxy.test import tutils +@contextlib.contextmanager +def chdir(path: str): + old_dir = os.getcwd() + os.chdir(path) + yield + os.chdir(old_dir) + + +def normPathOpts(prefix, match): + ret = [] + for s in commander.pathOptions(match): + s = s[len(prefix):] + s = s.replace(os.sep, "/") + ret.append(s) + return ret + + def test_pathOptions(): cd = os.path.normpath(tutils.test_data.path("mitmproxy/completion")) - - ret = [x[len(cd):] for x in commander.pathOptions(cd)] - assert ret == ['/aaa', '/aab', '/aac', '/bbb/'] - - ret = [x[len(cd):] for x in commander.pathOptions(os.path.join(cd, "a"))] - assert ret == ['/aaa', '/aab', '/aac'] + assert normPathOpts(cd, cd) == ['/aaa', '/aab', '/aac', '/bbb/'] + assert normPathOpts(cd, os.path.join(cd, "a")) == ['/aaa', '/aab', '/aac'] + with chdir(cd): + assert normPathOpts("", "./") == ['./aaa', './aab', './aac', './bbb/'] + assert normPathOpts("", "") == ['./aaa', './aab', './aac', './bbb/'] + assert commander.pathOptions("nonexistent") == ["nonexistent"] class TestListCompleter: @@ -59,6 +77,24 @@ class TestCommandBuffer: assert cb.buf == output[0] assert cb.cursor == output[1] + def test_left(self): + cursors = [3, 2, 1, 0, 0] + with taddons.context() as tctx: + cb = commander.CommandBuffer(tctx.master) + cb.buf, cb.cursor = "abcd", 4 + for c in cursors: + cb.left() + assert cb.cursor == c + + def test_right(self): + cursors = [1, 2, 3, 4, 4] + with taddons.context() as tctx: + cb = commander.CommandBuffer(tctx.master) + cb.buf, cb.cursor = "abcd", 0 + for c in cursors: + cb.right() + assert cb.cursor == c + def test_insert(self): tests = [ [("", 0), ("x", 1)], @@ -79,3 +115,9 @@ class TestCommandBuffer: cb.buf = "foo bar" cb.cursor = len(cb.buf) cb.cycle_completion() + + def test_render(self): + with taddons.context() as tctx: + cb = commander.CommandBuffer(tctx.master) + cb.buf = "foo" + assert cb.render() == "foo"