diff --git a/completions/Makefile.am b/completions/Makefile.am index 219ea10dab6..ceced249b8d 100644 --- a/completions/Makefile.am +++ b/completions/Makefile.am @@ -51,6 +51,7 @@ bashcomp_DATA = 2to3 \ check_db \ check_perms \ checksec \ + chflags \ _chfn \ chgrp \ chkconfig \ diff --git a/completions/chflags b/completions/chflags new file mode 100644 index 00000000000..9bac75c9dd9 --- /dev/null +++ b/completions/chflags @@ -0,0 +1,52 @@ +# chflags(1) completion -*- shell-script -*- + +[[ $OSTYPE == *@(bsd|darwin)* ]] || return 1 + +# References +# +# [1] FreeBSD - https://man.freebsd.org/cgi/man.cgi?chflags(1) +# [2] NetBSD - https://man.netbsd.org/NetBSD-9.0/chflags.1 +# [3] OpenBSD - https://man.openbsd.org/chflags.1 + +_comp_cmd_chflags() +{ + local cur prev words cword comp_args + _comp_initialize -- "$@" || return + + if [[ $cur == -* ]]; then + # Complete -options + local w opts="" + for w in "${words[@]}"; do + [[ $w == -R ]] && opts="-H -L -P" && break + done + [[ $OSTYPE == *freebsd* ]] && opts="$opts -x" + _comp_compgen -- -W '-f -h -v -R $opts' + else + local REPLY + # The first argument is a list of flags; the rest are filedir. + _comp_count_args + if ((REPLY == 1)); then + case "$OSTYPE" in + *netbsd*) + _comp_delimited , -W ' + arch opaque nodump sappnd schg uappnd uchg' + ;; + *openbsd*) + _comp_delimited , -W 'arch nodump sappnd schg uappnd uchg' + ;; + *) + _comp_delimited , -W ' + simmutable nosimmutable sappend nosappend archived + noarchived sunlink nosunlink opaque noopaque nodump + dump uimmutable nouimmutable uappend nouappend hidden + nohidden uunlink nouunlink' + ;; + esac + else + _comp_compgen_filedir + fi + fi +} && + complete -F _comp_cmd_chflags chflags + +# ex: filetype=sh diff --git a/test/t/Makefile.am b/test/t/Makefile.am index 5a599693d9d..7a2a0671435 100644 --- a/test/t/Makefile.am +++ b/test/t/Makefile.am @@ -73,6 +73,7 @@ EXTRA_DIST = \ test_check_db.py \ test_check_perms.py \ test_checksec.py \ + test_chflags.py \ test_chfn.py \ test_chgrp.py \ test_chkconfig.py \ diff --git a/test/t/test_chflags.py b/test/t/test_chflags.py new file mode 100644 index 00000000000..047a9aadefb --- /dev/null +++ b/test/t/test_chflags.py @@ -0,0 +1,19 @@ +import pytest + + +class TestChflags: + @pytest.mark.complete("chflags no") + def test_basic(self, completion): + assert completion + + @pytest.mark.complete("chflags -") + def test_options(self, completion): + assert completion and "-P" not in completion + + @pytest.mark.complete("chflags -R -") + def test_options_after_R(self, completion): + assert "-P" in completion + + @pytest.mark.complete("chflags -v sappend ") + def test_first_word(self, completion): + assert completion