Added '0install config' sub-command
[zeroinstall.git] / tests / basetest.py
blobc91e01aec4cfa4dc56ea94cfd2ab3347b8947471
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
15 from zeroinstall.zerostore import Store; 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 BaseTest(unittest.TestCase):
60 def setUp(self):
61 warnings.resetwarnings()
62 self.config_home = tempfile.mktemp()
63 self.cache_home = tempfile.mktemp()
64 self.cache_system = tempfile.mktemp()
65 self.gnupg_home = tempfile.mktemp()
66 os.environ['GNUPGHOME'] = self.gnupg_home
67 os.environ['XDG_CONFIG_HOME'] = self.config_home
68 os.environ['XDG_CONFIG_DIRS'] = ''
69 os.environ['XDG_CACHE_HOME'] = self.cache_home
70 os.environ['XDG_CACHE_DIRS'] = self.cache_system
71 reload(basedir)
72 assert basedir.xdg_config_home == self.config_home
73 iface_cache.iface_cache.__init__()
75 os.mkdir(self.config_home, 0700)
76 os.mkdir(self.cache_home, 0700)
77 os.mkdir(self.cache_system, 0500)
78 os.mkdir(self.gnupg_home, 0700)
80 if os.environ.has_key('DISPLAY'):
81 del os.environ['DISPLAY']
83 logging.getLogger().setLevel(logging.WARN)
85 download._downloads = {}
87 self.old_path = os.environ['PATH']
88 os.environ['PATH'] = dpkgdir + ':' + self.old_path
90 distro._host_distribution = distro.DebianDistribution(dpkgdir + '/status',
91 dpkgdir + '/pkgcache.bin')
92 distro._host_distribution._packagekit = DummyPackageKit()
94 my_dbus.system_services = {}
96 def tearDown(self):
97 shutil.rmtree(self.config_home)
98 support.ro_rmtree(self.cache_home)
99 shutil.rmtree(self.cache_system)
100 shutil.rmtree(self.gnupg_home)
102 os.environ['PATH'] = self.old_path