Debian Codegraph @

tests/parser_context.py

raw
1 import copy
2
3 from pytest import raises
4
5 from invoke.parser import Argument, Context
6 from invoke.tasks import task
7 from invoke.collection import Collection
8
9
10 class Context_:
11 "ParserContext" # meh
12
13 def may_have_a_name(self):
14 c = Context(name="taskname")
15 assert c.name == "taskname"
16
17 def may_have_aliases(self):
18 c = Context(name="realname", aliases=("othername", "yup"))
19 assert "othername" in c.aliases
20
21 def may_give_arg_list_at_init_time(self):
22 a1 = Argument("foo")
23 a2 = Argument("bar")
24 c = Context(name="name", args=(a1, a2))
25 assert c.args["foo"] is a1
26
27 # TODO: reconcile this sort of test organization with the .flags oriented
28 # tests within 'add_arg'. Some of this behavior is technically driven by
29 # add_arg.
30 class args:
31 def setup_method(self):
32 self.c = Context(
33 args=(
34 Argument("foo"),
35 Argument(names=("bar", "biz")),
36 Argument("baz", attr_name="wat"),
37 )
38 )
39
40 def exposed_as_dict(self):
41 assert "foo" in self.c.args.keys()
42
43 def exposed_as_Lexicon(self):
44 assert self.c.args.bar == self.c.args["bar"]
45
46 def args_dict_includes_all_arg_names(self):
47 for x in ("foo", "bar", "biz"):
48 assert x in self.c.args
49
50 def argument_attr_names_appear_in_args_but_not_flags(self):
51 # Both appear as "Python-facing" args
52 for x in ("baz", "wat"):
53 assert x in self.c.args
54 # But attr_name is for Python access only and isn't shown to the
55 # parser.
56 assert "wat" not in self.c.flags
57
58 class add_arg:
59 def setup_method(self):
60 self.c = Context()
61
62 def can_take_Argument_instance(self):
63 a = Argument(names=("foo",))
64 self.c.add_arg(a)
65 assert self.c.args["foo"] is a
66
67 def can_take_name_arg(self):
68 self.c.add_arg("foo")
69 assert "foo" in self.c.args
70
71 def can_take_kwargs_for_single_Argument(self):
72 self.c.add_arg(names=("foo", "bar"))
73 assert "foo" in self.c.args and "bar" in self.c.args
74
75 def raises_ValueError_on_duplicate(self):
76 self.c.add_arg(names=("foo", "bar"))
77 with raises(ValueError):
78 self.c.add_arg(name="bar")
79
80 def adds_flaglike_name_to_dot_flags(self):
81 "adds flaglike name to .flags"
82 self.c.add_arg("foo")
83 assert "--foo" in self.c.flags
84
85 def adds_all_names_to_dot_flags(self):
86 "adds all names to .flags"
87 self.c.add_arg(names=("foo", "bar"))
88 assert "--foo" in self.c.flags
89 assert "--bar" in self.c.flags
90
91 def adds_true_bools_to_inverse_flags(self):
92 self.c.add_arg(name="myflag", default=True, kind=bool)
93 assert "--myflag" in self.c.flags
94 assert "--no-myflag" in self.c.inverse_flags
95 assert self.c.inverse_flags["--no-myflag"] == "--myflag"
96
97 def inverse_flags_works_right_with_task_driven_underscored_names(self):
98 # Use a Task here instead of creating a raw argument, we're partly
99 # testing Task.get_arguments()' transform of underscored names
100 # here. Yes that makes this an integration test, but it's nice to
101 # test it here at this level & not just in cli tests.
102 @task
103 def mytask(c, underscored_option=True):
104 pass
105
106 self.c.add_arg(mytask.get_arguments()[0])
107 flags = self.c.inverse_flags["--no-underscored-option"]
108 assert flags == "--underscored-option"
109
110 def turns_single_character_names_into_short_flags(self):
111 self.c.add_arg("f")
112 assert "-f" in self.c.flags
113 assert "--f" not in self.c.flags
114
115 def adds_positional_args_to_positional_args(self):
116 self.c.add_arg(name="pos", positional=True)
117 assert self.c.positional_args[0].name == "pos"
118
119 def positional_args_empty_when_none_given(self):
120 assert len(self.c.positional_args) == 0
121
122 def positional_args_filled_in_order(self):
123 self.c.add_arg(name="pos1", positional=True)
124 assert self.c.positional_args[0].name == "pos1"
125 self.c.add_arg(name="abc", positional=True)
126 assert self.c.positional_args[1].name == "abc"
127
128 def positional_arg_modifications_affect_args_copy(self):
129 self.c.add_arg(name="hrm", positional=True)
130 assert self.c.args["hrm"].value == self.c.positional_args[0].value
131 self.c.positional_args[0].value = 17
132 assert self.c.args["hrm"].value == self.c.positional_args[0].value
133
134 class deepcopy:
135 "__deepcopy__"
136
137 def setup_method(self):
138 self.arg = Argument("--boolean")
139 self.orig = Context(
140 name="mytask", args=(self.arg,), aliases=("othername",)
141 )
142 self.new = copy.deepcopy(self.orig)
143
144 def returns_correct_copy(self):
145 assert self.new is not self.orig
146 assert self.new.name == "mytask"
147 assert "othername" in self.new.aliases
148
149 def includes_arguments(self):
150 assert len(self.new.args) == 1
151 assert self.new.args["--boolean"] is not self.arg
152
153 def modifications_to_copied_arguments_do_not_touch_originals(self):
154 new_arg = self.new.args["--boolean"]
155 new_arg.value = True
156 assert new_arg.value
157 assert not self.arg.value
158
159 class help_for:
160 def setup_method(self):
161 # Normal, non-task/collection related Context
162 self.vanilla = Context(
163 args=(Argument("foo"), Argument("bar", help="bar the baz"))
164 )
165
166 # Task/Collection generated Context
167 # (will expose flags n such)
168 @task(help={"otherarg": "other help"}, optional=["optval"])
169 def mytask(c, myarg, otherarg, optval, intval=5):
170 pass
171
172 col = Collection(mytask)
173 self.tasked = col.to_contexts()[0]
174
175 def raises_ValueError_for_non_flag_values(self):
176 with raises(ValueError):
177 self.vanilla.help_for("foo")
178
179 def vanilla_no_helpstr(self):
180 assert self.vanilla.help_for("--foo") == ("--foo=STRING", "")
181
182 def vanilla_with_helpstr(self):
183 result = self.vanilla.help_for("--bar")
184 assert result == ("--bar=STRING", "bar the baz")
185
186 def task_driven_with_helpstr(self):
187 result = self.tasked.help_for("--otherarg")
188 assert result == ("-o STRING, --otherarg=STRING", "other help")
189
190 # Yes, the next 3 tests are identical in form, but technically they
191 # test different behaviors. HERPIN' AN' DERPIN'
192 def task_driven_no_helpstr(self):
193 result = self.tasked.help_for("--myarg")
194 assert result == ("-m STRING, --myarg=STRING", "")
195
196 def short_form_before_long_form(self):
197 result = self.tasked.help_for("--myarg")
198 assert result == ("-m STRING, --myarg=STRING", "")
199
200 def equals_sign_for_long_form_only(self):
201 result = self.tasked.help_for("--myarg")
202 assert result == ("-m STRING, --myarg=STRING", "")
203
204 def kind_to_placeholder_map(self):
205 # Strings
206 helpfor = self.tasked.help_for("--myarg")
207 assert helpfor == ("-m STRING, --myarg=STRING", "")
208 # Ints
209 helpfor = self.tasked.help_for("--intval")
210 assert helpfor == ("-i INT, --intval=INT", "")
211 # TODO: others
212
213 def shortflag_inputs_work_too(self):
214 m = self.tasked.help_for("-m")
215 myarg = self.tasked.help_for("--myarg")
216 assert m == myarg
217
218 def optional_values_use_brackets(self):
219 result = self.tasked.help_for("--optval")
220 assert result == ("-p [STRING], --optval[=STRING]", "")
221
222 def underscored_args(self):
223 c = Context(args=(Argument("i_have_underscores", help="yup"),))
224 result = c.help_for("--i-have-underscores")
225 assert result == ("--i-have-underscores=STRING", "yup")
226
227 def true_default_args(self):
228 c = Context(args=(Argument("truthy", kind=bool, default=True),))
229 assert c.help_for("--truthy") == ("--[no-]truthy", "")
230
231 class help_tuples:
232 def returns_list_of_help_tuples(self):
233 # Walks own list of flags/args, ensures resulting map to help_for()
234 # TODO: consider redoing help_for to be more flexible on input --
235 # arg value or flag; or even Argument objects. ?
236 @task(help={"otherarg": "other help"})
237 def mytask(c, myarg, otherarg):
238 pass
239
240 c = Collection(mytask).to_contexts()[0]
241 expected = [c.help_for("--myarg"), c.help_for("--otherarg")]
242 assert c.help_tuples() == expected
243
244 def _assert_order(self, name_tuples, expected_flag_order):
245 c = Context(args=[Argument(names=x) for x in name_tuples])
246 expected = [c.help_for(x) for x in expected_flag_order]
247 assert c.help_tuples() == expected
248
249 def sorts_alphabetically_by_shortflag_first(self):
250 # Where shortflags exist, they take precedence
251 self._assert_order(
252 [("zarg", "a"), ("arg", "z")], ["--zarg", "--arg"]
253 )
254
255 def case_ignored_during_sorting(self):
256 self._assert_order(
257 [("a",), ("B",)],
258 # In raw cmp() uppercase would come before lowercase,
259 # and we'd get ['-B', '-a']
260 ["-a", "-B"],
261 )
262
263 def lowercase_wins_when_values_identical_otherwise(self):
264 self._assert_order([("V",), ("v",)], ["-v", "-V"])
265
266 def sorts_alphabetically_by_longflag_when_no_shortflag(self):
267 # Where no shortflag, sorts by longflag
268 self._assert_order(
269 [("otherarg",), ("longarg",)], ["--longarg", "--otherarg"]
270 )
271
272 def sorts_heterogenous_help_output_with_longflag_only_options_first(
273 self,
274 ): # noqa
275 # When both of the above mix, long-flag-only options come first.
276 # E.g.:
277 # --alpha
278 # --beta
279 # -a, --aaaagh
280 # -b, --bah
281 # -c
282 self._assert_order(
283 [("c",), ("a", "aaagh"), ("b", "bah"), ("beta",), ("alpha",)],
284 ["--alpha", "--beta", "-a", "-b", "-c"],
285 )
286
287 def mixed_corelike_options(self):
288 self._assert_order(
289 [
290 ("V", "version"),
291 ("c", "collection"),
292 ("h", "help"),
293 ("l", "list"),
294 ("r", "root"),
295 ],
296 ["-c", "-h", "-l", "-r", "-V"],
297 )
298
299 class missing_positional_args:
300 def represents_positional_args_missing_values(self):
301 arg1 = Argument("arg1", positional=True)
302 arg2 = Argument("arg2", positional=False)
303 arg3 = Argument("arg3", positional=True)
304 c = Context(name="foo", args=(arg1, arg2, arg3))
305 assert c.missing_positional_args == [arg1, arg3]
306 c.positional_args[0].value = "wat"
307 assert c.missing_positional_args == [arg3]
308 c.positional_args[1].value = "hrm"
309 assert c.missing_positional_args == []
310
311 class str:
312 "__str__"
313
314 def with_no_args_output_is_simple(self):
315 assert str(Context("foo")) == "<parser/Context 'foo'>"
316
317 def args_show_as_repr(self):
318 string = str(Context("bar", args=[Argument("arg1")]))
319 assert (
320 string == "<parser/Context 'bar': {'arg1': <Argument: arg1>}>"
321 ) # noqa