From db890b8ed84a7c1c66b4dd17a0969dc1d741636a Mon Sep 17 00:00:00 2001 From: Luke Campagnola Date: Wed, 6 Sep 2017 09:13:56 -0700 Subject: [PATCH] Add unit test for reload(); make travis use --assert=reinterp (because assert=rewrite does not work with reload) --- .travis.yml | 3 +- pyqtgraph/tests/test_reload.py | 74 ++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 pyqtgraph/tests/test_reload.py diff --git a/.travis.yml b/.travis.yml index c4a67ac3..1c075090 100644 --- a/.travis.yml +++ b/.travis.yml @@ -143,7 +143,8 @@ script: # Run unit tests - start_test "unit tests"; - PYTHONPATH=. py.test --cov pyqtgraph -sv; + # NOTE: tests/test_reload.py breaks without --assert=reinterp|plain + PYTHONPATH=. py.test --cov --assert=reinterp pyqtgraph -sv; check_output "unit tests"; - echo "test script finished. Current directory:" - pwd diff --git a/pyqtgraph/tests/test_reload.py b/pyqtgraph/tests/test_reload.py new file mode 100644 index 00000000..4b670fb6 --- /dev/null +++ b/pyqtgraph/tests/test_reload.py @@ -0,0 +1,74 @@ +import tempfile, os, sys, shutil, atexit +import pyqtgraph as pg +import pyqtgraph.reload +pgpath = os.path.join(os.path.dirname(pg.__file__), '..') + +# make temporary directory to write module code +path = tempfile.mkdtemp() +sys.path.insert(0, path) +def cleanup(): + shutil.rmtree(path) +atexit.register(cleanup) + + +code = """ +import sys +sys.path.append('{path}') + +import pyqtgraph as pg + +class C(pg.QtCore.QObject): + sig = pg.QtCore.Signal() + def fn(self): + print("{msg}") + +""" + +def test_reload(): + # write a module + mod = os.path.join(path, 'reload_test.py') + open(mod, 'w').write(code.format(path=path, msg="C.fn() Version1")) + + # import the new module + import reload_test + + c = reload_test.C() + c.sig.connect(c.fn) + v1 = (reload_test.C, reload_test.C.sig, reload_test.C.fn, reload_test.C.fn.__func__, c.sig, c.fn, c.fn.__func__) + + + + # write again and reload + open(mod, 'w').write(code.format(path=path, msg="C.fn() Version2")) + os.remove(mod+'c') + pg.reload.reloadAll(path, debug=True) + v2 = (reload_test.C, reload_test.C.sig, reload_test.C.fn, reload_test.C.fn.__func__, c.sig, c.fn, c.fn.__func__) + + assert c.fn.im_class is v2[0] + oldcfn = pg.reload.getPreviousVersion(c.fn) + assert oldcfn.im_class is v1[0] + assert oldcfn.im_func is v1[2].im_func + assert oldcfn.im_self is c + + + + # write again and reload + open(mod, 'w').write(code.format(path=path, msg="C.fn() Version2")) + os.remove(mod+'c') + pg.reload.reloadAll(path, debug=True) + v3 = (reload_test.C, reload_test.C.sig, reload_test.C.fn, reload_test.C.fn.__func__, c.sig, c.fn, c.fn.__func__) + + #for i in range(len(old)): + #print id(old[i]), id(new1[i]), id(new2[i]), old[i], new1[i] + + cfn1 = pg.reload.getPreviousVersion(c.fn) + cfn2 = pg.reload.getPreviousVersion(cfn1) + + assert cfn1.im_class is v2[0] + assert cfn1.im_func is v2[2].im_func + assert cfn1.im_self is c + assert cfn2.im_class is v1[0] + assert cfn2.im_func is v1[2].im_func + assert cfn2.im_self is c + c.sig.disconnect(cfn2) +