Get GPG to send output to stdout
[zeroinstall/solver.git] / tests / testmodel.py
blobcab24f4e0cd59cd8f209dfdebb55d186f7c23b4e
1 #!/usr/bin/env python2.4
2 # -*- coding: utf-8 -*-
3 from basetest import BaseTest, empty_feed
4 import sys
5 from xml.dom import minidom
6 import unittest
7 from StringIO import StringIO
9 sys.path.insert(0, '..')
10 from zeroinstall.injector import model, qdom
12 class TestModel(BaseTest):
13 def testLevels(self):
14 assert model.network_offline in model.network_levels
15 assert model.network_minimal in model.network_levels
16 assert model.network_full in model.network_levels
18 def testStabilities(self):
19 assert 'insecure' in model.stability_levels
20 assert 'buggy' in model.stability_levels
21 assert 'developer' in model.stability_levels
22 assert 'testing' in model.stability_levels
23 assert 'stable' in model.stability_levels
24 assert 'preferred' in model.stability_levels
25 str(model.insecure)
27 def testEscape(self):
28 self.assertEquals("", model.escape(""))
29 self.assertEquals("hello", model.escape("hello"))
30 self.assertEquals("%20", model.escape(" "))
32 self.assertEquals("file%3a%2f%2ffoo%7ebar",
33 model.escape("file://foo~bar"))
34 self.assertEquals("file%3a%2f%2ffoo%25bar",
35 model.escape("file://foo%bar"))
37 self.assertEquals("file:##foo%7ebar",
38 model._pretty_escape("file://foo~bar"))
39 self.assertEquals("file:##foo%25bar",
40 model._pretty_escape("file://foo%bar"))
42 def testUnescape(self):
43 self.assertEquals("", model.unescape(""))
44 self.assertEquals("hello", model.unescape("hello"))
45 self.assertEquals(" ", model.unescape("%20"))
47 self.assertEquals("file://foo~bar",
48 model.unescape("file%3a%2f%2ffoo%7ebar"))
49 self.assertEquals("file://foo%bar",
50 model.unescape("file%3a%2f%2ffoo%25bar"))
52 self.assertEquals("file://foo",
53 model.unescape("file:##foo"))
54 self.assertEquals("file://foo~bar",
55 model.unescape("file:##foo%7ebar"))
56 self.assertEquals("file://foo%bar",
57 model.unescape("file:##foo%25bar"))
59 def testEscaping(self):
60 def check(str):
61 self.assertEquals(str, model.unescape(model.escape(str)))
62 self.assertEquals(str, model.unescape(model._pretty_escape(str)))
64 check(u'http://example.com')
65 check(u'http://example%46com')
66 check(u'http:##example#com')
67 check(u'http://example.com/foo/bar.xml')
68 check(u'%20%21~&!"£ :@;,./{}$%^&()')
70 def testBadInterface(self):
71 try:
72 model.Interface('foo')
73 assert 0
74 except model.SafeException:
75 pass
77 def testInterface(self):
78 i = model.Interface('http://foo')
79 self.assertEquals('(foo)', i.get_name())
80 feed = model.ZeroInstallFeed(empty_feed, local_path = '/foo')
81 i._main_feed = feed
82 self.assertEquals('Empty', i.get_name())
83 repr(i)
85 def testMetadata(self):
86 i = model.Interface('http://foo')
87 i._main_feed = model.ZeroInstallFeed(empty_feed, local_path = '/foo')
88 e = qdom.parse(StringIO('<ns:b xmlns:ns="a" foo="bar"/>'))
89 i._main_feed.metadata = [e]
90 assert i.get_metadata('a', 'b') == [e]
91 assert i.get_metadata('b', 'b') == []
92 assert i.get_metadata('a', 'a') == []
93 assert e.getAttribute('foo') == 'bar'
95 def testStabPolicy(self):
96 i = model.Interface('http://foo')
97 self.assertEquals(None, i.stability_policy)
98 i.set_stability_policy(model.buggy)
99 self.assertEquals(model.buggy, i.stability_policy)
101 def testGetImpl(self):
102 i = model.Interface('http://foo')
103 feed = model.ZeroInstallFeed(empty_feed, local_path = '/foo')
104 a = feed._get_impl('foo')
105 b = feed._get_impl('bar')
106 c = feed._get_impl('foo')
107 assert a and b and c
108 assert a is c
109 assert a is not b
110 assert isinstance(a, model.ZeroInstallImplementation)
112 def testImpl(self):
113 i = model.Interface('http://foo')
114 a = model.ZeroInstallImplementation(i, 'foo')
115 assert a.id == 'foo'
116 assert a.size == a.version == a.user_stability == None
117 assert a.arch == a.upstream_stability == None
118 assert a.dependencies == {}
119 assert a.download_sources == []
120 assert a.get_stability() is model.testing
121 a.upstream_stability = model.stable
122 assert a.get_stability() is model.stable
123 a.user_stability = model.buggy
124 assert a.get_stability() is model.buggy
125 a.version = model.parse_version('1.2.3')
126 self.assertEquals('1.2.3', a.get_version())
127 a.version = model.parse_version('1.2.3-rc2-post')
128 self.assertEquals('1.2.3-rc2-post', a.get_version())
129 assert str(a) == 'foo'
131 b = model.ZeroInstallImplementation(i, 'foo')
132 b.version = [1,2,1]
133 assert b > a
135 def testDownloadSource(self):
136 f = model.ZeroInstallFeed(empty_feed, local_path = '/foo')
137 a = model.ZeroInstallImplementation(f, 'foo')
138 a.add_download_source('ftp://foo', 1024, None)
139 a.add_download_source('ftp://foo.tgz', 1025, 'foo')
140 assert a.download_sources[0].url == 'ftp://foo'
141 assert a.download_sources[0].size == 1024
142 assert a.download_sources[0].extract == None
143 assert a.feed is f
145 def testEnvBind(self):
146 a = model.EnvironmentBinding('PYTHONPATH', 'path')
147 assert a.name == 'PYTHONPATH'
148 assert a.insert == 'path'
149 str(a)
151 def testEnvModes(self):
152 prepend = model.EnvironmentBinding('PYTHONPATH', 'lib', None, model.EnvironmentBinding.PREPEND)
153 assert prepend.name == 'PYTHONPATH'
154 assert prepend.insert == 'lib'
155 assert prepend.mode is model.EnvironmentBinding.PREPEND
157 self.assertEquals('/impl/lib:/usr/lib', prepend.get_value('/impl', '/usr/lib'))
158 self.assertEquals('/impl/lib', prepend.get_value('/impl', None))
160 append = model.EnvironmentBinding('PYTHONPATH', 'lib', '/opt/lib', model.EnvironmentBinding.APPEND)
161 assert append.name == 'PYTHONPATH'
162 assert append.insert == 'lib'
163 assert append.mode is model.EnvironmentBinding.APPEND
165 self.assertEquals('/usr/lib:/impl/lib', append.get_value('/impl', '/usr/lib'))
166 self.assertEquals('/opt/lib:/impl/lib', append.get_value('/impl', None))
168 append = model.EnvironmentBinding('PYTHONPATH', 'lib', None, model.EnvironmentBinding.REPLACE)
169 assert append.name == 'PYTHONPATH'
170 assert append.insert == 'lib'
171 assert append.mode is model.EnvironmentBinding.REPLACE
173 self.assertEquals('/impl/lib', append.get_value('/impl', '/usr/lib'))
174 self.assertEquals('/impl/lib', append.get_value('/impl', None))
176 assert model.EnvironmentBinding('PYTHONPATH', 'lib').mode == model.EnvironmentBinding.PREPEND
178 def testOverlay(self):
179 for xml, expected in [('<overlay/>', '<overlay . on />'),
180 ('<overlay src="usr"/>', '<overlay usr on />'),
181 ('<overlay src="package" mount-point="/usr/games"/>', '<overlay package on /usr/games>')]:
182 e = qdom.parse(StringIO(xml))
183 ol = model.process_binding(e)
184 self.assertEquals(expected, str(ol))
186 doc = minidom.parseString('<doc/>')
187 new_xml = str(ol._toxml(doc).toxml())
188 new_e = qdom.parse(StringIO(new_xml))
189 new_ol = model.process_binding(new_e)
190 self.assertEquals(expected, str(new_ol))
192 def testDep(self):
193 b = model.InterfaceDependency('http://foo')
194 assert not b.restrictions
195 assert not b.bindings
196 str(b)
198 def testFeed(self):
199 f = model.Feed('http://feed', arch = None, user_override = False)
200 assert f.uri == 'http://feed'
201 assert f.os == None
202 assert f.machine == None
203 assert f.arch == None
204 assert f.user_override == False
206 f = model.Feed('http://feed', arch = 'Linux-*', user_override = True)
207 assert f.uri == 'http://feed'
208 assert f.os == 'Linux'
209 assert f.machine == None
210 assert f.arch == 'Linux-*'
211 assert f.user_override == True
213 f = model.Feed('http://feed', arch = '*-i386', user_override = True)
214 assert f.uri == 'http://feed'
215 assert f.os == None
216 assert f.machine == 'i386'
217 assert f.arch == '*-i386'
218 assert f.user_override == True
219 assert str(f).startswith('<Feed from')
221 try:
222 f = model.Feed('http://feed', arch = 'i386', user_override = True)
223 assert False
224 except model.SafeException, ex:
225 assert 'Malformed arch' in str(ex)
227 def testCanonical(self):
228 try:
229 model.canonical_iface_uri('http://foo')
230 assert False
231 except model.SafeException, ex:
232 assert 'Missing /' in str(ex)
234 self.assertEquals('http://foo/',
235 model.canonical_iface_uri('http://foo/'))
236 try:
237 model.canonical_iface_uri('bad-name')
238 assert False
239 except model.SafeException, ex:
240 assert 'Bad interface name' in str(ex)
242 def testVersions(self):
243 def pv(v):
244 parsed = model.parse_version(v)
245 assert model.format_version(parsed) == v
246 return parsed
248 assert pv('1.0') > pv('0.9')
249 assert pv('1.0') > pv('1')
250 assert pv('1.0') == pv('1.0')
251 assert pv('0.9.9') < pv('1.0')
253 def invalid(v):
254 try:
255 pv(v)
256 assert False
257 except model.SafeException, ex:
258 pass
259 invalid('hello')
260 invalid('2./1')
261 invalid('.1')
262 invalid('')
264 # Check parsing
265 assert pv('1') == [[1], 0]
266 assert pv('1.0') == [[1,0], 0]
267 assert pv('1.0-pre5') == [[1,0], -2, [5], 0]
268 assert pv('1.0-rc5') == [[1,0], -1, [5], 0]
269 assert pv('1.0-5') == [[1,0], 0, [5], 0]
270 assert pv('1.0-post5') == [[1,0], 1, [5], 0]
271 assert pv('1.0-post') == [[1,0], 1]
272 assert pv('1-rc2.0-pre2-post') == [[1], -1, [2,0], -2, [2], 1]
273 assert pv('1-rc2.0-pre-post') == [[1], -1, [2,0], -2, [], 1]
275 assert pv('1.0-0') > pv('1.0')
276 assert pv('1.0-1') > pv('1.0-0')
277 assert pv('1.0-0') < pv('1.0-1')
279 assert pv('1.0-pre99') > pv('1.0-pre1')
280 assert pv('1.0-pre99') < pv('1.0-rc1')
281 assert pv('1.0-rc1') < pv('1.0')
282 assert pv('1.0') < pv('1.0-0')
283 assert pv('1.0-0') < pv('1.0-post')
284 assert pv('2.1.9-pre-1') > pv('2.1.9-pre')
286 assert pv('2-post999') < pv('3-pre1')
288 suite = unittest.makeSuite(TestModel)
289 if __name__ == '__main__':
290 sys.argv.append('-v')
291 unittest.main()