|
| 1 | +import pytest |
| 2 | + |
| 3 | +from conftest import assert_bash_exec |
| 4 | + |
| 5 | + |
| 6 | +@pytest.mark.bashcomp(cmd=None) |
| 7 | +class TestUtilCompgenSplit: |
| 8 | + @pytest.fixture |
| 9 | + def functions(self, bash): |
| 10 | + assert_bash_exec( |
| 11 | + bash, |
| 12 | + "_comp__test_dump() { ((${#arr[@]})) && printf '<%s>' \"${arr[@]}\"; echo; }", |
| 13 | + ) |
| 14 | + assert_bash_exec( |
| 15 | + bash, |
| 16 | + '_comp__test_compgen() { local -a arr=(00); _comp_compgen -v arr "$@"; _comp__test_dump; }', |
| 17 | + ) |
| 18 | + |
| 19 | + assert_bash_exec( |
| 20 | + bash, |
| 21 | + "_comp__test_cmd1() { echo foo bar; echo baz; }", |
| 22 | + ) |
| 23 | + assert_bash_exec( |
| 24 | + bash, |
| 25 | + '_comp__test_attack() { echo "\\$(echo should_not_run >&2)"; }', |
| 26 | + ) |
| 27 | + |
| 28 | + def test_1_basic(self, bash, functions): |
| 29 | + output = assert_bash_exec( |
| 30 | + bash, |
| 31 | + '_comp__test_compgen split -- "$(_comp__test_cmd1)"', |
| 32 | + want_output=True, |
| 33 | + ) |
| 34 | + assert output.strip() == "<foo><bar><baz>" |
| 35 | + |
| 36 | + def test_2_attack(self, bash, functions): |
| 37 | + output = assert_bash_exec( |
| 38 | + bash, |
| 39 | + '_comp__test_compgen split -- "$(_comp__test_attack)"', |
| 40 | + want_output=True, |
| 41 | + ) |
| 42 | + assert output.strip() == "<$(echo><should_not_run><>&2)>" |
| 43 | + |
| 44 | + def test_3_sep1(self, bash, functions): |
| 45 | + output = assert_bash_exec( |
| 46 | + bash, |
| 47 | + '_comp__test_compgen split -l -- "$(_comp__test_cmd1)"', |
| 48 | + want_output=True, |
| 49 | + ) |
| 50 | + assert output.strip() == "<foo bar><baz>" |
| 51 | + |
| 52 | + def test_3_sep2(self, bash, functions): |
| 53 | + output = assert_bash_exec( |
| 54 | + bash, |
| 55 | + "_comp__test_compgen split -F $'b\\n' -- \"$(_comp__test_cmd1)\"", |
| 56 | + want_output=True, |
| 57 | + ) |
| 58 | + assert output.strip() == "<foo ><ar><az>" |
| 59 | + |
| 60 | + def test_4_optionX(self, bash, functions): |
| 61 | + output = assert_bash_exec( |
| 62 | + bash, |
| 63 | + '_comp__test_compgen split -X bar -- "$(_comp__test_cmd1)"', |
| 64 | + want_output=True, |
| 65 | + ) |
| 66 | + assert output.strip() == "<foo><baz>" |
| 67 | + |
| 68 | + def test_4_optionS(self, bash, functions): |
| 69 | + output = assert_bash_exec( |
| 70 | + bash, |
| 71 | + '_comp__test_compgen split -S .txt -- "$(_comp__test_cmd1)"', |
| 72 | + want_output=True, |
| 73 | + ) |
| 74 | + assert output.strip() == "<foo.txt><bar.txt><baz.txt>" |
| 75 | + |
| 76 | + def test_4_optionP(self, bash, functions): |
| 77 | + output = assert_bash_exec( |
| 78 | + bash, |
| 79 | + '_comp__test_compgen split -P /tmp/ -- "$(_comp__test_cmd1)"', |
| 80 | + want_output=True, |
| 81 | + ) |
| 82 | + assert output.strip() == "</tmp/foo></tmp/bar></tmp/baz>" |
| 83 | + |
| 84 | + def test_4_optionPS(self, bash, functions): |
| 85 | + output = assert_bash_exec( |
| 86 | + bash, |
| 87 | + '_comp__test_compgen split -P [ -S ] -- "$(_comp__test_cmd1)"', |
| 88 | + want_output=True, |
| 89 | + ) |
| 90 | + assert output.strip() == "<[foo]><[bar]><[baz]>" |
| 91 | + |
| 92 | + def test_5_empty(self, bash, functions): |
| 93 | + output = assert_bash_exec( |
| 94 | + bash, '_comp__test_compgen split -- ""', want_output=True |
| 95 | + ) |
| 96 | + assert output.strip() == "" |
| 97 | + |
| 98 | + def test_5_empty2(self, bash, functions): |
| 99 | + output = assert_bash_exec( |
| 100 | + bash, '_comp__test_compgen split -- " "', want_output=True |
| 101 | + ) |
| 102 | + assert output.strip() == "" |
0 commit comments