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