Start development series 1.7-post
[zeroinstall/solver.git] / tests / testalias.py
blob327c0f0dab5cf73350ed23c4fb7c8dfd21aaac7e
1 #!/usr/bin/env python
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 exec 0launch 'http://example.com/foo.xml' "$@"
13 """
15 old_script = """#!/bin/sh
16 if [ "$*" = "--versions" ]; then
17 exec 0launch -gd 'http://example.com/foo.xml' "$@"
18 else
19 exec 0launch 'http://example.com/foo.xml' "$@"
21 """
23 expected_script_main = """#!/bin/sh
24 exec 0launch --main 'a'\\'''\\''\\test' 'http://example.com/foo.xml' "$@"
25 """
27 expected_script_command = """#!/bin/sh
28 exec 0launch --command 'a'\\'''\\''\\test' 'http://example.com/foo.xml' "$@"
29 """
31 old_script_main = """#!/bin/sh
32 if [ "$*" = "--versions" ]; then
33 exec 0launch -gd 'http://example.com/foo.xml' "$@"
34 else
35 exec 0launch --main 'a'\\'''\\''\\test' 'http://example.com/foo.xml' "$@"
37 """
39 class TestAlias(BaseTest):
40 def setUp(self):
41 BaseTest.setUp(self)
43 def testWrite(self):
44 buf = StringIO()
45 alias.write_script(buf, 'http://example.com/foo.xml', None)
46 self.assertEqual(expected_script, buf.getvalue())
48 buf = StringIO()
49 alias.write_script(buf, 'http://example.com/foo.xml', 'a\'\'\\test')
50 self.assertEqual(expected_script_main, buf.getvalue())
52 buf = StringIO()
53 alias.write_script(buf, 'http://example.com/foo.xml', command = 'a\'\'\\test')
54 self.assertEqual(expected_script_command, buf.getvalue())
56 def testParse(self):
57 tmp = tempfile.NamedTemporaryFile()
58 tmp.write(expected_script)
59 tmp.flush()
60 tmp.seek(0)
61 uri, main = alias.parse_script(tmp.name)
62 self.assertEqual('http://example.com/foo.xml', uri)
63 self.assertEqual(None, main)
65 tmp = tempfile.NamedTemporaryFile()
66 tmp.write(expected_script_main)
67 tmp.flush()
68 tmp.seek(0)
69 uri, main = alias.parse_script(tmp.name)
70 self.assertEqual('http://example.com/foo.xml', uri)
71 self.assertEqual('a\'\'\\test', main)
73 tmp = tempfile.NamedTemporaryFile()
74 tmp.write(expected_script_command)
75 tmp.flush()
76 tmp.seek(0)
77 info = alias.parse_script(tmp.name)
78 self.assertEqual('http://example.com/foo.xml', info.uri)
79 self.assertEqual('a\'\'\\test', info.command)
80 self.assertEqual(None, info.main)
82 def testParseOld(self):
83 tmp = tempfile.NamedTemporaryFile()
84 tmp.write(old_script)
85 tmp.flush()
86 tmp.seek(0)
87 uri, main = alias.parse_script(tmp.name)
88 self.assertEqual('http://example.com/foo.xml', uri)
89 self.assertEqual(None, main)
91 tmp = tempfile.NamedTemporaryFile()
92 tmp.write(old_script_main)
93 tmp.flush()
94 tmp.seek(0)
95 uri, main = alias.parse_script(tmp.name)
96 self.assertEqual('http://example.com/foo.xml', uri)
97 self.assertEqual('a\'\'\\test', main)
99 def testParseException(self):
100 tmp = tempfile.NamedTemporaryFile()
101 tmp.write('hi' + expected_script)
102 tmp.flush()
103 tmp.seek(0)
104 try:
105 alias.parse_script(tmp.name)
106 assert False
107 except alias.NotAnAliasScript:
108 pass
110 tmp = tempfile.NamedTemporaryFile()
111 tmp.write(expected_script_command.replace('command', 'bob'))
112 tmp.flush()
113 tmp.seek(0)
114 try:
115 alias.parse_script(tmp.name)
116 assert False
117 except alias.NotAnAliasScript, ex:
118 assert 'bob' in str(ex)
119 pass
121 if __name__ == '__main__':
122 unittest.main()