From b8cf8c2b638107554d87a95306f29d09d962bf4f Mon Sep 17 00:00:00 2001 From: Dmitry Tyzhnenko Date: Thu, 30 Mar 2023 18:17:46 +0300 Subject: [PATCH] Add pre and post task lists into help output Issue: #860 --- invoke/program.py | 23 ++++++++++++++++++++++ tests/_support/decorators.py | 4 ++-- tests/program.py | 38 ++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 2 deletions(-) diff --git a/invoke/program.py b/invoke/program.py index c7e5cd004..cbbb5071d 100644 --- a/invoke/program.py +++ b/invoke/program.py @@ -781,6 +781,8 @@ def print_task_help(self, name: str) -> None: ctx = self.parser.contexts[name] tuples = ctx.help_tuples() docstring = inspect.getdoc(self.collection[name]) + pre_tasks = self.collection[name].pre + post_tasks = self.collection[name].post header = "Usage: {} [--core-opts] {} {}[other tasks here ...]" opts = "[--options] " if tuples else "" print(header.format(self.binary, name, opts)) @@ -804,6 +806,27 @@ def print_task_help(self, name: str) -> None: print(self.leading_indent + "none") print("") + print("Pre-tasks:") + if pre_tasks: + for pre in pre_tasks: + print( + self.leading_indent + self.collection.transform(pre.name) + ) + print("") + else: + print(self.leading_indent + "none") + print("") + print("Post-tasks:") + if post_tasks: + for post in post_tasks: + print( + self.leading_indent + self.collection.transform(post.name) + ) + print("") + else: + print(self.leading_indent + "none") + print("") + def list_tasks(self) -> None: # Short circuit if no tasks to show (Collection now implements bool) focus = self.scoped_collection diff --git a/tests/_support/decorators.py b/tests/_support/decorators.py index 320f63153..212d7e3c3 100644 --- a/tests/_support/decorators.py +++ b/tests/_support/decorators.py @@ -21,7 +21,7 @@ def foo2(c): pass -@task +@task(post=[foo2]) def foo3(c): """Foo the other bar: @@ -32,7 +32,7 @@ def foo3(c): pass -@task(default=True) +@task(default=True, pre=[foo3]) def biz(c): pass diff --git a/tests/program.py b/tests/program.py index 2a6d4f01c..f1b98d4ca 100644 --- a/tests/program.py +++ b/tests/program.py @@ -308,6 +308,7 @@ def copying_from_task_context_does_not_set_empty_list_values(self): # .value = actually ends up creating a # list-of-lists. p = Program() + # Set up core-args parser context with an iterable arg that hasn't # seen any value yet def filename_args(): @@ -643,6 +644,12 @@ def prints_help_for_task_only(self): -h STRING, --why=STRING Motive -w STRING, --who=STRING Who to punch +Pre-tasks: + none + +Post-tasks: + none + """.lstrip() for flag in ["-h", "--help"]: expect("-c decorators {} punch".format(flag), out=expected) @@ -657,6 +664,12 @@ def works_for_unparameterized_tasks(self): Options: none +Pre-tasks: + foo3 + +Post-tasks: + none + """.lstrip() expect("-c decorators -h biz", out=expected) @@ -676,6 +689,12 @@ def displays_docstrings_if_given(self): Options: none +Pre-tasks: + none + +Post-tasks: + none + """.lstrip() expect("-c decorators -h foo", out=expected) @@ -693,6 +712,12 @@ def dedents_correctly(self): Options: none +Pre-tasks: + none + +Post-tasks: + none + """.lstrip() expect("-c decorators -h foo2", out=expected) @@ -710,6 +735,12 @@ def dedents_correctly_for_alt_docstring_style(self): Options: none +Pre-tasks: + none + +Post-tasks: + foo2 + """.lstrip() expect("-c decorators -h foo3", out=expected) @@ -726,6 +757,12 @@ def exits_after_printing(self): -h STRING, --why=STRING Motive -w STRING, --who=STRING Who to punch +Pre-tasks: + none + +Post-tasks: + none + """.lstrip() expect("-c decorators -h punch --list", out=expected) @@ -1374,6 +1411,7 @@ def env_vars_load_with_prefix(self, monkeypatch): def env_var_prefix_can_be_overridden(self, monkeypatch): monkeypatch.setenv("MYAPP_RUN_HIDE", "both") + # This forces the execution stuff, including Executor, to run # NOTE: it's not really possible to rework the impl so this test is # cleaner - tasks require per-task/per-collection config, which can