Made 0launch depend on Python explicitly
[zeroinstall/solver.git] / tests / testdistro.py
blob7fcbebd17bc4d49ccd082c039495599f2601bcc4
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.assertEquals(None, cache.get("foo"))
34 cache.put("foo", "1")
35 self.assertEquals("1", cache.get("foo"))
36 cache.put("foo", "2")
37 self.assertEquals("2", cache.get("foo"))
39 # new cache...
40 cache = distro.Cache('test-cache', src.name, 1)
41 self.assertEquals("2", cache.get("foo"))
43 src.write("hi")
44 src.flush()
46 self.assertEquals(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.assertEquals(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.assertEquals(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.assertEquals(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.assertEquals({}, self.feed.implementations)
90 # Initially, we only get information about the installed version...
91 host.get_package_info('python-bittorrent', factory)
92 self.assertEquals(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.assertEquals(2, len(self.feed.implementations))
105 self.assertEquals(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.assertEquals('3.4.2-10', bittorrent_installed.get_version())
109 self.assertTrue(bittorrent_installed.installed)
110 self.assertFalse(bittorrent_uninstalled.installed)
111 self.assertEquals(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.assertEquals(1, len(self.feed.implementations))
116 libxcomposite = self.feed.implementations['package:deb:libxcomposite-dev:0.3.1-1:i386']
117 self.assertEquals('0.3.1-1', libxcomposite.get_version())
118 self.assertEquals('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.assertEquals(2, len(rpm.versions))
127 factory = self.make_factory(rpm)
128 rpm.get_package_info('yast2-update', factory)
129 self.assertEquals(1, len(self.feed.implementations))
130 yast = self.feed.implementations['package:rpm:yast2-update:2.15.23-21:i586']
131 self.assertEquals('2.15.23-21', yast.get_version())
132 self.assertEquals('*-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.assertEquals("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.assertEquals({}, self.feed.implementations)
174 slack.get_package_info('infozip', factory)
175 self.assertEquals(1, len(self.feed.implementations))
176 zip = self.feed.implementations['package:slack:infozip:5.52-2:i486']
177 self.assertEquals('5.52-2', zip.get_version())
178 self.assertEquals('i486', zip.machine)
180 def testGentoo(self):
181 pkgdir = os.path.join(os.path.dirname(__file__), 'gentoo')
182 ebuilds = distro.GentooDistribution(pkgdir)
184 factory = self.make_factory(ebuilds)
185 ebuilds.get_package_info('media-gfx/gimp', factory)
186 self.assertEquals({}, self.feed.implementations)
188 ebuilds.get_package_info('sys-apps/portage', factory)
189 self.assertEquals(1, len(self.feed.implementations))
190 impl = self.feed.implementations['package:gentoo:sys-apps/portage:2.1.7.16:x86_64']
191 self.assertEquals('2.1.7.16', impl.get_version())
192 self.assertEquals('x86_64', impl.machine)
194 ebuilds.get_package_info('sys-kernel/gentoo-sources', factory)
195 self.assertEquals(3, len(self.feed.implementations))
196 impl = self.feed.implementations['package:gentoo:sys-kernel/gentoo-sources:2.6.30-4:i686']
197 self.assertEquals('2.6.30-4', impl.get_version())
198 self.assertEquals('i686', impl.machine)
199 impl = self.feed.implementations['package:gentoo:sys-kernel/gentoo-sources:2.6.32:x86_64']
200 self.assertEquals('2.6.32', impl.get_version())
201 self.assertEquals('x86_64', impl.machine)
203 ebuilds.get_package_info('app-emulation/emul-linux-x86-baselibs', factory)
204 self.assertEquals(4, len(self.feed.implementations))
205 impl = self.feed.implementations['package:gentoo:app-emulation/emul-linux-x86-baselibs:20100220:i386']
206 self.assertEquals('20100220', impl.get_version())
207 self.assertEquals('i386', impl.machine)
209 def testPorts(self):
210 pkgdir = os.path.join(os.path.dirname(__file__), 'ports')
211 ports = distro.PortsDistribution(pkgdir)
213 factory = self.make_factory(ports)
214 ports.get_package_info('zeroinstall-injector', factory)
215 self.assertEquals(1, len(self.feed.implementations))
216 impl = self.feed.implementations['package:ports:zeroinstall-injector:0.41-2:' + distro.host_machine]
217 self.assertEquals('0.41-2', impl.get_version())
218 self.assertEquals(distro.host_machine, impl.machine)
220 def testCleanVersion(self):
221 self.assertEquals('0.3.1-1', distro.try_cleanup_distro_version('1:0.3.1-1'))
222 self.assertEquals('0.3.1-1', distro.try_cleanup_distro_version('0.3.1-1ubuntu0'))
223 self.assertEquals('0.3-post1-rc2', distro.try_cleanup_distro_version('0.3-post1-rc2'))
224 self.assertEquals('0.3.1-2', distro.try_cleanup_distro_version('0.3.1-r2-r3'))
226 if __name__ == '__main__':
227 unittest.main()