Skip to content

Commit 68084bc

Browse files
committed
refactor(quote): refactor a function { => _comp_}quote
1 parent 5c4fc14 commit 68084bc

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

bash_completion

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,16 @@ _comp_readline_variable_on()
148148
_comp_deprecate_func _rl_enabled _comp_readline_variable_on
149149

150150
# This function shell-quotes the argument
151+
# @param $1 String to be quoted
152+
# @var[out] ret Resulting string
153+
_comp_quote()
154+
{
155+
ret=\'${1//\'/\'\\\'\'}\'
156+
}
157+
158+
# This function shell-quotes the argument
159+
# @deprecated Use `_comp_quote` instead. Note that `_comp_quote` stores
160+
# the results in the variable `ret` instead of writing them to stdout.
151161
quote()
152162
{
153163
local quoted=${1//\'/\'\\\'\'}

test/t/unit/test_unit_quote.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,41 @@
33
from conftest import TestUnitBase, assert_bash_exec
44

55

6-
@pytest.mark.bashcomp(cmd=None)
6+
@pytest.mark.bashcomp(
7+
cmd=None,
8+
ignore_env=r"^\+declare -f __tester$",
9+
)
710
class TestUnitQuote(TestUnitBase):
811
def test_1(self, bash):
12+
assert_bash_exec(
13+
bash,
14+
'__tester() { local ret; _comp_quote "$1"; printf %s "$ret"; }',
15+
)
916
output = assert_bash_exec(
10-
bash, 'quote "a b"', want_output=True, want_newline=False
17+
bash, '__tester "a b"', want_output=True, want_newline=False
1118
)
1219
assert output.strip() == "'a b'"
1320

1421
def test_2(self, bash):
1522
output = assert_bash_exec(
16-
bash, 'quote "a b"', want_output=True, want_newline=False
23+
bash, '__tester "a b"', want_output=True, want_newline=False
1724
)
1825
assert output.strip() == "'a b'"
1926

2027
def test_3(self, bash):
2128
output = assert_bash_exec(
22-
bash, 'quote " a "', want_output=True, want_newline=False
29+
bash, '__tester " a "', want_output=True, want_newline=False
2330
)
2431
assert output.strip() == "' a '"
2532

2633
def test_4(self, bash):
2734
output = assert_bash_exec(
28-
bash, "quote \"a'b'c\"", want_output=True, want_newline=False
35+
bash, "__tester \"a'b'c\"", want_output=True, want_newline=False
2936
)
3037
assert output.strip() == r"'a'\''b'\''c'"
3138

3239
def test_5(self, bash):
3340
output = assert_bash_exec(
34-
bash, 'quote "a\'"', want_output=True, want_newline=False
41+
bash, '__tester "a\'"', want_output=True, want_newline=False
3542
)
3643
assert output.strip() == r"'a'\'''"

0 commit comments

Comments
 (0)