File: test_cmd_completion.py

package info (click to toggle)
doit 0.36.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,704 kB
  • sloc: python: 11,863; makefile: 33; ansic: 14; javascript: 3; sh: 1
file content (137 lines) | stat: -rw-r--r-- 5,155 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
from io import StringIO
import pytest

from doit.exceptions import InvalidCommand
from doit.cmdparse import CmdOption
from doit.plugin import PluginDict
from doit.task import Task
from doit.cmd_base import Command, DodoTaskLoader, TaskLoader2
from doit.cmd_completion import TabCompletion
from doit.cmd_help import Help
from .conftest import CmdFactory

# doesnt test the shell scripts. just test its creation!


class FakeLoader2(TaskLoader2):
    def load_doit_config(self):
        return {}

    def load_tasks(self, cmd, pos_args):
        task_list = [
            Task("t1", None, ),
            Task("t2", None, task_dep=['t2:a'], has_subtask=True, ),
            Task("t2:a", None, subtask_of='t2'),
            ]
        return task_list

@pytest.fixture
def commands(request):
    sub_cmds = {}
    sub_cmds['tabcompletion'] = TabCompletion
    sub_cmds['help'] = Help
    return PluginDict(sub_cmds)

def test_invalid_shell_option():
    cmd = CmdFactory(TabCompletion)
    pytest.raises(InvalidCommand, cmd.execute,
                  {'shell':'another_shell', 'hardcode_tasks': False}, [])


class TestCmdCompletionBash(object):

    def test_with_dodo__dynamic_tasks(self, commands):
        output = StringIO()
        cmd = CmdFactory(TabCompletion, task_loader=DodoTaskLoader(),
                         outstream=output, cmds=commands)
        cmd.execute({'shell':'bash', 'hardcode_tasks': False}, [])
        got = output.getvalue()
        assert 'dodof' in got
        assert 't1' not in got
        assert 'tabcompletion' in got

    @pytest.mark.parametrize('loader_class', [FakeLoader2])
    def test_no_dodo__hardcoded_tasks(self, commands, loader_class):
        output = StringIO()
        cmd = CmdFactory(TabCompletion, task_loader=loader_class(),
                         outstream=output, cmds=commands)
        cmd.execute({'shell':'bash', 'hardcode_tasks': True}, [])
        got = output.getvalue()
        assert 'dodo.py' not in got
        assert 't1' in got

    def test_cmd_takes_file_args(self, commands):
        output = StringIO()
        cmd = CmdFactory(TabCompletion, task_loader=FakeLoader2(),
                         outstream=output, cmds=commands)
        cmd.execute({'shell':'bash', 'hardcode_tasks': False}, [])
        got = output.getvalue()
        assert """help)
            COMPREPLY=( $(compgen -W "${tasks} ${sub_cmds}" -- $cur) )
            return 0"""  in got
        assert """tabcompletion)
            COMPREPLY=( $(compgen -f -- $cur) )
            return 0"""  in got


class TestCmdCompletionZsh(object):

    def test_zsh_arg_line(self):
        opt1 = CmdOption({'name':'o1', 'default':'', 'help':'my desc'})
        assert '' == TabCompletion._zsh_arg_line(opt1)

        opt2 = CmdOption({'name':'o2', 'default':'', 'help':'my desc',
                          'short':'s'})
        assert '"-s[my desc]" \\' == TabCompletion._zsh_arg_line(opt2)

        opt3 = CmdOption({'name':'o3', 'default':'', 'help':'my desc',
                          'long':'lll'})
        assert '"--lll[my desc]" \\' == TabCompletion._zsh_arg_line(opt3)

        opt4 = CmdOption({'name':'o4', 'default':'', 'help':'my desc [b]a',
                          'short':'s', 'long':'lll'})
        assert ('"(-s|--lll)"{-s,--lll}"[my desc [b\\]a]" \\' ==
                TabCompletion._zsh_arg_line(opt4))

        # escaping `"` test
        opt5 = CmdOption({'name':'o5', 'default':'',
                          'help':'''my "des'c [b]a''',
                          'short':'s', 'long':'lll'})
        assert ('''"(-s|--lll)"{-s,--lll}"[my \\"des'c [b\\]a]" \\''' ==
                TabCompletion._zsh_arg_line(opt5))


    def test_cmd_arg_list(self):
        no_args = TabCompletion._zsh_arg_list(Command())
        assert "'*::task:(($tasks))'" not in no_args
        assert "'::cmd:(($commands))'" not in no_args

        class CmdTakeTasks(Command):
            doc_usage = '[TASK ...]'
        with_task_args = TabCompletion._zsh_arg_list(CmdTakeTasks())
        assert "'*::task:(($tasks))'" in with_task_args
        assert "'::cmd:(($commands))'" not in with_task_args

        class CmdTakeCommands(Command):
            doc_usage = '[COMMAND ...]'
        with_cmd_args = TabCompletion._zsh_arg_list(CmdTakeCommands())
        assert "'*::task:(($tasks))'" not in with_cmd_args
        assert "'::cmd:(($commands))'" in with_cmd_args


    def test_cmds_with_params(self, commands):
        output = StringIO()
        cmd = CmdFactory(TabCompletion, task_loader=DodoTaskLoader(),
                         outstream=output, cmds=commands)
        cmd.execute({'shell':'zsh', 'hardcode_tasks': False}, [])
        got = output.getvalue()
        assert "tabcompletion: generate script" in got

    @pytest.mark.parametrize('loader_class', [FakeLoader2])
    def test_hardcoded_tasks(self, commands, loader_class):
        output = StringIO()
        cmd = CmdFactory(TabCompletion, task_loader=loader_class(),
                         outstream=output, cmds=commands)
        cmd.execute({'shell':'zsh', 'hardcode_tasks': True}, [])
        got = output.getvalue()
        assert 't1' in got