|
| 1 | +import pytest |
| 2 | +import os |
| 3 | + |
| 4 | +from conftest import assert_bash_exec, bash_env_saved, prepare_fixture_dir |
| 5 | + |
| 6 | + |
| 7 | +@pytest.mark.bashcomp(cmd=None, cwd="_comp_load") |
| 8 | +class TestCompLoad: |
| 9 | + @pytest.fixture |
| 10 | + def fixture_dir(self, request, bash): |
| 11 | + """Construct the fixture directory in a temporary directory. |
| 12 | +
|
| 13 | + Some of the tests use specific setups of symbolic links. However, if |
| 14 | + we put the symbolic links in the static fixture directory, Automake |
| 15 | + resolves them for tarballs. As a result, the tests fail when the files |
| 16 | + are extracted from the tarballs. There does not seem to be any option |
| 17 | + to change the behavior of Automake. |
| 18 | +
|
| 19 | + We instead manually set up all symbolic links needed for the tests |
| 20 | + here. The other normal files and directories are statically included |
| 21 | + in the repository as "/test/fixtures/_comp_load". We first copy the |
| 22 | + statically included files and directories to a temporary directory and |
| 23 | + set up symbolic links. |
| 24 | + """ |
| 25 | + |
| 26 | + tmpdir, _, _ = prepare_fixture_dir(request, files=[], dirs=[]) |
| 27 | + assert_bash_exec(bash, "cp -R %s/* %s/" % (os.getcwd(), tmpdir)) |
| 28 | + assert_bash_exec(bash, "mkdir -p %s/bin" % tmpdir) |
| 29 | + assert_bash_exec( |
| 30 | + bash, "ln -sf ../prefix1/bin/cmd1 %s/bin/cmd1" % tmpdir |
| 31 | + ) |
| 32 | + assert_bash_exec( |
| 33 | + bash, "ln -sf ../prefix1/sbin/cmd2 %s/bin/cmd2" % tmpdir |
| 34 | + ) |
| 35 | + return str(tmpdir) |
| 36 | + |
| 37 | + def test_userdir_1(self, bash, fixture_dir): |
| 38 | + with bash_env_saved(bash) as bash_env: |
| 39 | + bash_env.chdir(fixture_dir) |
| 40 | + bash_env.write_variable( |
| 41 | + "BASH_COMPLETION_USER_DIR", |
| 42 | + "$PWD/userdir1:$PWD/userdir2:$BASH_COMPLETION_USER_DIR", |
| 43 | + quote=False, |
| 44 | + ) |
| 45 | + bash_env.write_variable( |
| 46 | + "PATH", "$PWD/prefix1/bin:$PWD/prefix1/sbin", quote=False |
| 47 | + ) |
| 48 | + output = assert_bash_exec( |
| 49 | + bash, "_comp_load cmd1", want_output=True |
| 50 | + ) |
| 51 | + assert output.strip() == "cmd1: sourced from userdir1" |
| 52 | + output = assert_bash_exec( |
| 53 | + bash, "_comp_load cmd2", want_output=True |
| 54 | + ) |
| 55 | + assert output.strip() == "cmd2: sourced from userdir2" |
| 56 | + |
| 57 | + def test_PATH_1(self, bash, fixture_dir): |
| 58 | + with bash_env_saved(bash) as bash_env: |
| 59 | + bash_env.chdir(fixture_dir) |
| 60 | + bash_env.write_variable( |
| 61 | + "PATH", "$PWD/prefix1/bin:$PWD/prefix1/sbin", quote=False |
| 62 | + ) |
| 63 | + output = assert_bash_exec( |
| 64 | + bash, "_comp_load cmd1", want_output=True |
| 65 | + ) |
| 66 | + assert output.strip() == "cmd1: sourced from prefix1" |
| 67 | + output = assert_bash_exec( |
| 68 | + bash, "_comp_load cmd2", want_output=True |
| 69 | + ) |
| 70 | + assert output.strip() == "cmd2: sourced from prefix1" |
| 71 | + output = assert_bash_exec( |
| 72 | + bash, "complete -p cmd2", want_output=True |
| 73 | + ) |
| 74 | + assert " cmd2" in output |
| 75 | + output = assert_bash_exec( |
| 76 | + bash, 'complete -p "$PWD/prefix1/sbin/cmd2"', want_output=True |
| 77 | + ) |
| 78 | + assert "/prefix1/sbin/cmd2" in output |
| 79 | + |
| 80 | + def test_cmd_path_1(self, bash, fixture_dir): |
| 81 | + with bash_env_saved(bash) as bash_env: |
| 82 | + bash_env.chdir(fixture_dir) |
| 83 | + assert_bash_exec(bash, "complete -r cmd1 || :", want_output=None) |
| 84 | + output = assert_bash_exec( |
| 85 | + bash, "_comp_load prefix1/bin/cmd1", want_output=True |
| 86 | + ) |
| 87 | + assert output.strip() == "cmd1: sourced from prefix1" |
| 88 | + output = assert_bash_exec( |
| 89 | + bash, 'complete -p "$PWD/prefix1/bin/cmd1"', want_output=True |
| 90 | + ) |
| 91 | + assert "/prefix1/bin/cmd1" in output |
| 92 | + assert_bash_exec(bash, "! complete -p cmd1", want_output=None) |
| 93 | + output = assert_bash_exec( |
| 94 | + bash, "_comp_load prefix1/sbin/cmd2", want_output=True |
| 95 | + ) |
| 96 | + assert output.strip() == "cmd2: sourced from prefix1" |
| 97 | + output = assert_bash_exec( |
| 98 | + bash, "_comp_load bin/cmd1", want_output=True |
| 99 | + ) |
| 100 | + assert output.strip() == "cmd1: sourced from prefix1" |
| 101 | + output = assert_bash_exec( |
| 102 | + bash, "_comp_load bin/cmd2", want_output=True |
| 103 | + ) |
| 104 | + assert output.strip() == "cmd2: sourced from prefix1" |
| 105 | + |
| 106 | + def test_cmd_path_2(self, bash, fixture_dir): |
| 107 | + with bash_env_saved(bash) as bash_env: |
| 108 | + bash_env.chdir(fixture_dir) |
| 109 | + bash_env.write_variable("PATH", "$PWD/bin:$PATH", quote=False) |
| 110 | + output = assert_bash_exec( |
| 111 | + bash, "_comp_load cmd1", want_output=True |
| 112 | + ) |
| 113 | + assert output.strip() == "cmd1: sourced from prefix1" |
| 114 | + output = assert_bash_exec( |
| 115 | + bash, "_comp_load cmd2", want_output=True |
| 116 | + ) |
| 117 | + assert output.strip() == "cmd2: sourced from prefix1" |
| 118 | + |
| 119 | + def test_cmd_intree_precedence(self, bash, fixture_dir): |
| 120 | + """ |
| 121 | + Test in-tree, i.e. completions/$cmd relative to the main script |
| 122 | + has precedence over location derived from PATH. |
| 123 | + """ |
| 124 | + with bash_env_saved(bash) as bash_env: |
| 125 | + bash_env.chdir(fixture_dir) |
| 126 | + bash_env.write_variable("PATH", "$PWD/prefix1/bin", quote=False) |
| 127 | + # The in-tree `sh` completion should be loaded here, |
| 128 | + # and cause no output, unlike our `$PWD/prefix1/bin/sh` canary. |
| 129 | + assert_bash_exec(bash, "_comp_load sh", want_output=False) |
0 commit comments