| 1 |
import os |
| 2 |
import sys |
| 3 |
|
| 4 |
from invoke import Program, task, Collection |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
from _util import expect, trap, ROOT |
| 9 |
|
| 10 |
|
| 11 |
pytestmark = pytest.mark.usefixtures("integration") |
| 12 |
|
| 13 |
|
| 14 |
@trap |
| 15 |
def _complete(invocation, collection=None, **kwargs): |
| 16 |
colstr = "" |
| 17 |
if collection: |
| 18 |
colstr = "-c {}".format(collection) |
| 19 |
command = "inv --complete {0} -- inv {0} {1}".format(colstr, invocation) |
| 20 |
Program(**kwargs).run(command, exit=False) |
| 21 |
return sys.stdout.getvalue() |
| 22 |
|
| 23 |
|
| 24 |
# TODO: remove in favor of direct asserts, needs non shite way of getting at |
| 25 |
# stderr instead of just stdout. |
| 26 |
def _assert_contains(haystack, needle): |
| 27 |
assert needle in haystack |
| 28 |
|
| 29 |
|
| 30 |
class CompletionScriptPrinter: |
| 31 |
""" |
| 32 |
Printing the completion script |
| 33 |
""" |
| 34 |
|
| 35 |
def setup_method(self): |
| 36 |
self.prev_cwd = os.getcwd() |
| 37 |
# Chdir to system root to (hopefully) avoid any tasks.py. This will |
| 38 |
# prove that --print-completion-script works w/o nearby tasks. |
| 39 |
os.chdir(ROOT) |
| 40 |
|
| 41 |
def teardown_method(self): |
| 42 |
os.chdir(self.prev_cwd) |
| 43 |
|
| 44 |
def only_accepts_certain_shells(self): |
| 45 |
expect( |
| 46 |
"--print-completion-script", |
| 47 |
err="needed value and was not given one", |
| 48 |
test=_assert_contains, |
| 49 |
) |
| 50 |
expect( |
| 51 |
"--print-completion-script bla", |
| 52 |
# NOTE: this needs updating when the real world changes, just like |
| 53 |
# eg our --help output tests. That's OK & better than just |
| 54 |
# reimplementing the code under test here. |
| 55 |
err='Completion for shell "bla" not supported (options are: bash, fish, zsh).', # noqa |
| 56 |
test=_assert_contains, |
| 57 |
) |
| 58 |
|
| 59 |
def prints_for_custom_binary_names(self): |
| 60 |
out, err = expect( |
| 61 |
"myapp --print-completion-script zsh", |
| 62 |
program=Program(binary_names=["mya", "myapp"]), |
| 63 |
invoke=False, |
| 64 |
) |
| 65 |
# Combines some sentinels from vanilla test, with checks that it's |
| 66 |
# really replacing 'invoke' with desired binary names |
| 67 |
assert "_complete_mya() {" in out |
| 68 |
assert "invoke" not in out |
| 69 |
assert " mya myapp" in out |
| 70 |
|
| 71 |
def default_binary_names_is_completing_argv_0(self): |
| 72 |
out, err = expect( |
| 73 |
"someappname --print-completion-script zsh", |
| 74 |
program=Program(binary_names=None), |
| 75 |
invoke=False, |
| 76 |
) |
| 77 |
assert "_complete_someappname() {" in out |
| 78 |
assert " someappname" in out |
| 79 |
|
| 80 |
def bash_works(self): |
| 81 |
out, err = expect( |
| 82 |
"someappname --print-completion-script bash", invoke=False |
| 83 |
) |
| 84 |
assert "_complete_someappname() {" in out |
| 85 |
assert "complete -F" in out |
| 86 |
for line in out.splitlines(): |
| 87 |
if line.startswith("complete -F"): |
| 88 |
assert line.endswith(" someappname") |
| 89 |
|
| 90 |
def fish_works(self): |
| 91 |
out, err = expect( |
| 92 |
"someappname --print-completion-script fish", invoke=False |
| 93 |
) |
| 94 |
assert "function __complete_someappname" in out |
| 95 |
assert "complete --command someappname" in out |
| 96 |
|
| 97 |
|
| 98 |
class ShellCompletion: |
| 99 |
""" |
| 100 |
Shell tab-completion behavior |
| 101 |
""" |
| 102 |
|
| 103 |
def no_input_means_just_task_names(self): |
| 104 |
expect("-c simple_ns_list --complete", out="z-toplevel\na.b.subtask\n") |
| 105 |
|
| 106 |
def custom_binary_name_completes(self): |
| 107 |
expect( |
| 108 |
"myapp -c integration --complete -- ba", |
| 109 |
program=Program(binary="myapp"), |
| 110 |
invoke=False, |
| 111 |
out="bar", |
| 112 |
test=_assert_contains, |
| 113 |
) |
| 114 |
|
| 115 |
def aliased_custom_binary_name_completes(self): |
| 116 |
for used_binary in ("my", "myapp"): |
| 117 |
expect( |
| 118 |
"{0} -c integration --complete -- ba".format(used_binary), |
| 119 |
program=Program(binary="my[app]"), |
| 120 |
invoke=False, |
| 121 |
out="bar", |
| 122 |
test=_assert_contains, |
| 123 |
) |
| 124 |
|
| 125 |
def no_input_with_no_tasks_yields_empty_response(self): |
| 126 |
expect("-c empty --complete", out="") |
| 127 |
|
| 128 |
def task_name_completion_includes_aliases(self): |
| 129 |
for name in ("z\n", "toplevel"): |
| 130 |
assert name in _complete("", "alias_sorting") |
| 131 |
|
| 132 |
def top_level_with_dash_means_core_options(self): |
| 133 |
output = _complete("-") |
| 134 |
# No point mirroring all core options, just spot check a few |
| 135 |
for flag in ("--no-dedupe", "-d", "--debug", "-V", "--version"): |
| 136 |
assert "{}\n".format(flag) in output |
| 137 |
|
| 138 |
def bare_double_dash_shows_only_long_core_options(self): |
| 139 |
output = _complete("--") |
| 140 |
assert "--no-dedupe" in output |
| 141 |
assert "-V" not in output |
| 142 |
|
| 143 |
def task_names_only_complete_other_task_names(self): |
| 144 |
# Because only tokens starting with a dash should result in options. |
| 145 |
assert "print-name" in _complete("print-foo", "integration") |
| 146 |
|
| 147 |
def task_name_completion_includes_tasks_already_seen(self): |
| 148 |
# Because it's valid to call the same task >1 time. |
| 149 |
assert "print-foo" in _complete("print-foo", "integration") |
| 150 |
|
| 151 |
def per_task_flags_complete_with_single_dashes(self): |
| 152 |
for flag in ("--name", "-n"): |
| 153 |
assert flag in _complete("print-name -", "integration") |
| 154 |
|
| 155 |
def per_task_flags_complete_with_double_dashes(self): |
| 156 |
output = _complete("print-name --", "integration") |
| 157 |
assert "--name" in output |
| 158 |
assert "-n\n" not in output # newline because -n is in --name |
| 159 |
|
| 160 |
def flag_completion_includes_inverse_booleans(self): |
| 161 |
output = _complete("basic-bool -", "foo") |
| 162 |
assert "--no-mybool" in output |
| 163 |
|
| 164 |
def tasks_with_positional_args_complete_with_flags(self): |
| 165 |
# Because otherwise completing them is invalid anyways. |
| 166 |
# NOTE: this currently duplicates another test because this test cares |
| 167 |
# about a specific detail. |
| 168 |
output = _complete("print-name --", "integration") |
| 169 |
assert "--name" in output |
| 170 |
|
| 171 |
def core_flags_taking_values_have_no_completion_output(self): |
| 172 |
# So the shell's default completion is available. |
| 173 |
assert _complete("-f") == "" |
| 174 |
|
| 175 |
def per_task_flags_taking_values_have_no_completion_output(self): |
| 176 |
assert _complete("basic-arg --arg", "foo") == "" |
| 177 |
|
| 178 |
def core_bool_flags_have_task_name_completion(self): |
| 179 |
assert "mytask" in _complete("--echo", "foo") |
| 180 |
|
| 181 |
def per_task_bool_flags_have_task_name_completion(self): |
| 182 |
assert "mytask" in _complete("basic-bool --mybool", "foo") |
| 183 |
|
| 184 |
def core_partial_or_invalid_flags_print_all_flags(self): |
| 185 |
for flag in ("--echo", "--complete"): |
| 186 |
for given in ("--e", "--nope"): |
| 187 |
assert flag in _complete(given) |
| 188 |
|
| 189 |
def per_task_partial_or_invalid_flags_print_all_flags(self): |
| 190 |
for flag in ("--arg1", "--otherarg"): |
| 191 |
for given in ("--ar", "--nope"): |
| 192 |
completion = _complete("multiple-args {}".format(given), "foo") |
| 193 |
assert flag in completion |
| 194 |
|
| 195 |
@trap |
| 196 |
def completion_given_parser_by_program(self): |
| 197 |
# Trial case: means honoring config ignore_unknown_help option |
| 198 |
# (i.e. not exploding when any task in the target collection has unused |
| 199 |
# help keys; doesn't actually need the completion invocation to include |
| 200 |
# said task!) |
| 201 |
# NOTE: subclassing to mimic real world report case, and for ease of |
| 202 |
# shoving in a config override |
| 203 |
|
| 204 |
@task(help=dict(lol="nope")) |
| 205 |
def noboomplz(c): |
| 206 |
pass |
| 207 |
|
| 208 |
ns = Collection(noboomplz) |
| 209 |
|
| 210 |
class MyProgram(Program): |
| 211 |
def create_config(self): |
| 212 |
super().create_config() |
| 213 |
self.config.tasks.ignore_unknown_help = True |
| 214 |
|
| 215 |
MyProgram(namespace=ns).run("inv --complete -- inv noboom", exit=False) |
| 216 |
assert sys.stdout.getvalue().strip() == "noboomplz" |