3 from __future__
import print_function
5 from basetest
import BaseTest
6 import sys
, tempfile
, os
7 from StringIO
import StringIO
11 foo_iface_uri
= 'http://foo'
13 sys
.path
.insert(0, '..')
14 from zeroinstall
import SafeException
15 from zeroinstall
.injector
.policy
import Policy
16 from zeroinstall
.injector
import run
, cli
, namespaces
, qdom
, selections
17 from zeroinstall
.zerostore
import Store
; Store
._add
_with
_helper
= lambda *unused
: False
19 mydir
= os
.path
.abspath(os
.path
.dirname(__file__
))
21 class SilenceLogger(logging
.Filter
):
22 def filter(self
, record
):
24 silenceLogger
= SilenceLogger()
26 class TestLaunch(BaseTest
):
27 def run_0launch(self
, args
):
28 old_stdout
= sys
.stdout
29 old_stderr
= sys
.stderr
31 sys
.stdout
= StringIO()
32 sys
.stderr
= StringIO()
43 except AttributeError:
45 except AssertionError:
47 except Exception as ex
:
49 out
= sys
.stdout
.getvalue()
50 err
= sys
.stderr
.getvalue()
52 err
+= str(ex
.__class
__)
54 sys
.stdout
= old_stdout
55 sys
.stderr
= old_stderr
59 out
, err
= self
.run_0launch([])
60 assert out
.lower().startswith("usage:")
64 out
, err
= self
.run_0launch(['--list'])
66 self
.assertEquals("Finished\n", out
)
67 cached_ifaces
= os
.path
.join(self
.cache_home
,
68 '0install.net', 'interfaces')
70 os
.makedirs(cached_ifaces
)
71 file(os
.path
.join(cached_ifaces
, 'file%3a%2f%2ffoo'), 'w').close()
73 out
, err
= self
.run_0launch(['--list'])
75 self
.assertEquals("file://foo\nFinished\n", out
)
77 out
, err
= self
.run_0launch(['--list', 'foo'])
79 self
.assertEquals("file://foo\nFinished\n", out
)
81 out
, err
= self
.run_0launch(['--list', 'bar'])
83 self
.assertEquals("Finished\n", out
)
85 out
, err
= self
.run_0launch(['--list', 'one', 'two'])
87 assert out
.lower().startswith("usage:")
89 def testVersion(self
):
90 out
, err
= self
.run_0launch(['--version'])
92 assert out
.startswith("0launch (zero-install)")
94 def testInvalid(self
):
95 a
= tempfile
.NamedTemporaryFile()
96 out
, err
= self
.run_0launch(['-q', a
.name
])
100 out
, err
= self
.run_0launch(['--dry-run', 'http://foo/d'])
101 self
.assertEquals("Would download 'http://foo/d'\nFinished\n", out
)
102 self
.assertEquals("", err
)
105 out
, err
= self
.run_0launch(['Local.xml'])
106 self
.assertEquals("", out
)
107 assert "test-echo' does not exist" in err
, err
109 def testAbsMain(self
):
110 tmp
= tempfile
.NamedTemporaryFile(prefix
= 'test-')
112 """<?xml version="1.0" ?>
113 <interface last-modified="1110752708"
115 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
117 <summary>Foo</summary>
118 <description>Foo</description>
119 <group main='/bin/sh'>
120 <implementation id='.' version='1'/>
122 </interface>""" % foo_iface_uri
)
124 policy
= Policy(tmp
.name
, config
= self
.config
)
126 downloaded
= policy
.solve_and_download_impls()
128 policy
.handler
.wait_for_blocker(downloaded
)
129 run
.execute_selections(policy
.solver
.selections
, [], stores
= policy
.config
.stores
)
131 except SafeException
as ex
:
132 assert 'Command path must be relative' in str(ex
), ex
134 def testOffline(self
):
135 out
, err
= self
.run_0launch(['--offline', 'http://foo/d'])
136 self
.assertEquals("Interface 'http://foo/d' has no usable implementations in the cache (and 0install is in off-line mode)\n", err
)
137 self
.assertEquals("", out
)
139 def testDisplay(self
):
140 os
.environ
['DISPLAY'] = ':foo'
141 out
, err
= self
.run_0launch(['--dry-run', 'http://foo/d'])
142 # Uses local copy of GUI
143 assert out
.startswith("Would execute: ")
144 assert 'basetest.py' in out
145 self
.assertEquals("", err
)
147 del os
.environ
['DISPLAY']
148 out
, err
= self
.run_0launch(['--gui', '--dry-run'])
149 self
.assertEquals("", err
)
150 self
.assertEquals("Finished\n", out
)
152 def testRefreshDisplay(self
):
153 os
.environ
['DISPLAY'] = ':foo'
154 out
, err
= self
.run_0launch(['--dry-run', '--refresh', 'http://foo/d'])
155 assert out
.startswith("Would execute: ")
156 assert 'basetest.py' in out
157 self
.assertEquals("", err
)
159 def testNeedDownload(self
):
160 os
.environ
['DISPLAY'] = ':foo'
161 out
, err
= self
.run_0launch(['--download-only', '--dry-run', 'Foo.xml'])
162 self
.assertEquals("", err
)
163 self
.assertEquals("Finished\n", out
)
165 def testSelectOnly(self
):
166 os
.environ
['DISPLAY'] = ':foo'
167 out
, err
= self
.run_0launch(['--get-selections', '--select-only', 'Hello.xml'])
168 self
.assertEquals("", err
)
170 assert out
.endswith("Finished\n")
171 out
= out
[:-len("Finished\n")]
173 root
= qdom
.parse(StringIO(str(out
)))
174 self
.assertEquals(namespaces
.XMLNS_IFACE
, root
.uri
)
175 sels
= selections
.Selections(root
)
176 sel
,= sels
.selections
.values()
177 self
.assertEquals("sha1=3ce644dc725f1d21cfcf02562c76f375944b266a", sel
.id)
180 out
, err
= self
.run_0launch(['--dry-run', 'Foo.xml'])
181 self
.assertEquals("", err
)
182 assert out
.startswith("Would execute: ")
184 out
, err
= self
.run_0launch(['Foo.xml'])
185 # (Foo.xml tries to run a directory; plash gives a different error)
186 assert "Permission denied" in err
or "Is a directory" in err
188 def testSource(self
):
189 out
, err
= self
.run_0launch(['--dry-run', '--source', 'Source.xml'])
190 self
.assertEquals("", err
)
191 assert 'Compiler.xml' in out
193 def testRanges(self
):
194 out
, err
= self
.run_0launch(['--get-selections', '--before=1', '--not-before=0.2', 'Foo.xml'])
195 assert 'tests/rpm' in out
, out
196 self
.assertEquals("", err
)
198 def testLogging(self
):
199 log
= logging
.getLogger()
200 log
.addFilter(silenceLogger
)
202 out
, err
= self
.run_0launch(['-v', '--list', 'UNKNOWN'])
203 self
.assertEquals(logging
.INFO
, log
.level
)
205 out
, err
= self
.run_0launch(['-vv', '--version'])
206 self
.assertEquals(logging
.DEBUG
, log
.level
)
208 log
.removeFilter(silenceLogger
)
209 log
.setLevel(logging
.WARN
)
212 out
, err
= self
.run_0launch(['--help'])
213 self
.assertEquals("", err
)
214 assert 'options:' in out
.lower()
216 out
, err
= self
.run_0launch([])
217 self
.assertEquals("", err
)
218 assert 'options:' in out
.lower()
224 cli
.main(['--list', 'UNKNOWN'])
229 command_feed
= os
.path
.join(mydir
, 'Command.xml')
230 out
, err
= self
.run_0launch(['--show', command_feed
])
231 self
.assertEquals("", err
)
232 assert 'Local.xml' in out
, out
234 if __name__
== '__main__':