2 from basetest
import BaseTest
, TestStores
4 from StringIO
import StringIO
7 sys
.path
.insert(0, '..')
8 from zeroinstall
import cmd
9 from zeroinstall
.injector
import model
, selections
, qdom
, reader
, policy
, handler
, gpg
12 def __init__(self
, reply
):
18 class TestInstall(BaseTest
):
19 def run_0install(self
, args
):
20 old_stdout
= sys
.stdout
21 old_stderr
= sys
.stderr
23 sys
.stdout
= StringIO()
24 sys
.stderr
= StringIO()
27 cmd
.main(args
, config
= self
.config
)
34 except AttributeError:
36 except AssertionError:
40 out
= sys
.stdout
.getvalue()
41 err
= sys
.stderr
.getvalue()
43 err
+= str(ex
.__class
__)
45 sys
.stdout
= old_stdout
46 sys
.stderr
= old_stderr
50 out
, err
= self
.run_0install([])
51 assert out
.lower().startswith("usage:")
52 assert 'add-feed' in out
53 assert '--version' in out
56 out2
, err
= self
.run_0install(['--help'])
60 out
, err
= self
.run_0install(['--version'])
61 assert 'Thomas Leonard' in out
64 out
, err
= self
.run_0install(['foobar'])
65 assert 'Unknown sub-command' in err
, err
68 out
, err
= self
.run_0install(['select'])
69 assert out
.lower().startswith("usage:")
72 out
, err
= self
.run_0install(['select', 'Local.xml'])
74 assert 'Version: 0.1' in out
76 local_uri
= model
.canonical_iface_uri('Local.xml')
77 out
, err
= self
.run_0install(['select', 'Local.xml'])
79 assert 'Version: 0.1' in out
81 out
, err
= self
.run_0install(['select', 'Local.xml', '--xml'])
82 sels
= selections
.Selections(qdom
.parse(StringIO(str(out
))))
83 assert sels
.selections
[local_uri
].version
== '0.1'
85 out
, err
= self
.run_0install(['select', 'selections.xml'])
87 assert 'Version: 1\n' in out
88 assert '(not cached)' in out
90 def testDownload(self
):
91 out
, err
= self
.run_0install(['download'])
92 assert out
.lower().startswith("usage:")
93 assert '--show' in out
95 out
, err
= self
.run_0install(['download', 'Local.xml', '--show'])
97 assert 'Version: 0.1' in out
99 local_uri
= model
.canonical_iface_uri('Local.xml')
100 out
, err
= self
.run_0install(['download', 'Local.xml', '--xml'])
102 sels
= selections
.Selections(qdom
.parse(StringIO(str(out
))))
103 assert sels
.selections
[local_uri
].version
== '0.1'
105 out
, err
= self
.run_0install(['download', 'Local.xml', '--show', '--with-store=/foo'])
107 assert self
.config
.stores
.stores
[-1].dir == '/foo'
109 out
, err
= self
.run_0install(['download', '--offline', 'selections.xml'])
110 assert 'Would download' in err
111 self
.config
.network_use
= model
.network_full
113 self
.config
.stores
= TestStores()
114 digest
= 'sha1=3ce644dc725f1d21cfcf02562c76f375944b266a'
115 self
.config
.fetcher
.allow_download(digest
)
116 out
, err
= self
.run_0install(['download', 'Hello.xml', '--show'])
118 assert self
.config
.stores
.lookup_any([digest
]).startswith('/fake')
119 assert 'Version: 1\n' in out
121 out
, err
= self
.run_0install(['download', '--offline', 'selections.xml', '--show'])
122 assert '/fake_store' in out
123 self
.config
.network_use
= model
.network_full
125 def testDownloadSelections(self
):
126 self
.config
.stores
= TestStores()
127 digest
= 'sha1=3ce644dc725f1d21cfcf02562c76f375944b266a'
128 self
.config
.fetcher
.allow_download(digest
)
129 hello
= reader
.load_feed('Hello.xml')
130 self
.config
.fetcher
.allow_feed_download('http://example.com:8000/Hello.xml', hello
)
131 out
, err
= self
.run_0install(['download', 'selections.xml', '--show'])
133 assert self
.config
.stores
.lookup_any([digest
]).startswith('/fake')
134 assert 'Version: 1\n' in out
136 def testUpdate(self
):
137 out
, err
= self
.run_0install(['update'])
138 assert out
.lower().startswith("usage:")
139 assert '--message' in out
, out
141 # Updating a local feed with no dependencies
142 out
, err
= self
.run_0install(['update', 'Local.xml'])
144 assert 'No updates found' in out
, out
146 # Using a remote feed for the first time
147 self
.config
.stores
= TestStores()
148 binary_feed
= reader
.load_feed('Binary.xml')
149 self
.config
.fetcher
.allow_download('sha1=123')
150 self
.config
.fetcher
.allow_feed_download('http://foo/Binary.xml', binary_feed
)
151 out
, err
= self
.run_0install(['update', 'http://foo/Binary.xml'])
153 assert 'Binary.xml: new -> 1.0' in out
, out
156 self
.config
.fetcher
.allow_feed_download('http://foo/Binary.xml', binary_feed
)
157 out
, err
= self
.run_0install(['update', 'http://foo/Binary.xml'])
159 assert 'No updates found' in out
, out
161 # New binary release available.
162 new_binary_feed
= reader
.load_feed('Binary.xml')
163 new_binary_feed
.implementations
['sha1=123'].version
= model
.parse_version('1.1')
164 self
.config
.fetcher
.allow_feed_download('http://foo/Binary.xml', new_binary_feed
)
165 out
, err
= self
.run_0install(['update', 'http://foo/Binary.xml'])
167 assert 'Binary.xml: 1.0 -> 1.1' in out
, out
169 # Compiling from source for the first time.
170 source_feed
= reader
.load_feed('Source.xml')
171 compiler_feed
= reader
.load_feed('Compiler.xml')
172 self
.config
.fetcher
.allow_download('sha1=234')
173 self
.config
.fetcher
.allow_download('sha1=345')
174 self
.config
.fetcher
.allow_feed_download('http://foo/Compiler.xml', compiler_feed
)
175 self
.config
.fetcher
.allow_feed_download('http://foo/Binary.xml', binary_feed
)
176 self
.config
.fetcher
.allow_feed_download('http://foo/Source.xml', source_feed
)
177 out
, err
= self
.run_0install(['update', 'http://foo/Binary.xml', '--source'])
179 assert 'Binary.xml: new -> 1.0' in out
, out
180 assert 'Compiler.xml: new -> 1.0' in out
, out
182 # New compiler released.
183 new_compiler_feed
= reader
.load_feed('Compiler.xml')
184 new_compiler_feed
.implementations
['sha1=345'].version
= model
.parse_version('1.1')
185 self
.config
.fetcher
.allow_feed_download('http://foo/Compiler.xml', new_compiler_feed
)
186 self
.config
.fetcher
.allow_feed_download('http://foo/Binary.xml', binary_feed
)
187 self
.config
.fetcher
.allow_feed_download('http://foo/Source.xml', source_feed
)
188 out
, err
= self
.run_0install(['update', 'http://foo/Binary.xml', '--source'])
190 assert 'Compiler.xml: 1.0 -> 1.1' in out
, out
192 # A dependency disappears.
193 new_source_feed
= reader
.load_feed('Source.xml')
194 new_source_feed
.implementations
['sha1=234'].requires
= []
195 self
.config
.fetcher
.allow_feed_download('http://foo/Compiler.xml', new_compiler_feed
)
196 self
.config
.fetcher
.allow_feed_download('http://foo/Binary.xml', binary_feed
)
197 self
.config
.fetcher
.allow_feed_download('http://foo/Source.xml', new_source_feed
)
198 out
, err
= self
.run_0install(['update', 'http://foo/Binary.xml', '--source'])
200 assert 'No longer used: http://foo/Compiler.xml' in out
, out
202 def testConfig(self
):
203 out
, err
= self
.run_0install(['config', '--help'])
204 assert out
.lower().startswith("usage:")
205 assert '--console' in out
207 out
, err
= self
.run_0install(['config'])
209 assert 'full' in out
, out
210 assert 'freshness = 0' in out
, out
211 assert 'help_with_testing = False' in out
, out
213 out
, err
= self
.run_0install(['config', 'help_with_testing'])
214 assert out
== 'False\n', out
216 file_config
= policy
.load_config(handler
.Handler())
218 old_stdout
= sys
.stdout
219 sys
.stdout
= StringIO()
221 cmd
.config
.handle(file_config
, None, [name
])
222 cmd_output
= sys
.stdout
.getvalue()
224 sys
.stdout
= old_stdout
227 assert get_value('freshness') == '30d\n'
228 assert get_value('network_use') == 'full\n'
229 assert get_value('help_with_testing') == 'False\n'
231 cmd
.config
.handle(file_config
, None, ['freshness', '5m'])
232 cmd
.config
.handle(file_config
, None, ['help_with_testing', 'True'])
233 cmd
.config
.handle(file_config
, None, ['network_use', 'minimal'])
234 assert file_config
.freshness
== 5 * 60
235 assert file_config
.network_use
== model
.network_minimal
236 assert file_config
.help_with_testing
== True
238 file_config2
= policy
.load_config(handler
.Handler())
239 assert file_config2
.freshness
== 5 * 60
240 assert file_config2
.network_use
== model
.network_minimal
241 assert file_config2
.help_with_testing
== True
243 cmd
.config
.handle(file_config
, None, ['help_with_testing', 'falsE'])
244 assert file_config
.help_with_testing
== False
246 for period
in ['1s', '2d', '3.5m', '4h', '5d']:
247 secs
= cmd
.config
.TimeInterval
.parse(period
)
248 assert cmd
.config
.TimeInterval
.format(secs
) == period
250 def testAddFeed(self
):
251 binary_iface
= self
.config
.iface_cache
.get_interface('http://foo/Binary.xml')
253 out
, err
= self
.run_0install(['list-feeds', binary_iface
.uri
])
254 assert "(no feeds)" in out
, out
257 out
, err
= self
.run_0install(['add-feed'])
258 assert out
.lower().startswith("usage:")
259 assert 'NEW-FEED' in out
261 sys
.stdin
= Reply('1')
262 assert binary_iface
.extra_feeds
== []
264 out
, err
= self
.run_0install(['add-feed', 'Source.xml'])
266 assert "Add as feed for 'http://foo/Binary.xml'" in out
, out
267 assert len(binary_iface
.extra_feeds
) == 1
269 out
, err
= self
.run_0install(['list-feeds', binary_iface
.uri
])
270 assert "Source.xml" in out
273 out
, err
= self
.run_0install(['remove-feed', 'Source.xml'])
275 assert "Remove as feed for 'http://foo/Binary.xml'" in out
, out
276 assert len(binary_iface
.extra_feeds
) == 0
278 source_feed
= reader
.load_feed('Source.xml')
279 self
.config
.fetcher
.allow_feed_download('http://foo/Source.xml', source_feed
)
280 out
, err
= self
.run_0install(['add-feed', 'http://foo/Source.xml'])
282 assert 'Downloading feed; please wait' in out
, out
283 assert len(binary_iface
.extra_feeds
) == 1
285 def testImport(self
):
286 out
, err
= self
.run_0install(['import'])
287 assert out
.lower().startswith("usage:")
290 stream
= file('6FCF121BE2390E0B.gpg')
291 gpg
.import_key(stream
)
293 sys
.stdin
= Reply('Y\n')
294 out
, err
= self
.run_0install(['import', 'Hello.xml'])
296 assert 'Trusting DE937DD411906ACF7C263B396FCF121BE2390E0B for example.com:8000' in err
, out
299 out
, err
= self
.run_0install(['list', 'foo', 'bar'])
300 assert out
.lower().startswith("usage:")
301 assert 'PATTERN' in out
303 out
, err
= self
.run_0install(['list'])
305 assert '' == out
, repr(out
)
309 out
, err
= self
.run_0install(['list'])
311 assert 'http://example.com:8000/Hello.xml\n' == out
, repr(out
)
313 out
, err
= self
.run_0install(['list', 'foo'])
315 assert '' == out
, repr(out
)
317 out
, err
= self
.run_0install(['list', 'hello'])
319 assert 'http://example.com:8000/Hello.xml\n' == out
, repr(out
)
322 out
, err
= self
.run_0install(['run'])
323 assert out
.lower().startswith("usage:")
324 assert 'URI' in out
, out
326 out
, err
= self
.run_0install(['run', '--dry-run', 'runnable/Runnable.xml', '--help'])
328 assert 'arg-for-runner' in out
, out
329 assert '--help' in out
, out
331 if __name__
== '__main__':