1 #!/usr/bin/env python2.4
2 from basetest
import BaseTest
3 import sys
, tempfile
, os
, shutil
, logging
4 from StringIO
import StringIO
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
14 foo_iface_uri
= 'http://foo'
15 bar_iface_uri
= 'http://localhost/bar'
17 logger
= logging
.getLogger()
19 class TestReader(BaseTest
):
23 stream
= tempfile
.TemporaryFile()
24 stream
.write(data
.thomas_key
)
26 gpg
.import_key(stream
)
28 def write_with_version(self
, version
):
29 tmp
= tempfile
.NamedTemporaryFile(prefix
= 'test-')
31 """<?xml version="1.0" ?>
32 <interface last-modified="1110752708"
34 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
36 <summary>Foo</summary>
37 <description>Foo</description>
38 </interface>""" % (foo_iface_uri
, version
))
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
)
51 tmp
= self
.write_with_version('min-injector-version="1000"')
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-')
60 """<?xml version="1.0" ?>
61 <interface last-modified="1110752708"
63 xmlns="http://zero-install.sourceforge.net/2004/injector/interface"
64 xmlns:my='http://my/namespace'>
66 <summary>Foo</summary>
67 <description>Foo</description>
69 <requires interface='%s' my:foo='test'>
70 <version not-before='2.3.4' before='3.4.5'/>
72 <implementation id='sha1=123' version='1'/>
73 <requires interface='%s2'/>
75 </interface>""" % (foo_iface_uri
, bar_iface_uri
, bar_iface_uri
))
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
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-')
97 """<?xml version="1.0" ?>
98 <interface xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
100 <summary>Foo</summary>
101 <description>Foo</description>
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'/>
109 <implementation id='sha1=123' version='1'>
110 <environment name='SELF' insert='.' mode='replace'/>
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-')
140 """<?xml version="1.0" ?>
143 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
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
)
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-')
158 """<?xml version="1.0" ?>
159 <interface last-modified="1110752708"
161 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
163 <summary>Foo</summary>
164 <description>Foo</description>
165 <group main='/bin/sh'>
166 <implementation id='sha1=123' version='1'/>
168 </interface>""" % foo_iface_uri
)
170 iface
= model
.Interface(foo_iface_uri
)
172 reader
.update(iface
, tmp
.name
)
174 except reader
.InvalidInterface
, ex
:
175 assert 'main' in str(ex
)
178 tmp
= tempfile
.NamedTemporaryFile(prefix
= 'test-')
180 """<?xml version="1.0" ?>
181 <interface last-modified="1110752708"
183 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
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'/>
191 </interface>""" % foo_iface_uri
)
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-')
208 """<?xml version="1.0" ?>
209 <interface xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
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'/>
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
233 tmp
= tempfile
.NamedTemporaryFile(prefix
= 'test-')
235 """<?xml version="1.0" ?>
236 <interface xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
238 <summary>Foo</summary>
239 <description>Foo</description>
240 <feed langs='fr en_GB' src='http://localhost/feed.xml'/>
242 <group langs='fr en_GB'>
243 <implementation id='sha1=124' version='2' langs='fr'/>
244 <implementation id='sha1=234' version='2'/>
246 <implementation id='sha1=345' version='2'/>
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')