/dev/null => os.devnull, for portability
[zeroinstall/solver.git] / tests / testselections.py
blob79b242d95d45f531da421ddec22b78716922dac3
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, driver, requirements
10 mydir = os.path.dirname(os.path.abspath(__file__))
11 runexec = os.path.join(mydir, 'runnable', 'RunExec.xml')
12 runnable = os.path.join(mydir, 'runnable', 'Runnable.xml')
14 class TestSelections(BaseTest):
15 def testSelections(self):
16 p = policy.Policy('http://foo/Source.xml', src = True, config = self.config)
17 source = self.config.iface_cache.get_interface('http://foo/Source.xml')
18 compiler = self.config.iface_cache.get_interface('http://foo/Compiler.xml')
19 self.import_feed(source.uri, 'Source.xml')
20 self.import_feed(compiler.uri, 'Compiler.xml')
22 p.freshness = 0
23 p.network_use = model.network_full
24 #import logging
25 #logging.getLogger().setLevel(logging.DEBUG)
26 assert p.need_download()
28 def assertSel(s):
29 self.assertEqual('http://foo/Source.xml', s.interface)
30 self.assertEqual(2, len(s.selections))
32 sels = [(sel.interface, sel) for sel in s.selections.values()]
33 sels.sort()
34 sels = [sel for uri,sel in sels]
36 self.assertEqual('http://foo/Compiler.xml', sels[0].interface)
37 self.assertEqual('http://foo/Source.xml', sels[1].interface)
39 self.assertEqual("sha1=345", sels[0].id)
40 self.assertEqual("1.0", sels[0].version)
42 self.assertEqual('sha1=234', sels[1].id)
43 self.assertEqual("1.0", sels[1].version)
44 self.assertEqual("bar", sels[1].attrs['http://namespace foo'])
45 self.assertEqual("1.0", sels[1].attrs['version'])
46 assert 'version-modifier' not in sels[1].attrs
48 self.assertEqual(0, len(sels[0].bindings))
49 self.assertEqual(0, len(sels[0].dependencies))
51 self.assertEqual(2, len(sels[1].bindings))
52 self.assertEqual('.', sels[1].bindings[0].insert)
53 self.assertEqual('/', sels[1].bindings[1].mount_point)
55 self.assertEqual(1, len(sels[1].dependencies))
56 dep = sels[1].dependencies[0]
57 self.assertEqual('http://foo/Compiler.xml', dep.interface)
58 self.assertEqual(3, len(dep.bindings))
59 self.assertEqual('bin', dep.bindings[0].insert)
60 self.assertEqual('PATH', dep.bindings[0].name)
61 self.assertEqual('prepend', dep.bindings[0].mode)
62 assert dep.bindings[0].separator in ';:'
64 self.assertEqual('bin', dep.bindings[1].value)
65 self.assertEqual('NO_PATH', dep.bindings[1].name)
66 self.assertEqual(',', dep.bindings[1].separator)
68 self.assertEqual('bin', dep.bindings[2].insert)
69 self.assertEqual('BINDIR', dep.bindings[2].name)
70 self.assertEqual('replace', dep.bindings[2].mode)
72 self.assertEqual(["sha1=345"], sels[0].digests)
74 s1 = p.solver.selections
75 s1.selections['http://foo/Source.xml'].attrs['http://namespace foo'] = 'bar'
76 assertSel(s1)
78 xml = s1.toDOM().toxml("utf-8")
79 root = qdom.parse(StringIO(xml))
80 self.assertEqual(namespaces.XMLNS_IFACE, root.uri)
82 s2 = selections.Selections(root)
83 assertSel(s2)
85 def testLocalPath(self):
86 # 0launch --get-selections Local.xml
87 iface = os.path.join(mydir, "Local.xml")
88 p = policy.Policy(iface, config = self.config)
89 p.need_download()
90 assert p.ready
91 s1 = p.solver.selections
92 xml = s1.toDOM().toxml("utf-8")
94 # Reload selections and check they're the same
95 root = qdom.parse(StringIO(xml))
96 s2 = selections.Selections(root)
97 local_path = s2.selections[iface].local_path
98 assert os.path.isdir(local_path), local_path
99 assert not s2.selections[iface].digests, s2.selections[iface].digests
101 # Add a newer implementation and try again
102 feed = self.config.iface_cache.get_feed(iface)
103 impl = model.ZeroInstallImplementation(feed, "foo bar=123", local_path = None)
104 impl.version = model.parse_version('1.0')
105 impl.commands["run"] = model.Command(qdom.Element(namespaces.XMLNS_IFACE, 'command', {'path': 'dummy', 'name': 'run'}), None)
106 impl.add_download_source('http://localhost/bar.tgz', 1000, None)
107 feed.implementations = {impl.id: impl}
108 assert p.need_download()
109 assert p.ready, p.solver.get_failure_reason()
110 s1 = p.solver.selections
111 xml = s1.toDOM().toxml("utf-8")
112 root = qdom.parse(StringIO(xml))
113 s2 = selections.Selections(root)
114 xml = s2.toDOM().toxml("utf-8")
115 qdom.parse(StringIO(xml))
116 assert s2.selections[iface].local_path is None
117 assert not s2.selections[iface].digests, s2.selections[iface].digests
118 assert s2.selections[iface].id == 'foo bar=123'
120 def testCommands(self):
121 iface = os.path.join(mydir, "Command.xml")
122 p = policy.Policy(iface, config = self.config)
123 p.need_download()
124 assert p.ready
126 impl = p.solver.selections[self.config.iface_cache.get_interface(iface)]
127 assert impl.id == 'c'
128 assert impl.main == 'test-gui'
130 dep_impl_uri = impl.commands['run'].requires[0].interface
131 dep_impl = p.solver.selections[self.config.iface_cache.get_interface(dep_impl_uri)]
132 assert dep_impl.id == 'sha1=256'
134 s1 = p.solver.selections
135 assert s1.commands[0].path == 'test-gui'
136 xml = s1.toDOM().toxml("utf-8")
137 root = qdom.parse(StringIO(xml))
138 s2 = selections.Selections(root)
140 assert s2.commands[0].path == 'test-gui'
141 impl = s2.selections[iface]
142 assert impl.id == 'c'
144 assert s2.commands[0].qdom.attrs['http://custom attr'] == 'namespaced'
145 custom_element = s2.commands[0].qdom.childNodes[0]
146 assert custom_element.name == 'child'
148 dep_impl = s2.selections[dep_impl_uri]
149 assert dep_impl.id == 'sha1=256'
151 d = driver.Driver(self.config, requirements.Requirements(runexec))
152 need_download = d.need_download()
153 assert need_download == False
155 xml = d.solver.selections.toDOM().toxml("utf-8")
156 root = qdom.parse(StringIO(xml))
157 s3 = selections.Selections(root)
158 runnable_impl = s3.selections[runnable]
159 assert 'foo' in runnable_impl.commands
160 assert 'run' in runnable_impl.commands
162 def testOldCommands(self):
163 command_feed = os.path.join(mydir, 'old-selections.xml')
164 with open(command_feed) as stream:
165 s1 = selections.Selections(qdom.parse(stream))
166 self.assertEqual("run", s1.command)
167 self.assertEqual(2, len(s1.commands))
168 self.assertEqual("bin/java", s1.commands[1].path)
170 xml = s1.toDOM().toxml("utf-8")
171 root = qdom.parse(StringIO(xml))
172 s2 = selections.Selections(root)
174 self.assertEqual("run", s2.command)
175 self.assertEqual(2, len(s2.commands))
176 self.assertEqual("bin/java", s2.commands[1].path)
179 if __name__ == '__main__':
180 unittest.main()