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
import autopolicy
, model
, cli
, namespaces
, qdom
, selections
13 from zeroinstall
.zerostore
import Store
; Store
._add
_with
_helper
= lambda *unused
: False
14 from zeroinstall
.support
import basedir
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
= autopolicy
.AutoPolicy(tmp
.name
)
123 policy
.download_and_execute([])
125 except SafeException
, ex
:
126 assert 'Command path must be relative' in str(ex
), ex
128 def testOffline(self
):
129 out
, err
= self
.run_0launch(['--offline', 'http://foo/d'])
130 self
.assertEquals("Interface 'http://foo/d' has no usable implementations\n", err
)
131 self
.assertEquals("", out
)
133 def testDisplay(self
):
134 os
.environ
['DISPLAY'] = ':foo'
135 out
, err
= self
.run_0launch(['--dry-run', 'http://foo/d'])
136 # Uses local copy of GUI
137 assert out
.startswith("Would execute: ")
138 assert 'basetest.py' in out
139 self
.assertEquals("", err
)
141 del os
.environ
['DISPLAY']
142 out
, err
= self
.run_0launch(['--gui', '--dry-run'])
143 self
.assertEquals("", err
)
144 self
.assertEquals("Finished\n", out
)
146 def testRefreshDisplay(self
):
147 os
.environ
['DISPLAY'] = ':foo'
148 out
, err
= self
.run_0launch(['--dry-run', '--refresh', 'http://foo/d'])
149 assert out
.startswith("Would execute: ")
150 assert 'basetest.py' in out
151 self
.assertEquals("", err
)
153 def testNeedDownload(self
):
154 policy
= autopolicy
.AutoPolicy(foo_iface_uri
)
156 os
.environ
['DISPLAY'] = ':foo'
157 out
, err
= self
.run_0launch(['--download-only', '--dry-run', 'Foo.xml'])
158 self
.assertEquals("", err
)
159 self
.assertEquals("Finished\n", out
)
161 def testSelectOnly(self
):
162 policy
= autopolicy
.AutoPolicy(foo_iface_uri
)
164 os
.environ
['DISPLAY'] = ':foo'
165 out
, err
= self
.run_0launch(['--get-selections', '--select-only', 'Hello.xml'])
166 self
.assertEquals("", err
)
168 assert out
.endswith("Finished\n")
169 out
= out
[:-len("Finished\n")]
171 root
= qdom
.parse(StringIO(str(out
)))
172 self
.assertEquals(namespaces
.XMLNS_IFACE
, root
.uri
)
173 sels
= selections
.Selections(root
)
174 sel
,= sels
.selections
.values()
175 self
.assertEquals("sha1=3ce644dc725f1d21cfcf02562c76f375944b266a", sel
.id)
178 out
, err
= self
.run_0launch(['--dry-run', 'Foo.xml'])
179 self
.assertEquals("", err
)
180 assert out
.startswith("Would execute: ")
182 out
, err
= self
.run_0launch(['Foo.xml'])
183 # (Foo.xml tries to run a directory; plash gives a different error)
184 assert "Permission denied" in err
or "Is a directory" in err
186 def testSource(self
):
187 out
, err
= self
.run_0launch(['--dry-run', '--source', 'Source.xml'])
188 self
.assertEquals("", err
)
189 assert 'Compiler.xml' in out
191 def testRanges(self
):
192 out
, err
= self
.run_0launch(['--get-selections', '--before=1', '--not-before=0.2', 'Foo.xml'])
193 assert 'tests/rpm' in out
, out
194 self
.assertEquals("", err
)
196 def testLogging(self
):
197 log
= logging
.getLogger()
198 log
.addFilter(silenceLogger
)
200 out
, err
= self
.run_0launch(['-v', '--list', 'UNKNOWN'])
201 self
.assertEquals(logging
.INFO
, log
.level
)
203 out
, err
= self
.run_0launch(['-vv', '--version'])
204 self
.assertEquals(logging
.DEBUG
, log
.level
)
206 log
.removeFilter(silenceLogger
)
207 log
.setLevel(logging
.WARN
)
210 out
, err
= self
.run_0launch(['--help'])
211 self
.assertEquals("", err
)
212 assert 'options:' in out
.lower()
214 out
, err
= self
.run_0launch([])
215 self
.assertEquals("", err
)
216 assert 'options:' in out
.lower()
222 cli
.main(['--list', 'UNKNOWN'])
227 command_feed
= os
.path
.join(mydir
, 'Command.xml')
228 out
, err
= self
.run_0launch(['--show', command_feed
])
229 self
.assertEquals("", err
)
230 assert 'Local.xml' in out
, out
232 if __name__
== '__main__':