1 #!/usr/bin/env python2.4
2 from basetest
import BaseTest
3 import sys
, tempfile
, os
, shutil
, imp
4 from StringIO
import StringIO
8 foo_iface_uri
= 'http://foo'
10 sys
.path
.insert(0, '..')
11 from zeroinstall
.injector
import trust
, autopolicy
, namespaces
, model
, iface_cache
, 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 cache_iface(self
, name
, data
):
22 cached_ifaces
= basedir
.save_cache_path('0install.net',
25 f
= file(os
.path
.join(cached_ifaces
, model
.escape(name
)), 'w')
29 def run_0launch(self
, args
):
30 sys
.argv
= ['0launch'] + args
31 old_stdout
= sys
.stdout
32 old_stderr
= sys
.stderr
34 sys
.stdout
= StringIO()
35 sys
.stderr
= StringIO()
38 imp
.load_source('launch', '../0launch')
46 except AttributeError:
48 except AssertionError:
52 out
= sys
.stdout
.getvalue()
53 err
= sys
.stderr
.getvalue()
55 err
+= str(ex
.__class
__)
57 sys
.stdout
= old_stdout
58 sys
.stderr
= old_stderr
62 out
, err
= self
.run_0launch([])
63 assert out
.lower().startswith("usage:")
67 out
, err
= self
.run_0launch(['--list'])
69 self
.assertEquals("Finished\n", out
)
70 cached_ifaces
= os
.path
.join(self
.cache_home
,
71 '0install.net', 'interfaces')
73 os
.makedirs(cached_ifaces
)
74 file(os
.path
.join(cached_ifaces
, 'file%3a%2f%2ffoo'), 'w').close()
76 out
, err
= self
.run_0launch(['--list'])
78 self
.assertEquals("file://foo\nFinished\n", out
)
80 out
, err
= self
.run_0launch(['--list', 'foo'])
82 self
.assertEquals("file://foo\nFinished\n", out
)
84 out
, err
= self
.run_0launch(['--list', 'bar'])
86 self
.assertEquals("Finished\n", out
)
88 out
, err
= self
.run_0launch(['--list', 'one', 'two'])
90 assert out
.lower().startswith("usage:")
92 def testVersion(self
):
93 out
, err
= self
.run_0launch(['--version'])
95 assert out
.startswith("0launch (zero-install)")
97 def testInvalid(self
):
98 a
= tempfile
.NamedTemporaryFile()
99 out
, err
= self
.run_0launch(['-q', a
.name
])
103 out
, err
= self
.run_0launch(['--dry-run', 'http://foo/d'])
104 self
.assertEquals("Would download 'http://foo/d'\nFinished\n", out
)
105 self
.assertEquals("", err
)
107 def testOffline(self
):
108 out
, err
= self
.run_0launch(['--offline', 'http://foo/d'])
109 self
.assertEquals("Can't find all required implementations:\n- <Interface http://foo/d> -> None\n", err
)
110 self
.assertEquals("", out
)
112 def testDisplay(self
):
113 os
.environ
['DISPLAY'] = ':foo'
114 out
, err
= self
.run_0launch(['--dry-run', 'http://foo/d'])
115 # Uses local copy of GUI
116 assert out
.startswith("Would execute: ")
117 assert 'basetest.py' in out
118 self
.assertEquals("", err
)
120 del os
.environ
['DISPLAY']
121 out
, err
= self
.run_0launch(['--gui', '--dry-run'])
122 self
.assertEquals("", err
)
123 self
.assertEquals("Finished\n", out
)
125 def testRefreshDisplay(self
):
126 os
.environ
['DISPLAY'] = ':foo'
127 out
, err
= self
.run_0launch(['--dry-run', '--refresh', 'http://foo/d'])
128 assert out
.startswith("Would execute: ")
129 assert 'basetest.py' in out
130 self
.assertEquals("", err
)
132 def testNeedDownload(self
):
133 policy
= autopolicy
.AutoPolicy(foo_iface_uri
)
135 os
.environ
['DISPLAY'] = ':foo'
136 out
, err
= self
.run_0launch(['--download-only', '--dry-run', 'Foo.xml'])
137 self
.assertEquals("", err
)
138 self
.assertEquals("Finished\n", out
)
141 out
, err
= self
.run_0launch(['--dry-run', 'Foo.xml'])
142 self
.assertEquals("", err
)
143 assert out
.startswith("Would execute: ")
145 out
, err
= self
.run_0launch(['Foo.xml'])
146 # (Foo.xml tries to run a directory; plash gives a different error)
147 assert "Permission denied" in err
or "Is a directory" in err
149 def testSource(self
):
150 out
, err
= self
.run_0launch(['--dry-run', '--source', 'Source.xml'])
151 self
.assertEquals("", err
)
152 assert 'Compiler.xml' in out
154 def testRanges(self
):
155 out
, err
= self
.run_0launch(['--dry-run', '--before=1', '--not-before=0.2', 'Foo.xml'])
156 assert 'tests/two' in err
, err
157 self
.assertEquals("", out
)
159 def testLogging(self
):
160 log
= logging
.getLogger()
161 log
.addFilter(silenceLogger
)
163 out
, err
= self
.run_0launch(['-v', '--list', 'UNKNOWN'])
164 self
.assertEquals(logging
.INFO
, log
.level
)
166 out
, err
= self
.run_0launch(['-vv', '--version'])
167 self
.assertEquals(logging
.DEBUG
, log
.level
)
169 log
.removeFilter(silenceLogger
)
170 log
.setLevel(logging
.WARN
)
173 out
, err
= self
.run_0launch(['--help'])
174 self
.assertEquals("", err
)
175 assert 'options:' in out
.lower()
177 out
, err
= self
.run_0launch([])
178 self
.assertEquals("", err
)
179 assert 'options:' in out
.lower()
185 cli
.main(['--list', 'UNKNOWN'])
189 suite
= unittest
.makeSuite(TestLaunch
)
190 if __name__
== '__main__':
191 sys
.argv
.append('-v')