When creating a temporary directory for extracting an archive, always clear
[zeroinstall.git] / tests / testlaunch.py
blobed8c9e7e31c8ae6a608ae76004449e280db0141b
1 #!/usr/bin/env python2.3
2 from basetest import BaseTest
3 import sys, tempfile, os, shutil, imp
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 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):
16 return 0
17 silenceLogger = SilenceLogger()
19 class TestLaunch(BaseTest):
20 def cache_iface(self, name, data):
21 cached_ifaces = basedir.save_cache_path('0install.net',
22 'interfaces')
24 f = file(os.path.join(cached_ifaces, model.escape(name)), 'w')
25 f.write(data)
26 f.close()
28 def run_0launch(self, args):
29 sys.argv = ['0launch'] + args
30 old_stdout = sys.stdout
31 old_stderr = sys.stderr
32 try:
33 sys.stdout = StringIO()
34 sys.stderr = StringIO()
35 ex = None
36 try:
37 imp.load_source('launch', '../0launch')
38 print "Finished"
39 except SystemExit:
40 pass
41 except AssertionError:
42 raise
43 except Exception, ex:
44 pass
45 out = sys.stdout.getvalue()
46 err = sys.stderr.getvalue()
47 if ex is not None:
48 err += str(ex.__class__)
49 finally:
50 sys.stdout = old_stdout
51 sys.stderr = old_stderr
52 return (out, err)
54 def testHelp(self):
55 out, err = self.run_0launch([])
56 assert out.lower().startswith("usage:")
57 assert not err
59 def testList(self):
60 out, err = self.run_0launch(['--list'])
61 assert not err
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'])
70 assert not err
71 self.assertEquals("file://foo\nFinished\n", out)
73 out, err = self.run_0launch(['--list', 'foo'])
74 assert not err
75 self.assertEquals("file://foo\nFinished\n", out)
77 out, err = self.run_0launch(['--list', 'bar'])
78 assert not err
79 self.assertEquals("Finished\n", out)
81 out, err = self.run_0launch(['--list', 'one', 'two'])
82 assert not err
83 assert out.lower().startswith("usage:")
85 def testVersion(self):
86 out, err = self.run_0launch(['--version'])
87 assert not err
88 assert out.startswith("0launch (zero-install)")
90 def testInvalid(self):
91 a = tempfile.NamedTemporaryFile()
92 out, err = self.run_0launch(['-q', a.name])
93 assert err
95 def testOK(self):
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)
122 policy.save_config()
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)
128 def testHello(self):
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)
155 def testHelp(self):
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()
164 def testBadFD(self):
165 copy = os.dup(1)
166 try:
167 os.close(1)
168 cli.main(['--list', 'UNKNOWN'])
169 finally:
170 os.dup2(copy, 1)
172 suite = unittest.makeSuite(TestLaunch)
173 if __name__ == '__main__':
174 sys.argv.append('-v')
175 unittest.main()