~ubuntu-branches/ubuntu/karmic/zeroinstall-injector/karmic

« back to all changes in this revision

Viewing changes to tests/testreader.py

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Leonard
  • Date: 2007-01-23 21:50:46 UTC
  • Revision ID: james.westby@ubuntu.com-20070123215046-3ya2x81i99m5ya8r
Tags: upstream-0.25
ImportĀ upstreamĀ versionĀ 0.25

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python2.3
 
2
import sys, tempfile, os, shutil, logging
 
3
from StringIO import StringIO
 
4
import unittest
 
5
from logging import getLogger, DEBUG, INFO
 
6
 
 
7
sys.path.insert(0, '..')
 
8
 
 
9
from zeroinstall import NeedDownload
 
10
from zeroinstall.injector import model, basedir, autopolicy, gpg, iface_cache, namespaces, reader
 
11
import data
 
12
reload(basedir)
 
13
 
 
14
foo_iface_uri = 'http://foo'
 
15
bar_iface_uri = 'http://localhost/bar'
 
16
 
 
17
logger = logging.getLogger()
 
18
 
 
19
config_home = tempfile.mktemp()
 
20
cache_home = tempfile.mktemp()
 
21
os.environ['XDG_CONFIG_HOME'] = config_home
 
22
os.environ['XDG_CACHE_HOME'] = cache_home
 
23
os.environ['XDG_CACHE_DIRS'] = ''
 
24
 
 
25
assert not os.path.exists(config_home)
 
26
 
 
27
class TestReader(unittest.TestCase):
 
28
        def setUp(self):
 
29
                assert not os.path.exists(config_home)
 
30
                os.mkdir(config_home, 0700)
 
31
                os.mkdir(cache_home, 0700)
 
32
                if os.environ.has_key('DISPLAY'):
 
33
                        del os.environ['DISPLAY']
 
34
                self.gnupg_home = tempfile.mktemp()
 
35
                os.environ['GNUPGHOME'] = self.gnupg_home
 
36
                os.mkdir(self.gnupg_home, 0700)
 
37
                stream = tempfile.TemporaryFile()
 
38
                stream.write(data.thomas_key)
 
39
                stream.seek(0)
 
40
                gpg.import_key(stream)
 
41
                iface_cache.iface_cache._interfaces = {}
 
42
        
 
43
        def tearDown(self):
 
44
                shutil.rmtree(config_home)
 
45
                shutil.rmtree(cache_home)
 
46
                shutil.rmtree(self.gnupg_home)
 
47
        
 
48
        def write_with_version(self, version):
 
49
                tmp = tempfile.NamedTemporaryFile(prefix = 'test-')
 
50
                tmp.write(
 
51
"""<?xml version="1.0" ?>
 
52
<interface last-modified="1110752708"
 
53
 uri="%s" %s
 
54
 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
 
55
  <name>Foo</name>
 
56
  <summary>Foo</summary>
 
57
  <description>Foo</description>
 
58
</interface>""" % (foo_iface_uri, version))
 
59
                tmp.flush()
 
60
                return tmp
 
61
        
 
62
        def testNoVersion(self):
 
63
                tmp = self.write_with_version('')
 
64
                reader.check_readable(foo_iface_uri, tmp.name)
 
65
        
 
66
        def testNewEnough(self):
 
67
                tmp = self.write_with_version('min-injector-version="0.19"')
 
68
                reader.check_readable(foo_iface_uri, tmp.name)
 
69
        
 
70
        def testTooOld(self):
 
71
                tmp = self.write_with_version('min-injector-version="1000"')
 
72
                try:
 
73
                        reader.check_readable(foo_iface_uri, tmp.name)
 
74
                except reader.InvalidInterface, ex:
 
75
                        assert "1000" in str(ex)
 
76
        
 
77
        def testRequiresVersion(self):
 
78
                tmp = tempfile.NamedTemporaryFile(prefix = 'test-')
 
79
                tmp.write(
 
80
"""<?xml version="1.0" ?>
 
81
<interface last-modified="1110752708"
 
82
 uri="%s"
 
83
 xmlns="http://zero-install.sourceforge.net/2004/injector/interface"
 
84
 xmlns:my='http://my/namespace'>
 
85
  <name>Foo</name>
 
86
  <summary>Foo</summary>
 
87
  <description>Foo</description>
 
88
  <group>
 
89
   <requires interface='%s' my:foo='test'>
 
90
     <version not-before='2.3.4' before='3.4.5'/>
 
91
   </requires>
 
92
   <requires interface='%s2'/>
 
93
   <implementation id='sha1=123' version='1'/>
 
94
  </group>
 
95
</interface>""" % (foo_iface_uri, bar_iface_uri, bar_iface_uri))
 
