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

from conftest import assert_bash_exec


@pytest.mark.bashcomp(cmd=None)
class TestUtilCompgenSplit:
    @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_cmd1() { echo foo bar; echo baz; }",
        )
        assert_bash_exec(
            bash,
            '_comp__test_attack() { echo "\\$(echo should_not_run >&2)"; }',
        )

    def test_1_basic(self, bash, functions):
        output = assert_bash_exec(
            bash,
            '_comp__test_compgen split -- "$(_comp__test_cmd1)"',
            want_output=True,
        )
        assert output.strip() == "<foo><bar><baz>"

    def test_2_attack(self, bash, functions):
        output = assert_bash_exec(
            bash,
            '_comp__test_compgen split -- "$(_comp__test_attack)"',
            want_output=True,
        )
        assert output.strip() == "<$(echo><should_not_run><>&2)>"

    def test_3_sep1(self, bash, functions):
        output = assert_bash_exec(
            bash,
            '_comp__test_compgen split -l -- "$(_comp__test_cmd1)"',
            want_output=True,
        )
        assert output.strip() == "<foo bar><baz>"

    def test_3_sep2(self, bash, functions):
        output = assert_bash_exec(
            bash,
            "_comp__test_compgen split -F $'b\\n' -- \"$(_comp__test_cmd1)\"",
            want_output=True,
        )
        assert output.strip() == "<foo ><ar><az>"

    def test_4_optionX(self, bash, functions):
        output = assert_bash_exec(
            bash,
            '_comp__test_compgen split -X bar -- "$(_comp__test_cmd1)"',
            want_output=True,
        )
        assert output.strip() == "<foo><baz>"

    def test_4_optionS(self, bash, functions):
        output = assert_bash_exec(
            bash,
            '_comp__test_compgen split -S .txt -- "$(_comp__test_cmd1)"',
            want_output=True,
        )
        assert output.strip() == "<foo.txt><bar.txt><baz.txt>"

    def test_4_optionP(self, bash, functions):
        output = assert_bash_exec(
            bash,
            '_comp__test_compgen split -P /tmp/ -- "$(_comp__test_cmd1)"',
            want_output=True,
        )
        assert output.strip() == "</tmp/foo></tmp/bar></tmp/baz>"

    def test_4_optionPS(self, bash, functions):
        output = assert_bash_exec(
            bash,
            '_comp__test_compgen split -P [ -S ] -- "$(_comp__test_cmd1)"',
            want_output=True,
        )
        assert output.strip() == "<[foo]><[bar]><[baz]>"

    def test_5_empty(self, bash, functions):
        output = assert_bash_exec(
            bash, '_comp__test_compgen split -- ""', want_output=True
        )
        assert output.strip() == ""

    def test_5_empty2(self, bash, functions):
        output = assert_bash_exec(
            bash, '_comp__test_compgen split -- " "', want_output=True
        )
        assert output.strip() == ""