Import _ explicitly in the GUI
[zeroinstall/solver.git] / tests / testlaunch.py
blob21d0fbbc6db998a9c839ccc2e8bb0460fe5a62bb
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 import SafeException
12 from zeroinstall.injector.policy import Policy
13 from zeroinstall.injector import run, cli, namespaces, qdom, selections
14 from zeroinstall.zerostore import Store; Store._add_with_helper = lambda *unused: False
16 mydir = os.path.abspath(os.path.dirname(__file__))
18 class SilenceLogger(logging.Filter):
19 def filter(self, record):
20 return 0
21 silenceLogger = SilenceLogger()
23 class TestLaunch(BaseTest):
24 def run_0launch(self, args):
25 old_stdout = sys.stdout
26 old_stderr = sys.stderr
27 try:
28 sys.stdout = StringIO()
29 sys.stderr = StringIO()
30 ex = None
31 try:
32 cli.main(args)
33 print "Finished"
34 except NameError:
35 raise
36 except SystemExit:
37 pass
38 except TypeError:
39 raise
40 except AttributeError:
41 raise
42 except AssertionError:
43 raise
44 except Exception, ex:
45 pass
46 out = sys.stdout.getvalue()
47 err = sys.stderr.getvalue()
48 if ex is not None:
49 err += str(ex.__class__)
50 finally:
51 sys.stdout = old_stdout
52 sys.stderr = old_stderr
53 return (out, err)
55 def testHelp(self):
56 out, err = self.run_0launch([])
57 assert out.lower().startswith("usage:")
58 assert not err
60 def testList(self):
61 out, err = self.run_0launch(['--list'])
62 assert not err
63 self.assertEquals("Finished\n", out)
64 cached_ifaces = os.path.join(self.cache_home,
65 '0install.net', 'interfaces')
67 os.makedirs(cached_ifaces)
68 file(os.path.join(cached_ifaces, 'file%3a%2f%2ffoo'), 'w').close()
70 out, err = self.run_0launch(['--list'])
71 assert not err
72 self.assertEquals("file://foo\nFinished\n", out)
74 out, err = self.run_0launch(['--list', 'foo'])
75 assert not err
76 self.assertEquals("file://foo\nFinished\n", out)
78 out, err = self.run_0launch(['--list', 'bar'])
79 assert not err
80 self.assertEquals("Finished\n", out)
82 out, err = self.run_0launch(['--list', 'one', 'two'])
83 assert not err
84 assert out.lower().startswith("usage:")
86 def testVersion(self):
87 out, err = self.run_0launch(['--version'])
88 assert not err
89 assert out.startswith("0launch (zero-install)")
91 def testInvalid(self):
92 a = tempfile.NamedTemporaryFile()
93 out, err = self.run_0launch(['-q', a.name])
94 assert err
96 def testOK(self):
97 out, err = self.run_0launch(['--dry-run', 'http://foo/d'])
98 self.assertEquals("Would download 'http://foo/d'\nFinished\n", out)
99 self.assertEquals("", err)
101 def testRun(self):
102 out, err = self.run_0launch(['Local.xml'])
103 self.assertEquals("", out)
104 assert "test-echo' does not exist" in err, err
106 def testAbsMain(self):
107 tmp = tempfile.NamedTemporaryFile(prefix = 'test-')
108 tmp.write(
109 """<?xml version="1.0" ?>
110 <interface last-modified="1110752708"
111 uri="%s"
112 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
113 <name>Foo</name>
114 <summary>Foo</summary>
115 <description>Foo</description>
116 <group main='/bin/sh'>
117 <implementation id='.' version='1'/>
118 </group>
119 </interface>""" % foo_iface_uri)
120 tmp.flush()
121 policy = Policy(tmp.name, config = self.config)
122 try:
123 downloaded = policy.solve_and_download_impls()
124 if downloaded:
125 policy.handler.wait_for_blocker(downloaded)
126 run.execute_selections(policy.solver.selections, [], stores = policy.config.stores)
127 assert False
128 except SafeException, ex:
129 assert 'Command path must be relative' in str(ex), ex
131 def testOffline(self):
132 out, err = self.run_0launch(['--offline', 'http://foo/d'])
133 self.assertEquals("Interface 'http://foo/d' has no usable implementations\n", err)
134 self.assertEquals("", out)
136 def testDisplay(self):
137 os.environ['DISPLAY'] = ':foo'
138 out, err = self.run_0launch(['--dry-run', 'http://foo/d'])
139 # Uses local copy of GUI
140 assert out.startswith("Would execute: ")
141 assert 'basetest.py' in out
142 self.assertEquals("", err)
144 del os.environ['DISPLAY']
145 out, err = self.run_0launch(['--gui', '--dry-run'])
146 self.assertEquals("", err)
147 self.assertEquals("Finished\n", out)
149 def testRefreshDisplay(self):
150 os.environ['DISPLAY'] = ':foo'
151 out, err = self.run_0launch(['--dry-run', '--refresh', 'http://foo/d'])
152 assert out.startswith("Would execute: ")
153 assert 'basetest.py' in out
154 self.assertEquals("", err)
156 def testNeedDownload(self):
157 os.environ['DISPLAY'] = ':foo'
158 out, err = self.run_0launch(['--download-only', '--dry-run', 'Foo.xml'])
159 self.assertEquals("", err)
160 self.assertEquals("Finished\n", out)
162 def testSelectOnly(self):
163 os.environ['DISPLAY'] = ':foo'
164 out, err = self.run_0launch(['--get-selections', '--select-only', 'Hello.xml'])
165 self.assertEquals("", err)
167 assert out.endswith("Finished\n")
168 out = out[:-len("Finished\n")]
170 root = qdom.parse(StringIO(str(out)))
171 self.assertEquals(namespaces.XMLNS_IFACE, root.uri)
172 sels = selections.Selections(root)
173 sel,= sels.selections.values()
174 self.assertEquals("sha1=3ce644dc725f1d21cfcf02562c76f375944b266a", sel.id)
176 def testHello(self):
177 out, err = self.run_0launch(['--dry-run', 'Foo.xml'])
178 self.assertEquals("", err)
179 assert out.startswith("Would execute: ")
181 out, err = self.run_0launch(['Foo.xml'])
182 # (Foo.xml tries to run a directory; plash gives a different error)
183 assert "Permission denied" in err or "Is a directory" in err
185 def testSource(self):
186 out, err = self.run_0launch(['--dry-run', '--source', 'Source.xml'])
187 self.assertEquals("", err)
188 assert 'Compiler.xml' in out
190 def testRanges(self):
191 out, err = self.run_0launch(['--get-selections', '--before=1', '--not-before=0.2', 'Foo.xml'])
192 assert 'tests/rpm' in out, out
193 self.assertEquals("", err)
195 def testLogging(self):
196 log = logging.getLogger()
197 log.addFilter(silenceLogger)
199 out, err = self.run_0launch(['-v', '--list', 'UNKNOWN'])
200 self.assertEquals(logging.INFO, log.level)
202 out, err = self.run_0launch(['-vv', '--version'])
203 self.assertEquals(logging.DEBUG, log.level)
205 log.removeFilter(silenceLogger)
206 log.setLevel(logging.WARN)
208 def testHelp2(self):
209 out, err = self.run_0launch(['--help'])
210 self.assertEquals("", err)
211 assert 'options:' in out.lower()
213 out, err = self.run_0launch([])
214 self.assertEquals("", err)
215 assert 'options:' in out.lower()
217 def testBadFD(self):
218 copy = os.dup(1)
219 try:
220 os.close(1)
221 cli.main(['--list', 'UNKNOWN'])
222 finally:
223 os.dup2(copy, 1)
225 def testShow(self):
226 command_feed = os.path.join(mydir, 'Command.xml')
227 out, err = self.run_0launch(['--show', command_feed])
228 self.assertEquals("", err)
229 assert 'Local.xml' in out, out
231 if __name__ == '__main__':
232 unittest.main()