From 58e1b3a47f392a5f4f16e30318820f163568f54e Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sun, 12 Jan 2014 12:49:19 +1300 Subject: Start refactoring scripts - Move ScriptContext into script module - Use mock module instead of hand-rolled mock objects in tests --- test/test_script.py | 80 +++++++++++++++++++++++++---------------------------- 1 file changed, 37 insertions(+), 43 deletions(-) (limited to 'test/test_script.py') diff --git a/test/test_script.py b/test/test_script.py index ad2296ef..69386834 100644 --- a/test/test_script.py +++ b/test/test_script.py @@ -3,26 +3,16 @@ import tutils import shlex import os import time +import mock -class TCounter: - count = 0 - - def __call__(self, *args, **kwargs): - self.count += 1 - - -class TScriptContext(TCounter): - def log(self, msg): - self.__call__() - class TestScript: def test_simple(self): s = flow.State() fm = flow.FlowMaster(None, s) - ctx = flow.ScriptContext(fm) - - p = script.Script(shlex.split(tutils.test_data.path("scripts/a.py")+" --var 40", posix=(os.name != "nt")), ctx) + p = script.Script( + shlex.split(tutils.test_data.path("scripts/a.py")+" --var 40",posix=(os.name != "nt")), fm + ) p.load() assert "here" in p.ns @@ -50,28 +40,26 @@ class TestScript: def test_err(self): s = flow.State() fm = flow.FlowMaster(None, s) - ctx = flow.ScriptContext(fm) - - s = script.Script(["nonexistent"], ctx) + s = script.Script(["nonexistent"], fm) tutils.raises( "no such file", s.load ) - s = script.Script([tutils.test_data.path("scripts")], ctx) + s = script.Script([tutils.test_data.path("scripts")], fm) tutils.raises( "not a file", s.load ) - s = script.Script([tutils.test_data.path("scripts/syntaxerr.py")], ctx) + s = script.Script([tutils.test_data.path("scripts/syntaxerr.py")], fm) tutils.raises( script.ScriptError, s.load ) - s = script.Script([tutils.test_data.path("scripts/loaderr.py")], ctx) + s = script.Script([tutils.test_data.path("scripts/loaderr.py")], fm) tutils.raises( script.ScriptError, s.load @@ -82,38 +70,44 @@ class TestScript: fm = flow.FlowMaster(None, s) fm.load_script([tutils.test_data.path("scripts/concurrent_decorator.py")]) - reply = TCounter() - r1, r2 = tutils.treq(), tutils.treq() - r1.reply, r2.reply = reply, reply - t_start = time.time() - fm.handle_request(r1) - r1.reply() - fm.handle_request(r2) - r2.reply() - assert reply.count < 2 - assert (time.time() - t_start) < 0.09 - time.sleep(0.2) - assert reply.count == 2 + with mock.patch("libmproxy.controller.DummyReply.__call__") as m: + r1, r2 = tutils.treq(), tutils.treq() + t_start = time.time() + fm.handle_request(r1) + r1.reply() + fm.handle_request(r2) + r2.reply() + + # Two instantiations + assert m.call_count == 2 + assert (time.time() - t_start) < 0.09 + time.sleep(0.2) + # Plus two invocations + assert m.call_count == 4 def test_concurrent2(self): - ctx = TScriptContext() - s = script.Script([tutils.test_data.path("scripts/concurrent_decorator.py")], ctx) + s = flow.State() + fm = flow.FlowMaster(None, s) + s = script.Script([tutils.test_data.path("scripts/concurrent_decorator.py")], fm) s.load() f = tutils.tflow_full() f.error = tutils.terr(f.request) f.reply = f.request.reply - s.run("clientconnect", f) - s.run("serverconnect", f) - s.run("response", f) - s.run("error", f) - s.run("clientdisconnect", f) - time.sleep(0.1) - assert ctx.count == 5 + with mock.patch("libmproxy.controller.DummyReply.__call__") as m: + s.run("clientconnect", f) + s.run("serverconnect", f) + s.run("response", f) + s.run("error", f) + s.run("clientdisconnect", f) + time.sleep(0.1) + assert m.call_count == 5 def test_concurrent_err(self): - s = script.Script([tutils.test_data.path("scripts/concurrent_decorator_err.py")], TScriptContext()) + s = flow.State() + fm = flow.FlowMaster(None, s) + s = script.Script([tutils.test_data.path("scripts/concurrent_decorator_err.py")], fm) tutils.raises( "decorator not supported for this method", s.load - ) \ No newline at end of file + ) -- cgit v1.2.3 From e5776b8be3ea36c065beabe416506871f34892e6 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sun, 12 Jan 2014 13:59:32 +1300 Subject: Clean up and clarify script API --- test/test_script.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) (limited to 'test/test_script.py') diff --git a/test/test_script.py b/test/test_script.py index 69386834..2664b840 100644 --- a/test/test_script.py +++ b/test/test_script.py @@ -41,28 +41,24 @@ class TestScript: s = flow.State() fm = flow.FlowMaster(None, s) - s = script.Script(["nonexistent"], fm) tutils.raises( "no such file", - s.load + script.Script, ["nonexistent"], fm ) - s = script.Script([tutils.test_data.path("scripts")], fm) tutils.raises( "not a file", - s.load + script.Script, [tutils.test_data.path("scripts")], fm ) - s = script.Script([tutils.test_data.path("scripts/syntaxerr.py")], fm) tutils.raises( script.ScriptError, - s.load + script.Script, [tutils.test_data.path("scripts/syntaxerr.py")], fm ) - s = script.Script([tutils.test_data.path("scripts/loaderr.py")], fm) tutils.raises( script.ScriptError, - s.load + script.Script, [tutils.test_data.path("scripts/loaderr.py")], fm ) def test_concurrent(self): @@ -106,8 +102,7 @@ class TestScript: def test_concurrent_err(self): s = flow.State() fm = flow.FlowMaster(None, s) - s = script.Script([tutils.test_data.path("scripts/concurrent_decorator_err.py")], fm) tutils.raises( "decorator not supported for this method", - s.load + script.Script, [tutils.test_data.path("scripts/concurrent_decorator_err.py")], fm ) -- cgit v1.2.3 From 42d4a2fae96b8b4ba35d3a88e20f278d79a0ccc6 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sun, 12 Jan 2014 23:01:59 +1300 Subject: Script refactoring: move script command parsing into script module. --- test/test_script.py | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) (limited to 'test/test_script.py') diff --git a/test/test_script.py b/test/test_script.py index 2664b840..39aa12e9 100644 --- a/test/test_script.py +++ b/test/test_script.py @@ -10,10 +10,8 @@ class TestScript: def test_simple(self): s = flow.State() fm = flow.FlowMaster(None, s) - p = script.Script( - shlex.split(tutils.test_data.path("scripts/a.py")+" --var 40",posix=(os.name != "nt")), fm - ) - p.load() + sp = tutils.test_data.path("scripts/a.py") + p = script.Script("%s --var 40"%sp, fm) assert "here" in p.ns assert p.run("here") == (True, 41) @@ -30,7 +28,7 @@ class TestScript: def test_duplicate_flow(self): s = flow.State() fm = flow.FlowMaster(None, s) - fm.load_script([tutils.test_data.path("scripts/duplicate_flow.py")]) + fm.load_script(tutils.test_data.path("scripts/duplicate_flow.py")) r = tutils.treq() fm.handle_request(r) assert fm.state.flow_count() == 2 @@ -43,28 +41,28 @@ class TestScript: tutils.raises( "no such file", - script.Script, ["nonexistent"], fm + script.Script, "nonexistent", fm ) tutils.raises( "not a file", - script.Script, [tutils.test_data.path("scripts")], fm + script.Script, tutils.test_data.path("scripts"), fm ) tutils.raises( script.ScriptError, - script.Script, [tutils.test_data.path("scripts/syntaxerr.py")], fm + script.Script, tutils.test_data.path("scripts/syntaxerr.py"), fm ) tutils.raises( script.ScriptError, - script.Script, [tutils.test_data.path("scripts/loaderr.py")], fm + script.Script, tutils.test_data.path("scripts/loaderr.py"), fm ) def test_concurrent(self): s = flow.State() fm = flow.FlowMaster(None, s) - fm.load_script([tutils.test_data.path("scripts/concurrent_decorator.py")]) + fm.load_script(tutils.test_data.path("scripts/concurrent_decorator.py")) with mock.patch("libmproxy.controller.DummyReply.__call__") as m: r1, r2 = tutils.treq(), tutils.treq() @@ -84,7 +82,7 @@ class TestScript: def test_concurrent2(self): s = flow.State() fm = flow.FlowMaster(None, s) - s = script.Script([tutils.test_data.path("scripts/concurrent_decorator.py")], fm) + s = script.Script(tutils.test_data.path("scripts/concurrent_decorator.py"), fm) s.load() f = tutils.tflow_full() f.error = tutils.terr(f.request) @@ -104,5 +102,15 @@ class TestScript: fm = flow.FlowMaster(None, s) tutils.raises( "decorator not supported for this method", - script.Script, [tutils.test_data.path("scripts/concurrent_decorator_err.py")], fm + script.Script, tutils.test_data.path("scripts/concurrent_decorator_err.py"), fm ) + + +def test_command_parsing(): + s = flow.State() + fm = flow.FlowMaster(None, s) + absfilepath = os.path.normcase(tutils.test_data.path("scripts/a.py")) + s = script.Script(absfilepath, fm) + assert os.path.isfile(s.argv[0]) + + -- cgit v1.2.3 From 4f69eef8f310b87a45782b8d097dd148e815486a Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Mon, 13 Jan 2014 14:15:17 +1300 Subject: Extract command parsing and use in script grid editor --- test/test_script.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test/test_script.py') diff --git a/test/test_script.py b/test/test_script.py index 39aa12e9..025e9f37 100644 --- a/test/test_script.py +++ b/test/test_script.py @@ -40,7 +40,7 @@ class TestScript: fm = flow.FlowMaster(None, s) tutils.raises( - "no such file", + "not found", script.Script, "nonexistent", fm ) -- cgit v1.2.3