Invalid interfaces can be deleted from the cache viewer.
[zeroinstall.git] / tests / testautopolicy.py
blob420d28f50daf3b999e35a24b9b59c14a5b8e6200
1 #!/usr/bin/env python2.3
2 import sys, tempfile, os, shutil
3 from StringIO import StringIO
4 import unittest
5 from logging import getLogger, DEBUG, INFO
6 #getLogger().setLevel(DEBUG)
8 sys.path.insert(0, '..')
10 config_home = tempfile.mktemp()
11 cache_home = tempfile.mktemp()
12 os.environ['XDG_CONFIG_HOME'] = config_home
13 os.environ['XDG_CACHE_HOME'] = cache_home
15 from zeroinstall.injector import model, basedir, autopolicy, gpg, iface_cache
16 import data
17 reload(basedir)
19 foo_iface_uri = 'http://foo'
21 class TestAutoPolicy(unittest.TestCase):
22 def setUp(self):
23 os.mkdir(config_home, 0700)
24 os.mkdir(cache_home, 0700)
25 if os.environ.has_key('DISPLAY'):
26 del os.environ['DISPLAY']
27 self.gnupg_home = tempfile.mktemp()
28 os.environ['GNUPGHOME'] = self.gnupg_home
29 os.mkdir(self.gnupg_home, 0700)
30 stream = tempfile.TemporaryFile()
31 stream.write(data.thomas_key)
32 stream.seek(0)
33 gpg.import_key(stream)
34 iface_cache.iface_cache._interfaces = {}
36 def tearDown(self):
37 shutil.rmtree(config_home)
38 shutil.rmtree(cache_home)
39 shutil.rmtree(self.gnupg_home)
41 def cache_iface(self, name, data):
42 cached_ifaces = basedir.save_cache_path('0install.net',
43 'interfaces')
45 f = file(os.path.join(cached_ifaces, model.escape(name)), 'w')
46 f.write(data)
47 f.close()
49 def testNoNeedDl(self):
50 policy = autopolicy.AutoPolicy(foo_iface_uri,
51 download_only = False)
52 policy.freshness = 0
53 assert policy.need_download()
54 self.cache_iface(foo_iface_uri,
55 """<?xml version="1.0" ?>
56 <interface last-modified="1110752708"
57 uri="%s"
58 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
59 <name>Foo</name>
60 <summary>Foo</summary>
61 <description>Foo</description>
62 </interface>""" % foo_iface_uri)
63 iface_cache.iface_cache._interfaces = {}
64 assert not policy.need_download()
66 def testDownload(self):
67 tmp = tempfile.NamedTemporaryFile()
68 tmp.write(
69 """<?xml version="1.0" ?>
70 <interface
71 main='ThisBetterNotExist'
72 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
73 <name>Foo</name>
74 <summary>Foo</summary>
75 <description>Foo</description>
76 <implementation version='1.0' id='/bin'/>
77 </interface>""")
78 tmp.flush()
79 policy = autopolicy.AutoPolicy(tmp.name, False, False)
80 try:
81 policy.download_and_execute(['Hello'])
82 assert 0
83 except model.SafeException, ex:
84 assert "ThisBetterNotExist" in str(ex)
85 tmp.close()
87 def testNoMain(self):
88 tmp = tempfile.NamedTemporaryFile()
89 tmp.write(
90 """<?xml version="1.0" ?>
91 <interface
92 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
93 <name>Foo</name>
94 <summary>Foo</summary>
95 <description>Foo</description>
96 <implementation version='1.0' id='/bin'/>
97 </interface>""")
98 tmp.flush()
99 policy = autopolicy.AutoPolicy(tmp.name, False, False)
100 try:
101 policy.download_and_execute(['Hello'])
102 assert 0
103 except model.SafeException, ex:
104 assert "library" in str(ex)
105 tmp.close()
107 def testNeedDL(self):
108 policy = autopolicy.AutoPolicy(foo_iface_uri, False, False)
109 policy.freshness = 1
110 policy.network_use = model.network_full
111 self.cache_iface(foo_iface_uri,
112 """<?xml version="1.0" ?>
113 <interface last-modified="0"
114 uri="%s"
115 main='ThisBetterNotExist'
116 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
117 <name>Foo</name>
118 <summary>Foo</summary>
119 <description>Foo</description>
120 <implementation version='1.0' id='sha1=123'/>
121 </interface>""" % foo_iface_uri)
122 assert policy.need_download()
124 def testBinding(self):
125 tmp = tempfile.NamedTemporaryFile()
126 tmp.write(
127 """<?xml version="1.0" ?>
128 <interface
129 main='testautopolicy.py'
130 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
131 <name>Bar</name>
132 <summary>Bar</summary>
133 <description>Bar</description>
134 <group>
135 <requires interface='%s'>
136 <environment name='FOO_PATH' insert='.'/>
137 <environment name='BAR_PATH' insert='.' default='/a:/b'/>
138 <environment name='XDG_DATA_DIRS' insert='.'/>
139 </requires>
140 <implementation version='1.0' id='%s'/>
141 </group>
142 </interface>""" % (foo_iface_uri, os.path.dirname(os.path.abspath(__file__))))
143 tmp.flush()
144 self.cache_iface(foo_iface_uri,
145 """<?xml version="1.0" ?>
146 <interface last-modified="0"
147 uri="%s"
148 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
149 <name>Foo</name>
150 <summary>Foo</summary>
151 <description>Foo</description>
152 <implementation version='1.0' id='sha1=123'/>
153 </interface>""" % foo_iface_uri)
154 cached_impl = basedir.save_cache_path('0install.net',
155 'implementations',
156 'sha1=123')
157 policy = autopolicy.AutoPolicy(tmp.name, False,
158 dry_run = True)
159 policy.network_use = model.network_offline
160 os.environ['FOO_PATH'] = "old"
161 old, sys.stdout = sys.stdout, StringIO()
162 try:
163 policy.download_and_execute(['Hello'])
164 finally:
165 sys.stdout = old
166 self.assertEquals(cached_impl + '/.:old',
167 os.environ['FOO_PATH'])
168 self.assertEquals(cached_impl + '/.:/a:/b',
169 os.environ['BAR_PATH'])
171 del os.environ['FOO_PATH']
172 if 'XDG_DATA_DIRS' in os.environ:
173 del os.environ['XDG_DATA_DIRS']
174 os.environ['BAR_PATH'] = '/old'
175 old, sys.stdout = sys.stdout, StringIO()
176 try:
177 policy.download_and_execute(['Hello'])
178 finally:
179 sys.stdout = old
180 self.assertEquals(cached_impl + '/.',
181 os.environ['FOO_PATH'])
182 self.assertEquals(cached_impl + '/.:/old',
183 os.environ['BAR_PATH'])
184 self.assertEquals(cached_impl + '/.:/usr/local/share:/usr/share',
185 os.environ['XDG_DATA_DIRS'])
187 def testFeeds(self):
188 self.cache_iface(foo_iface_uri,
189 """<?xml version="1.0" ?>
190 <interface last-modified="0"
191 uri="%s"
192 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
193 <name>Foo</name>
194 <summary>Foo</summary>
195 <description>Foo</description>
196 <feed src='http://bar'/>
197 </interface>""" % foo_iface_uri)
198 self.cache_iface('http://bar',
199 """<?xml version="1.0" ?>
200 <interface last-modified="0"
201 uri="http://bar"
202 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
203 <feed-for interface='%s'/>
204 <name>Bar</name>
205 <summary>Bar</summary>
206 <description>Bar</description>
207 <implementation version='1.0' id='sha1=123'/>
208 </interface>""" % foo_iface_uri)
209 policy = autopolicy.AutoPolicy(foo_iface_uri, False,
210 dry_run = True)
211 policy.freshness = 0
212 policy.network_use = model.network_full
213 policy.recalculate()
214 assert policy.ready
215 foo_iface = policy.get_interface(foo_iface_uri)
216 self.assertEquals('sha1=123', policy.implementation[foo_iface].id)
218 suite = unittest.makeSuite(TestAutoPolicy)
219 if __name__ == '__main__':
220 sys.argv.append('-v')
221 unittest.main()