From 582e6a9fa62eeba05561ba2105bf7c8a73211b4d Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Fri, 15 Dec 2017 14:33:05 +1300 Subject: command: recursive command parsing This lets us complete commands passed to commands correctly. --- test/mitmproxy/test_command.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'test') 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()) -- cgit v1.2.3 From a8ae006f2e798e05cfc9ad29b077f75f6078444b Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Fri, 15 Dec 2017 16:00:36 +1300 Subject: command: path completion --- test/mitmproxy/tools/console/test_commander.py | 13 +++++ test/mitmproxy/tools/console/test_pathedit.py | 72 -------------------------- 2 files changed, 13 insertions(+), 72 deletions(-) delete mode 100644 test/mitmproxy/tools/console/test_pathedit.py (limited to 'test') diff --git a/test/mitmproxy/tools/console/test_commander.py b/test/mitmproxy/tools/console/test_commander.py index 1ac4c5c6..e8974869 100644 --- a/test/mitmproxy/tools/console/test_commander.py +++ b/test/mitmproxy/tools/console/test_commander.py @@ -1,5 +1,18 @@ +import os + from mitmproxy.tools.console.commander import commander from mitmproxy.test import taddons +from mitmproxy.test import tutils + + +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'] class TestListCompleter: 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 -- cgit v1.2.3 From 198c7b19a3c2777c064aeba54e16b3f0b78ba143 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Fri, 15 Dec 2017 17:12:44 +1300 Subject: commander: test++ --- test/mitmproxy/tools/console/test_commander.py | 54 +++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 6 deletions(-) (limited to 'test') diff --git a/test/mitmproxy/tools/console/test_commander.py b/test/mitmproxy/tools/console/test_commander.py index e8974869..823af06d 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 -def test_pathOptions(): - cd = os.path.normpath(tutils.test_data.path("mitmproxy/completion")) +@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 - 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'] +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: @@ -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" -- cgit v1.2.3