Fix zero-install.ui duplicate object
[zeroinstall/solver.git] / tests / testlaunch.py
blob483fcec696a09292626bec5f52151efb442db65b
1 #!/usr/bin/env python2.5
2 from basetest import BaseTest
3 import sys, tempfile, os
4 from StringIO import StringIO
5 import unittest
6 import logging
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):
17 return 0
18 silenceLogger = SilenceLogger()
20 class TestLaunch(BaseTest):
21 def run_0launch(self, args):
22 old_stdout = sys.stdout
23 old_stderr = sys.stderr
24 try:
25 sys.stdout = StringIO()
26 sys.stderr = StringIO()
27 ex = None
28 try:
29 cli.main(args)
30 print "Finished"
31 except NameError:
32 raise
33 except SystemExit:
34 pass
35 except TypeError:
36 raise
37 except AttributeError:
38 raise
39 except AssertionError:
40 raise
41 except Exception, ex:
42 pass
43 out = sys.stdout.getvalue()
44 err = sys.stderr.getvalue()
45 if ex is not None:
46 err += str(ex.__class__)
47 finally:
48 sys.stdout = old_stdout
49 sys.stderr = old_stderr
50 return (out, err)
52 def testHelp(self):
53 out, err = self.run_0launch([])
54 assert out.lower().startswith("usage:")
55 assert not err
57 def testList(self):
58 out, err = self.run_0launch(['--list'])
59 assert not err
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'])
68 assert not err
69 self.assertEquals("file://foo\nFinished\n", out)
71 out, err = self.run_0launch(['--list', 'foo'])
72 assert not err
73 self.assertEquals("file://foo\nFinished\n", out)
75 out, err = self.run_0launch(['--list', 'bar'])
76 assert not err
77 self.assertEquals("Finished\n", out)
79 out, err = self.run_0launch(['--list', 'one', 'two'])
80 assert not err
81 assert out.lower().startswith("usage:")
83 def testVersion(self):
84 out, err = self.run_0launch(['--version'])
85 assert not err
86 assert out.startswith("0launch (zero-install)")
88 def testInvalid(self):
89 a = tempfile.NamedTemporaryFile()
90 out, err = self.run_0launch(['-q', a.name])
91 assert err
93 def testOK(self):
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)
125 policy.save_config()
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)
131 def testHello(self):
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)
163 def testHelp2(self):
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()
172 def testBadFD(self):
173 copy = os.dup(1)
174 try:
175 os.close(1)
176 cli.main(['--list', 'UNKNOWN'])
177 finally:
178 os.dup2(copy, 1)
180 suite = unittest.makeSuite(TestLaunch)
181 if __name__ == '__main__':
182 sys.argv.append('-v')
183 unittest.main()