From 6de75e224ae29e732a57bc47d8592c874848ebee Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Sun, 10 Mar 2024 11:40:57 +0900 Subject: [PATCH] fix(available_interfaces): fix regression of unwanted trailing colons This is a fix for regression introduced in commit b6035350 and reported in GitHub #1129 [1]. The trailing colons of the generated interface names were removed before, but not they are not removed. [1] https://github.com/scop/bash-completion/issues/1129 In addition, generated words can have the form `veth0@veth1`, where we want only the first part `veth0` not containing any punctuation characters. In this patch, we remove any [[:punct:]] and all the later characters in the generated words. --- bash_completion | 2 +- test/t/unit/Makefile.am | 1 + .../test_unit_compgen_available_interfaces.py | 25 +++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 test/t/unit/test_unit_compgen_available_interfaces.py diff --git a/bash_completion b/bash_completion index e245cc55856..82d73b535dc 100644 --- a/bash_completion +++ b/bash_completion @@ -1737,7 +1737,7 @@ _comp_compgen_available_interfaces() fi } 2>/dev/null | _comp_awk \ '/^[^ \t]/ { if ($1 ~ /^[0-9]+:/) { print $2 } else { print $1 } }')" && - _comp_compgen -U generated set "${generated[@]}" + _comp_compgen -U generated set "${generated[@]%%[[:punct:]]*}" } # Echo number of CPUs, falling back to 1 on failure. diff --git a/test/t/unit/Makefile.am b/test/t/unit/Makefile.am index 54722dea934..a9bead09ef2 100644 --- a/test/t/unit/Makefile.am +++ b/test/t/unit/Makefile.am @@ -2,6 +2,7 @@ EXTRA_DIST = \ test_unit_abspath.py \ test_unit_command_offset.py \ test_unit_compgen.py \ + test_unit_compgen_available_interfaces.py \ test_unit_compgen_commands.py \ test_unit_count_args.py \ test_unit_delimited.py \ diff --git a/test/t/unit/test_unit_compgen_available_interfaces.py b/test/t/unit/test_unit_compgen_available_interfaces.py new file mode 100644 index 00000000000..5e931006c6d --- /dev/null +++ b/test/t/unit/test_unit_compgen_available_interfaces.py @@ -0,0 +1,25 @@ +import pytest + +from conftest import assert_bash_exec + + +@pytest.mark.bashcomp(cmd=None) +class TestUtilCompgenAvailableInterfaces: + @pytest.fixture + def functions(self, bash): + assert_bash_exec( + bash, + "_comp__test_dump() { ((${#arr[@]})) && printf '<%s>' \"${arr[@]}\"; echo; }", + ) + assert_bash_exec( + bash, + '_comp__test_compgen() { local -a arr=(00); _comp_compgen -v arr "$@"; _comp__test_dump; }', + ) + + def test_1_trailing_colons(self, bash, functions): + output = assert_bash_exec( + bash, + "_comp__test_compgen available_interfaces", + want_output=True, + ) + assert ":" not in output.strip()