WIP: Requirements object
[zeroinstall.git] / tests / basetest.py
blobce4e2d8a8274d77fd948d1b388509f5545ec464f
1 #!/usr/bin/env python
2 import sys, tempfile, os, shutil, StringIO
3 import unittest
4 import logging
5 import warnings
6 warnings.filterwarnings("ignore", message = 'The CObject type')
8 # Catch silly mistakes...
9 os.environ['HOME'] = '/home/idontexist'
10 os.environ['LANGUAGE'] = 'C'
12 sys.path.insert(0, '..')
13 from zeroinstall.injector import qdom
14 from zeroinstall.injector import iface_cache, download, distro, model, handler, policy
15 from zeroinstall.zerostore import Store, Stores; Store._add_with_helper = lambda *unused: False
16 from zeroinstall import support, helpers
17 from zeroinstall.support import basedir
19 dpkgdir = os.path.join(os.path.dirname(__file__), 'dpkg')
21 empty_feed = qdom.parse(StringIO.StringIO("""<interface xmlns='http://zero-install.sourceforge.net/2004/injector/interface'>
22 <name>Empty</name>
23 <summary>just for testing</summary>
24 </interface>"""))
26 import my_dbus
27 sys.modules['dbus'] = my_dbus
28 sys.modules['dbus.glib'] = my_dbus
29 my_dbus.types = my_dbus
30 sys.modules['dbus.types'] = my_dbus
31 sys.modules['dbus.mainloop'] = my_dbus
32 sys.modules['dbus.mainloop.glib'] = my_dbus
34 mydir = os.path.dirname(__file__)
36 # Catch us trying to run the GUI and return a dummy string instead
37 old_execvp = os.execvp
38 def test_execvp(prog, args):
39 if prog.endswith('/0launch-gui'):
40 prog = os.path.join(mydir, 'test-gui')
41 return old_execvp(prog, args)
43 os.execvp = test_execvp
45 test_locale = (None, None)
46 assert model.locale
47 class TestLocale:
48 LC_ALL = 0 # Note: LC_MESSAGES not present on Windows
49 def getlocale(self, x):
50 return test_locale
51 model.locale = TestLocale()
53 class DummyPackageKit:
54 available = False
56 def get_candidates(self, package, factory, prefix):
57 pass
59 class DummyHandler(handler.Handler):
60 __slots__ = ['ex', 'tb']
62 def __init__(self):
63 handler.Handler.__init__(self)
64 self.ex = None
66 def wait_for_blocker(self, blocker):
67 self.ex = None
68 handler.Handler.wait_for_blocker(self, blocker)
69 if self.ex:
70 raise self.ex, None, self.tb
72 def report_error(self, ex, tb = None):
73 assert self.ex is None, self.ex
74 self.ex = ex
75 self.tb = tb
77 #import traceback
78 #traceback.print_exc()
80 class TestFetcher:
81 def download_impls(self, impls, stores):
82 assert impls == [], impls
84 class TestConfig:
85 freshness = 0
86 help_test_new_versions = False
87 network_use = model.network_full
89 def __init__(self):
90 self.iface_cache = iface_cache.IfaceCache()
91 self.handler = DummyHandler()
92 self.stores = Stores()
93 self.fetcher = TestFetcher()
95 class BaseTest(unittest.TestCase):
96 def setUp(self):
97 warnings.resetwarnings()
99 self.config_home = tempfile.mktemp()
100 self.cache_home = tempfile.mktemp()
101 self.cache_system = tempfile.mktemp()
102 self.gnupg_home = tempfile.mktemp()
103 os.environ['GNUPGHOME'] = self.gnupg_home
104 os.environ['XDG_CONFIG_HOME'] = self.config_home
105 os.environ['XDG_CONFIG_DIRS'] = ''
106 os.environ['XDG_CACHE_HOME'] = self.cache_home
107 os.environ['XDG_CACHE_DIRS'] = self.cache_system
108 reload(basedir)
109 assert basedir.xdg_config_home == self.config_home
111 os.mkdir(self.config_home, 0700)
112 os.mkdir(self.cache_home, 0700)
113 os.mkdir(self.cache_system, 0500)
114 os.mkdir(self.gnupg_home, 0700)
116 if os.environ.has_key('DISPLAY'):
117 del os.environ['DISPLAY']
119 self.config = TestConfig()
120 policy._config = self.config # XXX
122 logging.getLogger().setLevel(logging.WARN)
124 download._downloads = {}
126 self.old_path = os.environ['PATH']
127 os.environ['PATH'] = dpkgdir + ':' + self.old_path
129 distro._host_distribution = distro.DebianDistribution(dpkgdir + '/status',
130 dpkgdir + '/pkgcache.bin')
131 distro._host_distribution._packagekit = DummyPackageKit()
133 my_dbus.system_services = {}
135 def tearDown(self):
136 assert self.config.handler.ex is None, self.config.handler.ex
138 shutil.rmtree(self.config_home)
139 support.ro_rmtree(self.cache_home)
140 shutil.rmtree(self.cache_system)
141 shutil.rmtree(self.gnupg_home)
143 os.environ['PATH'] = self.old_path