aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAldo Cortesi <aldo@nullcube.com>2017-12-15 17:12:44 +1300
committerAldo Cortesi <aldo@nullcube.com>2017-12-15 17:51:02 +1300
commit198c7b19a3c2777c064aeba54e16b3f0b78ba143 (patch)
tree5934e085ab0407c2000b638789e4b087b618accf
parent1d2cdcff07bc9db090c22699432a0006589abe37 (diff)
downloadmitmproxy-198c7b19a3c2777c064aeba54e16b3f0b78ba143.tar.gz
mitmproxy-198c7b19a3c2777c064aeba54e16b3f0b78ba143.tar.bz2
mitmproxy-198c7b19a3c2777c064aeba54e16b3f0b78ba143.zip
commander: test++
-rw-r--r--mitmproxy/tools/console/commander/commander.py6
-rw-r--r--test/mitmproxy/tools/console/test_commander.py54
2 files changed, 51 insertions, 9 deletions
diff --git a/mitmproxy/tools/console/commander/commander.py b/mitmproxy/tools/console/commander/commander.py
index bef43a7e..5fc7dd12 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 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"