Fix some bugs found by pychecker
[zeroinstall.git] / tests / basetest.py
blob583cbc01eee409a19a9ceb5c27a49cd8220bdee5
1 #!/usr/bin/env python
2 import sys, tempfile, os, shutil, StringIO
3 import unittest
4 import logging
5 import warnings
7 # Catch silly mistakes...
8 os.environ['HOME'] = '/home/idontexist'
9 os.environ['LANGUAGE'] = 'C'
11 sys.path.insert(0, '..')
12 from zeroinstall.injector import qdom
13 from zeroinstall.injector import iface_cache, download, distro, model
14 from zeroinstall.zerostore import Store; Store._add_with_helper = lambda *unused: False
15 from zeroinstall import support, helpers
16 from zeroinstall.support import basedir
18 dpkgdir = os.path.join(os.path.dirname(__file__), 'dpkg')
20 empty_feed = qdom.parse(StringIO.StringIO("""<interface xmlns='http://zero-install.sourceforge.net/2004/injector/interface'>
21 <name>Empty</name>
22 <summary>just for testing</summary>
23 </interface>"""))
25 import my_dbus
26 sys.modules['dbus'] = my_dbus
27 sys.modules['dbus.glib'] = my_dbus
28 my_dbus.types = my_dbus
29 sys.modules['dbus.types'] = my_dbus
30 sys.modules['dbus.mainloop'] = my_dbus
31 sys.modules['dbus.mainloop.glib'] = my_dbus
33 mydir = os.path.dirname(__file__)
35 # Catch us trying to run the GUI and return a dummy string instead
36 old_execvp = os.execvp
37 def test_execvp(prog, args):
38 if prog.endswith('/0launch-gui'):
39 prog = os.path.join(mydir, 'test-gui')
40 return old_execvp(prog, args)
42 os.execvp = test_execvp
44 test_locale = (None, None)
45 assert model.locale
46 class TestLocale:
47 LC_ALL = 0 # Note: LC_MESSAGES not present on Windows
48 def getlocale(self, x):
49 return test_locale
50 model.locale = TestLocale()
52 class DummyPackageKit:
53 available = False
55 def get_candidates(self, package, factory, prefix):
56 pass
58 class BaseTest(unittest.TestCase):
59 def setUp(self):
60 warnings.resetwarnings()
61 self.config_home = tempfile.mktemp()
62 self.cache_home = tempfile.mktemp()
63 self.cache_system = tempfile.mktemp()
64 self.gnupg_home = tempfile.mktemp()
65 os.environ['GNUPGHOME'] = self.gnupg_home
66 os.environ['XDG_CONFIG_HOME'] = self.config_home
67 os.environ['XDG_CONFIG_DIRS'] = ''
68 os.environ['XDG_CACHE_HOME'] = self.cache_home
69 os.environ['XDG_CACHE_DIRS'] = self.cache_system
70 reload(basedir)
71 assert basedir.xdg_config_home == self.config_home
72 iface_cache.iface_cache.__init__()
74 os.mkdir(self.config_home, 0700)
75 os.mkdir(self.cache_home, 0700)
76 os.mkdir(self.cache_system, 0500)
77 os.mkdir(self.gnupg_home, 0700)
79 if os.environ.has_key('DISPLAY'):
80 del os.environ['DISPLAY']
82 logging.getLogger().setLevel(logging.WARN)
84 download._downloads = {}
86 self.old_path = os.environ['PATH']
87 os.environ['PATH'] = dpkgdir + ':' + self.old_path
89 distro._host_distribution = distro.DebianDistribution(dpkgdir + '/status',
90 dpkgdir + '/pkgcache.bin')
91 distro._host_distribution._packagekit = DummyPackageKit()
93 def tearDown(self):
94 shutil.rmtree(self.config_home)
95 support.ro_rmtree(self.cache_home)
96 shutil.rmtree(self.cache_system)
97 shutil.rmtree(self.gnupg_home)
99 os.environ['PATH'] = self.old_path