diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/mitmproxy/test_command.py | 19 | ||||
-rw-r--r-- | test/mitmproxy/tools/console/test_commander.py | 55 | ||||
-rw-r--r-- | test/mitmproxy/tools/console/test_pathedit.py | 72 |
3 files changed, 74 insertions, 72 deletions
diff --git a/test/mitmproxy/test_command.py b/test/mitmproxy/test_command.py index 76ce2245..298b34fb 100644 --- a/test/mitmproxy/test_command.py +++ b/test/mitmproxy/test_command.py @@ -24,6 +24,10 @@ class TAddon: def cmd3(self, foo: int) -> int: return foo + @command.command("subcommand") + def subcommand(self, cmd: command.Cmd, *args: command.Arg) -> str: + return "ok" + @command.command("empty") def empty(self) -> None: pass @@ -102,6 +106,21 @@ class TestCommand: command.ParseResult(value = "", type = int), ] ], + [ + "subcommand ", + [ + command.ParseResult(value = "subcommand", type = command.Cmd), + command.ParseResult(value = "", type = command.Cmd), + ] + ], + [ + "subcommand cmd3 ", + [ + command.ParseResult(value = "subcommand", type = command.Cmd), + command.ParseResult(value = "cmd3", type = command.Cmd), + command.ParseResult(value = "", type = int), + ] + ], ] with taddons.context() as tctx: tctx.master.addons.add(TAddon()) diff --git a/test/mitmproxy/tools/console/test_commander.py b/test/mitmproxy/tools/console/test_commander.py index 1ac4c5c6..823af06d 100644 --- a/test/mitmproxy/tools/console/test_commander.py +++ b/test/mitmproxy/tools/console/test_commander.py @@ -1,5 +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")) + 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: @@ -46,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)], @@ -66,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" diff --git a/test/mitmproxy/tools/console/test_pathedit.py b/test/mitmproxy/tools/console/test_pathedit.py deleted file mode 100644 index b9f51f5a..00000000 --- a/test/mitmproxy/tools/console/test_pathedit.py +++ /dev/null @@ -1,72 +0,0 @@ -import os -from os.path import normpath -from unittest import mock - -from mitmproxy.tools.console import pathedit -from mitmproxy.test import tutils - - -class TestPathCompleter: - - def test_lookup_construction(self): - c = pathedit._PathCompleter() - - cd = os.path.normpath(tutils.test_data.path("mitmproxy/completion")) - ca = os.path.join(cd, "a") - assert c.complete(ca).endswith(normpath("/completion/aaa")) - assert c.complete(ca).endswith(normpath("/completion/aab")) - c.reset() - ca = os.path.join(cd, "aaa") - assert c.complete(ca).endswith(normpath("/completion/aaa")) - assert c.complete(ca).endswith(normpath("/completion/aaa")) - c.reset() - assert c.complete(cd).endswith(normpath("/completion/aaa")) - - def test_completion(self): - c = pathedit._PathCompleter(True) - c.reset() - c.lookup = [ - ("a", "x/a"), - ("aa", "x/aa"), - ] - assert c.complete("a") == "a" - assert c.final == "x/a" - assert c.complete("a") == "aa" - assert c.complete("a") == "a" - - c = pathedit._PathCompleter(True) - r = c.complete("l") - assert c.final.endswith(r) - - c.reset() - assert c.complete("/nonexistent") == "/nonexistent" - assert c.final == "/nonexistent" - c.reset() - assert c.complete("~") != "~" - - c.reset() - s = "thisisatotallynonexistantpathforsure" - assert c.complete(s) == s - assert c.final == s - - -class TestPathEdit: - - def test_keypress(self): - - pe = pathedit.PathEdit("", "") - - with mock.patch('urwid.widget.Edit.get_edit_text') as get_text, \ - mock.patch('urwid.widget.Edit.set_edit_text') as set_text: - - cd = os.path.normpath(tutils.test_data.path("mitmproxy/completion")) - get_text.return_value = os.path.join(cd, "a") - - # Pressing tab should set completed path - pe.keypress((1,), "tab") - set_text_called_with = set_text.call_args[0][0] - assert set_text_called_with.endswith(normpath("/completion/aaa")) - - # Pressing any other key should reset - pe.keypress((1,), "a") - assert pe.lookup is None |