mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2025-01-31 23:38:46 +00:00
add tests for mitmproxy.script.reloader
This commit is contained in:
parent
898f5d10b9
commit
7e49b8c186
@ -1,7 +1,7 @@
|
||||
import os
|
||||
import sys
|
||||
from watchdog.events import RegexMatchingEventHandler
|
||||
if sys.platform == 'darwin':
|
||||
if sys.platform == 'darwin': # pragma: no cover
|
||||
from watchdog.observers.polling import PollingObserver as Observer
|
||||
else:
|
||||
from watchdog.observers import Observer
|
||||
@ -14,8 +14,8 @@ _observers = {}
|
||||
def watch(script, callback):
|
||||
if script in _observers:
|
||||
raise RuntimeError("Script already observed")
|
||||
script_dir = os.path.dirname(os.path.abspath(script.args[0]))
|
||||
script_name = os.path.basename(script.args[0])
|
||||
script_dir = os.path.dirname(os.path.abspath(script.filename))
|
||||
script_name = os.path.basename(script.filename)
|
||||
event_handler = _ScriptModificationHandler(callback, filename=script_name)
|
||||
observer = Observer()
|
||||
observer.schedule(event_handler, script_dir)
|
||||
|
29
test/mitmproxy/script/test_reloader.py
Normal file
29
test/mitmproxy/script/test_reloader.py
Normal file
@ -0,0 +1,29 @@
|
||||
import mock
|
||||
from mitmproxy.script.reloader import watch, unwatch
|
||||
from test.mitmproxy import tutils
|
||||
from threading import Event
|
||||
|
||||
|
||||
def test_simple():
|
||||
with tutils.tmpdir():
|
||||
with open("foo.py", "wb"):
|
||||
pass
|
||||
|
||||
script = mock.Mock()
|
||||
script.filename = "foo.py"
|
||||
|
||||
e = Event()
|
||||
|
||||
def _onchange():
|
||||
e.set()
|
||||
|
||||
watch(script, _onchange)
|
||||
with tutils.raises("already observed"):
|
||||
watch(script, _onchange)
|
||||
|
||||
with open("foo.py", "ab") as f:
|
||||
f.write(".")
|
||||
|
||||
assert e.wait(10)
|
||||
|
||||
unwatch(script)
|
Loading…
Reference in New Issue
Block a user