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
, trust
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_maybe(self
, digests
):
137 if d
in self
.fake_impls
:
138 return '/fake_store/' + d
141 def lookup_any(self
, digests
):
142 path
= self
.lookup_maybe(digests
)
149 help_with_testing
= False
150 network_use
= model
.network_full
151 key_info_server
= None
152 auto_approve_keys
= False
155 self
.iface_cache
= iface_cache
.IfaceCache()
156 self
.handler
= DummyHandler()
157 self
.stores
= Stores()
158 self
.fetcher
= TestFetcher(self
)
159 self
.trust_db
= trust
.trust_db
160 self
.trust_mgr
= trust
.TrustMgr(self
)
162 class BaseTest(unittest
.TestCase
):
164 warnings
.resetwarnings()
166 self
.config_home
= tempfile
.mktemp()
167 self
.cache_home
= tempfile
.mktemp()
168 self
.cache_system
= tempfile
.mktemp()
169 self
.gnupg_home
= tempfile
.mktemp()
170 os
.environ
['GNUPGHOME'] = self
.gnupg_home
171 os
.environ
['XDG_CONFIG_HOME'] = self
.config_home
172 os
.environ
['XDG_CONFIG_DIRS'] = ''
173 os
.environ
['XDG_CACHE_HOME'] = self
.cache_home
174 os
.environ
['XDG_CACHE_DIRS'] = self
.cache_system
176 assert basedir
.xdg_config_home
== self
.config_home
178 os
.mkdir(self
.config_home
, 0700)
179 os
.mkdir(self
.cache_home
, 0700)
180 os
.mkdir(self
.cache_system
, 0500)
181 os
.mkdir(self
.gnupg_home
, 0700)
183 if 'DISPLAY' in os
.environ
:
184 del os
.environ
['DISPLAY']
186 self
.config
= TestConfig()
187 policy
._config
= self
.config
# XXX
188 iface_cache
.iface_cache
= self
.config
.iface_cache
190 logging
.getLogger().setLevel(logging
.WARN
)
192 download
._downloads
= {}
194 self
.old_path
= os
.environ
['PATH']
195 os
.environ
['PATH'] = dpkgdir
+ ':' + self
.old_path
197 distro
._host
_distribution
= distro
.DebianDistribution(dpkgdir
+ '/status',
198 dpkgdir
+ '/pkgcache.bin')
199 distro
._host
_distribution
._packagekit
= DummyPackageKit()
201 my_dbus
.system_services
= {}
204 assert self
.config
.handler
.ex
is None, self
.config
.handler
.ex
206 shutil
.rmtree(self
.config_home
)
207 support
.ro_rmtree(self
.cache_home
)
208 shutil
.rmtree(self
.cache_system
)
209 shutil
.rmtree(self
.gnupg_home
)
211 os
.environ
['PATH'] = self
.old_path
213 def import_feed(self
, url
, path
):
214 iface_cache
= self
.config
.iface_cache
215 iface_cache
.get_interface(url
)
216 feed
= iface_cache
._feeds
[url
] = reader
.load_feed(path
)