2 from basetest
import BaseTest
, empty_feed
, DummyPackageKit
3 import sys
, os
, tempfile
, imp
4 from StringIO
import StringIO
7 sys
.path
.insert(0, '..')
8 from zeroinstall
.injector
import distro
, model
, qdom
, iface_cache
, handler
9 from zeroinstall
.support
import basedir
11 def parse_impls(impls
):
12 xml
= """<?xml version="1.0" ?>
13 <interface xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
15 <summary>Foo</summary>
16 <description>Foo</description>
18 </interface>""" % impls
19 element
= qdom
.parse(StringIO(xml
))
20 return model
.ZeroInstallFeed(element
, "myfeed.xml")
22 class TestDistro(BaseTest
):
25 self
.feed
= model
.ZeroInstallFeed(empty_feed
, local_path
= '/empty.xml')
28 BaseTest
.tearDown(self
)
31 src
= tempfile
.NamedTemporaryFile()
33 cache
= distro
.Cache('test-cache', src
.name
, 1)
34 self
.assertEqual(None, cache
.get("foo"))
36 self
.assertEqual("1", cache
.get("foo"))
38 self
.assertEqual("2", cache
.get("foo"))
41 cache
= distro
.Cache('test-cache', src
.name
, 1)
42 self
.assertEqual("2", cache
.get("foo"))
47 self
.assertEqual(None, cache
.get("foo"))
50 # new cache... (format change)
51 cache
= distro
.Cache('test-cache', src
.name
, 2)
52 self
.assertEqual(None, cache
.get("foo"))
57 def make_factory(self
, distro
):
58 def factory(id, only_if_missing
= False, installed
= True):
59 assert not only_if_missing
60 impl
= model
.DistributionImplementation(self
.feed
, id, distro
)
61 assert id not in self
.feed
.implementations
62 self
.feed
.implementations
[id] = impl
63 impl
.installed
= installed
67 def testDefault(self
):
68 host
= distro
.Distribution()
70 factory
= self
.make_factory(host
)
71 host
.get_package_info('gimp', factory
)
72 self
.assertEqual(self
.feed
.implementations
, {})
74 # Special case: we can always find a version of Python
75 master_feed
= model
.ZeroInstallFeed(None)
76 master_feed
.url
= 'http://repo.roscidus.com/python/python'
77 feed
= host
.get_feed(master_feed
)
78 self
.assertEqual(1, len(feed
.implementations
))
81 dpkgdir
= os
.path
.join(os
.path
.dirname(__file__
), 'dpkg')
82 host
= distro
.DebianDistribution(
83 os
.path
.join(dpkgdir
, 'status'))
84 host
._packagekit
= DummyPackageKit()
86 factory
= self
.make_factory(host
)
87 host
.get_package_info('gimp', factory
)
88 self
.assertEqual({}, self
.feed
.implementations
)
90 # Initially, we only get information about the installed version...
91 host
.get_package_info('python-bittorrent', factory
)
92 self
.assertEqual(1, len(self
.feed
.implementations
))
94 # Tell distro to fetch information about candidates...
95 master_feed
= parse_impls("""<package-implementation package='python-bittorrent'/>""")
97 candidates
= host
.fetch_candidates(master_feed
)
99 h
.wait_for_blocker(candidates
)
100 # Now we see the uninstalled package
101 self
.feed
= model
.ZeroInstallFeed(empty_feed
, local_path
= '/empty.xml')
102 host
.get_package_info('python-bittorrent', factory
)
103 self
.assertEqual(2, len(self
.feed
.implementations
))
105 self
.assertEqual(2, len(self
.feed
.implementations
))
106 bittorrent_installed
= self
.feed
.implementations
['package:deb:python-bittorrent:3.4.2-10:*']
107 bittorrent_uninstalled
= self
.feed
.implementations
['package:deb:python-bittorrent:3.4.2-11.1:*']
108 self
.assertEqual('3.4.2-10', bittorrent_installed
.get_version())
109 self
.assertTrue(bittorrent_installed
.installed
)
110 self
.assertFalse(bittorrent_uninstalled
.installed
)
111 self
.assertEqual(None, bittorrent_installed
.machine
)
113 self
.feed
= model
.ZeroInstallFeed(empty_feed
, local_path
= '/empty.xml')
114 host
.get_package_info('libxcomposite-dev', factory
)
115 self
.assertEqual(1, len(self
.feed
.implementations
))
116 libxcomposite
= self
.feed
.implementations
['package:deb:libxcomposite-dev:0.3.1-1:i386']
117 self
.assertEqual('0.3.1-1', libxcomposite
.get_version())
118 self
.assertEqual('i386', libxcomposite
.machine
)
121 rpmdir
= os
.path
.join(os
.path
.dirname(__file__
), 'rpm')
122 os
.environ
['PATH'] = rpmdir
+ ':' + self
.old_path
123 rpm
= distro
.RPMDistribution(os
.path
.join(rpmdir
, 'status'))
125 self
.assertEqual(2, len(rpm
.versions
))
127 factory
= self
.make_factory(rpm
)
128 rpm
.get_package_info('yast2-update', factory
)
129 self
.assertEqual(1, len(self
.feed
.implementations
))
130 yast
= self
.feed
.implementations
['package:rpm:yast2-update:2.15.23-21:i586']
131 self
.assertEqual('2.15.23-21', yast
.get_version())
132 self
.assertEqual('*-i586', yast
.arch
)
134 icache
= iface_cache
.IfaceCache(distro
= rpm
)
136 feed
= parse_impls("""
137 <package-implementation distributions="Debian" package="yast2-mail"/>
138 <package-implementation distributions="RPM" package="yast2-update"/>
140 icache
._feeds
[feed
.url
] = feed
141 distro_feed_url
= feed
.get_distro_feed()
142 impls
= icache
.get_feed(distro_feed_url
).implementations
143 self
.assertEqual("distribution:myfeed.xml", distro_feed_url
)
144 assert len(impls
) == 1, impls
146 assert impl
== 'package:rpm:yast2-update:2.15.23-21:i586'
148 feed
= parse_impls("""
149 <package-implementation distributions="RPM" package="yast2-mail"/>
150 <package-implementation distributions="RPM" package="yast2-update"/>
152 icache
._feeds
[feed
.url
] = feed
153 del icache
._feeds
['distribution:' + feed
.url
]
154 impls
= icache
.get_feed(feed
.get_distro_feed()).implementations
155 assert len(impls
) == 2, impls
157 feed
= parse_impls("""
158 <package-implementation distributions="" package="yast2-mail"/>
159 <package-implementation package="yast2-update"/>
161 icache
._feeds
[feed
.url
] = feed
162 del icache
._feeds
['distribution:' + feed
.url
]
163 impls
= icache
.get_feed(feed
.get_distro_feed()).implementations
164 assert len(impls
) == 2, impls
167 slackdir
= os
.path
.join(os
.path
.dirname(__file__
), 'slack')
168 slack
= distro
.SlackDistribution(os
.path
.join(slackdir
, 'packages'))
170 factory
= self
.make_factory(slack
)
171 slack
.get_package_info('gimp', factory
)
172 self
.assertEqual({}, self
.feed
.implementations
)
174 slack
.get_package_info('infozip', factory
)
175 self
.assertEqual(1, len(self
.feed
.implementations
))
176 zip = self
.feed
.implementations
['package:slack:infozip:5.52-2:i486']
177 self
.assertEqual('5.52-2', zip.get_version())
178 self
.assertEqual('i486', zip.machine
)
181 archdir
= os
.path
.join(os
.path
.dirname(__file__
), 'arch')
182 arch
= distro
.ArchDistribution(archdir
)
184 factory
= self
.make_factory(arch
)
185 arch
.get_package_info('gimp', factory
)
186 self
.assertEqual({}, self
.feed
.implementations
)
188 arch
.get_package_info('zeroinstall-injector', factory
)
189 self
.assertEqual(1, len(self
.feed
.implementations
))
190 zip = self
.feed
.implementations
['package:arch:zeroinstall-injector:1.5-1:*']
191 self
.assertEqual('1.5-1', zip.get_version())
193 def testGentoo(self
):
194 pkgdir
= os
.path
.join(os
.path
.dirname(__file__
), 'gentoo')
195 ebuilds
= distro
.GentooDistribution(pkgdir
)
197 factory
= self
.make_factory(ebuilds
)
198 ebuilds
.get_package_info('media-gfx/gimp', factory
)
199 self
.assertEqual({}, self
.feed
.implementations
)
201 ebuilds
.get_package_info('sys-apps/portage', factory
)
202 self
.assertEqual(1, len(self
.feed
.implementations
))
203 impl
= self
.feed
.implementations
['package:gentoo:sys-apps/portage:2.1.7.16:x86_64']
204 self
.assertEqual('2.1.7.16', impl
.get_version())
205 self
.assertEqual('x86_64', impl
.machine
)
207 ebuilds
.get_package_info('sys-kernel/gentoo-sources', factory
)
208 self
.assertEqual(3, len(self
.feed
.implementations
))
209 impl
= self
.feed
.implementations
['package:gentoo:sys-kernel/gentoo-sources:2.6.30-4:i686']
210 self
.assertEqual('2.6.30-4', impl
.get_version())
211 self
.assertEqual('i686', impl
.machine
)
212 impl
= self
.feed
.implementations
['package:gentoo:sys-kernel/gentoo-sources:2.6.32:x86_64']
213 self
.assertEqual('2.6.32', impl
.get_version())
214 self
.assertEqual('x86_64', impl
.machine
)
216 ebuilds
.get_package_info('app-emulation/emul-linux-x86-baselibs', factory
)
217 self
.assertEqual(4, len(self
.feed
.implementations
))
218 impl
= self
.feed
.implementations
['package:gentoo:app-emulation/emul-linux-x86-baselibs:20100220:i386']
219 self
.assertEqual('20100220', impl
.get_version())
220 self
.assertEqual('i386', impl
.machine
)
223 pkgdir
= os
.path
.join(os
.path
.dirname(__file__
), 'ports')
224 ports
= distro
.PortsDistribution(pkgdir
)
226 factory
= self
.make_factory(ports
)
227 ports
.get_package_info('zeroinstall-injector', factory
)
228 self
.assertEqual(1, len(self
.feed
.implementations
))
229 impl
= self
.feed
.implementations
['package:ports:zeroinstall-injector:0.41-2:' + distro
.host_machine
]
230 self
.assertEqual('0.41-2', impl
.get_version())
231 self
.assertEqual(distro
.host_machine
, impl
.machine
)
233 def testMacPorts(self
):
234 pkgdir
= os
.path
.join(os
.path
.dirname(__file__
), 'macports')
235 os
.environ
['PATH'] = pkgdir
+ ':' + self
.old_path
236 ports
= distro
.MacPortsDistribution(os
.path
.join(pkgdir
, 'registry.db'))
238 factory
= self
.make_factory(ports
)
239 ports
.get_package_info('zeroinstall-injector', factory
)
240 self
.assertEqual(1, len(self
.feed
.implementations
))
241 impl
= self
.feed
.implementations
['package:macports:zeroinstall-injector:1.0-0:*']
242 self
.assertEqual('1.0-0', impl
.get_version())
243 self
.assertEqual(None, impl
.machine
)
245 def testCleanVersion(self
):
246 self
.assertEqual('0.3.1-1', distro
.try_cleanup_distro_version('1:0.3.1-1'))
247 self
.assertEqual('0.3.1-1', distro
.try_cleanup_distro_version('0.3.1-1ubuntu0'))
248 self
.assertEqual('0.3-post1-rc2', distro
.try_cleanup_distro_version('0.3-post1-rc2'))
249 self
.assertEqual('0.3.1-2', distro
.try_cleanup_distro_version('0.3.1-r2-r3'))
250 self
.assertEqual('6.17', distro
.try_cleanup_distro_version('6b17'))
251 self
.assertEqual('20-1', distro
.try_cleanup_distro_version('b20_1'))
252 self
.assertEqual('17', distro
.try_cleanup_distro_version('p17'))
253 self
.assertEqual(None, distro
.try_cleanup_distro_version('cvs'))
255 def testCommand(self
):
256 dpkgdir
= os
.path
.join(os
.path
.dirname(__file__
), 'dpkg')
257 host
= distro
.DebianDistribution(
258 os
.path
.join(dpkgdir
, 'status'))
259 host
._packagekit
= DummyPackageKit()
261 factory
= self
.make_factory(host
)
263 master_feed
= parse_impls("""<package-implementation main='/unused' package='python-bittorrent'><command path='/bin/sh' name='run'/></package-implementation>""")
264 icache
= iface_cache
.IfaceCache(distro
= host
)
265 icache
._feeds
[master_feed
.url
] = master_feed
266 #del icache._feeds['distribution:' + master_feed.url]
267 impl
, = icache
.get_feed(master_feed
.get_distro_feed()).implementations
.values()
268 self
.assertEqual('/bin/sh', impl
.main
)
270 def testPortable(self
):
271 # Overrides all XDG_* variables
272 os
.environ
['ZEROINSTALL_PORTABLE_BASE'] = '/portable'
274 self
.assertEqual('/portable/config', basedir
.xdg_config_home
)
275 self
.assertEqual('/portable/cache', basedir
.xdg_cache_home
)
276 self
.assertEqual('/portable/data', basedir
.xdg_data_home
)
278 self
.assertEqual(['/portable/config'], basedir
.xdg_config_dirs
)
279 self
.assertEqual(['/portable/cache'], basedir
.xdg_cache_dirs
)
280 self
.assertEqual(['/portable/data'], basedir
.xdg_data_dirs
)
282 del os
.environ
['ZEROINSTALL_PORTABLE_BASE']
283 os
.environ
['XDG_CONFIG_HOME'] = '/home/me/config'
284 os
.environ
['XDG_CONFIG_DIRS'] = '/system/config'
286 os
.environ
['XDG_DATA_HOME'] = '/home/me/data'
287 os
.environ
['XDG_DATA_DIRS'] = '/system/data' + os
.pathsep
+ '/disto/data'
289 os
.environ
['XDG_CACHE_HOME'] = '/home/me/cache'
290 os
.environ
['XDG_CACHE_DIRS'] = '/system/cache'
293 self
.assertEqual('/home/me/config', basedir
.xdg_config_home
)
294 self
.assertEqual('/home/me/cache', basedir
.xdg_cache_home
)
295 self
.assertEqual('/home/me/data', basedir
.xdg_data_home
)
297 self
.assertEqual(['/home/me/config', '/system/config'], basedir
.xdg_config_dirs
)
298 self
.assertEqual(['/home/me/cache', '/system/cache'], basedir
.xdg_cache_dirs
)
299 self
.assertEqual(['/home/me/data', '/system/data', '/disto/data'], basedir
.xdg_data_dirs
)
301 if __name__
== '__main__':