2 from basetest
import BaseTest
4 from StringIO
import StringIO
7 sys
.path
.insert(0, '..')
9 from zeroinstall
import alias
11 expected_script
= """#!/bin/sh
12 exec 0launch 'http://example.com/foo.xml' "$@"
15 old_script
= """#!/bin/sh
16 if [ "$*" = "--versions" ]; then
17 exec 0launch -gd 'http://example.com/foo.xml' "$@"
19 exec 0launch 'http://example.com/foo.xml' "$@"
23 expected_script_main
= """#!/bin/sh
24 exec 0launch --main 'a'\\'''\\''\\test' 'http://example.com/foo.xml' "$@"
27 expected_script_command
= """#!/bin/sh
28 exec 0launch --command 'a'\\'''\\''\\test' 'http://example.com/foo.xml' "$@"
31 old_script_main
= """#!/bin/sh
32 if [ "$*" = "--versions" ]; then
33 exec 0launch -gd 'http://example.com/foo.xml' "$@"
35 exec 0launch --main 'a'\\'''\\''\\test' 'http://example.com/foo.xml' "$@"
39 class TestAlias(BaseTest
):
45 alias
.write_script(buf
, 'http://example.com/foo.xml', None)
46 self
.assertEqual(expected_script
, buf
.getvalue())
49 alias
.write_script(buf
, 'http://example.com/foo.xml', 'a\'\'\\test')
50 self
.assertEqual(expected_script_main
, buf
.getvalue())
53 alias
.write_script(buf
, 'http://example.com/foo.xml', command
= 'a\'\'\\test')
54 self
.assertEqual(expected_script_command
, buf
.getvalue())
57 tmp
= tempfile
.NamedTemporaryFile()
58 tmp
.write(expected_script
)
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
)
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
)
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()
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
)
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
)
105 alias
.parse_script(tmp
.name
)
107 except alias
.NotAnAliasScript
:
110 tmp
= tempfile
.NamedTemporaryFile()
111 tmp
.write(expected_script_command
.replace('command', 'bob'))
115 alias
.parse_script(tmp
.name
)
117 except alias
.NotAnAliasScript
, ex
:
118 assert 'bob' in str(ex
)
121 if __name__
== '__main__':