aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorMaximilian Hils <git@maximilianhils.com>2019-11-18 02:55:51 +0100
committerMaximilian Hils <git@maximilianhils.com>2019-11-18 03:05:41 +0100
commitcb723c53fab93dae67f68b4414a35c8bec7fc144 (patch)
treea6cf5923a8e6c04d68ab634e23e42727351c9501 /test
parent8a6370f1c2ae55fb03f606cb055de455ffe25eec (diff)
downloadmitmproxy-cb723c53fab93dae67f68b4414a35c8bec7fc144.tar.gz
mitmproxy-cb723c53fab93dae67f68b4414a35c8bec7fc144.tar.bz2
mitmproxy-cb723c53fab93dae67f68b4414a35c8bec7fc144.zip
revamp command processing
- Display the parameter name instead of the parameter type whenver users interact with commands. This makes it easy to enter commands just by their signature. We may want to expose type information in the command list, but some quick testing showed that this are rather intuitive anyways. - Add shift tab backward cycling for the command completion. - Use inspect.Signature instead of homebrew argument matching solution. This gets rid of quite a bit of cruft. - Remove some type checking hacks in mitmproxy.types
Diffstat (limited to 'test')
-rw-r--r--test/mitmproxy/test_command.py250
-rw-r--r--test/mitmproxy/test_types.py16
2 files changed, 150 insertions, 116 deletions
diff --git a/test/mitmproxy/test_command.py b/test/mitmproxy/test_command.py
index eb3857bf..2a1dfd08 100644
--- a/test/mitmproxy/test_command.py
+++ b/test/mitmproxy/test_command.py
@@ -1,13 +1,15 @@
-import typing
import inspect
+import io
+import typing
+
+import pytest
+
+import mitmproxy.types
from mitmproxy import command
-from mitmproxy import flow
from mitmproxy import exceptions
-from mitmproxy.test import tflow
+from mitmproxy import flow
from mitmproxy.test import taddons
-import mitmproxy.types
-import io
-import pytest
+from mitmproxy.test import tflow
class TAddon:
@@ -29,7 +31,7 @@ class TAddon:
return "ok"
@command.command("subcommand")
- def subcommand(self, cmd: mitmproxy.types.Cmd, *args: mitmproxy.types.Arg) -> str:
+ def subcommand(self, cmd: mitmproxy.types.Cmd, *args: mitmproxy.types.CmdArgs) -> str:
return "ok"
@command.command("empty")
@@ -83,14 +85,14 @@ class TestCommand:
with pytest.raises(exceptions.CommandError):
command.Command(cm, "invalidret", a.invalidret)
with pytest.raises(exceptions.CommandError):
- command.Command(cm, "invalidarg", a.invalidarg)
+ assert command.Command(cm, "invalidarg", a.invalidarg)
def test_varargs(self):
with taddons.context() as tctx:
cm = command.CommandManager(tctx.master)
a = TAddon()
c = command.Command(cm, "varargs", a.varargs)
- assert c.signature_help() == "varargs str *str -> [str]"
+ assert c.signature_help() == "varargs one *var -> str[]"
assert c.call(["one", "two", "three"]) == ["two", "three"]
def test_call(self):
@@ -99,7 +101,7 @@ class TestCommand:
a = TAddon()
c = command.Command(cm, "cmd.path", a.cmd1)
assert c.call(["foo"]) == "ret foo"
- assert c.signature_help() == "cmd.path str -> str"
+ assert c.signature_help() == "cmd.path foo -> str"
c = command.Command(cm, "cmd.two", a.cmd2)
with pytest.raises(exceptions.CommandError):
@@ -113,239 +115,272 @@ class TestCommand:
[
"foo bar",
[
- command.ParseResult(value = "foo", type = mitmproxy.types.Cmd, valid = False),
- command.ParseResult(value = " ", type = mitmproxy.types.Unknown, valid = False),
- command.ParseResult(value = "bar", type = mitmproxy.types.Unknown, valid = False)
+ command.ParseResult(value="foo", type=mitmproxy.types.Cmd, valid=False),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ command.ParseResult(value="bar", type=mitmproxy.types.Unknown, valid=False)
],
[],
],
[
"cmd1 'bar",
[
- command.ParseResult(value = "cmd1", type = mitmproxy.types.Cmd, valid = True),
- command.ParseResult(value = " ", type = mitmproxy.types.Unknown, valid = False),
- command.ParseResult(value = "'bar", type = str, valid = True)
+ command.ParseResult(value="cmd1", type=mitmproxy.types.Cmd, valid=True),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ command.ParseResult(value="'bar", type=str, valid=True)
],
[],
],
[
"a",
- [command.ParseResult(value = "a", type = mitmproxy.types.Cmd, valid = False)],
+ [command.ParseResult(value="a", type=mitmproxy.types.Cmd, valid=False)],
[],
],
[
"",
[],
- []
+ [
+ command.CommandParameter("", mitmproxy.types.Cmd),
+ command.CommandParameter("", mitmproxy.types.CmdArgs)
+ ]
],
[
"cmd3 1",
[
- command.ParseResult(value = "cmd3", type = mitmproxy.types.Cmd, valid = True),
- command.ParseResult(value = " ", type = mitmproxy.types.Unknown, valid = False),
- command.ParseResult(value = "1", type = int, valid = True),
+ command.ParseResult(value="cmd3", type=mitmproxy.types.Cmd, valid=True),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ command.ParseResult(value="1", type=int, valid=True),
],
[]
],
[
"cmd3 ",
[
- command.ParseResult(value = "cmd3", type = mitmproxy.types.Cmd, valid = True),
- command.ParseResult(value = " ", type = mitmproxy.types.Unknown, valid = False),
+ command.ParseResult(value="cmd3", type=mitmproxy.types.Cmd, valid=True),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
],
- ['int']
+ [command.CommandParameter('foo', int)]
],
[
"subcommand ",
[
- command.ParseResult(value = "subcommand", type = mitmproxy.types.Cmd, valid = True,),
- command.ParseResult(value = " ", type = mitmproxy.types.Unknown, valid = False),
+ command.ParseResult(value="subcommand", type=mitmproxy.types.Cmd, valid=True, ),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ ],
+ [
+ command.CommandParameter('cmd', mitmproxy.types.Cmd),
+ command.CommandParameter('*args', mitmproxy.types.CmdArgs),
],
- ["cmd", "arg"],
],
[
"subcommand cmd3 ",
[
- command.ParseResult(value = "subcommand", type = mitmproxy.types.Cmd, valid = True),
- command.ParseResult(value = " ", type = mitmproxy.types.Unknown, valid = False),
- command.ParseResult(value = "cmd3", type = mitmproxy.types.Cmd, valid = True),
- command.ParseResult(value = " ", type = mitmproxy.types.Unknown, valid = False),
+ command.ParseResult(value="subcommand", type=mitmproxy.types.Cmd, valid=True),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ command.ParseResult(value="cmd3", type=mitmproxy.types.Cmd, valid=True),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
],
- ["int"]
+ [command.CommandParameter('foo', int)]
],
[
"cmd4",
[
- command.ParseResult(value = "cmd4", type = mitmproxy.types.Cmd, valid = True),
+ command.ParseResult(value="cmd4", type=mitmproxy.types.Cmd, valid=True),
],
- ["int", "str", "path"]
+ [
+ command.CommandParameter('a', int),
+ command.CommandParameter('b', str),
+ command.CommandParameter('c', mitmproxy.types.Path),
+ ]
],
[
"cmd4 ",
[
- command.ParseResult(value = "cmd4", type = mitmproxy.types.Cmd, valid = True),
- command.ParseResult(value = " ", type = mitmproxy.types.Unknown, valid = False),
+ command.ParseResult(value="cmd4", type=mitmproxy.types.Cmd, valid=True),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
],
- ["int", "str", "path"]
+ [
+ command.CommandParameter('a', int),
+ command.CommandParameter('b', str),
+ command.CommandParameter('c', mitmproxy.types.Path),
+ ]
],
[
"cmd4 1",
[
- command.ParseResult(value = "cmd4", type = mitmproxy.types.Cmd, valid = True),
- command.ParseResult(value = " ", type = mitmproxy.types.Unknown, valid = False),
- command.ParseResult(value = "1", type = int, valid = True),
+ command.ParseResult(value="cmd4", type=mitmproxy.types.Cmd, valid=True),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ command.ParseResult(value="1", type=int, valid=True),
],
- ["str", "path"]
+ [
+ command.CommandParameter('b', str),
+ command.CommandParameter('c', mitmproxy.types.Path),
+ ]
],
[
"flow",
[
- command.ParseResult(value = "flow", type = mitmproxy.types.Cmd, valid = True),
+ command.ParseResult(value="flow", type=mitmproxy.types.Cmd, valid=True),
],
- ["flow", "str"]
+ [
+ command.CommandParameter('f', flow.Flow),
+ command.CommandParameter('s', str),
+ ]
],
[
"flow ",
[
- command.ParseResult(value = "flow", type = mitmproxy.types.Cmd, valid = True),
- command.ParseResult(value = " ", type = mitmproxy.types.Unknown, valid = False),
+ command.ParseResult(value="flow", type=mitmproxy.types.Cmd, valid=True),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
],
- ["flow", "str"]
+ [
+ command.CommandParameter('f', flow.Flow),
+ command.CommandParameter('s', str),
+ ]
],
[
"flow x",
[
- command.ParseResult(value = "flow", type = mitmproxy.types.Cmd, valid = True),
- command.ParseResult(value = " ", type = mitmproxy.types.Unknown, valid = False),
- command.ParseResult(value = "x", type = flow.Flow, valid = False),
+ command.ParseResult(value="flow", type=mitmproxy.types.Cmd, valid=True),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ command.ParseResult(value="x", type=flow.Flow, valid=False),
],
- ["str"]
+ [
+ command.CommandParameter('s', str),
+ ]
],
[
"flow x ",
[
- command.ParseResult(value = "flow", type = mitmproxy.types.Cmd, valid = True),
- command.ParseResult(value = " ", type = mitmproxy.types.Unknown, valid = False),
- command.ParseResult(value = "x", type = flow.Flow, valid = False),
- command.ParseResult(value = " ", type = mitmproxy.types.Unknown, valid = False),
+ command.ParseResult(value="flow", type=mitmproxy.types.Cmd, valid=True),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ command.ParseResult(value="x", type=flow.Flow, valid=False),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
],
- ["str"]
+ [
+ command.CommandParameter('s', str),
+ ]
],
[
"flow \"one two",
[
- command.ParseResult(value = "flow", type = mitmproxy.types.Cmd, valid = True),
- command.ParseResult(value = " ", type = mitmproxy.types.Unknown, valid = False),
- command.ParseResult(value = "\"one two", type = flow.Flow, valid = False),
+ command.ParseResult(value="flow", type=mitmproxy.types.Cmd, valid=True),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ command.ParseResult(value="\"one two", type=flow.Flow, valid=False),
],
- ["str"]
+ [
+ command.CommandParameter('s', str),
+ ]
],
[
"flow \"three four\"",
[
- command.ParseResult(value = "flow", type = mitmproxy.types.Cmd, valid = True),
- command.ParseResult(value = " ", type = mitmproxy.types.Unknown, valid = False),
- command.ParseResult(value = '"three four"', type = flow.Flow, valid = False),
+ command.ParseResult(value="flow", type=mitmproxy.types.Cmd, valid=True),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ command.ParseResult(value='"three four"', type=flow.Flow, valid=False),
],
- ["str"]
+ [
+ command.CommandParameter('s', str),
+ ]
],
[
"spaces ' '",
[
- command.ParseResult(value = "spaces", type = mitmproxy.types.Cmd, valid = False),
- command.ParseResult(value = " ", type = mitmproxy.types.Unknown, valid = False),
- command.ParseResult(value = "' '", type = mitmproxy.types.Unknown, valid = False)
+ command.ParseResult(value="spaces", type=mitmproxy.types.Cmd, valid=False),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ command.ParseResult(value="' '", type=mitmproxy.types.Unknown, valid=False)
],
[],
],
[
'spaces2 " "',
[
- command.ParseResult(value = "spaces2", type = mitmproxy.types.Cmd, valid = False),
- command.ParseResult(value = " ", type = mitmproxy.types.Unknown, valid = False),
- command.ParseResult(value = '" "', type = mitmproxy.types.Unknown, valid = False)
+ command.ParseResult(value="spaces2", type=mitmproxy.types.Cmd, valid=False),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ command.ParseResult(value='" "', type=mitmproxy.types.Unknown, valid=False)
],
[],
],
[
'"abc"',
[
- command.ParseResult(value = '"abc"', type = mitmproxy.types.Cmd, valid = False),
+ command.ParseResult(value='"abc"', type=mitmproxy.types.Cmd, valid=False),
],
[],
],
[
"'def'",
[
- command.ParseResult(value = "'def'", type = mitmproxy.types.Cmd, valid = False),
+ command.ParseResult(value="'def'", type=mitmproxy.types.Cmd, valid=False),
],
[],
],
[
"cmd10 'a' \"b\" c",
[
- command.ParseResult(value = "cmd10", type = mitmproxy.types.Cmd, valid = False),
- command.ParseResult(value = " ", type = mitmproxy.types.Unknown, valid = False),
- command.ParseResult(value = "'a'", type = mitmproxy.types.Unknown, valid = False),
- command.ParseResult(value = " ", type = mitmproxy.types.Unknown, valid = False),
- command.ParseResult(value = '"b"', type = mitmproxy.types.Unknown, valid = False),
- command.ParseResult(value = " ", type = mitmproxy.types.Unknown, valid = False),
- command.ParseResult(value = "c", type = mitmproxy.types.Unknown, valid = False),
+ command.ParseResult(value="cmd10", type=mitmproxy.types.Cmd, valid=False),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ command.ParseResult(value="'a'", type=mitmproxy.types.Unknown, valid=False),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ command.ParseResult(value='"b"', type=mitmproxy.types.Unknown, valid=False),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ command.ParseResult(value="c", type=mitmproxy.types.Unknown, valid=False),
],
[],
],
[
"cmd11 'a \"b\" c'",
[
- command.ParseResult(value = "cmd11", type = mitmproxy.types.Cmd, valid = False),
- command.ParseResult(value = " ", type = mitmproxy.types.Unknown, valid = False),
- command.ParseResult(value = "'a \"b\" c'", type = mitmproxy.types.Unknown, valid = False),
+ command.ParseResult(value="cmd11", type=mitmproxy.types.Cmd, valid=False),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ command.ParseResult(value="'a \"b\" c'", type=mitmproxy.types.Unknown, valid=False),
],
[],
],
[
'cmd12 "a \'b\' c"',
[
- command.ParseResult(value = "cmd12", type = mitmproxy.types.Cmd, valid = False),
- command.ParseResult(value = " ", type = mitmproxy.types.Unknown, valid = False),
- command.ParseResult(value = '"a \'b\' c"', type = mitmproxy.types.Unknown, valid = False),
+ command.ParseResult(value="cmd12", type=mitmproxy.types.Cmd, valid=False),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ command.ParseResult(value='"a \'b\' c"', type=mitmproxy.types.Unknown, valid=False),
],
[],
],
[
r'cmd13 "a \"b\" c"',
[
- command.ParseResult(value = "cmd13", type = mitmproxy.types.Cmd, valid = False),
- command.ParseResult(value = " ", type = mitmproxy.types.Unknown, valid = False),
- command.ParseResult(value = r'"a \"b\" c"', type = mitmproxy.types.Unknown, valid = False),
+ command.ParseResult(value="cmd13", type=mitmproxy.types.Cmd, valid=False),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ command.ParseResult(value=r'"a \"b\" c"', type=mitmproxy.types.Unknown, valid=False),
],
[],
],
[
r"cmd14 'a \'b\' c'",
[
- command.ParseResult(value = "cmd14", type = mitmproxy.types.Cmd, valid = False),
- command.ParseResult(value = " ", type = mitmproxy.types.Unknown, valid = False),
- command.ParseResult(value = r"'a \'b\' c'", type = mitmproxy.types.Unknown, valid = False),
+ command.ParseResult(value="cmd14", type=mitmproxy.types.Cmd, valid=False),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ command.ParseResult(value=r"'a \'b\' c'", type=mitmproxy.types.Unknown, valid=False),
],
[],
],
[
" spaces_at_the_begining_are_not_stripped",
[
- command.ParseResult(value = " ", type = mitmproxy.types.Unknown, valid = False),
- command.ParseResult(value = "spaces_at_the_begining_are_not_stripped", type = mitmproxy.types.Cmd, valid = False),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ command.ParseResult(value="spaces_at_the_begining_are_not_stripped", type=mitmproxy.types.Cmd,
+ valid=False),
],
[],
],
[
" spaces_at_the_begining_are_not_stripped neither_at_the_end ",
[
- command.ParseResult(value = " ", type = mitmproxy.types.Unknown, valid = False),
- command.ParseResult(value = "spaces_at_the_begining_are_not_stripped", type = mitmproxy.types.Cmd, valid = False),
- command.ParseResult(value = " ", type = mitmproxy.types.Unknown, valid = False),
- command.ParseResult(value = "neither_at_the_end", type = mitmproxy.types.Unknown, valid = False),
- command.ParseResult(value = " ", type = mitmproxy.types.Unknown, valid = False),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ command.ParseResult(value="spaces_at_the_begining_are_not_stripped", type=mitmproxy.types.Cmd,
+ valid=False),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ command.ParseResult(value="neither_at_the_end", type=mitmproxy.types.Unknown, valid=False),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
],
[],
],
@@ -356,8 +391,7 @@ class TestCommand:
tctx.master.addons.add(TAddon())
for s, expected, expectedremain in tests:
current, remain = tctx.master.commands.parse_partial(s)
- assert current == expected
- assert expectedremain == remain
+ assert (s, current, expectedremain) == (s, expected, remain)
def test_simple():
@@ -365,11 +399,11 @@ def test_simple():
c = command.CommandManager(tctx.master)
a = TAddon()
c.add("one.two", a.cmd1)
- assert(c.commands["one.two"].help == "cmd1 help")
- assert(c.execute("one.two foo") == "ret foo")
- assert(c.execute("one.two \"foo\"") == "ret foo")
- assert(c.execute("one.two \"foo bar\"") == "ret \"foo bar\"")
- assert(c.call("one.two", "foo") == "ret foo")
+ assert (c.commands["one.two"].help == "cmd1 help")
+ assert (c.execute("one.two foo") == "ret foo")
+ assert (c.execute("one.two \"foo\"") == "ret foo")
+ assert (c.execute("one.two \"foo bar\"") == "ret foo bar")
+ assert (c.call("one.two", "foo") == "ret foo")
with pytest.raises(exceptions.CommandError, match="Unknown"):
c.execute("nonexistent")
with pytest.raises(exceptions.CommandError, match="Invalid"):
@@ -397,13 +431,13 @@ def test_simple():
def test_typename():
assert command.typename(str) == "str"
- assert command.typename(typing.Sequence[flow.Flow]) == "[flow]"
+ assert command.typename(typing.Sequence[flow.Flow]) == "flow[]"
- assert command.typename(mitmproxy.types.Data) == "[data]"
- assert command.typename(mitmproxy.types.CutSpec) == "[cut]"
+ assert command.typename(mitmproxy.types.Data) == "data[][]"
+ assert command.typename(mitmproxy.types.CutSpec) == "cut[]"
assert command.typename(flow.Flow) == "flow"
- assert command.typename(typing.Sequence[str]) == "[str]"
+ assert command.typename(typing.Sequence[str]) == "str[]"
assert command.typename(mitmproxy.types.Choice("foo")) == "choice"
assert command.typename(mitmproxy.types.Path) == "path"
diff --git a/test/mitmproxy/test_types.py b/test/mitmproxy/test_types.py
index 571985fb..c8f7afde 100644
--- a/test/mitmproxy/test_types.py
+++ b/test/mitmproxy/test_types.py
@@ -2,7 +2,6 @@ import pytest
import os
import typing
import contextlib
-from unittest import mock
import mitmproxy.exceptions
import mitmproxy.types
@@ -64,13 +63,14 @@ def test_int():
b.parse(tctx.master.commands, int, "foo")
-def test_path(tdata):
+def test_path(tdata, monkeypatch):
with taddons.context() as tctx:
b = mitmproxy.types._PathType()
assert b.parse(tctx.master.commands, mitmproxy.types.Path, "/foo") == "/foo"
assert b.parse(tctx.master.commands, mitmproxy.types.Path, "/bar") == "/bar"
- with mock.patch.dict("os.environ", {"HOME": "/home/test"}):
- assert b.parse(tctx.master.commands, mitmproxy.types.Path, "~/mitm") == "/home/test/mitm"
+ monkeypatch.setenv("HOME", "/home/test")
+ monkeypatch.setenv("USERPROFILE", "/home/test")
+ assert b.parse(tctx.master.commands, mitmproxy.types.Path, "~/mitm") == "/home/test/mitm"
assert b.is_valid(tctx.master.commands, mitmproxy.types.Path, "foo") is True
assert b.is_valid(tctx.master.commands, mitmproxy.types.Path, "~/mitm") is True
assert b.is_valid(tctx.master.commands, mitmproxy.types.Path, 3) is False
@@ -127,10 +127,10 @@ def test_cutspec():
def test_arg():
with taddons.context() as tctx:
b = mitmproxy.types._ArgType()
- assert b.completion(tctx.master.commands, mitmproxy.types.Arg, "") == []
- assert b.parse(tctx.master.commands, mitmproxy.types.Arg, "foo") == "foo"
- assert b.is_valid(tctx.master.commands, mitmproxy.types.Arg, "foo") is True
- assert b.is_valid(tctx.master.commands, mitmproxy.types.Arg, 1) is False
+ assert b.completion(tctx.master.commands, mitmproxy.types.CmdArgs, "") == []
+ with pytest.raises(mitmproxy.exceptions.TypeError):
+ b.parse(tctx.master.commands, mitmproxy.types.CmdArgs, "foo")
+ assert b.is_valid(tctx.master.commands, mitmproxy.types.CmdArgs, 1) is False
def test_strseq():