Skip to content

Commit 52244e6

Browse files
akinomyogascop
authored andcommitted
feat(bash_completion): add a utility _comp_deprecate_func
1 parent c9faaee commit 52244e6

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

bash_completion

+16-12
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,21 @@ _comp__init_blacklist_glob='@(acroread.sh)'
4444
# Turn on extended globbing and programmable completion
4545
shopt -s extglob progcomp
4646

47+
# Declare a compatibility function name
48+
# @param $1 Old function name
49+
# @param $2 New function name
50+
_comp_deprecate_func()
51+
{
52+
if [[ $1 != [a-zA-Z_]*([a-zA-Z_0-9]) ]]; then
53+
printf 'bash_completion: %s: %s\n' "$FUNCNAME" "\$1: invalid function name '$1'" >&2
54+
return 2
55+
elif [[ $2 != [a-zA-Z_]*([a-zA-Z_0-9]) ]]; then
56+
printf 'bash_completion: %s: %s\n' "$FUNCNAME" "\$2: invalid function name '$2'" >&2
57+
return 2
58+
fi
59+
eval -- "$1() { $2 \"\$@\"; }"
60+
}
61+
4762
# A lot of the following one-liners were taken directly from the
4863
# completion examples provided with the bash 2.04 source distribution
4964

@@ -2442,18 +2457,7 @@ _comp_xfunc()
24422457
"$xfunc_name" "${@:3}"
24432458
}
24442459
2445-
# Function for loading and calling functions from dynamically loaded
2446-
# completion files that may not have been sourced yet.
2447-
# @param $1 completion file to load function from in case it is missing
2448-
# @param $2... function and its arguments
2449-
_xfunc()
2450-
{
2451-
set -- "$@"
2452-
local srcfile=$1
2453-
shift
2454-
declare -F $1 &>/dev/null || __load_completion "$srcfile"
2455-
"$@"
2456-
}
2460+
_comp_deprecate_func _xfunc _comp_xfunc
24572461
24582462
# source compat completion directory definitions
24592463
_comp__init_compat_dir=${BASH_COMPLETION_COMPAT_DIR:-/etc/bash_completion.d}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import pytest
2+
3+
from conftest import assert_bash_exec
4+
5+
6+
@pytest.mark.bashcomp(cmd=None, ignore_env=r"^\+declare -f func[12]$")
7+
class TestUnitDeprecateFunc:
8+
def test_1(self, bash):
9+
assert_bash_exec(
10+
bash,
11+
'func1() { echo "func1($*)"; }; _comp_deprecate_func func2 func1',
12+
)
13+
output = assert_bash_exec(bash, "func2 1 2 3", want_output=True)
14+
assert output.strip() == "func1(1 2 3)"

0 commit comments

Comments
 (0)