summaryrefslogtreecommitdiffstats
path: root/test/t/unit/test_unit_compgen.py
blob: cfdec8ee017b1bfd1b2e333bc33b0be6593fd79f (plain)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import pytest
import re

from conftest import assert_bash_exec, bash_env_saved, assert_complete


@pytest.mark.bashcomp(cmd=None)
class TestUtilCompgen:
    @pytest.fixture
    def functions(self, bash):
        assert_bash_exec(
            bash,
            "_comp__test_dump() { ((${#arr[@]})) && printf '<%s>' \"${arr[@]}\"; echo; }",
        )
        assert_bash_exec(
            bash,
            '_comp__test_compgen() { local -a arr=(00); _comp_compgen -v arr "$@"; _comp__test_dump; }',
        )
        assert_bash_exec(
            bash,
            '_comp__test_words() { local -a input=("${@:1:$#-1}"); _comp__test_compgen -c "${@:$#}" -- -W \'${input[@]+"${input[@]}"}\'; }',
        )
        assert_bash_exec(
            bash,
            '_comp__test_words_ifs() { local input=$2; _comp__test_compgen -F "$1" -c "${@:$#}" -- -W \'$input\'; }',
        )

        assert_bash_exec(
            bash,
            '_comp_cmd_fc() { _comp_compgen -c "$(_get_cword)" -C _filedir filedir; }; '
            "complete -F _comp_cmd_fc fc; "
            "complete -F _comp_cmd_fc -o filenames fc2",
        )
        assert_bash_exec(
            bash,
            '_comp_cmd_fcd() { _comp_compgen -c "$(_get_cword)" -C _filedir filedir -d; }; '
            "complete -F _comp_cmd_fcd fcd",
        )

        # test_8_option_U
        assert_bash_exec(
            bash,
            "_comp_compgen_gen8() { local -a arr=(x y z); _comp_compgen -U arr -- -W '\"${arr[@]}\"'; }",
        )

        # test_9_inherit_a
        assert_bash_exec(
            bash,
            '_comp_compgen_gen9sub() { local -a gen=(00); _comp_compgen -v gen -- -W 11; _comp_compgen_set "${gen[@]}"; }; '
            "_comp_compgen_gen9() { _comp_compgen_gen9sub; _comp_compgen -a gen9sub; }",
        )

    def test_1_basic(self, bash, functions):
        output = assert_bash_exec(
            bash, "_comp__test_words 12 34 56 ''", want_output=True
        )
        assert output.strip() == "<12><34><56>"

    def test_2_space(self, bash, functions):
        output = assert_bash_exec(
            bash,
            "_comp__test_words $'a b' $'c d\\t' '  e  ' $'\\tf\\t' ''",
            want_output=True,
        )
        assert output.strip() == "<a b><c d\t><  e  ><\tf\t>"

    def test_2_IFS(self, bash, functions):
        with bash_env_saved(bash) as bash_env:
            bash_env.write_variable("IFS", "34")
            output = assert_bash_exec(
                bash, "_comp__test_words 12 34 56 ''", want_output=True
            )
            assert output.strip() == "<12><34><56>"

    def test_3_glob(self, bash, functions):
        output = assert_bash_exec(
            bash,
            "_comp__test_words '*' '[a-z]*' '[a][b][c]' ''",
            want_output=True,
        )
        assert output.strip() == "<*><[a-z]*><[a][b][c]>"

    def test_3_failglob(self, bash, functions):
        with bash_env_saved(bash) as bash_env:
            bash_env.shopt("failglob", True)
            output = assert_bash_exec(
                bash,
                "_comp__test_words '*' '[a-z]*' '[a][b][c]' ''",
                want_output=True,
            )
            assert output.strip() == "<*><[a-z]*><[a][b][c]>"

    def test_3_nullglob(self, bash, functions):
        with bash_env_saved(bash) as bash_env:
            bash_env.shopt("nullglob", True)
            output = assert_bash_exec(
                bash,
                "_comp__test_words '*' '[a-z]*' '[a][b][c]' ''",
                want_output=True,
            )
            assert output.strip() == "<*><[a-z]*><[a][b][c]>"

    def test_4_empty(self, bash, functions):
        output = assert_bash_exec(
            bash, "_comp__test_words ''", want_output=True
        )
        assert output.strip() == ""

    def test_5_option_F(self, bash, functions):
        output = assert_bash_exec(
            bash,
            "_comp__test_words_ifs '25' ' 123 456 555 ' ''",
            want_output=True,
        )
        assert output.strip() == "< 1><3 4><6 >< >"

    def test_6_option_C_1(self, bash, functions):
        output = assert_bash_exec(
            bash,
            "_comp__test_compgen -c a -C _filedir filedir",
            want_output=True,
        )
        set1 = set(re.findall(r"<[^<>]*>", output.strip()))
        assert set1 == {"<a b>", "<a$b>", "<a&b>", "<a'b>", "<ab>", "<aé>"}

    def test_6_option_C_2(self, bash, functions):
        output = assert_bash_exec(
            bash,
            "_comp__test_compgen -c b -C _filedir -- -d",
            want_output=True,
        )
        assert output.strip() == "<brackets>"

    @pytest.mark.parametrize("funcname", "fc fc2".split())
    def test_6_option_C_3(self, bash, functions, funcname):
        completion = assert_complete(bash, "%s _filedir ab/" % funcname)
        assert completion == "e"

    @pytest.mark.complete(r"fcd a\ ")
    def test_6_option_C_4(self, functions, completion):
        # Note: we are not in the original directory that "b" exists, so Bash
        # will not suffix a slash to the directory name.
        assert completion == "b"

    def test_7_icmd(self, bash, functions):
        with bash_env_saved(bash) as bash_env:
            bash_env.write_variable(
                "BASH_COMPLETION_USER_DIR", "$PWD/_comp_compgen", quote=False
            )

            completions = assert_complete(bash, "compgen-cmd1 '")
            assert completions == ["012", "123", "234", "5abc", "6def", "7ghi"]

    def test_7_xcmd(self, bash, functions):
        with bash_env_saved(bash) as bash_env:
            bash_env.write_variable(
                "BASH_COMPLETION_USER_DIR", "$PWD/_comp_compgen", quote=False
            )

            completions = assert_complete(bash, "compgen-cmd2 '")
            assert completions == ["012", "123", "234", "5foo", "6bar", "7baz"]

    def test_8_option_U(self, bash, functions):
        output = assert_bash_exec(
            bash, "_comp__test_compgen gen8", want_output=True
        )
        assert output.strip() == "<x><y><z>"

    def test_9_inherit_a(self, bash, functions):
        output = assert_bash_exec(
            bash, "_comp__test_compgen gen9", want_output=True
        )
        assert output.strip() == "<11><11>"