In selections, store <command> elements inside <selection> elements
[zeroinstall.git] / tests / testselections.py
blobb0a34923ef7f982800a73bbfeb49f3fa38a206b6
1 #!/usr/bin/env python
2 from basetest import BaseTest
3 from StringIO import StringIO
4 import sys, os
5 import unittest
7 sys.path.insert(0, '..')
8 from zeroinstall.injector import selections, model, policy, namespaces, qdom
10 mydir = os.path.dirname(os.path.abspath(__file__))
12 class TestSelections(BaseTest):
13 def testSelections(self):
14 p = policy.Policy('http://foo/Source.xml', src = True, config = self.config)
15 source = self.config.iface_cache.get_interface('http://foo/Source.xml')
16 compiler = self.config.iface_cache.get_interface('http://foo/Compiler.xml')
17 self.import_feed(source.uri, 'Source.xml')
18 self.import_feed(compiler.uri, 'Compiler.xml')
20 p.freshness = 0
21 p.network_use = model.network_full
22 #import logging
23 #logging.getLogger().setLevel(logging.DEBUG)
24 assert p.need_download()
26 def assertSel(s):
27 self.assertEquals('http://foo/Source.xml', s.interface)
28 self.assertEquals(2, len(s.selections))
30 sels = [(sel.interface, sel) for sel in s.selections.values()]
31 sels.sort()
32 sels = [sel for uri,sel in sels]
34 self.assertEquals('http://foo/Compiler.xml', sels[0].interface)
35 self.assertEquals('http://foo/Source.xml', sels[1].interface)
37 self.assertEquals("sha1=345", sels[0].id)
38 self.assertEquals("1.0", sels[0].version)
40 self.assertEquals('sha1=234', sels[1].id)
41 self.assertEquals("1.0", sels[1].version)
42 self.assertEquals("bar", sels[1].attrs['http://namespace foo'])
43 self.assertEquals("1.0", sels[1].attrs['version'])
44 assert 'version-modifier' not in sels[1].attrs
46 self.assertEquals(0, len(sels[0].bindings))
47 self.assertEquals(0, len(sels[0].dependencies))
49 self.assertEquals(1, len(sels[1].bindings))
50 self.assertEquals('.', sels[1].bindings[0].insert)
52 self.assertEquals(1, len(sels[1].dependencies))
53 dep = sels[1].dependencies[0]
54 self.assertEquals('http://foo/Compiler.xml', dep.interface)
55 self.assertEquals(3, len(dep.bindings))
56 self.assertEquals('bin', dep.bindings[0].insert)
57 self.assertEquals('PATH', dep.bindings[0].name)
58 self.assertEquals('prepend', dep.bindings[0].mode)
59 assert dep.bindings[0].separator in ';:'
61 self.assertEquals('bin', dep.bindings[1].value)
62 self.assertEquals('NO_PATH', dep.bindings[1].name)
63 self.assertEquals(',', dep.bindings[1].separator)
65 self.assertEquals('bin', dep.bindings[2].insert)
66 self.assertEquals('BINDIR', dep.bindings[2].name)
67 self.assertEquals('replace', dep.bindings[2].mode)
69 self.assertEquals(["sha1=345"], sels[0].digests)
71 s1 = p.solver.selections
72 s1.selections['http://foo/Source.xml'].attrs['http://namespace foo'] = 'bar'
73 assertSel(s1)
75 xml = s1.toDOM().toxml("utf-8")
76 root = qdom.parse(StringIO(xml))
77 self.assertEquals(namespaces.XMLNS_IFACE, root.uri)
79 s2 = selections.Selections(root)
80 assertSel(s2)
82 def testLocalPath(self):
83 # 0launch --get-selections Local.xml
84 iface = os.path.join(mydir, "Local.xml")
85 p = policy.Policy(iface, config = self.config)
86 p.need_download()
87 assert p.ready
88 s1 = p.solver.selections
89 xml = s1.toDOM().toxml("utf-8")
91 # Reload selections and check they're the same
92 root = qdom.parse(StringIO(xml))
93 s2 = selections.Selections(root)
94 local_path = s2.selections[iface].local_path
95 assert os.path.isdir(local_path), local_path
96 assert not s2.selections[iface].digests, s2.selections[iface].digests
98 # Add a newer implementation and try again
99 feed = self.config.iface_cache.get_feed(iface)
100 impl = model.ZeroInstallImplementation(feed, "foo bar=123", local_path = None)
101 impl.version = model.parse_version('1.0')
102 impl.commands["run"] = model.Command(qdom.Element(namespaces.XMLNS_IFACE, 'command', {'path': 'dummy'}), None)
103 impl.add_download_source('http://localhost/bar.tgz', 1000, None)
104 feed.implementations = {impl.id: impl}
105 assert p.need_download()
106 assert p.ready, p.solver.get_failure_reason()
107 s1 = p.solver.selections
108 xml = s1.toDOM().toxml("utf-8")
109 root = qdom.parse(StringIO(xml))
110 s2 = selections.Selections(root)
111 xml = s2.toDOM().toxml("utf-8")
112 qdom.parse(StringIO(xml))
113 assert s2.selections[iface].local_path is None
114 assert not s2.selections[iface].digests, s2.selections[iface].digests
115 assert s2.selections[iface].id == 'foo bar=123'
117 def testCommands(self):
118 iface = os.path.join(mydir, "Command.xml")
119 p = policy.Policy(iface, config = self.config)
120 p.need_download()
121 assert p.ready
123 impl = p.solver.selections[self.config.iface_cache.get_interface(iface)]
124 assert impl.id == 'c'
125 assert impl.main == 'runnable/missing'
127 dep_impl_uri = impl.commands['run'].requires[0].interface
128 dep_impl = p.solver.selections[self.config.iface_cache.get_interface(dep_impl_uri)]
129 assert dep_impl.id == 'sha1=256'
131 s1 = p.solver.selections
132 assert s1.commands[0].path == 'runnable/missing'
133 xml = s1.toDOM().toxml("utf-8")
134 root = qdom.parse(StringIO(xml))
135 s2 = selections.Selections(root)
137 assert s2.commands[0].path == 'runnable/missing'
138 impl = s2.selections[iface]
139 assert impl.id == 'c'
141 assert s2.commands[0].qdom.attrs['http://custom attr'] == 'namespaced'
142 custom_element = s2.commands[0].qdom.childNodes[0]
143 assert custom_element.name == 'child'
145 dep_impl = s2.selections[dep_impl_uri]
146 assert dep_impl.id == 'sha1=256'
148 def testOldCommands(self):
149 command_feed = os.path.join(mydir, 'old-selections.xml')
150 with open(command_feed) as stream:
151 s1 = selections.Selections(qdom.parse(stream))
152 self.assertEquals("run", s1.command)
153 self.assertEquals(2, len(s1.commands))
154 self.assertEquals("bin/java", s1.commands[1].path)
156 xml = s1.toDOM().toxml("utf-8")
157 root = qdom.parse(StringIO(xml))
158 s2 = selections.Selections(root)
160 self.assertEquals("run", s2.command)
161 self.assertEquals(2, len(s2.commands))
162 self.assertEquals("bin/java", s2.commands[1].path)
165 if __name__ == '__main__':
166 unittest.main()