Skip to content

Commit 4fe877d

Browse files
committed
refactor: deprecate quote_readline
1 parent 3366080 commit 4fe877d

File tree

3 files changed

+48
-28
lines changed

3 files changed

+48
-28
lines changed

bash_completion

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,6 @@ _comp_quote()
135135
ret=\'${1//\'/\'\\\'\'}\'
136136
}
137137

138-
# @see _comp_quote_compgen()
139-
quote_readline()
140-
{
141-
local ret
142-
_comp_quote_compgen "$1"
143-
printf %s "$ret"
144-
} # quote_readline()
145-
146138
# shellcheck disable=SC1003
147139
_comp_dequote__initialize()
148140
{
@@ -2360,11 +2352,15 @@ _filedir_xspec()
23602352
23612353
_tilde "$cur" || return
23622354
2355+
local ret
2356+
_comp_quote_compgen "$cur"
2357+
local quoted=$ret
2358+
23632359
local IFS=$'\n' xspec=${_xspecs[${1##*/}]} tmp
23642360
local -a toks
23652361
23662362
toks=($(
2367-
compgen -d -- "$(quote_readline "$cur")" | {
2363+
compgen -d -- "$quoted" | {
23682364
while read -r tmp; do
23692365
printf '%s\n' "$tmp"
23702366
done
@@ -2383,7 +2379,7 @@ _filedir_xspec()
23832379
xspec="$matchop($xspec|${xspec^^})"
23842380
23852381
toks+=($(
2386-
eval compgen -f -X "'!$xspec'" -- '$(quote_readline "$cur")' | {
2382+
eval compgen -f -X "'!$xspec'" -- '$quoted' | {
23872383
while read -r tmp; do
23882384
[[ $tmp ]] && printf '%s\n' "$tmp"
23892385
done
@@ -2395,7 +2391,7 @@ _filedir_xspec()
23952391
${#toks[@]} -lt 1 ]] && {
23962392
local reset=$(shopt -po noglob)
23972393
set -o noglob
2398-
toks+=($(compgen -f -- "$(quote_readline "$cur")"))
2394+
toks+=($(compgen -f -- "$quoted"))
23992395
IFS=' '
24002396
$reset
24012397
IFS=$'\n'

bash_completion.d/000_bash_completion_compat.bash

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ quote()
3434
printf "'%s'" "$quoted"
3535
}
3636

37+
# @deprecated Use `_comp_quote_compgen`
38+
quote_readline()
39+
{
40+
local ret
41+
_comp_quote_compgen "$1"
42+
printf %s "$ret"
43+
} # quote_readline()
44+
3745
# This function is the same as `_comp_quote_compgen`, but receives the second
3846
# argument specifying the variable name to store the result.
3947
# @param $1 Argument to quote

test/t/unit/test_unit_quote_compgen.py

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,30 @@
55
from conftest import assert_bash_exec, assert_complete, bash_env_saved
66

77

8-
@pytest.mark.bashcomp(cmd=None, temp_cwd=True)
8+
@pytest.mark.bashcomp(
9+
cmd=None,
10+
temp_cwd=True,
11+
ignore_env=r"^\+declare -f _comp_test_quote_compgen$",
12+
)
913
class TestUnitQuoteCompgen:
10-
def test_exec(self, bash):
11-
assert_bash_exec(bash, "quote_readline '' >/dev/null")
14+
@pytest.fixture(scope="class")
15+
def functions(self, bash):
16+
assert_bash_exec(
17+
bash,
18+
'_comp_test_quote_compgen() { local ret; _comp_quote_compgen "$1"; printf %s "$ret"; }',
19+
)
20+
21+
def test_exec(self, bash, functions):
22+
assert_bash_exec(bash, "_comp_test_quote_compgen '' >/dev/null")
1223

13-
def test_env_non_pollution(self, bash):
24+
def test_env_non_pollution(self, bash, functions):
1425
"""Test environment non-pollution, detected at teardown."""
1526
assert_bash_exec(
16-
bash, "foo() { quote_readline meh >/dev/null; }; foo; unset -f foo"
27+
bash,
28+
"foo() { _comp_test_quote_compgen meh >/dev/null; }; foo; unset -f foo",
1729
)
1830

19-
def test_github_issue_189_1(self, bash):
31+
def test_github_issue_189_1(self, bash, functions):
2032
"""Test error messages on a certain command line
2133
2234
Reported at https://github.com/scop/bash-completion/issues/189
@@ -28,9 +40,9 @@ def test_github_issue_189_1(self, bash):
2840
$ rm -- '${[TAB]
2941
3042
"""
31-
assert_bash_exec(bash, "quote_readline $'\\'${' >/dev/null")
43+
assert_bash_exec(bash, "_comp_test_quote_compgen $'\\'${' >/dev/null")
3244

33-
def test_github_issue_492_1(self, bash):
45+
def test_github_issue_492_1(self, bash, functions):
3446
"""Test unintended code execution on a certain command line
3547
3648
Reported at https://github.com/scop/bash-completion/pull/492
@@ -44,11 +56,11 @@ def test_github_issue_492_1(self, bash):
4456
4557
"""
4658
assert_bash_exec(
47-
bash, "quote_readline $'\\'$(touch 1.txt)' >/dev/null"
59+
bash, "_comp_test_quote_compgen $'\\'$(touch 1.txt)' >/dev/null"
4860
)
4961
assert not os.path.exists("./1.txt")
5062

51-
def test_github_issue_492_2(self, bash):
63+
def test_github_issue_492_2(self, bash, functions):
5264
"""Test the file clear by unintended redirection on a certain command line
5365
5466
Reported at https://github.com/scop/bash-completion/pull/492
@@ -59,23 +71,25 @@ def test_github_issue_492_2(self, bash):
5971
$ awk '$1 > 1.0[TAB]
6072
6173
"""
62-
assert_bash_exec(bash, "quote_readline $'\\'$1 > 1.0' >/dev/null")
74+
assert_bash_exec(
75+
bash, "_comp_test_quote_compgen $'\\'$1 > 1.0' >/dev/null"
76+
)
6377
assert not os.path.exists("./1.0")
6478

65-
def test_github_issue_492_3(self, bash):
79+
def test_github_issue_492_3(self, bash, functions):
6680
"""Test code execution through unintended pathname expansions
6781
6882
When there is a file named "quote=$(COMMAND)" (for _filedir) or
69-
"ret=$(COMMAND)" (for quote_readline), the completion of the word '$*
70-
results in the execution of COMMAND.
83+
"ret=$(COMMAND)" (for _comp_quote_compgen), the completion of the word
84+
'$* results in the execution of COMMAND.
7185
7286
$ echo '$*[TAB]
7387
7488
"""
7589
os.mkdir("./ret=$(echo injected >&2)")
76-
assert_bash_exec(bash, "quote_readline $'\\'$*' >/dev/null")
90+
assert_bash_exec(bash, "_comp_test_quote_compgen $'\\'$*' >/dev/null")
7791

78-
def test_github_issue_492_4(self, bash):
92+
def test_github_issue_492_4(self, bash, functions):
7993
"""Test error messages through unintended pathname expansions
8094
8195
When "shopt -s failglob" is set by the user, the completion of the word
@@ -88,7 +102,9 @@ def test_github_issue_492_4(self, bash):
88102
"""
89103
with bash_env_saved(bash) as bash_env:
90104
bash_env.shopt("failglob", True)
91-
assert_bash_exec(bash, "quote_readline $'a\\\\\\tb*' >/dev/null")
105+
assert_bash_exec(
106+
bash, "_comp_test_quote_compgen $'a\\\\\\tb*' >/dev/null"
107+
)
92108

93109
def test_github_issue_526_1(self, bash):
94110
r"""Regression tests for unprocessed escape sequences after quotes

0 commit comments

Comments
 (0)