| 1 |
from io import StringIO |
| 2 |
import pytest |
| 3 |
|
| 4 |
from doit.exceptions import InvalidCommand |
| 5 |
from doit.cmdparse import CmdOption |
| 6 |
from doit.plugin import PluginDict |
| 7 |
from doit.task import Task |
| 8 |
from doit.cmd_base import Command, DodoTaskLoader, TaskLoader2 |
| 9 |
from doit.cmd_completion import TabCompletion |
| 10 |
from doit.cmd_help import Help |
| 11 |
from .conftest import CmdFactory |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
class FakeLoader2(TaskLoader2): |
| 17 |
def load_doit_config(self): |
| 18 |
return {} |
| 19 |
|
| 20 |
def load_tasks(self, cmd, pos_args): |
| 21 |
task_list = [ |
| 22 |
Task("t1", None, ), |
| 23 |
Task("t2", None, task_dep=['t2:a'], has_subtask=True, ), |
| 24 |
Task("t2:a", None, subtask_of='t2'), |
| 25 |
] |
| 26 |
return task_list |
| 27 |
|
| 28 |
@pytest.fixture |
| 29 |
def commands(request): |
| 30 |
sub_cmds = {} |
| 31 |
sub_cmds['tabcompletion'] = TabCompletion |
| 32 |
sub_cmds['help'] = Help |
| 33 |
return PluginDict(sub_cmds) |
| 34 |
|
| 35 |
def test_invalid_shell_option(): |
| 36 |
cmd = CmdFactory(TabCompletion) |
| 37 |
pytest.raises(InvalidCommand, cmd.execute, |
| 38 |
{'shell':'another_shell', 'hardcode_tasks': False}, []) |
| 39 |
|
| 40 |
|
| 41 |
class TestCmdCompletionBash(object): |
| 42 |
|
| 43 |
def test_with_dodo__dynamic_tasks(self, commands): |
| 44 |
output = StringIO() |
| 45 |
cmd = CmdFactory(TabCompletion, task_loader=DodoTaskLoader(), |
| 46 |
outstream=output, cmds=commands) |
| 47 |
cmd.execute({'shell':'bash', 'hardcode_tasks': False}, []) |
| 48 |
got = output.getvalue() |
| 49 |
assert 'dodof' in got |
| 50 |
assert 't1' not in got |
| 51 |
assert 'tabcompletion' in got |
| 52 |
|
| 53 |
@pytest.mark.parametrize('loader_class', [FakeLoader2]) |
| 54 |
def test_no_dodo__hardcoded_tasks(self, commands, loader_class): |
| 55 |
output = StringIO() |
| 56 |
cmd = CmdFactory(TabCompletion, task_loader=loader_class(), |
| 57 |
outstream=output, cmds=commands) |
| 58 |
cmd.execute({'shell':'bash', 'hardcode_tasks': True}, []) |
| 59 |
got = output.getvalue() |
| 60 |
assert 'dodo.py' not in got |
| 61 |
assert 't1' in got |
| 62 |
|
| 63 |
def test_cmd_takes_file_args(self, commands): |
| 64 |
output = StringIO() |
| 65 |
cmd = CmdFactory(TabCompletion, task_loader=FakeLoader2(), |
| 66 |
outstream=output, cmds=commands) |
| 67 |
cmd.execute({'shell':'bash', 'hardcode_tasks': False}, []) |
| 68 |
got = output.getvalue() |
| 69 |
assert """help) |
| 70 |
COMPREPLY=( $(compgen -W "${tasks} ${sub_cmds}" -- $cur) ) |
| 71 |
return 0""" in got |
| 72 |
assert """tabcompletion) |
| 73 |
COMPREPLY=( $(compgen -f -- $cur) ) |
| 74 |
return 0""" in got |
| 75 |
|
| 76 |
|
| 77 |
class TestCmdCompletionZsh(object): |
| 78 |
|
| 79 |
def test_zsh_arg_line(self): |
| 80 |
opt1 = CmdOption({'name':'o1', 'default':'', 'help':'my desc'}) |
| 81 |
assert '' == TabCompletion._zsh_arg_line(opt1) |
| 82 |
|
| 83 |
opt2 = CmdOption({'name':'o2', 'default':'', 'help':'my desc', |
| 84 |
'short':'s'}) |
| 85 |
assert '"-s[my desc]" \\' == TabCompletion._zsh_arg_line(opt2) |
| 86 |
|
| 87 |
opt3 = CmdOption({'name':'o3', 'default':'', 'help':'my desc', |
| 88 |
'long':'lll'}) |
| 89 |
assert '"--lll[my desc]" \\' == TabCompletion._zsh_arg_line(opt3) |
| 90 |
|
| 91 |
opt4 = CmdOption({'name':'o4', 'default':'', 'help':'my desc [b]a', |
| 92 |
'short':'s', 'long':'lll'}) |
| 93 |
assert ('"(-s|--lll)"{-s,--lll}"[my desc [b\\]a]" \\' == |
| 94 |
TabCompletion._zsh_arg_line(opt4)) |
| 95 |
|
| 96 |
|
| 97 |
opt5 = CmdOption({'name':'o5', 'default':'', |
| 98 |
'help':'''my "des'c [b]a''', |
| 99 |
'short':'s', 'long':'lll'}) |
| 100 |
assert ('''"(-s|--lll)"{-s,--lll}"[my \\"des'c [b\\]a]" \\''' == |
| 101 |
TabCompletion._zsh_arg_line(opt5)) |
| 102 |
|
| 103 |
|
| 104 |
def test_cmd_arg_list(self): |
| 105 |
no_args = TabCompletion._zsh_arg_list(Command()) |
| 106 |
assert "'*::task:(($tasks))'" not in no_args |
| 107 |
assert "'::cmd:(($commands))'" not in no_args |
| 108 |
|
| 109 |
class CmdTakeTasks(Command): |
| 110 |
doc_usage = '[TASK ...]' |
| 111 |
with_task_args = TabCompletion._zsh_arg_list(CmdTakeTasks()) |
| 112 |
assert "'*::task:(($tasks))'" in with_task_args |
| 113 |
assert "'::cmd:(($commands))'" not in with_task_args |
| 114 |
|
| 115 |
class CmdTakeCommands(Command): |
| 116 |
doc_usage = '[COMMAND ...]' |
| 117 |
with_cmd_args = TabCompletion._zsh_arg_list(CmdTakeCommands()) |
| 118 |
assert "'*::task:(($tasks))'" not in with_cmd_args |
| 119 |
assert "'::cmd:(($commands))'" in with_cmd_args |
| 120 |
|
| 121 |
|
| 122 |
def test_cmds_with_params(self, commands): |
| 123 |
output = StringIO() |
| 124 |
cmd = CmdFactory(TabCompletion, task_loader=DodoTaskLoader(), |
| 125 |
outstream=output, cmds=commands) |
| 126 |
cmd.execute({'shell':'zsh', 'hardcode_tasks': False}, []) |
| 127 |
got = output.getvalue() |
| 128 |
assert "tabcompletion: generate script" in got |
| 129 |
|
| 130 |
@pytest.mark.parametrize('loader_class', [FakeLoader2]) |
| 131 |
def test_hardcoded_tasks(self, commands, loader_class): |
| 132 |
output = StringIO() |
| 133 |
cmd = CmdFactory(TabCompletion, task_loader=loader_class(), |
| 134 |
outstream=output, cmds=commands) |
| 135 |
cmd.execute({'shell':'zsh', 'hardcode_tasks': True}, []) |
| 136 |
got = output.getvalue() |
| 137 |
assert 't1' in got |