summaryrefslogtreecommitdiffstats
path: root/test/t/unit/test_unit_compgen_commands.py
blob: d8662395e2e51e18bb80e4f4bc6df4910b9cece9 (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
import pytest

from conftest import assert_bash_exec, assert_complete, bash_env_saved


@pytest.mark.bashcomp(cmd=None, ignore_env=r"^\+COMPREPLY=")
class TestUtilCompgenCommands:
    @pytest.fixture(scope="class")
    def functions(self, request, bash):
        assert_bash_exec(
            bash,
            r"_comp_compgen_commands__test() {"
            r"    local COMPREPLY=() cur=${1-};"
            r"    _comp_compgen_commands;"
            r'    printf "%s\n" "${COMPREPLY[@]-}";'
            r"}",
        )
        assert_bash_exec(
            bash,
            "_comp_cmd_ccc() {"
            "    local cur;"
            "    _comp_get_words cur;"
            "     unset -v COMPREPLY;"
            "    _comp_compgen_commands;"
            "}; complete -F _comp_cmd_ccc ccc",
        )

    def test_basic(self, bash, functions):
        output = assert_bash_exec(
            bash, "_comp_compgen_commands__test sh", want_output=True
        )
        assert output.strip()

    @pytest.mark.parametrize(
        "shopt_no_empty,result_empty", ((True, True), (False, False))
    )
    def test_empty(self, bash, functions, shopt_no_empty, result_empty):
        with bash_env_saved(bash) as bash_env:
            bash_env.shopt("no_empty_cmd_completion", shopt_no_empty)
            output = assert_bash_exec(
                bash, "_comp_compgen_commands__test", want_output=True
            )
        assert (output.strip() == "") == result_empty

    def test_spaces(self, bash, functions):
        completion = assert_complete(bash, "ccc shared/default/bar")
        assert completion == r"\ bar.d/"