forked from aws/aws-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_shorthand.py
More file actions
269 lines (237 loc) · 10.5 KB
/
test_shorthand.py
File metadata and controls
269 lines (237 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
from awscli import shorthand
from awscli.testutils import unittest
from botocore import model
from nose.tools import assert_equal
def test_parse():
# Key val pairs with scalar value.
yield (_can_parse, 'foo=bar', {'foo': 'bar'})
yield (_can_parse, 'foo=bar', {'foo': 'bar'})
yield (_can_parse, 'foo=bar,baz=qux', {'foo': 'bar', 'baz': 'qux'})
yield (_can_parse, 'a=b,c=d,e=f', {'a': 'b', 'c': 'd', 'e': 'f'})
# Empty values are allowed.
yield (_can_parse, 'foo=', {'foo': ''})
yield (_can_parse, 'foo=,bar=', {'foo': '', 'bar': ''})
# Unicode is allowed.
yield (_can_parse, u'foo=\u2713', {'foo': u'\u2713'})
yield (_can_parse, u'foo=\u2713,\u2713', {'foo': [u'\u2713', u'\u2713']})
# Key val pairs with csv values.
yield (_can_parse, 'foo=a,b', {'foo': ['a', 'b']})
yield (_can_parse, 'foo=a,b,c', {'foo': ['a', 'b', 'c']})
yield (_can_parse, 'foo=a,b,bar=c,d', {'foo': ['a', 'b'],
'bar': ['c', 'd']})
yield (_can_parse, 'foo=a,b,c,bar=d,e,f',
{'foo': ['a', 'b', 'c'], 'bar': ['d', 'e', 'f']})
# Spaces in values are allowed.
yield (_can_parse, 'foo=a,b=with space', {'foo': 'a', 'b': 'with space'})
# Trailing spaces are still ignored.
yield (_can_parse, 'foo=a,b=with trailing space ',
{'foo': 'a', 'b': 'with trailing space'})
yield (_can_parse, 'foo=first space',
{'foo': 'first space'})
yield (_can_parse, 'foo=a space,bar=a space,baz=a space',
{'foo': 'a space', 'bar': 'a space', 'baz': 'a space'})
# Dashes are allowed in key names.
yield (_can_parse, 'with-dash=bar', {'with-dash': 'bar'})
# Underscore are also allowed.
yield (_can_parse, 'with_underscore=bar', {'with_underscore': 'bar'})
# Dots are allowed.
yield (_can_parse, 'with.dot=bar', {'with.dot': 'bar'})
# Pound signs are allowed.
yield (_can_parse, '#key=value', {'#key': 'value'})
# Forward slashes are allowed in keys.
yield (_can_parse, 'some/thing=value', {'some/thing': 'value'})
# Colon chars are allowed in keys:
yield (_can_parse, 'aws:service:region:124:foo/bar=baz',
{'aws:service:region:124:foo/bar': 'baz'})
# Explicit lists.
yield (_can_parse, 'foo=[]', {'foo': []})
yield (_can_parse, 'foo=[a]', {'foo': ['a']})
yield (_can_parse, 'foo=[a,b]', {'foo': ['a', 'b']})
yield (_can_parse, 'foo=[a,b,c]', {'foo': ['a', 'b', 'c']})
yield (_can_parse, 'foo=[a,b],bar=c,d',
{'foo': ['a', 'b'], 'bar': ['c', 'd']})
yield (_can_parse, 'foo=[a,b],bar=[c,d]',
{'foo': ['a', 'b'], 'bar': ['c', 'd']})
yield (_can_parse, 'foo=a,b,bar=[c,d]',
{'foo': ['a', 'b'], 'bar': ['c', 'd']})
yield (_can_parse, 'foo=[a=b,c=d]', {'foo': ['a=b', 'c=d']})
yield (_can_parse, 'foo=[a=b,c=d]', {'foo': ['a=b', 'c=d']})
# Lists with whitespace.
yield (_can_parse, 'foo=[ a , b , c ]',
{'foo': ['a', 'b', 'c']})
yield (_can_parse, 'foo = [ a , b , c ]',
{'foo': ['a', 'b', 'c']})
yield (_can_parse, 'foo=[,,]', {'foo': ['', '']})
# Single quoted strings.
yield (_can_parse, "foo='bar'", {"foo": "bar"})
yield (_can_parse, "foo='bar,baz'", {"foo": "bar,baz"})
# Single quoted strings for each value in a CSV list.
yield (_can_parse, "foo='bar','baz'", {"foo": ['bar', 'baz']})
# Can mix single quoted and non quoted values.
yield (_can_parse, "foo=bar,'baz'", {"foo": ['bar', 'baz']})
# Quoted strings can include chars not allowed in unquoted strings.
yield (_can_parse, "foo=bar,'baz=qux'", {"foo": ['bar', 'baz=qux']})
yield (_can_parse, "foo=bar,'--option=bar space'",
{"foo": ['bar', '--option=bar space']})
# Can escape the single quote.
yield (_can_parse, "foo='bar\\'baz'", {"foo": "bar'baz"})
yield (_can_parse, "foo='bar\\\\baz'", {"foo": "bar\\baz"})
# Double quoted strings.
yield (_can_parse, 'foo="bar"', {'foo': 'bar'})
yield (_can_parse, 'foo="bar,baz"', {'foo': 'bar,baz'})
yield (_can_parse, 'foo="bar","baz"', {'foo': ['bar', 'baz']})
yield (_can_parse, 'foo=bar,"baz=qux"', {'foo': ['bar', 'baz=qux']})
yield (_can_parse, 'foo=bar,"--option=bar space"',
{'foo': ['bar', '--option=bar space']})
yield (_can_parse, 'foo="bar\\"baz"', {'foo': 'bar"baz'})
yield (_can_parse, 'foo="bar\\\\baz"', {'foo': 'bar\\baz'})
# Can escape comma in CSV list.
yield (_can_parse, 'foo=a\\,b', {"foo": "a,b"})
yield (_can_parse, 'foo=a\\,b', {"foo": "a,b"})
yield (_can_parse, 'foo=a\\,', {"foo": "a,"})
yield (_can_parse, 'foo=\\,', {"foo": ","})
yield (_can_parse, 'foo=a,b\\,c', {"foo": ['a', 'b,c']})
yield (_can_parse, 'foo=a,b\\,', {"foo": ['a', 'b,']})
yield (_can_parse, 'foo=a,\\,bc', {"foo": ['a', ',bc']})
# Ignores whitespace around '=' and ','
yield (_can_parse, 'foo= bar', {'foo': 'bar'})
yield (_can_parse, 'foo =bar', {'foo': 'bar'})
yield (_can_parse, 'foo = bar', {'foo': 'bar'})
yield (_can_parse, 'foo = bar', {'foo': 'bar'})
yield (_can_parse, 'foo = bar,baz = qux', {'foo': 'bar', 'baz': 'qux'})
yield (_can_parse, 'a = b, c = d , e = f', {'a': 'b', 'c': 'd', 'e': 'f'})
yield (_can_parse, 'foo = ', {'foo': ''})
yield (_can_parse, 'a=b,c= d, e, f', {'a': 'b', 'c': ['d', 'e', 'f']})
yield (_can_parse, 'Name=foo,Values= a , b , c ',
{'Name': 'foo', 'Values': ['a', 'b', 'c']})
yield (_can_parse, 'Name=foo,Values= a, b , c',
{'Name': 'foo', 'Values': ['a', 'b', 'c']})
# Can handle newlines between values.
yield (_can_parse, 'Name=foo,\nValues=a,b,c',
{'Name': 'foo', 'Values': ['a', 'b', 'c']})
yield (_can_parse, 'A=b,\nC=d,\nE=f\n',
{'A': 'b', 'C': 'd', 'E': 'f'})
# Hashes
yield (_can_parse, 'Name={foo=bar,baz=qux}',
{'Name': {'foo': 'bar', 'baz': 'qux'}})
yield (_can_parse, 'Name={foo=[a,b,c],bar=baz}',
{'Name': {'foo': ['a', 'b', 'c'], 'bar': 'baz'}})
yield (_can_parse, 'Name={foo=bar},Bar=baz',
{'Name': {'foo': 'bar'}, 'Bar': 'baz'})
yield (_can_parse, 'Bar=baz,Name={foo=bar}',
{'Bar': 'baz', 'Name': {'foo': 'bar'}})
yield (_can_parse, 'a={b={c=d}}',
{'a': {'b': {'c': 'd'}}})
yield (_can_parse, 'a={b={c=d,e=f},g=h}',
{'a': {'b': {'c': 'd', 'e': 'f'}, 'g': 'h'}})
# Combining lists and hashes.
yield (_can_parse, 'Name=[{foo=bar}, {baz=qux}]',
{'Name': [{'foo': 'bar'}, {'baz': 'qux'}]})
# Combining hashes and lists.
yield (_can_parse, 'Name=[{foo=[a,b]}, {bar=[c,d]}]',
{'Name': [{'foo': ['a', 'b']}, {'bar': ['c', 'd']}]})
def test_error_parsing():
yield (_is_error, 'foo')
# Missing closing quotes
yield (_is_error, 'foo="bar')
yield (_is_error, "foo='bar")
yield (_is_error, "foo=[bar")
yield (_is_error, "foo={bar")
yield (_is_error, "foo={bar}")
yield (_is_error, "foo={bar=bar")
yield (_is_error, "foo=bar,")
yield (_is_error, "foo==bar,\nbar=baz")
def _is_error(expr):
try:
shorthand.ShorthandParser().parse(expr)
except shorthand.ShorthandParseError:
pass
except Exception as e:
raise AssertionError(
"Expected ShorthandParseError, but received unexpected "
"exception instead (%s): %s" % (e.__class__, e))
else:
raise AssertionError("Expected ShorthandParseError, but no "
"exception was raised for expression: %s" % expr)
def _can_parse(data, expected):
actual = shorthand.ShorthandParser().parse(data)
assert_equal(actual, expected)
class TestModelVisitor(unittest.TestCase):
def test_promote_to_list_of_ints(self):
m = model.DenormalizedStructureBuilder().with_members({
'A': {
'type': 'list',
'member': {'type': 'string'}
},
}).build_model()
b = shorthand.BackCompatVisitor()
params = {'A': 'foo'}
b.visit(params, m)
self.assertEqual(params, {'A': ['foo']})
def test_dont_promote_list_if_none_value(self):
m = model.DenormalizedStructureBuilder().with_members({
'A': {
'type': 'list',
'member': {
'type': 'structure',
'members': {
'Single': {'type': 'string'}
},
},
},
}).build_model()
b = shorthand.BackCompatVisitor()
params = {}
b.visit(params, m)
self.assertEqual(params, {})
def test_can_convert_scalar_types_from_string(self):
m = model.DenormalizedStructureBuilder().with_members({
'A': {'type': 'integer'},
'B': {'type': 'string'},
'C': {'type': 'float'},
'D': {'type': 'boolean'},
'E': {'type': 'boolean'},
}).build_model()
b = shorthand.BackCompatVisitor()
params = {'A': '24', 'B': '24', 'C': '24.12345',
'D': 'true', 'E': 'false'}
b.visit(params, m)
self.assertEqual(
params,
{'A': 24, 'B': '24', 'C': float('24.12345'),
'D': True, 'E': False})
def test_empty_values_not_added(self):
m = model.DenormalizedStructureBuilder().with_members({
'A': {'type': 'boolean'},
}).build_model()
b = shorthand.BackCompatVisitor()
params = {}
b.visit(params, m)
self.assertEqual(params, {})
def test_can_convert_list_of_integers(self):
m = model.DenormalizedStructureBuilder().with_members({
'A': {
'type': 'list',
'member': {
'type': 'integer',
},
},
}).build_model()
b = shorthand.BackCompatVisitor()
params = {'A': ['1', '2']}
b.visit(params, m)
# We should have converted each list element to an integer
# because the type of the list member is integer.
self.assertEqual(params, {'A': [1, 2]})