Debian Codegraph @

tests/executor.py

raw
1 from unittest.mock import Mock
2
3 import pytest
4 from _util import expect
5
6 from invoke import Collection, Config, Context, Executor, Task, call, task
7 from invoke.parser import ParserContext, ParseResult
8
9 # TODO: why does this not work as a decorator? probably relaxed's fault - but
10 # how?
11 pytestmark = pytest.mark.usefixtures("integration")
12
13
14 class Executor_:
15 def setup_method(self):
16 self.task1 = Task(Mock(return_value=7))
17 self.task2 = Task(Mock(return_value=10), pre=[self.task1])
18 self.task3 = Task(Mock(), pre=[self.task1])
19 self.task4 = Task(Mock(return_value=15), post=[self.task1])
20 self.contextualized = Task(Mock())
21 coll = Collection()
22 coll.add_task(self.task1, name="task1")
23 coll.add_task(self.task2, name="task2")
24 coll.add_task(self.task3, name="task3")
25 coll.add_task(self.task4, name="task4")
26 coll.add_task(self.contextualized, name="contextualized")
27 self.executor = Executor(collection=coll)
28
29 class init:
30 "__init__"
31
32 def allows_collection_and_config(self):
33 coll = Collection()
34 conf = Config()
35 e = Executor(collection=coll, config=conf)
36 assert e.collection is coll
37 assert e.config is conf
38
39 def uses_blank_config_by_default(self):
40 e = Executor(collection=Collection())
41 assert isinstance(e.config, Config)
42
43 def can_grant_access_to_core_arg_parse_result(self):
44 c = ParseResult([ParserContext(name="mytask")])
45 e = Executor(collection=Collection(), core=c)
46 assert e.core is c
47 # Sanity test of real-world access/usage
48 assert len(e.core) == 1
49 assert e.core[0].name == "mytask"
50 assert len(e.core[0].args) == 0
51
52 def core_arg_parse_result_defaults_to_empty_ParseResult(self):
53 assert Executor(collection=Collection()).core == ParseResult()
54
55 class execute:
56 def base_case(self):
57 self.executor.execute("task1")
58 assert self.task1.body.called
59
60 def kwargs(self):
61 k = {"foo": "bar"}
62 self.executor.execute(("task1", k))
63 args = self.task1.body.call_args[0]
64 kwargs = self.task1.body.call_args[1]
65 assert isinstance(args[0], Context)
66 assert len(args) == 1
67 assert kwargs["foo"] == "bar"
68
69 def contextualized_tasks_are_given_parser_context_arg(self):
70 self.executor.execute("contextualized")
71 args = self.contextualized.body.call_args[0]
72 assert len(args) == 1
73 assert isinstance(args[0], Context)
74
75 def default_tasks_called_when_no_tasks_specified(self):
76 # NOTE: when no tasks AND no default, Program will print global
77 # help. We just won't do anything at all, which is fine for now.
78 task = Task(Mock("default-task"))
79 coll = Collection()
80 coll.add_task(task, name="mytask", default=True)
81 executor = Executor(collection=coll)
82 executor.execute()
83 args = task.body.call_args[0]
84 assert isinstance(args[0], Context)
85 assert len(args) == 1
86
87 class basic_pre_post:
88 "basic pre/post task functionality"
89
90 def pre_tasks(self):
91 self.executor.execute("task2")
92 assert self.task1.body.call_count == 1
93
94 def post_tasks(self):
95 self.executor.execute("task4")
96 assert self.task1.body.call_count == 1
97
98 def calls_default_to_empty_args_always(self):
99 pre_body, post_body = Mock(), Mock()
100 t1 = Task(pre_body)
101 t2 = Task(post_body)
102 t3 = Task(Mock(), pre=[t1], post=[t2])
103 e = Executor(collection=Collection(t1=t1, t2=t2, t3=t3))
104 e.execute(("t3", {"something": "meh"}))
105 for body in (pre_body, post_body):
106 args = body.call_args[0]
107 assert len(args) == 1
108 assert isinstance(args[0], Context)
109
110 def _call_objs(self):
111 # Setup
112 pre_body, post_body = Mock(), Mock()
113 t1 = Task(pre_body)
114 t2 = Task(post_body)
115 t3 = Task(
116 Mock(),
117 pre=[call(t1, 5, foo="bar")],
118 post=[call(t2, 7, biz="baz")],
119 )
120 c = Collection(t1=t1, t2=t2, t3=t3)
121 e = Executor(collection=c)
122 e.execute("t3")
123 # Pre-task asserts
124 args, kwargs = pre_body.call_args
125 assert kwargs == {"foo": "bar"}
126 assert isinstance(args[0], Context)
127 assert args[1] == 5
128 # Post-task asserts
129 args, kwargs = post_body.call_args
130 assert kwargs == {"biz": "baz"}
131 assert isinstance(args[0], Context)
132 assert args[1] == 7
133
134 def call_objs_play_well_with_context_args(self):
135 self._call_objs()
136
137 class deduping_and_chaining:
138 def chaining_is_depth_first(self):
139 expect(
140 "-c depth_first deploy",
141 out="""
142 Cleaning HTML
143 Cleaning .tar.gz files
144 Cleaned everything
145 Making directories
146 Building
147 Deploying
148 Preparing for testing
149 Testing
150 """.lstrip(),
151 )
152
153 def _expect(self, args, expected):
154 expect("-c integration {}".format(args), out=expected.lstrip())
155
156 class adjacent_hooks:
157 def deduping(self):
158 self._expect(
159 "biz",
160 """
161 foo
162 bar
163 biz
164 post1
165 post2
166 """,
167 )
168
169 def no_deduping(self):
170 self._expect(
171 "--no-dedupe biz",
172 """
173 foo
174 foo
175 bar
176 biz
177 post1
178 post2
179 post2
180 """,
181 )
182
183 class non_adjacent_hooks:
184 def deduping(self):
185 self._expect(
186 "boz",
187 """
188 foo
189 bar
190 boz
191 post2
192 post1
193 """,
194 )
195
196 def no_deduping(self):
197 self._expect(
198 "--no-dedupe boz",
199 """
200 foo
201 bar
202 foo
203 boz
204 post2
205 post1
206 post2
207 """,
208 )
209
210 # AKA, a (foo) (foo -> bar) scenario arising from foo + bar
211 class adjacent_top_level_tasks:
212 def deduping(self):
213 self._expect(
214 "foo bar",
215 """
216 foo
217 bar
218 """,
219 )
220
221 def no_deduping(self):
222 self._expect(
223 "--no-dedupe foo bar",
224 """
225 foo
226 foo
227 bar
228 """,
229 )
230
231 # AKA (foo -> bar) (foo)
232 class non_adjacent_top_level_tasks:
233 def deduping(self):
234 self._expect(
235 "foo bar",
236 """
237 foo
238 bar
239 """,
240 )
241
242 def no_deduping(self):
243 self._expect(
244 "--no-dedupe foo bar",
245 """
246 foo
247 foo
248 bar
249 """,
250 )
251
252 def deduping_treats_different_calls_to_same_task_differently(self):
253 body = Mock()
254 t1 = Task(body)
255 pre = [call(t1, 5), call(t1, 7), call(t1, 5)]
256 t2 = Task(Mock(), pre=pre)
257 c = Collection(t1=t1, t2=t2)
258 e = Executor(collection=c)
259 e.execute("t2")
260 # Does not call the second t1(5)
261 param_list = []
262 for body_call in body.call_args_list:
263 assert isinstance(body_call[0][0], Context)
264 param_list.append(body_call[0][1])
265 assert set(param_list) == {5, 7}
266
267 class collection_driven_config:
268 "Collection-driven config concerns"
269
270 def hands_collection_configuration_to_context(self):
271 @task
272 def mytask(c):
273 assert c.my_key == "value"
274
275 c = Collection(mytask)
276 c.configure({"my_key": "value"})
277 Executor(collection=c).execute("mytask")
278
279 def hands_task_specific_configuration_to_context(self):
280 @task
281 def mytask(c):
282 assert c.my_key == "value"
283
284 @task
285 def othertask(c):
286 assert c.my_key == "othervalue"
287
288 inner1 = Collection("inner1", mytask)
289 inner1.configure({"my_key": "value"})
290 inner2 = Collection("inner2", othertask)
291 inner2.configure({"my_key": "othervalue"})
292 c = Collection(inner1, inner2)
293 e = Executor(collection=c)
294 e.execute("inner1.mytask", "inner2.othertask")
295
296 def subcollection_config_works_with_default_tasks(self):
297 @task(default=True)
298 def mytask(c):
299 assert c.my_key == "value"
300
301 # Sets up a task "known as" sub.mytask which may be called as
302 # just 'sub' due to being default.
303 sub = Collection("sub", mytask=mytask)
304 sub.configure({"my_key": "value"})
305 main = Collection(sub=sub)
306 # Execute via collection default 'task' name.
307 Executor(collection=main).execute("sub")
308
309 class returns_return_value_of_specified_task:
310 def base_case(self):
311 assert self.executor.execute("task1") == {self.task1: 7}
312
313 def with_pre_tasks(self):
314 result = self.executor.execute("task2")
315 assert result == {self.task1: 7, self.task2: 10}
316
317 def with_post_tasks(self):
318 result = self.executor.execute("task4")
319 assert result == {self.task1: 7, self.task4: 15}
320
321 class autoprinting:
322 def defaults_to_off_and_no_output(self):
323 expect("-c autoprint nope", out="")
324
325 def prints_return_value_to_stdout_when_on(self):
326 expect("-c autoprint yup", out="It's alive!\n")
327
328 def prints_return_value_to_stdout_when_on_and_in_collection(self):
329 expect("-c autoprint sub.yup", out="It's alive!\n")
330
331 def does_not_fire_on_pre_tasks(self):
332 expect("-c autoprint pre-check", out="")
333
334 def does_not_fire_on_post_tasks(self):
335 expect("-c autoprint post-check", out="")
336
337 class inter_task_context_and_config_sharing:
338 def context_is_new_but_config_is_same(self):
339 @task
340 def task1(c):
341 return c
342
343 @task
344 def task2(c):
345 return c
346
347 coll = Collection(task1, task2)
348 ret = Executor(collection=coll).execute("task1", "task2")
349 c1 = ret[task1]
350 c2 = ret[task2]
351 assert c1 is not c2
352 # TODO: eventually we may want to change this again, as long as the
353 # effective values within the config are still matching...? Ehh
354 assert c1.config is c2.config
355
356 def new_config_data_is_preserved_between_tasks(self):
357 @task
358 def task1(c):
359 c.foo = "bar"
360 # NOTE: returned for test inspection, not as mechanism of
361 # sharing data!
362 return c
363
364 @task
365 def task2(c):
366 return c
367
368 coll = Collection(task1, task2)
369 ret = Executor(collection=coll).execute("task1", "task2")
370 c2 = ret[task2]
371 assert "foo" in c2.config
372 assert c2.foo == "bar"
373
374 def config_mutation_is_preserved_between_tasks(self):
375 @task
376 def task1(c):
377 c.config.run.echo = True
378 # NOTE: returned for test inspection, not as mechanism of
379 # sharing data!
380 return c
381
382 @task
383 def task2(c):
384 return c
385
386 coll = Collection(task1, task2)
387 ret = Executor(collection=coll).execute("task1", "task2")
388 c2 = ret[task2]
389 assert c2.config.run.echo is True
390
391 def config_deletion_is_preserved_between_tasks(self):
392 @task
393 def task1(c):
394 del c.config.run.echo
395 # NOTE: returned for test inspection, not as mechanism of
396 # sharing data!
397 return c
398
399 @task
400 def task2(c):
401 return c
402
403 coll = Collection(task1, task2)
404 ret = Executor(collection=coll).execute("task1", "task2")
405 c2 = ret[task2]
406 assert "echo" not in c2.config.run