2 import sys
, tempfile
, os
, shutil
, StringIO
6 from xml
.dom
import minidom
7 warnings
.filterwarnings("ignore", message
= 'The CObject type')
9 # Catch silly mistakes...
10 os
.environ
['HOME'] = '/home/idontexist'
11 os
.environ
['LANGUAGE'] = 'C'
13 sys
.path
.insert(0, '..')
14 from zeroinstall
.injector
import qdom
15 from zeroinstall
.injector
import iface_cache
, download
, distro
, model
, handler
, policy
, reader
16 from zeroinstall
.zerostore
import NotStored
, Store
, Stores
; Store
._add
_with
_helper
= lambda *unused
: False
17 from zeroinstall
import support
18 from zeroinstall
.support
import basedir
, tasks
20 dpkgdir
= os
.path
.join(os
.path
.dirname(__file__
), 'dpkg')
22 empty_feed
= qdom
.parse(StringIO
.StringIO("""<interface xmlns='http://zero-install.sourceforge.net/2004/injector/interface'>
24 <summary>just for testing</summary>
28 sys
.modules
['dbus'] = my_dbus
29 sys
.modules
['dbus.glib'] = my_dbus
30 my_dbus
.types
= my_dbus
31 sys
.modules
['dbus.types'] = my_dbus
32 sys
.modules
['dbus.mainloop'] = my_dbus
33 sys
.modules
['dbus.mainloop.glib'] = my_dbus
35 mydir
= os
.path
.dirname(__file__
)
37 # Catch us trying to run the GUI and return a dummy string instead
38 old_execvp
= os
.execvp
39 def test_execvp(prog
, args
):
40 if prog
.endswith('/0launch-gui'):
41 prog
= os
.path
.join(mydir
, 'test-gui')
42 return old_execvp(prog
, args
)
44 os
.execvp
= test_execvp
46 test_locale
= (None, None)
49 LC_ALL
= 0 # Note: LC_MESSAGES not present on Windows
50 def getlocale(self
, x
):
52 model
.locale
= TestLocale()
54 class DummyPackageKit
:
57 def get_candidates(self
, package
, factory
, prefix
):
60 class DummyHandler(handler
.Handler
):
61 __slots__
= ['ex', 'tb', 'allow_downloads']
64 handler
.Handler
.__init
__(self
)
66 self
.allow_downloads
= False
68 def get_download(self
, url
, force
= False, hint
= None, factory
= None):
69 if self
.allow_downloads
:
70 return handler
.Handler
.get_download(self
, url
, force
, hint
, factory
)
71 raise model
.SafeException("DummyHandler: " + url
)
73 def wait_for_blocker(self
, blocker
):
75 handler
.Handler
.wait_for_blocker(self
, blocker
)
77 raise self
.ex
, None, self
.tb
79 def report_error(self
, ex
, tb
= None):
80 assert self
.ex
is None, self
.ex
85 #traceback.print_exc()
88 def __init__(self
, fpr
):
90 self
.info
= [minidom
.parseString('<item vote="bad"/>')]
94 def __init__(self
, config
):
95 self
.allowed_downloads
= set()
96 self
.allowed_feed_downloads
= {}
99 def allow_download(self
, digest
):
100 assert isinstance(self
.config
.stores
, TestStores
)
101 self
.allowed_downloads
.add(digest
)
103 def allow_feed_download(self
, url
, feed
):
104 self
.allowed_feed_downloads
[url
] = feed
106 def download_impls(self
, impls
, stores
):
111 assert impl
.id in self
.allowed_downloads
, impl
112 self
.allowed_downloads
.remove(impl
.id)
113 self
.config
.stores
.add_fake(impl
.id)
114 return fake_download()
116 def download_and_import_feed(self
, feed_url
, iface_cache
, force
= False):
120 assert feed_url
in self
.allowed_feed_downloads
, feed_url
121 self
.config
.iface_cache
._feeds
[feed_url
] = self
.allowed_feed_downloads
[feed_url
]
122 del self
.allowed_feed_downloads
[feed_url
]
123 return fake_download()
125 def fetch_key_info(self
, fingerprint
):
126 return DummyKeyInfo(fingerprint
)
130 self
.fake_impls
= set()
132 def add_fake(self
, digest
):
133 self
.fake_impls
.add(digest
)
135 def lookup_any(self
, digests
):
137 if d
in self
.fake_impls
:
138 return '/fake_store/' + d
143 help_with_testing
= False
144 network_use
= model
.network_full
147 self
.iface_cache
= iface_cache
.IfaceCache()
148 self
.handler
= DummyHandler()
149 self
.stores
= Stores()
150 self
.fetcher
= TestFetcher(self
)
152 class BaseTest(unittest
.TestCase
):
154 warnings
.resetwarnings()
156 self
.config_home
= tempfile
.mktemp()
157 self
.cache_home
= tempfile
.mktemp()
158 self
.cache_system
= tempfile
.mktemp()
159 self
.gnupg_home
= tempfile
.mktemp()
160 os
.environ
['GNUPGHOME'] = self
.gnupg_home
161 os
.environ
['XDG_CONFIG_HOME'] = self
.config_home
162 os
.environ
['XDG_CONFIG_DIRS'] = ''
163 os
.environ
['XDG_CACHE_HOME'] = self
.cache_home
164 os
.environ
['XDG_CACHE_DIRS'] = self
.cache_system
166 assert basedir
.xdg_config_home
== self
.config_home
168 os
.mkdir(self
.config_home
, 0700)
169 os
.mkdir(self
.cache_home
, 0700)
170 os
.mkdir(self
.cache_system
, 0500)
171 os
.mkdir(self
.gnupg_home
, 0700)
173 if os
.environ
.has_key('DISPLAY'):
174 del os
.environ
['DISPLAY']
176 self
.config
= TestConfig()
177 policy
._config
= self
.config
# XXX
178 iface_cache
.iface_cache
= self
.config
.iface_cache
180 logging
.getLogger().setLevel(logging
.WARN
)
182 download
._downloads
= {}
184 self
.old_path
= os
.environ
['PATH']
185 os
.environ
['PATH'] = dpkgdir
+ ':' + self
.old_path
187 distro
._host
_distribution
= distro
.DebianDistribution(dpkgdir
+ '/status',
188 dpkgdir
+ '/pkgcache.bin')
189 distro
._host
_distribution
._packagekit
= DummyPackageKit()
191 my_dbus
.system_services
= {}
194 assert self
.config
.handler
.ex
is None, self
.config
.handler
.ex
196 shutil
.rmtree(self
.config_home
)
197 support
.ro_rmtree(self
.cache_home
)
198 shutil
.rmtree(self
.cache_system
)
199 shutil
.rmtree(self
.gnupg_home
)
201 os
.environ
['PATH'] = self
.old_path
203 def import_feed(self
, url
, path
):
204 iface_cache
= self
.config
.iface_cache
205 iface_cache
.get_interface(url
)
206 iface_cache
._feeds
[url
] = reader
.load_feed(path
)