Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(_comp_compgen_filedir,_comp_compgen_filedir_xspec): don’t suggest . and .. #1230

Merged
merged 2 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions bash_completion
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,15 @@ _comp_compgen_filedir()
-f ${_plusdirs+"${_plusdirs[@]}"}
fi

if ((${#toks[@]} != 0)); then
# Remove . and .. (as well as */. and */..) from suggestions, unless
# .. or */.. was typed explicitly by the user (for users who use
# tab-completion to append a slash after '..')
if [[ $cur != ?(*/).. ]]; then
_comp_compgen -Rv toks -- -X '?(*/)@(.|..)' -W '"${toks[@]}"'
fi
fi

if ((${#toks[@]} != 0)); then
# 2>/dev/null for direct invocation, e.g. in the _comp_compgen_filedir
# unit test
Expand Down Expand Up @@ -3042,6 +3051,13 @@ _comp_compgen_filedir_xspec()

((${#toks[@]})) || return 1

# Remove . and .. (as well as */. and */..) from suggestions, unless .. or
# */.. was typed explicitly by the user (for users who use tab-completion
# to append a slash after '..')
if [[ $cur != ?(*/).. ]]; then
_comp_compgen -Rv toks -- -X '?(*/)@(.|..)' -W '"${toks[@]}"' || return 1
fi

compopt -o filenames
_comp_compgen -RU toks -- -W '"${toks[@]}"'
}
Expand Down
Empty file.
Empty file.
Empty file.
1 change: 1 addition & 0 deletions test/fixtures/_filedir/dotdot/..data
Empty file.
50 changes: 48 additions & 2 deletions test/t/unit/test_unit_compgen_filedir.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import pytest

from conftest import assert_bash_exec, assert_complete
from conftest import assert_bash_exec, assert_complete, bash_env_saved


@pytest.mark.bashcomp(cmd=None, ignore_env=r"^\+COMPREPLY=")
Expand Down Expand Up @@ -59,7 +59,9 @@ def utf8_ctype(self, bash):
return lc_ctype

def test_1(self, bash):
assert_bash_exec(bash, "_comp_compgen_filedir >/dev/null")
with bash_env_saved(bash) as bash_env:
bash_env.write_variable("cur", "")
assert_bash_exec(bash, 'cur="" _comp_compgen_filedir >/dev/null')

@pytest.mark.parametrize("funcname", "f f2".split())
def test_2(self, bash, functions, funcname):
Expand Down Expand Up @@ -233,3 +235,47 @@ def test_26(self, bash, functions, funcname):
def test_27(self, bash, functions, funcname, utf8_ctype):
completion = assert_complete(bash, "%s aé/" % funcname, cwd="_filedir")
assert completion == "g"

@pytest.mark.parametrize("funcname", "f f2".split())
def test_28_dot_1(self, bash, functions, funcname):
"""Exclude . and .. when the completion is attempted for '.[TAB]'"""
completion = assert_complete(bash, r"%s ." % funcname, cwd="_filedir")
assert completion == [".dotfile1", ".dotfile2"]

@pytest.mark.parametrize("funcname", "f f2".split())
def test_28_dot_2(self, bash, functions, funcname):
"""Exclude . and .. when the completion is attempted for 'dir/.[TAB]'"""
completion = assert_complete(bash, r"%s _filedir/." % funcname)
assert completion == [".dotfile1", ".dotfile2"]

@pytest.mark.parametrize("funcname", "f f2".split())
def test_28_dot_3(self, bash, functions, funcname):
"""Include . when the completion is attempted for '..[TAB]'"""
completion = assert_complete(bash, r"%s .." % funcname, cwd="_filedir")
assert completion == "/"

@pytest.mark.parametrize("funcname", "f f2".split())
def test_28_dot_4(self, bash, functions, funcname):
"""Include . when the completion is attempted for '..[TAB]'"""
completion = assert_complete(bash, r"%s _filedir/.." % funcname)
assert completion == "/"

@pytest.mark.parametrize("funcname", "f f2".split())
def test_29_dotdot(self, bash, functions, funcname):
"""Complete files starting with "..".

These types of files are used by the go kubernetes atomic writer [0],
and presumably other types of systems, and we want to make sure they
will be completed correctly.

[0] https://pkg.go.dev/k8s.io/kubernetes/pkg/volume/util#AtomicWriter.Write
"""
completion = assert_complete(
bash, r"%s .." % funcname, cwd="_filedir/dotdot/"
)
assert completion == [
"../",
"..2016_02_01_15_04_05.123456",
"..data",
"..folder/",
]
5 changes: 4 additions & 1 deletion test/t/unit/test_unit_expand_glob.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ def functions(self, bash):

def test_match_all(self, bash, functions):
output = assert_bash_exec(bash, "__tester '*'", want_output=True)
assert output.strip() == "<a b><a$b><a&b><a'b><ab><aé><brackets><ext>"
assert (
output.strip()
== "<a b><a$b><a&b><a'b><ab><aé><brackets><dotdot><ext>"
)

def test_match_pattern(self, bash, functions):
output = assert_bash_exec(bash, "__tester 'a*'", want_output=True)
Expand Down