From eba970473c224e0c211b9f0a6939c9d427b019f4 Mon Sep 17 00:00:00 2001 From: Thomas Leonard Date: Sat, 26 Apr 2008 13:27:37 +0100 Subject: [PATCH] Added unit-tests for background updates Fixed a bug they found. --- tests/my_dbus.py | 31 +++++++++++++++++++ tests/testautopolicy.py | 3 ++ tests/testdownload.py | 63 ++++++++++++++++++++++++++++++++++++-- tests/testsolver.py | 1 + zeroinstall/injector/background.py | 6 +--- 5 files changed, 97 insertions(+), 7 deletions(-) create mode 100644 tests/my_dbus.py diff --git a/tests/my_dbus.py b/tests/my_dbus.py new file mode 100644 index 0000000..a473858 --- /dev/null +++ b/tests/my_dbus.py @@ -0,0 +1,31 @@ +user_callback = None # Invoked in a callback when a question box is displayed + +class SessionBus: + def get_object(self, service, path): + return None + +class Interface: + def __init__(self, obj, iface): + self.callback = {} + self.boxes = [] + + def GetCapabilities(self): + pass + + def Notify(self, *args): + self.boxes.append(args) + nid = len(self.boxes) + + app, replaces_id, icon, title, message, actions, hints, timeout = args + + if actions: + user_callback(self.callback['ActionInvoked'], nid, actions) + + return nid + + def connect_to_signal(self, signal, callback): + self.callback[signal] = callback + +class Byte: + def __init__(self, value): + pass diff --git a/tests/testautopolicy.py b/tests/testautopolicy.py index c6131bc..32a3a44 100755 --- a/tests/testautopolicy.py +++ b/tests/testautopolicy.py @@ -200,6 +200,9 @@ class TestAutoPolicy(BaseTest): os.environ['BAR_PATH']) self.assertEquals(cached_impl + '/.:/usr/local/share:/usr/share', os.environ['XDG_DATA_DIRS']) + + policy.download_only = True + policy.download_and_execute(['Hello']) def testFeeds(self): self.cache_iface(foo_iface_uri, diff --git a/tests/testdownload.py b/tests/testdownload.py index d552a25..2d28e56 100755 --- a/tests/testdownload.py +++ b/tests/testdownload.py @@ -7,13 +7,25 @@ from logging import getLogger, DEBUG, INFO, WARN, ERROR sys.path.insert(0, '..') -from zeroinstall.injector import model, autopolicy, gpg, iface_cache, download, reader, trust, handler +from zeroinstall.injector import model, autopolicy, gpg, iface_cache, download, reader, trust, handler, background, arch from zeroinstall.zerostore import Store; Store._add_with_helper = lambda *unused: False -from zeroinstall.support import basedir +from zeroinstall.support import basedir, tasks import data +import my_dbus import server +ran_gui = False +def raise_gui(*args): + global ran_gui + ran_gui = True +background._detach = lambda: False +background._exec_gui = raise_gui +sys.modules['dbus'] = my_dbus +sys.modules['dbus.glib'] = my_dbus +my_dbus.types = my_dbus +sys.modules['dbus.types'] = my_dbus + class Reply: def __init__(self, reply): self.reply = reply @@ -264,6 +276,53 @@ class TestDownload(BaseTest): finally: sys.stdout = old_out + def testBackground(self): + p = autopolicy.AutoPolicy('http://localhost:8000/Hello.xml') + reader.update(iface_cache.iface_cache.get_interface(p.root), 'Hello.xml') + p.freshness = 0 + p.network_use = model.network_minimal + p.solver.solve(p.root, arch.get_host_architecture()) + assert p.ready + + @tasks.async + def choose_download(registed_cb, nid, actions): + try: + assert actions == ['download', 'Download'], actions + registed_cb(nid, 'download') + except: + import traceback + traceback.print_exc() + yield None + + global ran_gui + ran_gui = False + old_out = sys.stdout + try: + sys.stdout = StringIO() + self.child = server.handle_requests('Hello.xml', '6FCF121BE2390E0B.gpg') + my_dbus.user_callback = choose_download + pid = os.getpid() + old_exit = os._exit + def my_exit(code): + # The background handler runs in the same process + # as the tests, so don't let it abort. + if os.getpid() == pid: + raise SystemExit(code) + # But, child download processes are OK + old_exit(code) + try: + try: + os._exit = my_exit + background.spawn_background_update(p, False) + assert False + except SystemExit, ex: + self.assertEquals(1, ex.code) + finally: + os._exit = old_exit + finally: + sys.stdout = old_out + assert ran_gui + suite = unittest.makeSuite(TestDownload) if __name__ == '__main__': sys.argv.append('-v') diff --git a/tests/testsolver.py b/tests/testsolver.py index c882452..39fabf0 100755 --- a/tests/testsolver.py +++ b/tests/testsolver.py @@ -24,6 +24,7 @@ class TestSolver(BaseTest): reader.update(compiler, 'Compiler.xml') binary_arch = arch.Architecture({None: 1}, {None: 1}) + assert str(binary_arch).startswith("