1 #!/usr/bin/env python2.4
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
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
)
98 def testOffline(self
):
99 out
, err
= self
.run_0launch(['--offline', 'http://foo/d'])
100 self
.assertEquals("Can't find all required implementations:\n- <Interface http://foo/d> -> None\n", err
)
101 self
.assertEquals("", out
)
103 def testDisplay(self
):
104 os
.environ
['DISPLAY'] = ':foo'
105 out
, err
= self
.run_0launch(['--dry-run', 'http://foo/d'])
106 # Uses local copy of GUI
107 assert out
.startswith("Would execute: ")
108 assert 'basetest.py' in out
109 self
.assertEquals("", err
)
111 del os
.environ
['DISPLAY']
112 out
, err
= self
.run_0launch(['--gui', '--dry-run'])
113 self
.assertEquals("", err
)
114 self
.assertEquals("Finished\n", out
)
116 def testRefreshDisplay(self
):
117 os
.environ
['DISPLAY'] = ':foo'
118 out
, err
= self
.run_0launch(['--dry-run', '--refresh', 'http://foo/d'])
119 assert out
.startswith("Would execute: ")
120 assert 'basetest.py' in out
121 self
.assertEquals("", err
)
123 def testNeedDownload(self
):
124 policy
= autopolicy
.AutoPolicy(foo_iface_uri
)
126 os
.environ
['DISPLAY'] = ':foo'
127 out
, err
= self
.run_0launch(['--download-only', '--dry-run', 'Foo.xml'])
128 self
.assertEquals("", err
)
129 self
.assertEquals("Finished\n", out
)
132 out
, err
= self
.run_0launch(['--dry-run', 'Foo.xml'])
133 self
.assertEquals("", err
)
134 assert out
.startswith("Would execute: ")
136 out
, err
= self
.run_0launch(['Foo.xml'])
137 # (Foo.xml tries to run a directory; plash gives a different error)
138 assert "Permission denied" in err
or "Is a directory" in err
140 def testSource(self
):
141 out
, err
= self
.run_0launch(['--dry-run', '--source', 'Source.xml'])
142 self
.assertEquals("", err
)
143 assert 'Compiler.xml' in out
145 def testRanges(self
):
146 out
, err
= self
.run_0launch(['--dry-run', '--before=1', '--not-before=0.2', 'Foo.xml'])
147 assert 'tests/two' in err
, err
148 self
.assertEquals("", out
)
150 def testLogging(self
):
151 log
= logging
.getLogger()
152 log
.addFilter(silenceLogger
)
154 out
, err
= self
.run_0launch(['-v', '--list', 'UNKNOWN'])
155 self
.assertEquals(logging
.INFO
, log
.level
)
157 out
, err
= self
.run_0launch(['-vv', '--version'])
158 self
.assertEquals(logging
.DEBUG
, log
.level
)
160 log
.removeFilter(silenceLogger
)
161 log
.setLevel(logging
.WARN
)
164 out
, err
= self
.run_0launch(['--help'])
165 self
.assertEquals("", err
)
166 assert 'options:' in out
.lower()
168 out
, err
= self
.run_0launch([])
169 self
.assertEquals("", err
)
170 assert 'options:' in out
.lower()
176 cli
.main(['--list', 'UNKNOWN'])
180 suite
= unittest
.makeSuite(TestLaunch
)
181 if __name__
== '__main__':
182 sys
.argv
.append('-v')