|
| 1 | +import pytest |
| 2 | + |
| 3 | +from conftest import assert_bash_exec, bash_env_saved |
| 4 | + |
| 5 | + |
| 6 | +@pytest.mark.bashcomp( |
| 7 | + cmd=None, cwd="shared", ignore_env=r"^\+declare -f __tester$" |
| 8 | +) |
| 9 | +class TestUnitRealCommand: |
| 10 | + @pytest.fixture |
| 11 | + def functions(self, bash): |
| 12 | + assert_bash_exec( |
| 13 | + bash, |
| 14 | + ( |
| 15 | + "__tester() { " |
| 16 | + "local ret rc; " |
| 17 | + '_comp_realcommand "$1"; ' |
| 18 | + "rc=$?; " |
| 19 | + 'printf %s "$ret"; ' |
| 20 | + "return $rc; " |
| 21 | + "}" |
| 22 | + ), |
| 23 | + ) |
| 24 | + |
| 25 | + def test_non_pollution(self, bash): |
| 26 | + """Test environment non-pollution, detected at teardown.""" |
| 27 | + assert_bash_exec( |
| 28 | + bash, |
| 29 | + "foo() { local ret=; _comp_realcommand bar; }; foo; unset -f foo", |
| 30 | + want_output=None, |
| 31 | + ) |
| 32 | + |
| 33 | + def test_basename(self, bash, functions): |
| 34 | + with bash_env_saved(bash) as bash_env: |
| 35 | + bash_env.write_variable("PATH", "$PWD/bin:$PATH", quote=False) |
| 36 | + output = assert_bash_exec( |
| 37 | + bash, |
| 38 | + "__tester arp", |
| 39 | + want_output=True, |
| 40 | + want_newline=False, |
| 41 | + ) |
| 42 | + assert output.strip().endswith("/shared/bin/arp") |
| 43 | + |
| 44 | + def test_basename_nonexistent(self, bash, functions): |
| 45 | + filename = "non-existent-file-for-bash-completion-tests" |
| 46 | + skipif = "! type -P %s" % filename |
| 47 | + try: |
| 48 | + assert_bash_exec(bash, skipif, want_output=None) |
| 49 | + except AssertionError: |
| 50 | + pytest.skipif(skipif) |
| 51 | + output = assert_bash_exec( |
| 52 | + bash, |
| 53 | + "! __tester %s" % filename, |
| 54 | + want_output=False, |
| 55 | + ) |
| 56 | + assert output.strip() == "" |
| 57 | + |
| 58 | + def test_relative(self, bash, functions): |
| 59 | + output = assert_bash_exec( |
| 60 | + bash, |
| 61 | + "__tester bin/arp", |
| 62 | + want_output=True, |
| 63 | + want_newline=False, |
| 64 | + ) |
| 65 | + assert output.strip().endswith("/shared/bin/arp") |
| 66 | + |
| 67 | + def test_relative_nonexistent(self, bash, functions): |
| 68 | + output = assert_bash_exec( |
| 69 | + bash, |
| 70 | + "! __tester bin/non-existent", |
| 71 | + want_output=False, |
| 72 | + ) |
| 73 | + assert output.strip() == "" |
| 74 | + |
| 75 | + def test_absolute(self, bash, functions): |
| 76 | + output = assert_bash_exec( |
| 77 | + bash, |
| 78 | + '__tester "$PWD/bin/arp"', |
| 79 | + want_output=True, |
| 80 | + want_newline=False, |
| 81 | + ) |
| 82 | + assert output.strip().endswith("/shared/bin/arp") |
| 83 | + |
| 84 | + def test_absolute_nonexistent(self, bash, functions): |
| 85 | + output = assert_bash_exec( |
| 86 | + bash, |
| 87 | + '! __tester "$PWD/bin/non-existent"', |
| 88 | + want_output=False, |
| 89 | + ) |
| 90 | + assert output.strip() == "" |
0 commit comments