1 #!/usr/bin/env python2.5
2 from basetest
import BaseTest
3 import sys
, tempfile
, logging
6 sys
.path
.insert(0, '..')
8 from zeroinstall
.injector
import model
, gpg
, reader
11 foo_iface_uri
= 'http://foo'
12 bar_iface_uri
= 'http://localhost/bar'
14 logger
= logging
.getLogger()
16 class TestReader(BaseTest
):
20 stream
= tempfile
.TemporaryFile()
21 stream
.write(data
.thomas_key
)
23 gpg
.import_key(stream
)
25 def write_with_version(self
, version
):
26 tmp
= tempfile
.NamedTemporaryFile(prefix
= 'test-')
28 """<?xml version="1.0" ?>
29 <interface last-modified="1110752708"
31 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
33 <summary>Foo</summary>
34 <description>Foo</description>
35 </interface>""" % (foo_iface_uri
, version
))
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
)
48 tmp
= self
.write_with_version('min-injector-version="1000"')
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-')
57 """<?xml version="1.0" ?>
58 <interface last-modified="1110752708"
60 xmlns="http://zero-install.sourceforge.net/2004/injector/interface"
61 xmlns:my='http://my/namespace'>
63 <summary>Foo</summary>
64 <description>Foo</description>
66 <requires interface='%s' my:foo='test'>
67 <version not-before='2.3.4' before='3.4.5'/>
69 <implementation id='sha1=123' version='1'/>
70 <requires interface='%s2'/>
72 </interface>""" % (foo_iface_uri
, bar_iface_uri
, bar_iface_uri
))
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
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-')
94 """<?xml version="1.0" ?>
95 <interface xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
97 <summary>Foo</summary>
98 <description>Foo</description>
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'/>
106 <implementation id='sha1=123' version='1'>
107 <environment name='SELF' insert='.' mode='replace'/>
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-')
137 """<?xml version="1.0" ?>
140 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
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
)
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-')
155 """<?xml version="1.0" ?>
156 <interface last-modified="1110752708"
158 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
160 <summary>Foo</summary>
161 <description>Foo</description>
162 <group main='/bin/sh'>
163 <implementation id='sha1=123' version='1'/>
165 </interface>""" % foo_iface_uri
)
167 iface
= model
.Interface(foo_iface_uri
)
169 reader
.update(iface
, tmp
.name
)
171 except reader
.InvalidInterface
, ex
:
172 assert 'main' in str(ex
)
175 tmp
= tempfile
.NamedTemporaryFile(prefix
= 'test-')
177 """<?xml version="1.0" ?>
178 <interface last-modified="1110752708"
180 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
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'/>
188 </interface>""" % foo_iface_uri
)
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-')
205 """<?xml version="1.0" ?>
206 <interface xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
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'/>
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
230 tmp
= tempfile
.NamedTemporaryFile(prefix
= 'test-')
232 """<?xml version="1.0" ?>
233 <interface xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
235 <summary>Foo</summary>
236 <description>Foo</description>
237 <feed langs='fr en_GB' src='http://localhost/feed.xml'/>
239 <group langs='fr en_GB'>
240 <implementation id='sha1=124' version='2' langs='fr'/>
241 <implementation id='sha1=234' version='2'/>
243 <implementation id='sha1=345' version='2'/>
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')