Skip to content

Commit

Permalink
fix(_comp_delimited): prepend prefix to all compreply args
Browse files Browse the repository at this point in the history
Fixes #552.

Previously prefix was only prepended if COMPREPLY only contained one
argument - this commit fixes it so prefix is prepended to all arguments.
  • Loading branch information
Rogach committed Apr 3, 2023
1 parent df8cdda commit 6c8eb52
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 6 deletions.
3 changes: 2 additions & 1 deletion bash_completion
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,8 @@ _comp_delimited()
COMPREPLY=($(compgen "$@" -- "${cur##*"$delimiter"}"))
fi

((${#COMPREPLY[@]} == 1)) && COMPREPLY=(${COMPREPLY/#/$prefix})
((${#COMPREPLY[@]})) && COMPREPLY=("${COMPREPLY[@]/#/"$prefix"}")

[[ $delimiter != : ]] || __ltrim_colon_completions "$cur"
}

Expand Down
1 change: 0 additions & 1 deletion test/t/test_pgrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,3 @@ def test_nslist(self, completion):
)
def test_nslist_after_comma(self, completion):
assert completion
assert not any("," in x for x in completion)
2 changes: 0 additions & 2 deletions test/t/test_ssh_keygen.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@ def test_filedir_pub_at_end_of_s(self, completion):
@pytest.mark.complete("ssh-keygen -s foo_key -n foo,")
def test_usernames_for_n(self, completion):
assert completion
assert not any("," in x for x in completion)
# TODO check that these are usernames

@pytest.mark.complete("ssh-keygen -s foo_key -h -n foo,")
def test_host_for_h_n(self, completion):
assert completion
assert not any("," in x for x in completion)
# TODO check that these are hostnames

@pytest.mark.complete("ssh-keygen -Y foo -n ")
Expand Down
2 changes: 1 addition & 1 deletion test/t/test_tox.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def test_2(self, completion):

@pytest.mark.complete("tox -e foo,", cwd="tox")
def test_3(self, completion):
assert all(x in completion for x in "py37 ALL".split())
assert all("foo," + x in completion for x in "py37 ALL".split())

@pytest.mark.complete("tox -e foo -- ", cwd="tox")
def test_default_after_dashdash(self, completion):
Expand Down
2 changes: 1 addition & 1 deletion test/t/test_tshark.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def test_2(self, completion):
@pytest.mark.complete("tshark -O foo,htt", require_cmd=True)
def test_3(self, completion):
# p: one completion only; http: e.g. http and http2
assert completion == "p" or "http" in completion
assert completion == "p" or "foo,http" in completion

@pytest.mark.complete("tshark -o tcp", require_cmd=True)
def test_4(self, completion):
Expand Down
1 change: 1 addition & 0 deletions test/t/unit/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
EXTRA_DIST = \
test_unit_command_offset.py \
test_unit_count_args.py \
test_unit_delimited.py \
test_unit_deprecate_func.py \
test_unit_dequote.py \
test_unit_expand.py \
Expand Down
42 changes: 42 additions & 0 deletions test/t/unit/test_unit_delimited.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import pytest

from conftest import assert_bash_exec


@pytest.mark.bashcomp(cmd=None)
class TestUnitDelimited:
@pytest.fixture(scope="class")
def functions(self, request, bash):
assert_bash_exec(
bash,
"_comp_cmd_test_delim() {"
" local cur prev words cword comp_args;"
" _comp_get_words cur;"
" _comp_delimited , -W 'alpha beta bravo';"
"};"
"complete -F _comp_cmd_test_delim test_delim",
)

@pytest.mark.complete("test_delim --opt=a")
def test_1(self, functions, completion):
assert completion == ["lpha"]

@pytest.mark.complete("test_delim --opt=b")
def test_2(self, functions, completion):
assert completion == ["beta", "bravo"]

@pytest.mark.complete("test_delim --opt=alpha,b")
def test_3(self, functions, completion):
assert completion == ["alpha,beta", "alpha,bravo"]

@pytest.mark.complete("test_delim --opt=alpha,be")
def test_4(self, functions, completion):
assert completion == ["ta"]

@pytest.mark.complete("test_delim --opt=beta,a")
def test_5(self, functions, completion):
assert completion == ["lpha"]

@pytest.mark.complete("test_delim --opt=c")
def test_6(self, functions, completion):
assert not completion

0 comments on commit 6c8eb52

Please sign in to comment.