Skip to content

Commit 355eb4d

Browse files
akinomyogascop
andcommitted
fix(cd): generate only cdable_vars containing valid directory paths
Co-authored-by: Ville Skyttä <[email protected]>
1 parent ca06988 commit 355eb4d

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

completions/cd

+12-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,18 @@ _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+
# Note: ${!vars[_i]} produces the "nounset" error when vars[_i] is an
14+
# empty array name.
15+
[[ -d ${!vars[_i]-} ]] || unset -v 'vars[_i]'
16+
done
17+
18+
_comp_compgen -U vars set "${vars[@]}"
819
}
920

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

test/t/test_cd.py

+14
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/default/foo")
42+
bash_env.write_variable("foo6", "shared/default/bar")
43+
completion = assert_complete(bash, "cd f")
44+
assert completion == ["foo1", "foo2"]

0 commit comments

Comments
 (0)