1 #!/usr/bin/env python2.3
2 import sys
, tempfile
, os
, shutil
4 from logging
import getLogger
, DEBUG
, INFO
5 #getLogger().setLevel(DEBUG)
7 sys
.path
.insert(0, '..')
9 config_home
= tempfile
.mktemp()
10 cache_home
= tempfile
.mktemp()
11 os
.environ
['XDG_CONFIG_HOME'] = config_home
12 os
.environ
['XDG_CACHE_HOME'] = cache_home
14 from zeroinstall
.injector
import model
, basedir
, autopolicy
, gpg
, iface_cache
18 foo_iface_uri
= 'http://foo'
20 class TestAutoPolicy(unittest
.TestCase
):
22 os
.mkdir(config_home
, 0700)
23 os
.mkdir(cache_home
, 0700)
24 if os
.environ
.has_key('DISPLAY'):
25 del os
.environ
['DISPLAY']
26 self
.gnupg_home
= tempfile
.mktemp()
27 os
.environ
['GNUPGHOME'] = self
.gnupg_home
28 os
.mkdir(self
.gnupg_home
, 0700)
29 stream
= tempfile
.TemporaryFile()
30 stream
.write(data
.thomas_key
)
32 gpg
.import_key(stream
)
33 iface_cache
.iface_cache
._interfaces
= {}
36 shutil
.rmtree(config_home
)
37 shutil
.rmtree(cache_home
)
38 shutil
.rmtree(self
.gnupg_home
)
40 def cache_iface(self
, name
, data
):
41 cached_ifaces
= basedir
.save_cache_path('0install.net',
44 f
= file(os
.path
.join(cached_ifaces
, model
.escape(name
)), 'w')
48 def testNoNeedDl(self
):
49 policy
= autopolicy
.AutoPolicy(foo_iface_uri
,
50 download_only
= False)
52 assert policy
.need_download()
53 self
.cache_iface(foo_iface_uri
,
54 """<?xml version="1.0" ?>
55 <interface last-modified="1110752708"
57 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
59 <summary>Foo</summary>
60 <description>Foo</description>
61 </interface>""" % foo_iface_uri
)
62 assert not policy
.need_download()
64 def testDownload(self
):
65 policy
= autopolicy
.AutoPolicy(foo_iface_uri
, False, False)
67 self
.cache_iface(foo_iface_uri
,
68 """<?xml version="1.0" ?>
69 <interface last-modified="1110752708"
71 main='ThisBetterNotExist'
72 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
74 <summary>Foo</summary>
75 <description>Foo</description>
76 <implementation version='1.0' id='/bin'/>
77 </interface>""" % foo_iface_uri
)
79 policy
.download_and_execute(['Hello'])
81 except model
.SafeException
, ex
:
82 assert "ThisBetterNotExist" in str(ex
)
84 suite
= unittest
.makeSuite(TestAutoPolicy
)
85 if __name__
== '__main__':