Skip to content

Commit ae1a164

Browse files
cykerwayscop
authored andcommitted
fix(_command,_command_offset): missing quotes for command variables
Some command variables may contain spaces but were used without double quotes. This commit fixes this error by double quoting these variables.
1 parent e869fa3 commit ae1a164

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

bash_completion

+7-7
Original file line numberDiff line numberDiff line change
@@ -2059,18 +2059,18 @@ _command_offset()
20592059
COMPREPLY=($(compgen -d -c -- "$cur"))
20602060
else
20612061
local cmd=${COMP_WORDS[0]} compcmd=${COMP_WORDS[0]}
2062-
local cspec=$(complete -p $cmd 2>/dev/null)
2062+
local cspec=$(complete -p "$cmd" 2>/dev/null)
20632063

20642064
# If we have no completion for $cmd yet, see if we have for basename
20652065
if [[ ! $cspec && $cmd == */* ]]; then
2066-
cspec=$(complete -p ${cmd##*/} 2>/dev/null)
2066+
cspec=$(complete -p "${cmd##*/}" 2>/dev/null)
20672067
[[ $cspec ]] && compcmd=${cmd##*/}
20682068
fi
20692069
# If still nothing, just load it for the basename
20702070
if [[ ! $cspec ]]; then
20712071
compcmd=${cmd##*/}
2072-
_completion_loader $compcmd
2073-
cspec=$(complete -p $compcmd 2>/dev/null)
2072+
_completion_loader "$compcmd"
2073+
cspec=$(complete -p "$compcmd" 2>/dev/null)
20742074
fi
20752075

20762076
if [[ -n $cspec ]]; then
@@ -2082,9 +2082,9 @@ _command_offset()
20822082
func=${func%% *}
20832083

20842084
if ((${#COMP_WORDS[@]} >= 2)); then
2085-
$func $cmd "${COMP_WORDS[-1]}" "${COMP_WORDS[-2]}"
2085+
$func "$cmd" "${COMP_WORDS[-1]}" "${COMP_WORDS[-2]}"
20862086
else
2087-
$func $cmd "${COMP_WORDS[-1]}"
2087+
$func "$cmd" "${COMP_WORDS[-1]}"
20882088
fi
20892089

20902090
# restore initial compopts
@@ -2098,7 +2098,7 @@ _command_offset()
20982098
done
20992099
else
21002100
cspec=${cspec#complete}
2101-
cspec=${cspec%%$compcmd}
2101+
cspec=${cspec%%"$compcmd"}
21022102
COMPREPLY=($(eval compgen "$cspec" -- '$cur'))
21032103
fi
21042104
elif ((${#COMPREPLY[@]} == 0)); then

test/t/test_sudo.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22

3-
from conftest import assert_complete
3+
from conftest import assert_bash_exec, assert_complete
44

55

66
class TestSudo:
@@ -81,3 +81,7 @@ def test_11(self, bash, part_full_group):
8181
part, _ = part_full_group
8282
completion = assert_complete(bash, "sudo chown foo:bar:%s" % part)
8383
assert not completion
84+
85+
def test_12(self, bash):
86+
assert_complete(bash, 'sudo "/tmp/aaa bbb" ')
87+
assert_bash_exec(bash, "! complete -p aaa", want_output=None)
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import pytest
2+
3+
from conftest import assert_bash_exec, assert_complete
4+
5+
6+
@pytest.mark.bashcomp(
7+
cmd=None,
8+
ignore_env=r"^[+-](COMP(_(WORDS|CWORD|LINE|POINT)|REPLY)|cur|cword|words)=",
9+
)
10+
class TestUnitCommandOffset:
11+
@pytest.fixture(scope="class")
12+
def functions(self, bash):
13+
assert_bash_exec(
14+
bash,
15+
"_co1() { _command_offset 1; }; complete -F _co1 co1",
16+
)
17+
18+
def test_1(self, bash, functions):
19+
assert_complete(bash, 'co1 "/tmp/aaa bbb" ')
20+
assert_bash_exec(bash, "! complete -p aaa", want_output=None)

0 commit comments

Comments
 (0)