mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2025-02-02 00:05:27 +00:00
commander: test++
This commit is contained in:
parent
1d2cdcff07
commit
198c7b19a3
@ -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):
|
||||
|
@ -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"
|
||||
|
Loading…
Reference in New Issue
Block a user