1 #!/usr/bin/env python2.3
2 import sys
, tempfile
, os
, shutil
, imp
3 from StringIO
import StringIO
6 foo_iface_uri
= 'http://foo'
8 sys
.path
.insert(0, '..')
9 from zeroinstall
.injector
import trust
, basedir
, autopolicy
, namespaces
, model
, iface_cache
, cli
11 class TestLaunch(unittest
.TestCase
):
13 self
.config_home
= tempfile
.mktemp()
14 self
.cache_home
= tempfile
.mktemp()
15 os
.environ
['XDG_CONFIG_HOME'] = self
.config_home
16 os
.environ
['XDG_CACHE_HOME'] = self
.cache_home
17 os
.environ
['XDG_CACHE_DIRS'] = self
.cache_home
19 iface_cache
.iface_cache
.__init
__()
21 os
.mkdir(self
.config_home
, 0700)
22 os
.mkdir(self
.cache_home
, 0700)
23 if os
.environ
.has_key('DISPLAY'):
24 del os
.environ
['DISPLAY']
27 shutil
.rmtree(self
.config_home
)
28 shutil
.rmtree(self
.cache_home
)
30 def cache_iface(self
, name
, data
):
31 cached_ifaces
= basedir
.save_cache_path('0install.net',
34 f
= file(os
.path
.join(cached_ifaces
, model
.escape(name
)), 'w')
38 def run_0launch(self
, args
):
39 sys
.argv
= ['0launch'] + args
40 old_stdout
= sys
.stdout
41 old_stderr
= sys
.stderr
43 sys
.stdout
= StringIO()
44 sys
.stderr
= StringIO()
47 imp
.load_source('launch', '../0launch')
51 except AssertionError:
55 out
= sys
.stdout
.getvalue()
56 err
= sys
.stderr
.getvalue()
58 err
+= str(ex
.__class
__)
60 sys
.stdout
= old_stdout
61 sys
.stderr
= old_stderr
65 out
, err
= self
.run_0launch([])
66 assert out
.lower().startswith("usage:")
70 out
, err
= self
.run_0launch(['--list'])
72 self
.assertEquals("", out
)
73 cached_ifaces
= os
.path
.join(self
.cache_home
,
74 '0install.net', 'interfaces')
76 os
.makedirs(cached_ifaces
)
77 file(os
.path
.join(cached_ifaces
, 'file%3a%2f%2ffoo'), 'w').close()
79 out
, err
= self
.run_0launch(['--list'])
81 self
.assertEquals("file://foo\n", out
)
83 out
, err
= self
.run_0launch(['--list', 'foo'])
85 self
.assertEquals("file://foo\n", out
)
87 out
, err
= self
.run_0launch(['--list', 'bar'])
89 self
.assertEquals("", out
)
91 out
, err
= self
.run_0launch(['--list', 'one', 'two'])
93 assert out
.lower().startswith("usage:")
95 def testVersion(self
):
96 out
, err
= self
.run_0launch(['--version'])
98 assert out
.startswith("0launch (zero-install)")
100 def testInvalid(self
):
101 a
= tempfile
.NamedTemporaryFile()
102 out
, err
= self
.run_0launch(['-q', a
.name
])
106 out
, err
= self
.run_0launch(['--dry-run', 'http://foo'])
107 self
.assertEquals("Would download 'http://foo'\n", out
)
108 self
.assertEquals("", err
)
110 def testDisplay(self
):
111 os
.environ
['DISPLAY'] = ':foo'
112 out
, err
= self
.run_0launch(['--dry-run', 'http://foo'])
113 # Uses local copy of GUI
114 assert out
.startswith("Would execute: ")
115 assert '0launch-gui' in out
116 self
.assertEquals("", err
)
118 def testRefreshDisplay(self
):
119 os
.environ
['DISPLAY'] = ':foo'
120 out
, err
= self
.run_0launch(['--dry-run', '--download-only',
121 '--refresh', 'http://foo'])
122 assert out
.startswith("Would execute: ")
123 assert '0launch-gui' in out
124 self
.assertEquals("", err
)
126 def testNeedDownload(self
):
127 policy
= autopolicy
.AutoPolicy(foo_iface_uri
)
129 os
.environ
['DISPLAY'] = ':foo'
130 out
, err
= self
.run_0launch(['--download-only', '--dry-run', 'Foo.xml'])
131 self
.assertEquals("", err
)
132 self
.assertEquals("Finished\n", out
)
135 out
, err
= self
.run_0launch(['--dry-run', 'Foo.xml'])
136 self
.assertEquals("", err
)
137 assert out
.startswith("Would execute: ")
139 out
, err
= self
.run_0launch(['Foo.xml'])
140 # (Foo.xml tries to run a directory; plash gives a different error)
141 assert "Permission denied" in err
or "Is a directory" in err
143 def testSource(self
):
144 out
, err
= self
.run_0launch(['--dry-run', '--source', 'Source.xml'])
145 self
.assertEquals("", err
)
146 assert 'Compiler.xml' in out
153 cli
.main(['--list', 'UNKNOWN'])
160 suite
= unittest
.makeSuite(TestLaunch
)
161 if __name__
== '__main__':
162 sys
.argv
.append('-v')