diff options
author | Aldo Cortesi <aldo@nullcube.com> | 2016-11-03 19:36:34 +1300 |
---|---|---|
committer | Aldo Cortesi <aldo@nullcube.com> | 2016-11-03 19:36:34 +1300 |
commit | e9a96f4d7f087bb9f01a48224a0b0090635c91f1 (patch) | |
tree | 2921d027a45cf8698cdb2c6c97ef97a8edd06fe7 /test | |
parent | d9538637c3f235f10e38ff58879867e2aaadd529 (diff) | |
download | mitmproxy-e9a96f4d7f087bb9f01a48224a0b0090635c91f1.tar.gz mitmproxy-e9a96f4d7f087bb9f01a48224a0b0090635c91f1.tar.bz2 mitmproxy-e9a96f4d7f087bb9f01a48224a0b0090635c91f1.zip |
addons.script: 100% test coverage
Diffstat (limited to 'test')
-rw-r--r-- | test/mitmproxy/addons/test_script.py | 51 |
1 files changed, 49 insertions, 2 deletions
diff --git a/test/mitmproxy/addons/test_script.py b/test/mitmproxy/addons/test_script.py index 23e9f1f1..c31f4e9b 100644 --- a/test/mitmproxy/addons/test_script.py +++ b/test/mitmproxy/addons/test_script.py @@ -9,12 +9,59 @@ from mitmproxy.test import taddons from mitmproxy import exceptions from mitmproxy import options from mitmproxy import proxy -from mitmproxy.addons import script from mitmproxy import master +from mitmproxy.addons import script + +import watchdog.events + from .. import tutils as ttutils +def test_ns(): + n = script.NS({}) + n.one = "one" + assert n.one == "one" + assert n.__dict__["ns"]["one"] == "one" + + +def test_scriptenv(): + with taddons.context() as tctx: + with script.scriptenv("path", []): + raise SystemExit + assert tctx.master.event_log[0][0] == "error" + assert "exited" in tctx.master.event_log[0][1] + + tctx.master.clear() + with script.scriptenv("path", []): + raise ValueError("fooo") + assert tctx.master.event_log[0][0] == "error" + assert "foo" in tctx.master.event_log[0][1] + + +class Called: + def __init__(self): + self.called = False + + def __call__(self, *args, **kwargs): + self.called = True + + +def test_reloadhandler(): + rh = script.ReloadHandler(Called()) + assert not rh.filter(watchdog.events.DirCreatedEvent("path")) + assert not rh.filter(watchdog.events.FileModifiedEvent("/foo/.bar")) + assert rh.filter(watchdog.events.FileModifiedEvent("/foo/bar")) + + assert not rh.callback.called + rh.on_modified(watchdog.events.FileModifiedEvent("/foo/bar")) + assert rh.callback.called + rh.callback.called = False + + rh.on_created(watchdog.events.FileCreatedEvent("foo")) + assert rh.callback.called + + class TestParseCommand: def test_empty_command(self): with tutils.raises(exceptions.AddonError): @@ -65,7 +112,7 @@ def test_load_script(): class TestScript: def test_simple(self): - with taddons.context() as tctx: + with taddons.context(): sc = script.Script( tutils.test_data.path( "mitmproxy/data/addonscripts/recorder.py" |