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
.injector
import autopolicy
, model
, cli
, namespaces
, qdom
, selections
12 from zeroinstall
.zerostore
import Store
; Store
._add
_with
_helper
= lambda *unused
: False
13 from zeroinstall
.support
import basedir
15 class SilenceLogger(logging
.Filter
):
16 def filter(self
, record
):
18 silenceLogger
= SilenceLogger()
20 class TestLaunch(BaseTest
):
21 def run_0launch(self
, args
):
22 old_stdout
= sys
.stdout
23 old_stderr
= sys
.stderr
25 sys
.stdout
= StringIO()
26 sys
.stderr
= StringIO()
37 except AttributeError:
39 except AssertionError:
43 out
= sys
.stdout
.getvalue()
44 err
= sys
.stderr
.getvalue()
46 err
+= str(ex
.__class
__)
48 sys
.stdout
= old_stdout
49 sys
.stderr
= old_stderr
53 out
, err
= self
.run_0launch([])
54 assert out
.lower().startswith("usage:")
58 out
, err
= self
.run_0launch(['--list'])
60 self
.assertEquals("Finished\n", out
)
61 cached_ifaces
= os
.path
.join(self
.cache_home
,
62 '0install.net', 'interfaces')
64 os
.makedirs(cached_ifaces
)
65 file(os
.path
.join(cached_ifaces
, 'file%3a%2f%2ffoo'), 'w').close()
67 out
, err
= self
.run_0launch(['--list'])
69 self
.assertEquals("file://foo\nFinished\n", out
)
71 out
, err
= self
.run_0launch(['--list', 'foo'])
73 self
.assertEquals("file://foo\nFinished\n", out
)
75 out
, err
= self
.run_0launch(['--list', 'bar'])
77 self
.assertEquals("Finished\n", out
)
79 out
, err
= self
.run_0launch(['--list', 'one', 'two'])
81 assert out
.lower().startswith("usage:")
83 def testVersion(self
):
84 out
, err
= self
.run_0launch(['--version'])
86 assert out
.startswith("0launch (zero-install)")
88 def testInvalid(self
):
89 a
= tempfile
.NamedTemporaryFile()
90 out
, err
= self
.run_0launch(['-q', a
.name
])
94 out
, err
= self
.run_0launch(['--dry-run', 'http://foo/d'])
95 self
.assertEquals("Would download 'http://foo/d'\nFinished\n", out
)
96 self
.assertEquals("", err
)
99 out
, err
= self
.run_0launch(['Local.xml'])
100 self
.assertEquals("", out
)
101 assert "test-echo' does not exist" in err
103 def testOffline(self
):
104 out
, err
= self
.run_0launch(['--offline', 'http://foo/d'])
105 self
.assertEquals("Can't find all required implementations:\n- <Interface http://foo/d> -> None\n", err
)
106 self
.assertEquals("", out
)
108 def testDisplay(self
):
109 os
.environ
['DISPLAY'] = ':foo'
110 out
, err
= self
.run_0launch(['--dry-run', 'http://foo/d'])
111 # Uses local copy of GUI
112 assert out
.startswith("Would execute: ")
113 assert 'basetest.py' in out
114 self
.assertEquals("", err
)
116 del os
.environ
['DISPLAY']
117 out
, err
= self
.run_0launch(['--gui', '--dry-run'])
118 self
.assertEquals("", err
)
119 self
.assertEquals("Finished\n", out
)
121 def testRefreshDisplay(self
):
122 os
.environ
['DISPLAY'] = ':foo'
123 out
, err
= self
.run_0launch(['--dry-run', '--refresh', 'http://foo/d'])
124 assert out
.startswith("Would execute: ")
125 assert 'basetest.py' in out
126 self
.assertEquals("", err
)
128 def testNeedDownload(self
):
129 policy
= autopolicy
.AutoPolicy(foo_iface_uri
)
131 os
.environ
['DISPLAY'] = ':foo'
132 out
, err
= self
.run_0launch(['--download-only', '--dry-run', 'Foo.xml'])
133 self
.assertEquals("", err
)
134 self
.assertEquals("Finished\n", out
)
136 def testSelectOnly(self
):
137 policy
= autopolicy
.AutoPolicy(foo_iface_uri
)
139 os
.environ
['DISPLAY'] = ':foo'
140 out
, err
= self
.run_0launch(['--get-selections', '--select-only', 'Hello.xml'])
141 self
.assertEquals("", err
)
143 assert out
.endswith("Finished\n")
144 out
= out
[:-len("Finished\n")]
146 root
= qdom
.parse(StringIO(str(out
)))
147 self
.assertEquals(namespaces
.XMLNS_IFACE
, root
.uri
)
148 sels
= selections
.Selections(root
)
149 sel
,= sels
.selections
.values()
150 self
.assertEquals("sha1=3ce644dc725f1d21cfcf02562c76f375944b266a", sel
.id)
153 out
, err
= self
.run_0launch(['--dry-run', 'Foo.xml'])
154 self
.assertEquals("", err
)
155 assert out
.startswith("Would execute: ")
157 out
, err
= self
.run_0launch(['Foo.xml'])
158 # (Foo.xml tries to run a directory; plash gives a different error)
159 assert "Permission denied" in err
or "Is a directory" in err
161 def testSource(self
):
162 out
, err
= self
.run_0launch(['--dry-run', '--source', 'Source.xml'])
163 self
.assertEquals("", err
)
164 assert 'Compiler.xml' in out
166 def testRanges(self
):
167 out
, err
= self
.run_0launch(['--get-selections', '--before=1', '--not-before=0.2', 'Foo.xml'])
168 assert 'tests/rpm' in out
, out
169 self
.assertEquals("", err
)
171 def testLogging(self
):
172 log
= logging
.getLogger()
173 log
.addFilter(silenceLogger
)
175 out
, err
= self
.run_0launch(['-v', '--list', 'UNKNOWN'])
176 self
.assertEquals(logging
.INFO
, log
.level
)
178 out
, err
= self
.run_0launch(['-vv', '--version'])
179 self
.assertEquals(logging
.DEBUG
, log
.level
)
181 log
.removeFilter(silenceLogger
)
182 log
.setLevel(logging
.WARN
)
185 out
, err
= self
.run_0launch(['--help'])
186 self
.assertEquals("", err
)
187 assert 'options:' in out
.lower()
189 out
, err
= self
.run_0launch([])
190 self
.assertEquals("", err
)
191 assert 'options:' in out
.lower()
197 cli
.main(['--list', 'UNKNOWN'])
201 if __name__
== '__main__':