Debian Codegraph @

test/units/parsing/test_splitter.py

raw
1 # coding: utf-8
2 # (c) 2015, Toshio Kuratomi <[email protected]>
3 #
4 # This file is part of Ansible
5 #
6 # Ansible is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # Ansible is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
18
19 from __future__ import annotations
20
21 from ansible.parsing.splitter import split_args, parse_kv
22 from ansible.errors import AnsibleParserError
23
24 import pytest
25
26 SPLIT_DATA: tuple[tuple[str | None, list[str], dict[str, str]], ...] = (
27 (None,
28 [],
29 {}),
30 (u'',
31 [],
32 {}),
33 (u'a',
34 [u'a'],
35 {u'_raw_params': u'a'}),
36 (u'a=b',
37 [u'a=b'],
38 {u'a': u'b'}),
39 (u'a="foo bar"',
40 [u'a="foo bar"'],
41 {u'a': u'foo bar'}),
42 (u'"foo bar baz"',
43 [u'"foo bar baz"'],
44 {u'_raw_params': '"foo bar baz"'}),
45 (u'foo bar baz',
46 [u'foo', u'bar', u'baz'],
47 {u'_raw_params': u'foo bar baz'}),
48 (u'a=b c="foo bar"',
49 [u'a=b', u'c="foo bar"'],
50 {u'a': u'b', u'c': u'foo bar'}),
51 (u'a="echo \\"hello world\\"" b=bar',
52 [u'a="echo \\"hello world\\""', u'b=bar'],
53 {u'a': u'echo "hello world"', u'b': u'bar'}),
54 (u'a="nest\'ed"',
55 [u'a="nest\'ed"'],
56 {u'a': u'nest\'ed'}),
57 (u' ',
58 [u' '],
59 {u'_raw_params': u' '}),
60 (u'\\ ',
61 [u' '],
62 {u'_raw_params': u' '}),
63 (u'a\\=escaped',
64 [u'a\\=escaped'],
65 {u'_raw_params': u'a=escaped'}),
66 (u'a="multi\nline"',
67 [u'a="multi\nline"'],
68 {u'a': u'multi\nline'}),
69 (u'a="blank\n\nline"',
70 [u'a="blank\n\nline"'],
71 {u'a': u'blank\n\nline'}),
72 (u'a="blank\n\n\nlines"',
73 [u'a="blank\n\n\nlines"'],
74 {u'a': u'blank\n\n\nlines'}),
75 (u'a="a long\nmessage\\\nabout a thing\n"',
76 [u'a="a long\nmessage\\\nabout a thing\n"'],
77 {u'a': u'a long\nmessage\\\nabout a thing\n'}),
78 (u'a="multiline\nmessage1\\\n" b="multiline\nmessage2\\\n"',
79 [u'a="multiline\nmessage1\\\n"', u'b="multiline\nmessage2\\\n"'],
80 {u'a': 'multiline\nmessage1\\\n', u'b': u'multiline\nmessage2\\\n'}),
81 (u'line \\\ncontinuation',
82 [u'line', u'continuation'],
83 {u'_raw_params': u'line continuation'}),
84 (u'not jinja}}',
85 [u'not', u'jinja}}'],
86 {u'_raw_params': u'not jinja}}'}),
87 (u'a={{multiline\njinja}}',
88 [u'a={{multiline\njinja}}'],
89 {u'a': u'{{multiline\njinja}}'}),
90 (u'a={{jinja}}',
91 [u'a={{jinja}}'],
92 {u'a': u'{{jinja}}'}),
93 (u'a={{ jinja }}',
94 [u'a={{ jinja }}'],
95 {u'a': u'{{ jinja }}'}),
96 (u'a={% jinja %}',
97 [u'a={% jinja %}'],
98 {u'a': u'{% jinja %}'}),
99 (u'a={# jinja #}',
100 [u'a={# jinja #}'],
101 {u'a': u'{# jinja #}'}),
102 (u'a="{{jinja}}"',
103 [u'a="{{jinja}}"'],
104 {u'a': u'{{jinja}}'}),
105 (u'a={{ jinja }}{{jinja2}}',
106 [u'a={{ jinja }}{{jinja2}}'],
107 {u'a': u'{{ jinja }}{{jinja2}}'}),
108 (u'a="{{ jinja }}{{jinja2}}"',
109 [u'a="{{ jinja }}{{jinja2}}"'],
110 {u'a': u'{{ jinja }}{{jinja2}}'}),
111 (u'a={{jinja}} b={{jinja2}}',
112 [u'a={{jinja}}', u'b={{jinja2}}'],
113 {u'a': u'{{jinja}}', u'b': u'{{jinja2}}'}),
114 (u'a="{{jinja}}\n" b="{{jinja2}}\n"',
115 [u'a="{{jinja}}\n"', u'b="{{jinja2}}\n"'],
116 {u'a': u'{{jinja}}\n', u'b': u'{{jinja2}}\n'}),
117 (u'a="café eñyei"',
118 [u'a="café eñyei"'],
119 {u'a': u'café eñyei'}),
120 (u'a=café b=eñyei',
121 [u'a=café', u'b=eñyei'],
122 {u'a': u'café', u'b': u'eñyei'}),
123 (u'a={{ foo | some_filter(\' \', " ") }} b=bar',
124 [u'a={{ foo | some_filter(\' \', " ") }}', u'b=bar'],
125 {u'a': u'{{ foo | some_filter(\' \', " ") }}', u'b': u'bar'}),
126 (u'One\n Two\n Three\n',
127 [u'One\n ', u'Two\n ', u'Three\n'],
128 {u'_raw_params': u'One\n Two\n Three\n'}),
129 (u'\nOne\n Two\n Three\n',
130 [u'\n', u'One\n ', u'Two\n ', u'Three\n'],
131 {u'_raw_params': u'\nOne\n Two\n Three\n'}),
132 )
133
134 PARSE_KV_CHECK_RAW = (
135 (u'raw=yes', {u'_raw_params': u'raw=yes'}),
136 (u'creates=something', {u'creates': u'something'}),
137 )
138
139 PARSER_ERROR = (
140 '"',
141 "'",
142 '{{',
143 '{%',
144 '{#',
145 )
146
147 SPLIT_ARGS = tuple((test[0], test[1]) for test in SPLIT_DATA)
148 PARSE_KV = tuple((test[0], test[2]) for test in SPLIT_DATA)
149
150
151 @pytest.mark.parametrize("args, expected", SPLIT_ARGS, ids=[str(arg[0]) for arg in SPLIT_ARGS])
152 def test_split_args(args, expected):
153 assert split_args(args) == expected
154
155
156 @pytest.mark.parametrize("args, expected", PARSE_KV, ids=[str(arg[0]) for arg in PARSE_KV])
157 def test_parse_kv(args, expected):
158 assert parse_kv(args) == expected
159
160
161 @pytest.mark.parametrize("args, expected", PARSE_KV_CHECK_RAW, ids=[str(arg[0]) for arg in PARSE_KV_CHECK_RAW])
162 def test_parse_kv_check_raw(args, expected):
163 assert parse_kv(args, check_raw=True) == expected
164
165
166 @pytest.mark.parametrize("args", PARSER_ERROR)
167 def test_split_args_error(args):
168 with pytest.raises(AnsibleParserError):
169 split_args(args)
170
171
172 @pytest.mark.parametrize("args", PARSER_ERROR)
173 def test_parse_kv_error(args):
174 with pytest.raises(AnsibleParserError):
175 parse_kv(args)