Skip to content

Commit a5637e9

Browse files
authored
Merge pull request #1427 from akinomyoga/fix1426
fix(_comp_compgen): go to the original directory on compgen failure
2 parents d6cfcf9 + 83d92c2 commit a5637e9

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

bash_completion

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,8 @@ if ((BASH_VERSINFO[0] > 5 || BASH_VERSINFO[0] == 5 && BASH_VERSINFO[1] >= 3)); t
809809
# pat, the effect of -X '' is overwritten by the specified one.
810810
IFS=$_ifs compgen -V _result -X '' "$@" ${_cur:+-- "$_cur"} || {
811811
_comp_compgen__error_fallback
812+
# shellcheck disable=SC2164
813+
[[ $_dir ]] && command cd -- "$_original_pwd"
812814
return
813815
}
814816

test/config/bashrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ add_comp_wordbreak_char()
6464

6565
_comp__test_get_env()
6666
{
67+
# Note: PWD is supposed to reflect the current working directory, but the
68+
# real current working directory of the process can be different, e.g. when
69+
# PWD is manually altered. Therefore, we also check the result of $(pwd)
70+
# explicitly via a new variable `_comp_test_pwd`.
71+
local _comp_test_pwd=$(pwd)
72+
6773
(
6874
# Do not output the state of test variables "_comp__test_+([0-9])_*"
6975
# and internal mutable variables "_comp_*_mut_*".

test/t/unit/test_unit_compgen.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ def functions(self, bash):
2626
'_comp__test_words_ifs() { local input=$2; _comp__test_compgen -F "$1" -c "${@:$#}" -- -W \'$input\'; }',
2727
)
2828

29+
assert_bash_exec(
30+
bash,
31+
'_comp_cmd_fb() { _comp_compgen -c "$(_get_cword)" -C _filedir -- -f; }; '
32+
"complete -F _comp_cmd_fb fb",
33+
)
2934
assert_bash_exec(
3035
bash,
3136
'_comp_cmd_fc() { _comp_compgen -c "$(_get_cword)" -C _filedir filedir; }; '
@@ -143,6 +148,10 @@ def test_6_option_C_4(self, functions, completion):
143148
# will not suffix a slash to the directory name.
144149
assert completion == "b"
145150

151+
@pytest.mark.complete(r"fb nonexistent")
152+
def test_6_option_C_5(self, bash, functions, completion):
153+
assert not completion
154+
146155
def test_7_icmd(self, bash, functions):
147156
with bash_env_saved(bash) as bash_env:
148157
bash_env.write_variable(

0 commit comments

Comments
 (0)