96
                tmp.flush()
 
97
                iface = model.Interface(foo_iface_uri)
 
98
                reader.update(iface, tmp.name)
 
99
                impl = iface.implementations['sha1=123']
 
100
                assert len(impl.dependencies) == 2
 
101
                dep = impl.dependencies[bar_iface_uri]
 
102
                assert len(dep.restrictions) == 1
 
103
                res = dep.restrictions[0]
 
104
                assert res.not_before == [[2, 3, 4], 0]
 
105
                assert res.before == [[3, 4, 5], 0]
 
106
                dep2 = impl.dependencies[bar_iface_uri + '2']
 
107
                assert len(dep2.restrictions) == 0
 
108
                str(dep)
 
109
                str(dep2)
 
110
 
 
111
                assert dep.metadata.get('http://my/namespace foo') == 'test'
 
112
                assert dep.metadata.get('http://my/namespace food', None) == None
 
113
        
 
114
        def testVersions(self):
 
115
                tmp = tempfile.NamedTemporaryFile(prefix = 'test-')
 
116
                tmp.write(
 
117
"""<?xml version="1.0" ?>
 
118
<interface
 
119
 uri="%s"
 
120
 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
 
121
  <name>Foo</name>
 
122
  <summary>Foo</summary>
 
123
  <description>Foo</description>
 
124
  <implementation id='sha1=123' version='1.0-rc3' version-modifier='-pre'/>
 
125
</interface>""" % foo_iface_uri)
 
126
                tmp.flush()
 
127
                iface = model.Interface(foo_iface_uri)
 
128
                reader.update(iface, tmp.name)
 
129
                impl = iface.implementations['sha1=123']
 
130
                assert impl.version == [[1, 0], -1, [3], -2]
 
131
        
 
132
        def testAbsMain(self):
 
133
                tmp = tempfile.NamedTemporaryFile(prefix = 'test-')
 
134
                tmp.write(
 
135
"""<?xml version="1.0" ?>
 
136
<interface last-modified="1110752708"
 
137
 uri="%s"
 
138
 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
 
139
  <name>Foo</name>
 
140
  <summary>Foo</summary>
 
141
  <description>Foo</description>
 
142
  <group main='/bin/sh'>
 
143
   <implementation id='sha1=123' version='1'/>
 
144
  </group>
 
145
</interface>""" % foo_iface_uri)
 
146
                tmp.flush()
 
147
                iface = model.Interface(foo_iface_uri)
 
148
                try:
 
149
                        reader.update(iface, tmp.name)
 
150
                        assert False
 
151
                except reader.InvalidInterface, ex:
 
152
                        assert 'main' in str(ex)
 
153
        
 
154
        def testAttrs(self):
 
155
                tmp = tempfile.NamedTemporaryFile(prefix = 'test-')
 
156
                tmp.write(
 
157
"""<?xml version="1.0" ?>
 
158
<interface last-modified="1110752708"
 
159
 uri="%s"
 
160
 xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
 
161
  <name>Foo</name>
 
162
  <summary>Foo</summary>
 
163
  <description>Foo</description>
 
164
  <group main='bin/sh' foo='foovalue' xmlns:bobpre='http://bob' bobpre:bob='bobvalue'>
 
165
   <implementation id='sha1=123' version='1' bobpre:bob='newbobvalue'/>
 
166
   <implementation id='sha1=124' version='2' main='next'/>
 
167
  </group>
 
168
</interface>""" % foo_iface_uri)
 
169
                tmp.flush()
 
170
                iface = model.Interface(foo_iface_uri)
 
171
                reader.update(iface, tmp.name)
 
172
 
 
173
                assert len(iface.implementations) == 2
 
174
 
 
175
                assert iface.get_impl('sha1=123').metadata['foo'] == 'foovalue'
 
176
                assert iface.get_impl('sha1=123').metadata['main'] == 'bin/sh'
 
177
                assert iface.get_impl('sha1=123').metadata['http://bob bob'] == 'newbobvalue'
 
178
 
 
179
                assert iface.get_impl('sha1=124').metadata['http://bob bob'] == 'bobvalue'
 
180
                assert iface.get_impl('sha1=124').metadata['main'] == 'next'
 
181
        
 
182
suite = unittest.makeSuite(TestReader)
 
183
if __name__ == '__main__':
 
184
        sys.argv.append('-v')
 
185
        unittest.main()