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