diff --git a/completions-core/Makefile.am b/completions-core/Makefile.am index abcd93aea21..39d407e21ff 100644 --- a/completions-core/Makefile.am +++ b/completions-core/Makefile.am @@ -82,6 +82,7 @@ cross_platform = 2to3.bash \ dmypy.bash \ dnssec-keygen.bash \ dnsspoof.bash \ + doas.bash \ dot.bash \ dpkg.bash \ dpkg-source.bash \ diff --git a/completions-core/doas.bash b/completions-core/doas.bash new file mode 100644 index 00000000000..43a3d2a999d --- /dev/null +++ b/completions-core/doas.bash @@ -0,0 +1,46 @@ +# doas(1) completion + +_comp_cmd_doas() +{ + local cur prev words cword was_split comp_args + _comp_initialize -s -- "$@" || return + + local noargopts='!(-*|*[uCLs]*)' + + local i + for ((i = 1; i <= cword; i++)); do + if [[ ${words[i]} != -* ]]; then + local PATH=$PATH:/sbin:/usr/sbin:/usr/local/sbin + local root_command=${words[i]} + local _comp_root_command=$1 + _comp_command_offset "$i" + return + fi + [[ ${words[i]} == -@(${noargopts}[uCLs]) ]] && + ((i++)) + done + + # shellcheck disable=SC2254 # glob $noargopts should not be quoted + case $prev in + -${noargopts}u) + _comp_compgen -- -u + return + ;; + -${noargopts}C) + _comp_compgen_filedir + return + ;; + -${noargopts}[Ls]) + return + ;; + esac + + [[ $was_split ]] && return + + if [[ $cur == -* ]]; then + _comp_compgen_usage + [[ ${COMPREPLY-} == *= ]] && compopt -o nospace + return + fi +} && + complete -F _comp_cmd_doas doas diff --git a/test/t/Makefile.am b/test/t/Makefile.am index 115f5736f10..b0736686f87 100644 --- a/test/t/Makefile.am +++ b/test/t/Makefile.am @@ -140,6 +140,7 @@ EXTRA_DIST = \ test_dmypy.py \ test_dnssec_keygen.py \ test_dnsspoof.py \ + test_doas.py \ test_dot.py \ test_dpkg.py \ test_dpkg_deb.py \ diff --git a/test/t/test_doas.py b/test/t/test_doas.py new file mode 100644 index 00000000000..a8039c58608 --- /dev/null +++ b/test/t/test_doas.py @@ -0,0 +1,17 @@ +import pytest + + +class TestDoas: + @pytest.mark.complete("doas -", require_cmd=True) + def test_1(self, completion): + assert completion + + @pytest.mark.complete("doas cd foo", cwd="shared/default") + def test_2(self, completion): + assert completion == ".d/" + assert not completion.endswith(" ") + + @pytest.mark.complete("doas sh share") + def test_3(self, completion): + assert completion == "d/" + assert not completion.endswith(" ") diff --git a/test/test-cmd-list.txt b/test/test-cmd-list.txt index bec7631b8d2..004c0153633 100644 --- a/test/test-cmd-list.txt +++ b/test/test-cmd-list.txt @@ -79,6 +79,7 @@ display dmesg dmypy dnsspoof +doas dpkg dpkg-deb dpkg-query