Skip to content

Commit d070a04

Browse files
committed
fix(cd): generate only cdable_vars containing valid directory paths
1 parent 8dd99f0 commit d070a04

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

completions/cd

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,16 @@ _comp_cmd_cd__compgen_cdable_vars()
44
{
55
shopt -q cdable_vars || return 1
66

7-
_comp_compgen -- -v
7+
local vars
8+
_comp_compgen -v vars -- -v || return "$?"
9+
10+
# Remove variables that do not contain a valid directory path.
11+
local _i
12+
for _i in "${!vars[@]}"; do
13+
[[ -d ${!vars[_i]} ]] || unset -v 'vars[_i]'
14+
done
15+
16+
_comp_compgen -U vars set "${vars[@]}"
817
}
918

1019
# This generator function observes the CDPATH variable, to additionally

test/t/test_cd.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import pytest
22

3+
from conftest import assert_complete, bash_env_saved
4+
35

46
@pytest.mark.bashcomp(ignore_env=r"^\+CDPATH=$")
57
class TestCd:
@@ -28,3 +30,15 @@ def test_dir_at_point(self, completion):
2830
@pytest.mark.complete("cd -")
2931
def test_options(self, completion):
3032
assert completion
33+
34+
def test_cdable_vars(self, bash):
35+
with bash_env_saved(bash) as bash_env:
36+
bash_env.shopt("cdable_vars", True)
37+
bash_env.write_variable("foo1", "shared")
38+
bash_env.write_variable("foo2", "shared/default")
39+
bash_env.write_variable("foo3", "nonexistent")
40+
bash_env.write_variable("foo4", "nonexistent")
41+
bash_env.write_variable("foo5", "shared/detault/foo")
42+
bash_env.write_variable("foo6", "shared/detault/bar")
43+
completion = assert_complete(bash, "cd f")
44+
assert completion == ["foo1", "foo2"]

0 commit comments

Comments
 (0)