Minimum Python version is now 2.5
[zeroinstall/solver.git] / tests / testalias.py
blob44212e8f0ec1003a41e5e9d52f1c689649b3d7a5
1 #!/usr/bin/env python2.5
2 from basetest import BaseTest
3 import sys, tempfile
4 from StringIO import StringIO
5 import unittest
7 sys.path.insert(0, '..')
9 from zeroinstall import alias
11 expected_script = """#!/bin/sh
12 if [ "$*" = "--versions" ]; then
13 exec 0launch -gd 'http://example.com/foo.xml' "$@"
14 else
15 exec 0launch 'http://example.com/foo.xml' "$@"
17 """
19 expected_script_main = """#!/bin/sh
20 if [ "$*" = "--versions" ]; then
21 exec 0launch -gd 'http://example.com/foo.xml' "$@"
22 else
23 exec 0launch --main 'a'\\'''\\''\\test' 'http://example.com/foo.xml' "$@"
25 """
27 class TestAlias(BaseTest):
28 def setUp(self):
29 BaseTest.setUp(self)
31 def testWrite(self):
32 buf = StringIO()
33 alias.write_script(buf, 'http://example.com/foo.xml', None)
34 self.assertEquals(expected_script, buf.getvalue())
36 buf = StringIO()
37 alias.write_script(buf, 'http://example.com/foo.xml', 'a\'\'\\test')
38 self.assertEquals(expected_script_main, buf.getvalue())
40 def testParse(self):
41 tmp = tempfile.NamedTemporaryFile()
42 tmp.write(expected_script)
43 tmp.flush()
44 tmp.seek(0)
45 uri, main = alias.parse_script(tmp.name)
46 self.assertEquals('http://example.com/foo.xml', uri)
47 self.assertEquals(None, main)
49 tmp = tempfile.NamedTemporaryFile()
50 tmp.write(expected_script_main)
51 tmp.flush()
52 tmp.seek(0)
53 uri, main = alias.parse_script(tmp.name)
54 self.assertEquals('http://example.com/foo.xml', uri)
55 self.assertEquals('a\'\'\\test', main)
57 def testParseException(self):
58 tmp = tempfile.NamedTemporaryFile()
59 tmp.write('hi' + expected_script)
60 tmp.flush()
61 tmp.seek(0)
62 try:
63 alias.parse_script(tmp.name)
64 assert False
65 except alias.NotAnAliasScript:
66 pass
68 suite = unittest.makeSuite(TestAlias)
69 if __name__ == '__main__':
70 sys.argv.append('-v')
71 unittest.main()