1 #!/usr/bin/env python2.3
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
, basedir
, autopolicy
, namespaces
, model
, iface_cache
, cli
12 from zeroinstall
.zerostore
import Store
; Store
._add
_with
_helper
= lambda *unused
: False
14 class SilenceLogger(logging
.Filter
):
15 def filter(self
, record
):
17 silenceLogger
= SilenceLogger()
19 class TestLaunch(BaseTest
):
20 def cache_iface(self
, name
, data
):
21 cached_ifaces
= basedir
.save_cache_path('0install.net',
24 f
= file(os
.path
.join(cached_ifaces
, model
.escape(name
)), 'w')
28 def run_0launch(self
, args
):
29 sys
.argv
= ['0launch'] + args
30 old_stdout
= sys
.stdout
31 old_stderr
= sys
.stderr
33 sys
.stdout
= StringIO()
34 sys
.stderr
= StringIO()
37 imp
.load_source('launch', '../0launch')
41 except AssertionError:
45 out
= sys
.stdout
.getvalue()
46 err
= sys
.stderr
.getvalue()
48 err
+= str(ex
.__class
__)
50 sys
.stdout
= old_stdout
51 sys
.stderr
= old_stderr
55 out
, err
= self
.run_0launch([])
56 assert out
.lower().startswith("usage:")
60 out
, err
= self
.run_0launch(['--list'])
62 self
.assertEquals("Finished\n", out
)
63 cached_ifaces
= os
.path
.join(self
.cache_home
,
64 '0install.net', 'interfaces')
66 os
.makedirs(cached_ifaces
)
67 file(os
.path
.join(cached_ifaces
, 'file%3a%2f%2ffoo'), 'w').close()
69 out
, err
= self
.run_0launch(['--list'])
71 self
.assertEquals("file://foo\nFinished\n", out
)
73 out
, err
= self
.run_0launch(['--list', 'foo'])
75 self
.assertEquals("file://foo\nFinished\n", out
)
77 out
, err
= self
.run_0launch(['--list', 'bar'])
79 self
.assertEquals("Finished\n", out
)
81 out
, err
= self
.run_0launch(['--list', 'one', 'two'])
83 assert out
.lower().startswith("usage:")
85 def testVersion(self
):
86 out
, err
= self
.run_0launch(['--version'])
88 assert out
.startswith("0launch (zero-install)")
90 def testInvalid(self
):
91 a
= tempfile
.NamedTemporaryFile()
92 out
, err
= self
.run_0launch(['-q', a
.name
])
96 out
, err
= self
.run_0launch(['--dry-run', 'http://foo'])
97 self
.assertEquals("Would download 'http://foo'\nFinished\n", out
)
98 self
.assertEquals("", err
)
100 def testDisplay(self
):
101 os
.environ
['DISPLAY'] = ':foo'
102 out
, err
= self
.run_0launch(['--dry-run', 'http://foo'])
103 # Uses local copy of GUI
104 assert out
.startswith("Would execute: ")
105 assert 'basetest.py' in out
106 self
.assertEquals("", err
)
108 del os
.environ
['DISPLAY']
109 out
, err
= self
.run_0launch(['--gui', '--dry-run'])
110 self
.assertEquals("", err
)
111 self
.assertEquals("Finished\n", out
)
113 def testRefreshDisplay(self
):
114 os
.environ
['DISPLAY'] = ':foo'
115 out
, err
= self
.run_0launch(['--dry-run', '--refresh', 'http://foo'])
116 assert out
.startswith("Would execute: ")
117 assert 'basetest.py' in out
118 self
.assertEquals("", err
)
120 def testNeedDownload(self
):
121 policy
= autopolicy
.AutoPolicy(foo_iface_uri
)
123 os
.environ
['DISPLAY'] = ':foo'
124 out
, err
= self
.run_0launch(['--download-only', '--dry-run', 'Foo.xml'])
125 self
.assertEquals("", err
)
126 self
.assertEquals("Finished\n", out
)
129 out
, err
= self
.run_0launch(['--dry-run', 'Foo.xml'])
130 self
.assertEquals("", err
)
131 assert out
.startswith("Would execute: ")
133 out
, err
= self
.run_0launch(['Foo.xml'])
134 # (Foo.xml tries to run a directory; plash gives a different error)
135 assert "Permission denied" in err
or "Is a directory" in err
137 def testSource(self
):
138 out
, err
= self
.run_0launch(['--dry-run', '--source', 'Source.xml'])
139 self
.assertEquals("", err
)
140 assert 'Compiler.xml' in out
142 def testLogging(self
):
143 log
= logging
.getLogger()
144 log
.addFilter(silenceLogger
)
146 out
, err
= self
.run_0launch(['-v', '--list', 'UNKNOWN'])
147 self
.assertEquals(logging
.INFO
, log
.level
)
149 out
, err
= self
.run_0launch(['-vv', '--version'])
150 self
.assertEquals(logging
.DEBUG
, log
.level
)
152 log
.removeFilter(silenceLogger
)
153 log
.setLevel(logging
.WARN
)
156 out
, err
= self
.run_0launch(['--help'])
157 self
.assertEquals("", err
)
158 assert 'options:' in out
.lower()
160 out
, err
= self
.run_0launch([])
161 self
.assertEquals("", err
)
162 assert 'options:' in out
.lower()
168 cli
.main(['--list', 'UNKNOWN'])
172 suite
= unittest
.makeSuite(TestLaunch
)
173 if __name__
== '__main__':
174 sys
.argv
.append('-v')