All unit-tests now pass with Python 3
[zeroinstall/solver.git] / tests / testselections.py
blobcd103f312755e97d7674ecccbd7c07a6ea624b67
1 #!/usr/bin/env python
2 from basetest import BaseTest
3 from io import BytesIO
4 import sys, os
5 import unittest
7 sys.path.insert(0, '..')
8 from zeroinstall.injector import selections, model, namespaces, qdom, requirements
9 from zeroinstall.injector.requirements import Requirements
10 from zeroinstall.injector.driver import Driver
12 mydir = os.path.dirname(os.path.abspath(__file__))
13 runexec = os.path.join(mydir, 'runnable', 'RunExec.xml')
14 runnable = os.path.join(mydir, 'runnable', 'Runnable.xml')
16 class TestSelections(BaseTest):
17 def testSelections(self):
18 requirements = Requirements('http://foo/Source.xml')
19 requirements.source = True
20 requirements.command = 'compile'
21 driver = Driver(requirements = requirements, config = self.config)
22 source = self.config.iface_cache.get_interface('http://foo/Source.xml')
23 compiler = self.config.iface_cache.get_interface('http://foo/Compiler.xml')
24 self.import_feed(source.uri, 'Source.xml')
25 self.import_feed(compiler.uri, 'Compiler.xml')
27 self.config.network_use = model.network_full
28 #import logging
29 #logging.getLogger().setLevel(logging.DEBUG)
30 assert driver.need_download()
32 def assertSel(s):
33 self.assertEqual('http://foo/Source.xml', s.interface)
34 self.assertEqual(2, len(s.selections))
36 sels = [(sel.interface, sel) for sel in s.selections.values()]
37 sels.sort()
38 sels = [sel for uri,sel in sels]
40 self.assertEqual('http://foo/Compiler.xml', sels[0].interface)
41 self.assertEqual('http://foo/Source.xml', sels[1].interface)
43 self.assertEqual("sha1=345", sels[0].id)
44 self.assertEqual("1.0", sels[0].version)
46 self.assertEqual('sha1=234', sels[1].id)
47 self.assertEqual("1.0", sels[1].version)
48 self.assertEqual("bar", sels[1].attrs['http://namespace foo'])
49 self.assertEqual("1.0", sels[1].attrs['version'])
50 assert 'version-modifier' not in sels[1].attrs
52 self.assertEqual(0, len(sels[0].bindings))
53 self.assertEqual(0, len(sels[0].dependencies))
55 self.assertEqual(2, len(sels[1].bindings))
56 self.assertEqual('.', sels[1].bindings[0].insert)
57 self.assertEqual('/', sels[1].bindings[1].mount_point)
59 self.assertEqual(1, len(sels[1].dependencies))
60 dep = sels[1].dependencies[0]
61 self.assertEqual('http://foo/Compiler.xml', dep.interface)
62 self.assertEqual(3, len(dep.bindings))
63 self.assertEqual('bin', dep.bindings[0].insert)
64 self.assertEqual('PATH', dep.bindings[0].name)
65 self.assertEqual('prepend', dep.bindings[0].mode)
66 assert dep.bindings[0].separator in ';:'
68 self.assertEqual('bin', dep.bindings[1].value)
69 self.assertEqual('NO_PATH', dep.bindings[1].name)
70 self.assertEqual(',', dep.bindings[1].separator)
72 self.assertEqual('bin', dep.bindings[2].insert)
73 self.assertEqual('BINDIR', dep.bindings[2].name)
74 self.assertEqual('replace', dep.bindings[2].mode)
76 self.assertEqual(["sha1=345"], sels[0].digests)
78 assert driver.solver.ready, driver.solver.get_failure_reason()
79 s1 = driver.solver.selections
80 s1.selections['http://foo/Source.xml'].attrs['http://namespace foo'] = 'bar'
81 assertSel(s1)
83 xml = s1.toDOM().toxml("utf-8")
84 root = qdom.parse(BytesIO(xml))
85 self.assertEqual(namespaces.XMLNS_IFACE, root.uri)
87 s2 = selections.Selections(root)
88 assertSel(s2)
90 def testLocalPath(self):
91 # 0launch --get-selections Local.xml
92 iface = os.path.join(mydir, "Local.xml")
93 driver = Driver(requirements = Requirements(iface), config = self.config)
94 driver.need_download()
95 assert driver.solver.ready
96 s1 = driver.solver.selections
97 xml = s1.toDOM().toxml("utf-8")
99 # Reload selections and check they're the same
100 root = qdom.parse(BytesIO(xml))
101 s2 = selections.Selections(root)
102 local_path = s2.selections[iface].local_path
103 assert os.path.isdir(local_path), local_path
104 assert not s2.selections[iface].digests, s2.selections[iface].digests
106 # Add a newer implementation and try again
107 feed = self.config.iface_cache.get_feed(iface)
108 impl = model.ZeroInstallImplementation(feed, "foo bar=123", local_path = None)
109 impl.version = model.parse_version('1.0')
110 impl.commands["run"] = model.Command(qdom.Element(namespaces.XMLNS_IFACE, 'command', {'path': 'dummy', 'name': 'run'}), None)
111 impl.add_download_source('http://localhost/bar.tgz', 1000, None)
112 feed.implementations = {impl.id: impl}
113 assert driver.need_download()
114 assert driver.solver.ready, driver.solver.get_failure_reason()
115 s1 = driver.solver.selections
116 xml = s1.toDOM().toxml("utf-8")
117 root = qdom.parse(BytesIO(xml))
118 s2 = selections.Selections(root)
119 xml = s2.toDOM().toxml("utf-8")
120 qdom.parse(BytesIO(xml))
121 assert s2.selections[iface].local_path is None
122 assert not s2.selections[iface].digests, s2.selections[iface].digests
123 assert s2.selections[iface].id == 'foo bar=123'
125 def testCommands(self):
126 iface = os.path.join(mydir, "Command.xml")
127 driver = Driver(requirements = Requirements(iface), config = self.config)
128 driver.need_download()
129 assert driver.solver.ready
131 impl = driver.solver.selections[self.config.iface_cache.get_interface(iface)]
132 assert impl.id == 'c'
133 assert impl.main == 'test-gui'
135 dep_impl_uri = impl.commands['run'].requires[0].interface
136 dep_impl = driver.solver.selections[self.config.iface_cache.get_interface(dep_impl_uri)]
137 assert dep_impl.id == 'sha1=256'
139 s1 = driver.solver.selections
140 assert s1.commands[0].path == 'test-gui'
141 xml = s1.toDOM().toxml("utf-8")
142 root = qdom.parse(BytesIO(xml))
143 s2 = selections.Selections(root)
145 assert s2.commands[0].path == 'test-gui'
146 impl = s2.selections[iface]
147 assert impl.id == 'c'
149 assert s2.commands[0].qdom.attrs['http://custom attr'] == 'namespaced'
150 custom_element = s2.commands[0].qdom.childNodes[0]
151 assert custom_element.name == 'child'
153 dep_impl = s2.selections[dep_impl_uri]
154 assert dep_impl.id == 'sha1=256'
156 d = Driver(self.config, requirements.Requirements(runexec))
157 need_download = d.need_download()
158 assert need_download == False
160 xml = d.solver.selections.toDOM().toxml("utf-8")
161 root = qdom.parse(BytesIO(xml))
162 s3 = selections.Selections(root)
163 runnable_impl = s3.selections[runnable]
164 assert 'foo' in runnable_impl.commands
165 assert 'run' in runnable_impl.commands
167 def testOldCommands(self):
168 command_feed = os.path.join(mydir, 'old-selections.xml')
169 with open(command_feed, 'rb') as stream:
170 s1 = selections.Selections(qdom.parse(stream))
171 self.assertEqual("run", s1.command)
172 self.assertEqual(2, len(s1.commands))
173 self.assertEqual("bin/java", s1.commands[1].path)
175 xml = s1.toDOM().toxml("utf-8")
176 root = qdom.parse(BytesIO(xml))
177 s2 = selections.Selections(root)
179 self.assertEqual("run", s2.command)
180 self.assertEqual(2, len(s2.commands))
181 self.assertEqual("bin/java", s2.commands[1].path)
184 if __name__ == '__main__':
185 unittest.main()