Changed Implementation.interface to Implementation.feed.
[zeroinstall/zeroinstall-rsl.git] / tests / testreader.py
bloba3f4244924ea5fdbf4d90b2ac434cfdac97c2fba
1 #!/usr/bin/env python2.4
2 from basetest import BaseTest
3 import sys, tempfile, os, shutil, logging
4 from StringIO import StringIO
5 import unittest
6 from logging import getLogger, DEBUG, INFO
8 sys.path.insert(0, '..')
10 from zeroinstall import NeedDownload
11 from zeroinstall.injector import model, basedir, autopolicy, gpg, iface_cache, namespaces, reader
12 import data
14 foo_iface_uri = 'http://foo'
15 bar_iface_uri = 'http://localhost/bar'
17 logger = logging.getLogger()
19 class TestReader(BaseTest):
20 def setUp(self):
21 BaseTest.setUp(self)
23 stream = tempfile.TemporaryFile()
24 stream.write(data.thomas_key)
25 stream.seek(0)
26 gpg.import_key(stream)
28 def write_with_version(self, version):
29 tmp = tempfile.NamedTemporaryFile(prefix = 'test-')
30 tmp.write(
31 """<?xml version="1.0" ?>
32 <interface last-modified="1110752708"
33 uri="%s" %s
34 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
35 <name>Foo</name>
36 <summary>Foo</summary>
37 <description>Foo</description>
38 </interface>""" % (foo_iface_uri, version))
39 tmp.flush()
40 return tmp
42 def testNoVersion(self):
43 tmp = self.write_with_version('')
44 reader.check_readable(foo_iface_uri, tmp.name)
46 def testNewEnough(self):
47 tmp = self.write_with_version('min-injector-version="0.19"')
48 reader.check_readable(foo_iface_uri, tmp.name)
50 def testTooOld(self):
51 tmp = self.write_with_version('min-injector-version="1000"')
52 try:
53 reader.check_readable(foo_iface_uri, tmp.name)
54 except reader.InvalidInterface, ex:
55 assert "1000" in str(ex)
57 def testRequiresVersion(self):
58 tmp = tempfile.NamedTemporaryFile(prefix = 'test-')
59 tmp.write(
60 """<?xml version="1.0" ?>
61 <interface last-modified="1110752708"
62 uri="%s"
63 xmlns="http://zero-install.sourceforge.net/2004/injector/interface"
64 xmlns:my='http://my/namespace'>
65 <name>Foo</name>
66 <summary>Foo</summary>
67 <description>Foo</description>
68 <group>
69 <requires interface='%s' my:foo='test'>
70 <version not-before='2.3.4' before='3.4.5'/>
71 </requires>
72 <implementation id='sha1=123' version='1'/>
73 <requires interface='%s2'/>
74 </group>
75 </interface>""" % (foo_iface_uri, bar_iface_uri, bar_iface_uri))
76 tmp.flush()
77 iface = model.Interface(foo_iface_uri)
78 reader.update(iface, tmp.name)
79 impl = iface.implementations['sha1=123']
80 assert len(impl.dependencies) == 2
81 dep = impl.dependencies[bar_iface_uri]
82 assert len(dep.restrictions) == 1
83 res = dep.restrictions[0]
84 assert res.not_before == [[2, 3, 4], 0]
85 assert res.before == [[3, 4, 5], 0]
86 dep2 = impl.dependencies[bar_iface_uri + '2']
87 assert len(dep2.restrictions) == 0
88 str(dep)
89 str(dep2)
91 assert dep.metadata.get('http://my/namespace foo') == 'test'
92 assert dep.metadata.get('http://my/namespace food', None) == None
94 def testBindings(self):
95 tmp = tempfile.NamedTemporaryFile(prefix = 'test-')
96 tmp.write(
97 """<?xml version="1.0" ?>
98 <interface xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
99 <name>Foo</name>
100 <summary>Foo</summary>
101 <description>Foo</description>
102 <group>
103 <requires interface='http://example.com/foo.xml'>
104 <environment name='PATH' insert='bin'/>
105 <environment name='PATH' insert='bin' mode='prepend'/>
106 <environment name='PATH' insert='bin' default='/bin' mode='append'/>
107 <environment name='PATH' insert='bin' mode='replace'/>
108 </requires>
109 <implementation id='sha1=123' version='1'>
110 <environment name='SELF' insert='.' mode='replace'/>
111 </implementation>
112 </group>
113 </interface>""")
114 tmp.flush()
115 iface = model.Interface(foo_iface_uri)
116 reader.update(iface, tmp.name, local = True)
117 impl = iface.implementations['sha1=123']
119 assert len(impl.bindings) == 1
120 self.assertEquals(model.EnvironmentBinding.REPLACE, impl.bindings[0].mode)
122 assert len(impl.requires) == 1
123 dep = impl.requires[0]
125 assert len(dep.bindings) == 4
126 for b in dep.bindings:
127 self.assertEquals('PATH', b.name)
128 self.assertEquals('bin', b.insert)
129 self.assertEquals(model.EnvironmentBinding.PREPEND, dep.bindings[0].mode)
130 self.assertEquals(model.EnvironmentBinding.PREPEND, dep.bindings[1].mode)
131 self.assertEquals(model.EnvironmentBinding.APPEND, dep.bindings[2].mode)
132 self.assertEquals(model.EnvironmentBinding.REPLACE, dep.bindings[3].mode)
134 self.assertEquals(None, dep.bindings[1].default)
135 self.assertEquals('/bin', dep.bindings[2].default)
137 def testVersions(self):
138 tmp = tempfile.NamedTemporaryFile(prefix = 'test-')
139 tmp.write(
140 """<?xml version="1.0" ?>
141 <interface
142 uri="%s"
143 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
144 <name>Foo</name>
145 <summary>Foo</summary>
146 <description>Foo</description>
147 <implementation id='sha1=123' version='1.0-rc3' version-modifier='-pre'/>
148 </interface>""" % foo_iface_uri)
149 tmp.flush()
150 iface = model.Interface(foo_iface_uri)
151 reader.update(iface, tmp.name)
152 impl = iface.implementations['sha1=123']
153 assert impl.version == [[1, 0], -1, [3], -2]
155 def testAbsMain(self):
156 tmp = tempfile.NamedTemporaryFile(prefix = 'test-')
157 tmp.write(
158 """<?xml version="1.0" ?>
159 <interface last-modified="1110752708"
160 uri="%s"
161 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
162 <name>Foo</name>
163 <summary>Foo</summary>
164 <description>Foo</description>
165 <group main='/bin/sh'>
166 <implementation id='sha1=123' version='1'/>
167 </group>
168 </interface>""" % foo_iface_uri)
169 tmp.flush()
170 iface = model.Interface(foo_iface_uri)
171 try:
172 reader.update(iface, tmp.name)
173 assert False
174 except reader.InvalidInterface, ex:
175 assert 'main' in str(ex)
177 def testAttrs(self):
178 tmp = tempfile.NamedTemporaryFile(prefix = 'test-')
179 tmp.write(
180 """<?xml version="1.0" ?>
181 <interface last-modified="1110752708"
182 uri="%s"
183 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
184 <name>Foo</name>
185 <summary>Foo</summary>
186 <description>Foo</description>
187 <group main='bin/sh' foo='foovalue' xmlns:bobpre='http://bob' bobpre:bob='bobvalue'>
188 <implementation id='sha1=123' version='1' bobpre:bob='newbobvalue'/>
189 <implementation id='sha1=124' version='2' main='next'/>
190 </group>
191 </interface>""" % foo_iface_uri)
192 tmp.flush()
193 iface = model.Interface(foo_iface_uri)
194 reader.update(iface, tmp.name)
196 assert len(iface.implementations) == 2
198 assert iface.implementations['sha1=123'].metadata['foo'] == 'foovalue'
199 assert iface.implementations['sha1=123'].metadata['main'] == 'bin/sh'
200 assert iface.implementations['sha1=123'].metadata['http://bob bob'] == 'newbobvalue'
202 assert iface.implementations['sha1=124'].metadata['http://bob bob'] == 'bobvalue'
203 assert iface.implementations['sha1=124'].metadata['main'] == 'next'
205 def testNative(self):
206 tmp = tempfile.NamedTemporaryFile(prefix = 'test-')
207 tmp.write(
208 """<?xml version="1.0" ?>
209 <interface xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
210 <name>Foo</name>
211 <summary>Foo</summary>
212 <description>Foo</description>
213 <package-implementation package='gimp'/>
214 <package-implementation package='python-bittorrent' foo='bar' main='/usr/bin/pbt'/>
215 </interface>""")
216 tmp.flush()
218 iface = model.Interface(foo_iface_uri)
219 reader.update(iface, tmp.name, True)
221 assert len(iface.implementations) == 1
223 impl = iface.implementations['package:deb:python-bittorrent:3.4.2-10']
224 assert impl.id == 'package:deb:python-bittorrent:3.4.2-10'
225 assert impl.upstream_stability == model.packaged
226 assert impl.user_stability == None
227 assert impl.requires == []
228 assert impl.main == '/usr/bin/pbt'
229 assert impl.metadata['foo'] == 'bar'
230 assert impl.feed == iface._main_feed
232 def testLang(self):
233 tmp = tempfile.NamedTemporaryFile(prefix = 'test-')
234 tmp.write(
235 """<?xml version="1.0" ?>
236 <interface xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
237 <name>Foo</name>
238 <summary>Foo</summary>
239 <description>Foo</description>
240 <feed langs='fr en_GB' src='http://localhost/feed.xml'/>
241 <group>
242 <group langs='fr en_GB'>
243 <implementation id='sha1=124' version='2' langs='fr'/>
244 <implementation id='sha1=234' version='2'/>
245 </group>
246 <implementation id='sha1=345' version='2'/>
247 </group>
248 </interface>""")
249 tmp.flush()
251 iface = model.Interface(foo_iface_uri)
252 reader.update(iface, tmp.name, True)
254 assert len(iface.implementations) == 3
255 assert len(iface.feeds) == 1, iface.feeds
257 self.assertEquals('fr en_GB', iface.feeds[0].langs)
259 self.assertEquals('fr', iface.implementations['sha1=124'].langs)
260 self.assertEquals('fr en_GB', iface.implementations['sha1=234'].langs)
261 self.assertEquals(None, iface.implementations['sha1=345'].langs)
263 suite = unittest.makeSuite(TestReader)
264 if __name__ == '__main__':
265 sys.argv.append('-v')
266 unittest.main()