Skip to content

Commit 6b2b97e

Browse files
committed
refactor(quote): refactor a function { => _comp_}quote
1 parent b39640b commit 6b2b97e

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
@@ -144,6 +144,16 @@ _rl_enabled()
144144
}
145145

146146
# This function shell-quotes the argument
147+
# @param $1 String to be quoted
148+
# @var[out] ret Resulting string
149+
_comp_quote()
150+
{
151+
ret=\'${1//\'/\'\\\'\'}\'
152+
}
153+
154+
# This function shell-quotes the argument
155+
# @deprecated Use `_comp_quote` instead. Note that `_comp_quote` stores
156+
# the results in the variable `ret` instead of writing them to stdout.
147157
quote()
148158
{
149159
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)