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
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
18 iface_cache
.iface_cache
.__init
__()
20 os
.mkdir(self
.config_home
, 0700)
21 os
.mkdir(self
.cache_home
, 0700)
22 if os
.environ
.has_key('DISPLAY'):
23 del os
.environ
['DISPLAY']
26 shutil
.rmtree(self
.config_home
)
27 shutil
.rmtree(self
.cache_home
)
29 def cache_iface(self
, name
, data
):
30 cached_ifaces
= basedir
.save_cache_path('0install.net',
33 f
= file(os
.path
.join(cached_ifaces
, model
.escape(name
)), 'w')
37 def run_0launch(self
, args
):
38 sys
.argv
= ['0launch'] + args
39 old_stdout
= sys
.stdout
40 old_stderr
= sys
.stderr
42 sys
.stdout
= StringIO()
43 sys
.stderr
= StringIO()
46 imp
.load_source('launch', '../0launch')
52 out
= sys
.stdout
.getvalue()
53 err
= sys
.stderr
.getvalue()
55 err
+= str(ex
.__class
__)
57 sys
.stdout
= old_stdout
58 sys
.stderr
= old_stderr
62 out
, err
= self
.run_0launch([])
63 assert out
.startswith("usage:")
67 out
, err
= self
.run_0launch(['--list'])
69 self
.assertEquals("", out
)
70 cached_ifaces
= os
.path
.join(self
.cache_home
,
71 '0install.net', 'interfaces')
73 os
.makedirs(cached_ifaces
)
74 file(os
.path
.join(cached_ifaces
, 'file%3a%2f%2ffoo'), 'w').close()
76 out
, err
= self
.run_0launch(['--list'])
78 self
.assertEquals("file://foo\n", out
)
80 out
, err
= self
.run_0launch(['--list', 'foo'])
82 self
.assertEquals("file://foo\n", out
)
84 out
, err
= self
.run_0launch(['--list', 'bar'])
86 self
.assertEquals("", out
)
88 out
, err
= self
.run_0launch(['--list', 'one', 'two'])
90 assert out
.startswith("usage:")
92 def testVersion(self
):
93 out
, err
= self
.run_0launch(['--version'])
95 assert out
.startswith("0launch (zero-install)")
97 def testInvalid(self
):
98 a
= tempfile
.NamedTemporaryFile()
99 out
, err
= self
.run_0launch(['-q', a
.name
])
103 out
, err
= self
.run_0launch(['--dry-run', 'http://foo'])
104 self
.assertEquals("Would download 'http://foo'\n", out
)
105 self
.assertEquals("", err
)
107 def testDisplay(self
):
108 os
.environ
['DISPLAY'] = ':foo'
109 out
, err
= self
.run_0launch(['--dry-run', 'http://foo'])
110 # Uses local copy of GUI
111 assert out
.startswith("Would execute: ")
112 assert '0launch-gui' in out
113 self
.assertEquals("", err
)
115 def testRefreshDisplay(self
):
116 os
.environ
['DISPLAY'] = ':foo'
117 out
, err
= self
.run_0launch(['--dry-run', '--download-only',
118 '--refresh', 'http://foo'])
119 assert out
.startswith("Would execute: ")
120 assert '0launch-gui' in out
121 self
.assertEquals("", err
)
123 def testNeedDownload(self
):
124 policy
= autopolicy
.AutoPolicy(foo_iface_uri
)
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
)
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)
138 assert "Permission denied" in err
140 suite
= unittest
.makeSuite(TestLaunch
)
141 if __name__
== '__main__':
142 sys
.argv
.append('-v')