Run unit-tests in C locale
[zeroinstall/solver.git] / tests / testlaunch.py
blob03da8a75aacaa36420d6562e61990075c79abfb7
1 #!/usr/bin/env python
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, 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):
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 testRun(self):
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)
130 policy.save_config()
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)
138 policy.save_config()
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)
152 def testHello(self):
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)
184 def testHelp2(self):
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()
193 def testBadFD(self):
194 copy = os.dup(1)
195 try:
196 os.close(1)
197 cli.main(['--list', 'UNKNOWN'])
198 finally:
199 os.dup2(copy, 1)
201 if __name__ == '__main__':
202 unittest.main()