|
| 1 | +import pytest |
| 2 | + |
| 3 | +from conftest import assert_bash_exec, bash_env_saved |
| 4 | + |
| 5 | + |
| 6 | +@pytest.mark.bashcomp( |
| 7 | + cmd=None, |
| 8 | + cwd="_filedir", |
| 9 | + ignore_env=r"^\+(my_array=|declare -f dump_array$)", |
| 10 | +) |
| 11 | +class TestExpandGlob: |
| 12 | + def test_match_all(self, bash): |
| 13 | + assert_bash_exec( |
| 14 | + bash, |
| 15 | + "dump_array() { ((${#my_array[@]})) && printf '<%s>' \"${my_array[@]}\"; echo; }", |
| 16 | + ) |
| 17 | + output = assert_bash_exec( |
| 18 | + bash, |
| 19 | + "LC_ALL= LC_COLLATE=C _comp_expand_glob my_array '*';dump_array", |
| 20 | + want_output=True, |
| 21 | + ) |
| 22 | + assert output.strip() == "<a b><a$b><a&b><a'b><ab><aé><brackets><ext>" |
| 23 | + |
| 24 | + def test_match_pattern(self, bash): |
| 25 | + output = assert_bash_exec( |
| 26 | + bash, |
| 27 | + "LC_ALL= LC_COLLATE=C _comp_expand_glob my_array 'a*';dump_array", |
| 28 | + want_output=True, |
| 29 | + ) |
| 30 | + assert output.strip() == "<a b><a$b><a&b><a'b><ab><aé>" |
| 31 | + |
| 32 | + def test_match_unmatched(self, bash): |
| 33 | + output = assert_bash_exec( |
| 34 | + bash, |
| 35 | + "_comp_expand_glob my_array 'unmatched-*';dump_array", |
| 36 | + want_output=True, |
| 37 | + ) |
| 38 | + assert output.strip() == "" |
| 39 | + |
| 40 | + def test_match_multiple_words(self, bash): |
| 41 | + output = assert_bash_exec( |
| 42 | + bash, |
| 43 | + "_comp_expand_glob my_array 'b* e*';dump_array", |
| 44 | + want_output=True, |
| 45 | + ) |
| 46 | + assert output.strip() == "<brackets><ext>" |
| 47 | + |
| 48 | + def test_match_brace_expansion(self, bash): |
| 49 | + output = assert_bash_exec( |
| 50 | + bash, |
| 51 | + "_comp_expand_glob my_array 'brac{ket,unmatched}*';dump_array", |
| 52 | + want_output=True, |
| 53 | + ) |
| 54 | + assert output.strip() == "<brackets>" |
| 55 | + |
| 56 | + def test_protect_from_noglob(self, bash): |
| 57 | + with bash_env_saved(bash) as bash_env: |
| 58 | + bash_env.set("noglob", True) |
| 59 | + output = assert_bash_exec( |
| 60 | + bash, |
| 61 | + "LC_ALL= LC_COLLATE=C _comp_expand_glob my_array 'a*';dump_array", |
| 62 | + want_output=True, |
| 63 | + ) |
| 64 | + assert output.strip() == "<a b><a$b><a&b><a'b><ab><aé>" |
| 65 | + |
| 66 | + def test_protect_from_failglob(self, bash): |
| 67 | + with bash_env_saved(bash) as bash_env: |
| 68 | + bash_env.shopt("failglob", True) |
| 69 | + output = assert_bash_exec( |
| 70 | + bash, |
| 71 | + "_comp_expand_glob my_array 'unmatched-*';dump_array", |
| 72 | + want_output=True, |
| 73 | + ) |
| 74 | + assert output.strip() == "" |
| 75 | + |
| 76 | + def test_protect_from_nullglob(self, bash): |
| 77 | + with bash_env_saved(bash) as bash_env: |
| 78 | + bash_env.shopt("nullglob", False) |
| 79 | + output = assert_bash_exec( |
| 80 | + bash, |
| 81 | + "_comp_expand_glob my_array 'unmatched-*';dump_array", |
| 82 | + want_output=True, |
| 83 | + ) |
| 84 | + assert output.strip() == "" |
| 85 | + |
| 86 | + def test_protect_from_dotglob(self, bash): |
| 87 | + with bash_env_saved(bash) as bash_env: |
| 88 | + bash_env.shopt("dotglob", True) |
| 89 | + output = assert_bash_exec( |
| 90 | + bash, |
| 91 | + "_comp_expand_glob my_array 'ext/foo/*';dump_array", |
| 92 | + want_output=True, |
| 93 | + ) |
| 94 | + assert output.strip() == "" |
| 95 | + |
| 96 | + def test_protect_from_GLOBIGNORE(self, bash): |
| 97 | + with bash_env_saved(bash) as bash_env: |
| 98 | + # Note: dotglob is changed by GLOBIGNORE |
| 99 | + bash_env.save_shopt("dotglob") |
| 100 | + bash_env.write_variable("GLOBIGNORE", "*") |
| 101 | + output = assert_bash_exec( |
| 102 | + bash, |
| 103 | + "LC_ALL= LC_COLLATE=C _comp_expand_glob my_array 'a*';dump_array", |
| 104 | + want_output=True, |
| 105 | + ) |
| 106 | + assert output.strip() == "<a b><a$b><a&b><a'b><ab><aé>" |
0 commit comments