1 | #!/usr/bin/env python2
|
2 | """
|
3 | args_test.py: Tests for args.py
|
4 | """
|
5 |
|
6 | import unittest
|
7 |
|
8 | from _devbuild.gen.runtime_asdl import cmd_value
|
9 | from _devbuild.gen.syntax_asdl import loc, loc_t
|
10 | from _devbuild.gen.value_asdl import value
|
11 | from core import error
|
12 | from frontend import flag_spec
|
13 | from frontend import args # module under test
|
14 |
|
15 | from typing import Tuple
|
16 |
|
17 |
|
18 | def _MakeBuiltinArgv(argv):
|
19 | """Different than test_lib.MakeBuiltinArgv()"""
|
20 | argv = [''] + argv # add dummy since arg_vec includes argv[0]
|
21 | # no location info
|
22 | missing = loc.Missing # type: loc_t
|
23 | return cmd_value.Argv(argv, [missing] * len(argv), False, None, None)
|
24 |
|
25 |
|
26 | def _MakeReader(argv):
|
27 | cmd_val = _MakeBuiltinArgv(argv)
|
28 | arg_r = args.Reader(cmd_val.argv, locs=cmd_val.arg_locs)
|
29 | arg_r.Next()
|
30 | return arg_r
|
31 |
|
32 |
|
33 | def _ParseCmdVal(spec, cmd_val):
|
34 | # type: (cmd_value.Argv) -> Tuple[args._Attributes, int]
|
35 | """For testing only."""
|
36 | arg_r = args.Reader(cmd_val.argv, locs=cmd_val.arg_locs)
|
37 | arg_r.Next() # move past the builtin name
|
38 | return args.Parse(spec, arg_r), arg_r.i
|
39 |
|
40 |
|
41 | class ArgsTest(unittest.TestCase):
|
42 |
|
43 | def testFlagSpecAndMore(self):
|
44 | s = flag_spec._FlagSpecAndMore()
|
45 | s.ShortFlag('-c', args.String)
|
46 | s.ShortFlag('-i', args.String)
|
47 |
|
48 | s.LongFlag('--help')
|
49 | s.LongFlag('--rcfile', args.String)
|
50 | s.LongFlag('--rcdir', args.String)
|
51 | s.LongFlag('--norc')
|
52 |
|
53 | s.LongFlag('--ast-format', ['text', 'html'])
|
54 |
|
55 | s.InitOptions()
|
56 | s.Option('e', 'errexit')
|
57 | s.Option('u', 'nounset')
|
58 | s.Option(None, 'pipefail')
|
59 |
|
60 | # don't parse args afterward
|
61 | arg_r = args.Reader(
|
62 | ['-c', 'echo hi', '-e', '-o', 'nounset', 'foo', '--help'])
|
63 | arg = args.ParseMore(s, arg_r)
|
64 |
|
65 | self.assertEqual(['foo', '--help'], arg_r.Rest())
|
66 | self.assertEqual('echo hi', arg.attrs['c'].s)
|
67 | self.assertEqual(False, arg.attrs['help'].b)
|
68 | self.assertEqual(value.Undef, arg.attrs['i'])
|
69 |
|
70 | self.assertEqual([('errexit', True), ('nounset', True)],
|
71 | arg.opt_changes)
|
72 |
|
73 | arg_r = args.Reader(['+e', '+o', 'nounset', '-o', 'pipefail', 'foo'])
|
74 | arg = args.ParseMore(s, arg_r)
|
75 |
|
76 | self.assertEqual(['foo'], arg_r.Rest())
|
77 | self.assertEqual(value.Undef, arg.attrs['i'])
|
78 | self.assertEqual([('errexit', False), ('nounset', False),
|
79 | ('pipefail', True)], arg.opt_changes)
|
80 |
|
81 | arg_r = args.Reader([
|
82 | '-c', 'echo hi', '--help', '--rcfile', 'bashrc', '--rcdir',
|
83 | 'bashrcdir'
|
84 | ])
|
85 | arg = args.ParseMore(s, arg_r)
|
86 | self.assertEqual('echo hi', arg.attrs['c'].s)
|
87 | self.assertEqual(True, arg.attrs['help'].b)
|
88 | self.assertEqual('bashrc', arg.attrs['rcfile'].s)
|
89 | self.assertEqual('bashrcdir', arg.attrs['rcdir'].s)
|
90 |
|
91 | # This is an odd syntax!
|
92 | arg_r = args.Reader(['-euo', 'pipefail'])
|
93 | arg = args.ParseMore(s, arg_r)
|
94 | self.assertEqual([('errexit', True), ('nounset', True),
|
95 | ('pipefail', True)], arg.opt_changes)
|
96 | self.assertEqual(2, arg_r.i)
|
97 |
|
98 | # Even weirder!
|
99 | arg_r = args.Reader(['+oeu', 'pipefail'])
|
100 | arg = args.ParseMore(s, arg_r)
|
101 | self.assertEqual([('pipefail', False), ('errexit', False),
|
102 | ('nounset', False)], arg.opt_changes)
|
103 | self.assertEqual(2, arg_r.i)
|
104 |
|
105 | # Even weirder!
|
106 | arg_r = args.Reader(['+oo', 'pipefail', 'errexit'])
|
107 | arg = args.ParseMore(s, arg_r)
|
108 | self.assertEqual([('pipefail', False), ('errexit', False)],
|
109 | arg.opt_changes)
|
110 | self.assertEqual(3, arg_r.i)
|
111 |
|
112 | # Now this is an arg. Gah.
|
113 | arg_r = args.Reader(['+o', 'pipefail', 'errexit'])
|
114 | arg = args.ParseMore(s, arg_r)
|
115 | self.assertEqual([('pipefail', False)], arg.opt_changes)
|
116 | self.assertEqual(['errexit'], arg_r.Rest())
|
117 |
|
118 | # NOTE: 'set -ooo' and 'set -o -o -o' bash runs 'set -o' three times!
|
119 | # We're not going to replicate that silly behavior.
|
120 |
|
121 | def testChoices(self):
|
122 | s = flag_spec._FlagSpecAndMore()
|
123 | s.LongFlag('--ast-format', ['text', 'html'])
|
124 |
|
125 | arg_r = args.Reader(['--ast-format', 'text'])
|
126 | arg = args.ParseMore(s, arg_r)
|
127 | self.assertEqual('text', arg.attrs['ast_format'].s)
|
128 |
|
129 | self.assertRaises(error.Usage, args.Parse, s,
|
130 | args.Reader(['--ast-format', 'oops']))
|
131 |
|
132 | def testFlagSpec(self):
|
133 | s = flag_spec._FlagSpec()
|
134 | s.ShortFlag('-f')
|
135 | s.ShortFlag('-n')
|
136 | s.ShortFlag('-d', args.String) # delimiter
|
137 |
|
138 | # like declare +rx
|
139 | s.PlusFlag('r')
|
140 | s.PlusFlag('x')
|
141 |
|
142 | arg, i = _ParseCmdVal(s, _MakeBuiltinArgv(['-f', 'foo', 'bar']))
|
143 | self.assertEqual(1, i - 1)
|
144 | self.assertEqual(True, arg.attrs['f'].b)
|
145 | #self.assertEqual(False, arg.n)
|
146 | self.assertEqual(False, arg.attrs['n'].b)
|
147 |
|
148 | self.assertRaises(error.Usage, _ParseCmdVal, s,
|
149 | _MakeBuiltinArgv(['-f', '-d']))
|
150 |
|
151 | arg, i = _ParseCmdVal(s, _MakeBuiltinArgv(['-d', ' ', 'foo']))
|
152 | self.assertEqual(2, i - 1)
|
153 | self.assertEqual(' ', arg.attrs['d'].s)
|
154 |
|
155 | arg, i = _ParseCmdVal(s, _MakeBuiltinArgv(['-d,', 'foo']))
|
156 | self.assertEqual(1, i - 1)
|
157 | self.assertEqual(',', arg.attrs['d'].s)
|
158 | #self.assertEqual(False, arg.r)
|
159 | self.assertEqual(value.Undef, arg.attrs['r'])
|
160 |
|
161 | arg, i = _ParseCmdVal(s, _MakeBuiltinArgv(['-d,', '-r', '-x']))
|
162 | self.assertEqual(4, i)
|
163 | self.assertEqual(',', arg.attrs['d'].s)
|
164 | self.assertEqual('-', arg.attrs['r'].s)
|
165 | self.assertEqual('-', arg.attrs['x'].s)
|
166 |
|
167 | arg, i = _ParseCmdVal(s, _MakeBuiltinArgv(['-d,', '+rx']))
|
168 | self.assertEqual(3, i)
|
169 | self.assertEqual(',', arg.attrs['d'].s)
|
170 | self.assertEqual('+', arg.attrs['r'].s)
|
171 | self.assertEqual('+', arg.attrs['x'].s)
|
172 |
|
173 | def testReadFlagSpec(self):
|
174 | s = flag_spec._FlagSpec()
|
175 | s.ShortFlag('-r') # no backslash escapes
|
176 | s.ShortFlag('-t', args.Float) # timeout
|
177 | s.ShortFlag('-p', args.String) # prompt string
|
178 |
|
179 | arg, i = _ParseCmdVal(s, _MakeBuiltinArgv(['-r', 'foo']))
|
180 | self.assertEqual(True, arg.attrs['r'].b)
|
181 | self.assertEqual(1, i - 1)
|
182 |
|
183 | arg, i = _ParseCmdVal(s, _MakeBuiltinArgv(['-p', '>']))
|
184 | self.assertEqual(False, arg.attrs['r'].b)
|
185 | self.assertEqual('>', arg.attrs['p'].s)
|
186 | self.assertEqual(2, i - 1)
|
187 |
|
188 | arg, i = _ParseCmdVal(s, _MakeBuiltinArgv(['-rp', '>']))
|
189 | self.assertEqual(True, arg.attrs['r'].b)
|
190 | self.assertEqual('>', arg.attrs['p'].s)
|
191 | self.assertEqual(2, i - 1)
|
192 |
|
193 | # REALLY ANNOYING: The first r is a flag, the second R is the prompt! Only
|
194 | # works in that order
|
195 | # Does that mean anything with an arity consumes the rest?
|
196 | # read -p line
|
197 | #
|
198 | arg, i = _ParseCmdVal(s, _MakeBuiltinArgv(['-rpr']))
|
199 | self.assertEqual(True, arg.attrs['r'].b)
|
200 | self.assertEqual('r', arg.attrs['p'].s)
|
201 | self.assertEqual(1, i - 1)
|
202 |
|
203 | argv = ['-t1.5', '>']
|
204 | arg, i = _ParseCmdVal(s, _MakeBuiltinArgv(argv))
|
205 | self.assertEqual(1.5, arg.attrs['t'].f)
|
206 | self.assertEqual(1, i - 1)
|
207 |
|
208 | # Invalid flag 'z'
|
209 | self.assertRaises(error.Usage, _ParseCmdVal, s,
|
210 | _MakeBuiltinArgv(['-rz']))
|
211 |
|
212 | def testParseLikeEcho(self):
|
213 | s = flag_spec._FlagSpec()
|
214 | s.ShortFlag('-e') # no backslash escapes
|
215 | s.ShortFlag('-n')
|
216 |
|
217 | arg_r = _MakeReader(['-e', '-n', 'foo'])
|
218 | arg = args.ParseLikeEcho(s, arg_r)
|
219 | self.assertEqual(True, arg.attrs['e'].b)
|
220 | self.assertEqual(True, arg.attrs['n'].b)
|
221 | self.assertEqual(3, arg_r.i)
|
222 |
|
223 | arg_r = _MakeReader(['-en', 'foo'])
|
224 | arg = args.ParseLikeEcho(s, arg_r)
|
225 | self.assertEqual(True, arg.attrs['e'].b)
|
226 | self.assertEqual(True, arg.attrs['n'].b)
|
227 | self.assertEqual(2, arg_r.i)
|
228 |
|
229 | arg_r = _MakeReader(['-ez', 'foo'])
|
230 | arg = args.ParseLikeEcho(s, arg_r)
|
231 | self.assertEqual(False, arg.attrs['e'].b)
|
232 | self.assertEqual(False, arg.attrs['n'].b)
|
233 | self.assertEqual(1, arg_r.i)
|
234 |
|
235 |
|
236 | if __name__ == '__main__':
|
237 | unittest.main()
|