2 from basetest
import BaseTest
3 from StringIO
import StringIO
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')
21 p
.network_use
= model
.network_full
23 #logging.getLogger().setLevel(logging.DEBUG)
24 assert p
.need_download()
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()]
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(2, len(dep
.bindings
))
56 self
.assertEquals('bin', dep
.bindings
[0].insert
)
57 self
.assertEquals('PATH', dep
.bindings
[0].name
)
59 self
.assertEquals('bin', dep
.bindings
[1].value
)
60 self
.assertEquals('NO_PATH', dep
.bindings
[1].name
)
62 self
.assertEquals(["sha1=345"], sels
[0].digests
)
64 s1
= selections
.Selections(p
)
65 s1
.selections
['http://foo/Source.xml'].attrs
['http://namespace foo'] = 'bar'
68 xml
= s1
.toDOM().toxml("utf-8")
69 root
= qdom
.parse(StringIO(xml
))
70 self
.assertEquals(namespaces
.XMLNS_IFACE
, root
.uri
)
72 s2
= selections
.Selections(root
)
75 def testLocalPath(self
):
76 # 0launch --get-selections Local.xml
77 iface
= os
.path
.join(mydir
, "Local.xml")
78 p
= policy
.Policy(iface
, config
= self
.config
)
80 s1
= selections
.Selections(p
)
81 xml
= s1
.toDOM().toxml("utf-8")
83 # Reload selections and check they're the same
84 root
= qdom
.parse(StringIO(xml
))
85 s2
= selections
.Selections(root
)
86 local_path
= s2
.selections
[iface
].local_path
87 assert os
.path
.isdir(local_path
), local_path
88 assert not s2
.selections
[iface
].digests
, s2
.selections
[iface
].digests
90 # Add a newer implementation and try again
91 feed
= self
.config
.iface_cache
.get_feed(iface
)
92 impl
= model
.ZeroInstallImplementation(feed
, "foo bar=123", local_path
= None)
93 impl
.version
= model
.parse_version('1.0')
94 impl
.commands
["run"] = model
.Command(qdom
.Element(namespaces
.XMLNS_IFACE
, 'command', {'path': 'dummy'}), None)
95 impl
.add_download_source('http://localhost/bar.tgz', 1000, None)
96 feed
.implementations
= {impl
.id: impl
}
97 assert p
.need_download()
98 assert p
.ready
, p
.solver
.get_failure_reason()
99 s1
= selections
.Selections(p
)
100 xml
= s1
.toDOM().toxml("utf-8")
101 root
= qdom
.parse(StringIO(xml
))
102 s2
= selections
.Selections(root
)
103 xml
= s2
.toDOM().toxml("utf-8")
104 qdom
.parse(StringIO(xml
))
105 assert s2
.selections
[iface
].local_path
is None
106 assert not s2
.selections
[iface
].digests
, s2
.selections
[iface
].digests
107 assert s2
.selections
[iface
].id == 'foo bar=123'
109 def testCommands(self
):
110 iface
= os
.path
.join(mydir
, "Command.xml")
111 p
= policy
.Policy(iface
, config
= self
.config
)
115 impl
= p
.solver
.selections
[self
.config
.iface_cache
.get_interface(iface
)]
116 assert impl
.id == 'c'
117 assert impl
.main
== 'runnable/missing'
119 dep_impl_uri
= impl
.commands
['run'].requires
[0].interface
120 dep_impl
= p
.solver
.selections
[self
.config
.iface_cache
.get_interface(dep_impl_uri
)]
121 assert dep_impl
.id == 'sha1=256'
123 s1
= selections
.Selections(p
)
124 assert s1
.commands
[0].path
== 'runnable/missing'
125 xml
= s1
.toDOM().toxml("utf-8")
126 root
= qdom
.parse(StringIO(xml
))
127 s2
= selections
.Selections(root
)
129 assert s2
.commands
[0].path
== 'runnable/missing'
130 impl
= s2
.selections
[iface
]
131 assert impl
.id == 'c'
133 assert s2
.commands
[0].qdom
.attrs
['http://custom attr'] == 'namespaced'
134 custom_element
= s2
.commands
[0].qdom
.childNodes
[0]
135 assert custom_element
.name
== 'child'
137 dep_impl
= s2
.selections
[dep_impl_uri
]
138 assert dep_impl
.id == 'sha1=256'
140 if __name__
== '__main__':