Refuse to create an alias named "0launch"
[zeroinstall.git] / tests / testdistro.py
blobf44ea11d1f9f61bee960692be2e7087c1f51d8e4
1 #!/usr/bin/env python
2 from basetest import BaseTest, empty_feed, DummyPackageKit
3 import sys, os, tempfile
4 from StringIO import StringIO
5 import unittest
7 sys.path.insert(0, '..')
8 from zeroinstall.injector import distro, model, qdom, iface_cache, handler
10 def parse_impls(impls):
11 xml = """<?xml version="1.0" ?>
12 <interface xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
13 <name>Foo</name>
14 <summary>Foo</summary>
15 <description>Foo</description>
17 </interface>""" % impls
18 element = qdom.parse(StringIO(xml))
19 return model.ZeroInstallFeed(element, "myfeed.xml")
21 class TestDistro(BaseTest):
22 def setUp(self):
23 BaseTest.setUp(self)
24 self.feed = model.ZeroInstallFeed(empty_feed, local_path = '/empty.xml')
26 def tearDown(self):
27 BaseTest.tearDown(self)
29 def testCache(self):
30 src = tempfile.NamedTemporaryFile()
31 try:
32 cache = distro.Cache('test-cache', src.name, 1)
33 self.assertEqual(None, cache.get("foo"))
34 cache.put("foo", "1")
35 self.assertEqual("1", cache.get("foo"))
36 cache.put("foo", "2")
37 self.assertEqual("2", cache.get("foo"))
39 # new cache...
40 cache = distro.Cache('test-cache', src.name, 1)
41 self.assertEqual("2", cache.get("foo"))
43 src.write("hi")
44 src.flush()
46 self.assertEqual(None, cache.get("foo"))
47 cache.put("foo", "3")
49 # new cache... (format change)
50 cache = distro.Cache('test-cache', src.name, 2)
51 self.assertEqual(None, cache.get("foo"))
53 finally:
54 src.close()
56 def make_factory(self, distro):
57 def factory(id, only_if_missing = False, installed = True):
58 assert not only_if_missing
59 impl = model.DistributionImplementation(self.feed, id, distro)
60 assert id not in self.feed.implementations
61 self.feed.implementations[id] = impl
62 impl.installed = installed
63 return impl
64 return factory
66 def testDefault(self):
67 host = distro.Distribution()
69 factory = self.make_factory(host)
70 host.get_package_info('gimp', factory)
71 self.assertEqual(self.feed.implementations, {})
73 # Special case: we can always find a version of Python
74 master_feed = model.ZeroInstallFeed(None)
75 master_feed.url = 'http://repo.roscidus.com/python/python'
76 feed = host.get_feed(master_feed)
77 self.assertEqual(1, len(feed.implementations))
79 def testDebian(self):
80 dpkgdir = os.path.join(os.path.dirname(__file__), 'dpkg')
81 host = distro.DebianDistribution(
82 os.path.join(dpkgdir, 'status'),
83 os.path.join(dpkgdir, 'pkgcache.bin'))
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'/>""")
96 h = handler.Handler()
97 candidates = host.fetch_candidates(master_feed)
98 if candidates:
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)
120 def testRPM(self):
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"/>
139 """)
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
145 impl, = 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"/>
151 """)
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"/>
160 """)
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
166 def testSlack(self):
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)
180 def testArch(self):
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)
222 def testPorts(self):
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'))
251 if __name__ == '__main__':
252 unittest.main()