From a6fcf4bda791161844dbb722a2a59ce42063cc90 Mon Sep 17 00:00:00 2001 From: hellodword <46193371+hellodword@users.noreply.github.com> Date: Mon, 8 Jan 2024 01:46:59 +0000 Subject: [PATCH 1/2] fix: prefer using iproute2 instead of ifconfig For `_comp_compgen_available_interfaces`, we prefer to use ip (iproute) since long interface names will be truncated by ifconfig (respective packages in the operating system, e.g. inetutils) [1]. Even for the other functions that use "ifconfig" and "ip", we change to use `ip` because `ip`'s behavior is more uniform among the systems and also `ip` is becoming more common in Linux distributions. [1]: https://github.com/scop/bash-completion/pull/1090/files Co-authored-by: Koichi Murase Co-authored-by: Yedaya Katsman <43016107+yedayak@users.noreply.github.com> --- bash_completion | 11 +++++++---- test/fixtures/shared/bin/ip | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 4 deletions(-) create mode 100755 test/fixtures/shared/bin/ip diff --git a/bash_completion b/bash_completion index bf42e00885b..4316c172268 100644 --- a/bash_completion +++ b/bash_completion @@ -1628,7 +1628,7 @@ _comp_compgen_mac_addresses() # - ip link: link/ether _comp_compgen -v addresses split -- "$( { - LC_ALL=C ifconfig -a || ip -c=never link show || ip link show + ip -c=never link show || ip link show || LC_ALL=C ifconfig -a } 2>/dev/null | command sed -ne \ "s/.*[[:space:]]HWaddr[[:space:]]\{1,\}\($_re\)[[:space:]].*/\1/p" -ne \ "s/.*[[:space:]]HWaddr[[:space:]]\{1,\}\($_re\)[[:space:]]*$/\1/p" -ne \ @@ -1699,7 +1699,7 @@ _comp_compgen_ip_addresses() local PATH=$PATH:/sbin local addrs _comp_compgen -v addrs split -- "$({ - LC_ALL=C ifconfig -a || ip -c=never addr show || ip addr show + ip -c=never addr show || ip addr show || LC_ALL=C ifconfig -a } 2>/dev/null | command sed -e 's/[[:space:]]addr:/ /' -ne \ "s|.*inet${_n}[[:space:]]\{1,\}\([^[:space:]/]*\).*|\1|p")" || @@ -1733,9 +1733,12 @@ _comp_compgen_available_interfaces() if [[ ${1-} == -w ]]; then iwconfig elif [[ ${1-} == -a ]]; then - ifconfig || ip -c=never link show up || ip link show up + # Note: we prefer ip (iproute2) to ifconfig (inetutils) since long + # interface names will be truncated by ifconfig [1]. + # [1]: https://github.com/scop/bash-completion/issues/1089 + ip -c=never link show up || ip link show up || ifconfig else - ifconfig -a || ip -c=never link show || ip link show + ip -c=never link show || ip link show || ifconfig -a fi } 2>/dev/null | _comp_awk \ '/^[^ \t]/ { if ($1 ~ /^[0-9]+:/) { print $2 } else { print $1 } }')" && diff --git a/test/fixtures/shared/bin/ip b/test/fixtures/shared/bin/ip new file mode 100755 index 00000000000..d17490ec3d2 --- /dev/null +++ b/test/fixtures/shared/bin/ip @@ -0,0 +1,36 @@ +#!/bin/sh + +# Dummy "ip addr show" and "ip link show up" emulator + +for arg in "$@"; do +case "$arg" in +link) +cat < mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000 + link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 +2: eth0: mtu 1500 qdisc noqueue state UP mode DEFAULT group default + link/ether 33:33:33:33:33:33 brd ff:ff:ff:ff:ff:ff link-netnsid 0 +EOF +exit 0 + ;; +addr) +cat < mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 + link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 + inet 127.0.0.1/8 scope host lo + valid_lft forever preferred_lft forever + inet6 ::1/128 scope host + valid_lft forever preferred_lft forever +2: eth0: mtu 1500 qdisc noqueue state UP group default + link/ether 33:33:33:33:33:33 brd ff:ff:ff:ff:ff:ff link-netnsid 0 + inet 192.168.80.11/24 brd 192.168.80.255 scope global eth0 + valid_lft forever preferred_lft forever + inet6 fe80::000:0000:0000:0000/64 scope link + valid_lft forever preferred_lft forever +EOF +exit 0 + ;; +esac +done + +exit 1 From c2cafae0769a82513266b25d4c9f50974525b93e Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Tue, 14 May 2024 15:34:26 +0900 Subject: [PATCH 2/2] test(xinetd_services): mock a proper /etc/xinetd.d To test `_comp_compgen_xinetd_services`, we have been using a directory /test/fixtures/shared/bin (which contained two files `arp` and `ifconfig`as a mock /etc/xinetd.d. However, the directory /test/fixtures/shared/bin is shared with other tests, and the contents of the files therein are not proper xinetd configurations. We want to prepare a separate directory for a mock /etc/xinetd.d. This patch adds it under /test/fixtures/_comp_compgen_xinetd_services/xinetd.d. --- test/fixtures/_comp_compgen_xinetd_services/xinetd.d/arp | 0 test/fixtures/_comp_compgen_xinetd_services/xinetd.d/ifconfig | 0 test/t/unit/test_unit_compgen_xinetd_services.py | 2 +- 3 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 test/fixtures/_comp_compgen_xinetd_services/xinetd.d/arp create mode 100644 test/fixtures/_comp_compgen_xinetd_services/xinetd.d/ifconfig diff --git a/test/fixtures/_comp_compgen_xinetd_services/xinetd.d/arp b/test/fixtures/_comp_compgen_xinetd_services/xinetd.d/arp new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/fixtures/_comp_compgen_xinetd_services/xinetd.d/ifconfig b/test/fixtures/_comp_compgen_xinetd_services/xinetd.d/ifconfig new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/t/unit/test_unit_compgen_xinetd_services.py b/test/t/unit/test_unit_compgen_xinetd_services.py index fdc9b3f618f..915e1895717 100644 --- a/test/t/unit/test_unit_compgen_xinetd_services.py +++ b/test/t/unit/test_unit_compgen_xinetd_services.py @@ -17,7 +17,7 @@ def test_env_non_pollution(self, bash): def test_basic(self, bash): output = assert_bash_exec( bash, - "foo() { local _comp__test_xinetd_dir=$PWD/shared/bin; unset -v COMPREPLY; " + "foo() { local _comp__test_xinetd_dir=$PWD/_comp_compgen_xinetd_services/xinetd.d; unset -v COMPREPLY; " '_comp_compgen_xinetd_services; printf "%s\\n" "${COMPREPLY[@]}"; }; foo; unset -f foo', want_output=True, )