2 from basetest
import BaseTest
3 import sys
, tempfile
, os
4 from StringIO
import StringIO
8 foo_iface_uri
= 'http://foo'
10 sys
.path
.insert(0, '..')
11 from zeroinstall
import SafeException
12 from zeroinstall
.injector
.policy
import Policy
13 from zeroinstall
.injector
import run
, cli
, namespaces
, qdom
, selections
14 from zeroinstall
.zerostore
import Store
; Store
._add
_with
_helper
= lambda *unused
: False
16 mydir
= os
.path
.abspath(os
.path
.dirname(__file__
))
18 class SilenceLogger(logging
.Filter
):
19 def filter(self
, record
):
21 silenceLogger
= SilenceLogger()
23 class TestLaunch(BaseTest
):
24 def run_0launch(self
, args
):
25 old_stdout
= sys
.stdout
26 old_stderr
= sys
.stderr
28 sys
.stdout
= StringIO()
29 sys
.stderr
= StringIO()
40 except AttributeError:
42 except AssertionError:
46 out
= sys
.stdout
.getvalue()
47 err
= sys
.stderr
.getvalue()
49 err
+= str(ex
.__class
__)
51 sys
.stdout
= old_stdout
52 sys
.stderr
= old_stderr
56 out
, err
= self
.run_0launch([])
57 assert out
.lower().startswith("usage:")
61 out
, err
= self
.run_0launch(['--list'])
63 self
.assertEquals("Finished\n", out
)
64 cached_ifaces
= os
.path
.join(self
.cache_home
,
65 '0install.net', 'interfaces')
67 os
.makedirs(cached_ifaces
)
68 file(os
.path
.join(cached_ifaces
, 'file%3a%2f%2ffoo'), 'w').close()
70 out
, err
= self
.run_0launch(['--list'])
72 self
.assertEquals("file://foo\nFinished\n", out
)
74 out
, err
= self
.run_0launch(['--list', 'foo'])
76 self
.assertEquals("file://foo\nFinished\n", out
)
78 out
, err
= self
.run_0launch(['--list', 'bar'])
80 self
.assertEquals("Finished\n", out
)
82 out
, err
= self
.run_0launch(['--list', 'one', 'two'])
84 assert out
.lower().startswith("usage:")
86 def testVersion(self
):
87 out
, err
= self
.run_0launch(['--version'])
89 assert out
.startswith("0launch (zero-install)")
91 def testInvalid(self
):
92 a
= tempfile
.NamedTemporaryFile()
93 out
, err
= self
.run_0launch(['-q', a
.name
])
97 out
, err
= self
.run_0launch(['--dry-run', 'http://foo/d'])
98 self
.assertEquals("Would download 'http://foo/d'\nFinished\n", out
)
99 self
.assertEquals("", err
)
102 out
, err
= self
.run_0launch(['Local.xml'])
103 self
.assertEquals("", out
)
104 assert "test-echo' does not exist" in err
, err
106 def testAbsMain(self
):
107 tmp
= tempfile
.NamedTemporaryFile(prefix
= 'test-')
109 """<?xml version="1.0" ?>
110 <interface last-modified="1110752708"
112 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
114 <summary>Foo</summary>
115 <description>Foo</description>
116 <group main='/bin/sh'>
117 <implementation id='.' version='1'/>
119 </interface>""" % foo_iface_uri
)
121 policy
= Policy(tmp
.name
, config
= self
.config
)
123 downloaded
= policy
.solve_and_download_impls()
125 policy
.handler
.wait_for_blocker(downloaded
)
126 run
.execute_selections(policy
.solver
.selections
, [], stores
= policy
.config
.stores
)
128 except SafeException
, ex
:
129 assert 'Command path must be relative' in str(ex
), ex
131 def testOffline(self
):
132 out
, err
= self
.run_0launch(['--offline', 'http://foo/d'])
133 self
.assertEquals("Interface 'http://foo/d' has no usable implementations\n", err
)
134 self
.assertEquals("", out
)
136 def testDisplay(self
):
137 os
.environ
['DISPLAY'] = ':foo'
138 out
, err
= self
.run_0launch(['--dry-run', 'http://foo/d'])
139 # Uses local copy of GUI
140 assert out
.startswith("Would execute: ")
141 assert 'basetest.py' in out
142 self
.assertEquals("", err
)
144 del os
.environ
['DISPLAY']
145 out
, err
= self
.run_0launch(['--gui', '--dry-run'])
146 self
.assertEquals("", err
)
147 self
.assertEquals("Finished\n", out
)
149 def testRefreshDisplay(self
):
150 os
.environ
['DISPLAY'] = ':foo'
151 out
, err
= self
.run_0launch(['--dry-run', '--refresh', 'http://foo/d'])
152 assert out
.startswith("Would execute: ")
153 assert 'basetest.py' in out
154 self
.assertEquals("", err
)
156 def testNeedDownload(self
):
157 os
.environ
['DISPLAY'] = ':foo'
158 out
, err
= self
.run_0launch(['--download-only', '--dry-run', 'Foo.xml'])
159 self
.assertEquals("", err
)
160 self
.assertEquals("Finished\n", out
)
162 def testSelectOnly(self
):
163 os
.environ
['DISPLAY'] = ':foo'
164 out
, err
= self
.run_0launch(['--get-selections', '--select-only', 'Hello.xml'])
165 self
.assertEquals("", err
)
167 assert out
.endswith("Finished\n")
168 out
= out
[:-len("Finished\n")]
170 root
= qdom
.parse(StringIO(str(out
)))
171 self
.assertEquals(namespaces
.XMLNS_IFACE
, root
.uri
)
172 sels
= selections
.Selections(root
)
173 sel
,= sels
.selections
.values()
174 self
.assertEquals("sha1=3ce644dc725f1d21cfcf02562c76f375944b266a", sel
.id)
177 out
, err
= self
.run_0launch(['--dry-run', 'Foo.xml'])
178 self
.assertEquals("", err
)
179 assert out
.startswith("Would execute: ")
181 out
, err
= self
.run_0launch(['Foo.xml'])
182 # (Foo.xml tries to run a directory; plash gives a different error)
183 assert "Permission denied" in err
or "Is a directory" in err
185 def testSource(self
):
186 out
, err
= self
.run_0launch(['--dry-run', '--source', 'Source.xml'])
187 self
.assertEquals("", err
)
188 assert 'Compiler.xml' in out
190 def testRanges(self
):
191 out
, err
= self
.run_0launch(['--get-selections', '--before=1', '--not-before=0.2', 'Foo.xml'])
192 assert 'tests/rpm' in out
, out
193 self
.assertEquals("", err
)
195 def testLogging(self
):
196 log
= logging
.getLogger()
197 log
.addFilter(silenceLogger
)
199 out
, err
= self
.run_0launch(['-v', '--list', 'UNKNOWN'])
200 self
.assertEquals(logging
.INFO
, log
.level
)
202 out
, err
= self
.run_0launch(['-vv', '--version'])
203 self
.assertEquals(logging
.DEBUG
, log
.level
)
205 log
.removeFilter(silenceLogger
)
206 log
.setLevel(logging
.WARN
)
209 out
, err
= self
.run_0launch(['--help'])
210 self
.assertEquals("", err
)
211 assert 'options:' in out
.lower()
213 out
, err
= self
.run_0launch([])
214 self
.assertEquals("", err
)
215 assert 'options:' in out
.lower()
221 cli
.main(['--list', 'UNKNOWN'])
226 command_feed
= os
.path
.join(mydir
, 'Command.xml')
227 out
, err
= self
.run_0launch(['--show', command_feed
])
228 self
.assertEquals("", err
)
229 assert 'Local.xml' in out
, out
231 if __name__
== '__main__':