Merge branch 'shellcheck-completions' into 'main'

tests: Enable shellcheck for bash completion scripts

See merge request GNOME/glib!4012
This commit is contained in:
Philip Withnall 2024-04-17 07:43:12 +00:00
commit 869ef92858
6 changed files with 60 additions and 36 deletions

View File

@ -1,4 +1,6 @@
# shellcheck shell=bash
# Check for bash # Check for bash
[ -z "$BASH_VERSION" ] && return [ -z "$BASH_VERSION" ] && return
@ -7,6 +9,7 @@
__app() { __app() {
case "${COMP_CWORD}" in case "${COMP_CWORD}" in
1) 1)
# shellcheck disable=SC2207
COMPREPLY=($(compgen -W "help version list-apps launch action list-actions" -- "${COMP_WORDS[1]}")) COMPREPLY=($(compgen -W "help version list-apps launch action list-actions" -- "${COMP_WORDS[1]}"))
return 0 return 0
;; ;;
@ -14,7 +17,8 @@ __app() {
2) 2)
case "${COMP_WORDS[1]}" in case "${COMP_WORDS[1]}" in
launch|action|list-actions) launch|action|list-actions)
COMPREPLY=($(compgen -W "`gapplication list-apps`" -- "${COMP_WORDS[2]}")) # shellcheck disable=SC2207
COMPREPLY=($(compgen -W "$(gapplication list-apps)" -- "${COMP_WORDS[2]}"))
return 0 return 0
;; ;;
@ -31,7 +35,8 @@ __app() {
action) action)
# Word 3 is the action name. This is the only one we can help with. # Word 3 is the action name. This is the only one we can help with.
if [ "${COMP_CWORD}" == 3 ]; then if [ "${COMP_CWORD}" == 3 ]; then
COMPREPLY=($(compgen -W "`gapplication list-actions "${COMP_WORDS[2]}"`" -- "${COMP_WORDS[3]}")) # shellcheck disable=SC2207
COMPREPLY=($(compgen -W "$(gapplication list-actions "${COMP_WORDS[2]}")" -- "${COMP_WORDS[3]}"))
return 0 return 0
else else
COMPREPLY=() COMPREPLY=()
@ -40,6 +45,7 @@ __app() {
;; ;;
launch) launch)
# Filenames... # Filenames...
# shellcheck disable=SC2207
COMPREPLY=($(compgen -A file "${COMP_WORDS[COMP_CWORD]}")) COMPREPLY=($(compgen -A file "${COMP_WORDS[COMP_CWORD]}"))
return 0 return 0
;; ;;

View File

@ -1,4 +1,6 @@
# shellcheck shell=bash
# Check for bash # Check for bash
[ -z "$BASH_VERSION" ] && return [ -z "$BASH_VERSION" ] && return
@ -7,9 +9,12 @@
__gdbus() { __gdbus() {
local IFS=$'\n' local IFS=$'\n'
local cur=`_get_cword :` local cur
cur="$(_get_cword :)"
local suggestions=$(gdbus complete "${COMP_LINE}" ${COMP_POINT}) local suggestions
suggestions="$(gdbus complete "${COMP_LINE}" "${COMP_POINT}")"
# shellcheck disable=SC2207
COMPREPLY=($(compgen -W "$suggestions" -- "$cur")) COMPREPLY=($(compgen -W "$suggestions" -- "$cur"))
# Remove colon-word prefix from COMPREPLY items # Remove colon-word prefix from COMPREPLY items
@ -17,10 +22,10 @@ __gdbus() {
*:*) *:*)
case "$COMP_WORDBREAKS" in case "$COMP_WORDBREAKS" in
*:*) *:*)
local colon_word=${cur%${cur##*:}} local colon_word=${cur%"${cur##*:}"}
local i=${#COMPREPLY[*]} local i=${#COMPREPLY[*]}
while [ $((--i)) -ge 0 ]; do while [ $((--i)) -ge 0 ]; do
COMPREPLY[$i]=${COMPREPLY[$i]#"$colon_word"} COMPREPLY[i]=${COMPREPLY[i]#"$colon_word"}
done done
;; ;;
esac esac

View File

@ -15,6 +15,8 @@
# along with this library; if not, see <http://www.gnu.org/licenses/>. # along with this library; if not, see <http://www.gnu.org/licenses/>.
# #
# shellcheck shell=bash
# Check for bash # Check for bash
[ -z "$BASH_VERSION" ] && return [ -z "$BASH_VERSION" ] && return
@ -45,13 +47,13 @@ __gio_location() {
dir="$cur" dir="$cur"
elif [[ $cur =~ "/" ]]; then elif [[ $cur =~ "/" ]]; then
# Subtract basename because dirname cmd doesn't work well with schemes # Subtract basename because dirname cmd doesn't work well with schemes
dir=${cur%$(basename "$cur")} dir=${cur%"$(basename "$cur")"}
fi fi
# List volumes and mounts # List volumes and mounts
local mounts=( ) local mounts=( )
local mount local mount
while IFS=$'\n' read mount; do while IFS=$'\n' read -r mount; do
# Do not care about local mounts # Do not care about local mounts
[[ "$mount" =~ ^"file:" ]] && continue [[ "$mount" =~ ^"file:" ]] && continue
@ -66,10 +68,11 @@ __gio_location() {
# List files # List files
local files=() local files=()
local names=() local names=()
local name size type local name type
while IFS=$'\t' read name size type; do while IFS=$'\t' read -r name _ type; do
# Escape name properly # Escape name properly
local escaped_name="$(printf "%q" "$name")" local escaped_name
escaped_name="$(printf "%q" "$name")"
# Append slash for directories and space for files # Append slash for directories and space for files
if [[ "$type" == "(directory)" ]]; then if [[ "$type" == "(directory)" ]]; then
@ -107,7 +110,8 @@ __gio_location() {
__gio() { __gio() {
# Complete subcommands # Complete subcommands
if (( ${COMP_CWORD} == 1 )); then if (( COMP_CWORD == 1 )); then
# shellcheck disable=SC2207
COMPREPLY=($(compgen -W "help version cat copy info launch list mime mkdir monitor mount move open rename remove save set trash tree" -- "${COMP_WORDS[1]}")) COMPREPLY=($(compgen -W "help version cat copy info launch list mime mkdir monitor mount move open rename remove save set trash tree" -- "${COMP_WORDS[1]}"))
compopt +o nospace compopt +o nospace
return 0 return 0

View File

@ -1,15 +1,16 @@
# shellcheck shell=bash
# Check for bash # Check for bash
[ -z "$BASH_VERSION" ] && return [ -z "$BASH_VERSION" ] && return
#################################################################################################### ####################################################################################################
__gresource() { __gresource() {
local choices coffset section local choices coffset
if [ ${COMP_CWORD} -gt 2 ]; then if [ "${COMP_CWORD}" -gt 2 ]; then
if [ ${COMP_WORDS[1]} = --section ]; then if [ "${COMP_WORDS[1]}" = --section ]; then
section=${COMP_WORDS[2]}
coffset=2 coffset=2
else else
coffset=0 coffset=0
@ -18,13 +19,13 @@ __gresource() {
coffset=0 coffset=0
fi fi
case "$((${COMP_CWORD}-$coffset))" in case "$((COMP_CWORD-coffset))" in
1) 1)
choices=$'--section \nhelp \nsections \nlist \ndetails \nextract ' choices=$'--section \nhelp \nsections \nlist \ndetails \nextract '
;; ;;
2) 2)
case "${COMP_WORDS[$(($coffset+1))]}" in case "${COMP_WORDS[$((coffset+1))]}" in
--section) --section)
return 0 return 0
;; ;;
@ -34,22 +35,24 @@ __gresource() {
;; ;;
sections|list|details|extract) sections|list|details|extract)
COMPREPLY=($(compgen -f -- ${COMP_WORDS[${COMP_CWORD}]})) # shellcheck disable=SC2207
COMPREPLY=($(compgen -f -- "${COMP_WORDS[${COMP_CWORD}]}"))
return 0 return 0
;; ;;
esac esac
;; ;;
3) 3)
case "${COMP_WORDS[$(($coffset+1))]}" in case "${COMP_WORDS[$((coffset+1))]}" in
list|details|extract) list|details|extract)
choices="$(gresource list ${COMP_WORDS[$(($coffset+2))]} 2> /dev/null | sed -e 's.$. .')" choices="$(gresource list "${COMP_WORDS[$((coffset+2))]}" 2> /dev/null | sed -e 's.$. .')"
;; ;;
esac esac
;; ;;
esac esac
local IFS=$'\n' local IFS=$'\n'
# shellcheck disable=SC2207
COMPREPLY=($(compgen -W "${choices}" -- "${COMP_WORDS[${COMP_CWORD}]}")) COMPREPLY=($(compgen -W "${choices}" -- "${COMP_WORDS[${COMP_CWORD}]}"))
} }

View File

@ -1,4 +1,6 @@
# shellcheck shell=bash
# Check for bash # Check for bash
[ -z "$BASH_VERSION" ] && return [ -z "$BASH_VERSION" ] && return
@ -7,8 +9,8 @@
__gsettings() { __gsettings() {
local choices coffset schemadir="" local choices coffset schemadir=""
if [ ${COMP_CWORD} -gt 2 ]; then if [ "${COMP_CWORD}" -gt 2 ]; then
if [ ${COMP_WORDS[1]} = --schemadir ]; then if [ "${COMP_WORDS[1]}" = --schemadir ]; then
# this complexity is needed to perform correct tilde expansion # this complexity is needed to perform correct tilde expansion
schemadir=$(eval "echo --schemadir ${COMP_WORDS[2]}") schemadir=$(eval "echo --schemadir ${COMP_WORDS[2]}")
coffset=2 coffset=2
@ -19,15 +21,16 @@ __gsettings() {
coffset=0 coffset=0
fi fi
case "$((${COMP_CWORD}-$coffset))" in case "$((COMP_CWORD-coffset))" in
1) 1)
choices=$'--schemadir\n--version\nhelp \nlist-schemas\nlist-relocatable-schemas\nlist-keys \nlist-children \nlist-recursively \nget \nrange \nset \nreset \nreset-recursively \nwritable \nmonitor \ndescribe ' choices=$'--schemadir\n--version\nhelp \nlist-schemas\nlist-relocatable-schemas\nlist-keys \nlist-children \nlist-recursively \nget \nrange \nset \nreset \nreset-recursively \nwritable \nmonitor \ndescribe '
;; ;;
2) 2)
case "${COMP_WORDS[$(($coffset+1))]}" in case "${COMP_WORDS[$((coffset+1))]}" in
--schemadir) --schemadir)
COMPREPLY=($(compgen -o dirnames -- ${COMP_WORDS[${COMP_CWORD}]})) # shellcheck disable=SC2207
COMPREPLY=($(compgen -o dirnames -- "${COMP_WORDS[${COMP_CWORD}]}"))
return 0 return 0
;; ;;
@ -35,38 +38,40 @@ __gsettings() {
choices=$'list-schemas\nlist-relocatable-schemas\nlist-keys\nlist-children\nlist-recursively\nget\nrange\nset\nreset\nreset-recursively\nwritable\nmonitor' choices=$'list-schemas\nlist-relocatable-schemas\nlist-keys\nlist-children\nlist-recursively\nget\nrange\nset\nreset\nreset-recursively\nwritable\nmonitor'
;; ;;
list-keys|list-children|list-recursively|reset-recursively) list-keys|list-children|list-recursively|reset-recursively)
choices="$(gsettings $schemadir list-schemas 2> /dev/null)"$'\n'"$(gsettings $schemadir list-relocatable-schemas 2> /dev/null | sed -e 's.$.:/.')" choices="$(gsettings "$schemadir" list-schemas 2> /dev/null)"$'\n'"$(gsettings "$schemadir" list-relocatable-schemas 2> /dev/null | sed -e 's.$.:/.')"
;; ;;
list-schemas) list-schemas)
COMPREPLY=($(compgen -W "--print-paths" -- ${COMP_WORDS[${COMP_CWORD}]})) # shellcheck disable=SC2207
COMPREPLY=($(compgen -W "--print-paths" -- "${COMP_WORDS[${COMP_CWORD}]}"))
return 0 return 0
;; ;;
get|range|set|reset|writable|monitor|describe) get|range|set|reset|writable|monitor|describe)
choices="$(gsettings $schemadir list-schemas 2> /dev/null | sed -e 's.$. .')"$'\n'"$(gsettings $schemadir list-relocatable-schemas 2> /dev/null | sed -e 's.$.:/.')" choices="$(gsettings "$schemadir" list-schemas 2> /dev/null | sed -e 's.$. .')"$'\n'"$(gsettings "$schemadir" list-relocatable-schemas 2> /dev/null | sed -e 's.$.:/.')"
;; ;;
esac esac
;; ;;
3) 3)
case "${COMP_WORDS[$(($coffset+1))]}" in case "${COMP_WORDS[$((coffset+1))]}" in
set) set)
choices="$(gsettings $schemadir list-keys ${COMP_WORDS[$(($coffset+2))]} 2> /dev/null | sed -e 's.$. .')" choices="$(gsettings "$schemadir" list-keys "${COMP_WORDS[$((coffset+2))]}" 2> /dev/null | sed -e 's.$. .')"
;; ;;
get|range|reset|writable|monitor|describe) get|range|reset|writable|monitor|describe)
choices="$(gsettings $schemadir list-keys ${COMP_WORDS[$(($coffset+2))]} 2> /dev/null)" choices="$(gsettings "$schemadir" list-keys "${COMP_WORDS[$((coffset+2))]}" 2> /dev/null)"
;; ;;
esac esac
;; ;;
4) 4)
case "${COMP_WORDS[$(($coffset+2))]}" in case "${COMP_WORDS[$((coffset+2))]}" in
set) set)
range=($(gsettings $schemadir range ${COMP_WORDS[$(($coffset+2))]} ${COMP_WORDS[$(($coffset+3))]} 2> /dev/null)) # shellcheck disable=SC2207
range=($(gsettings "$schemadir" range "${COMP_WORDS[$((coffset+2))]}" "${COMP_WORDS[$((coffset+3))]}" 2> /dev/null))
case "${range[0]}" in case "${range[0]}" in
enum) enum)
unset range[0] unset 'range[0]'
;; ;;
*) *)
unset range unset range
@ -80,6 +85,7 @@ __gsettings() {
esac esac
local IFS=$'\n' local IFS=$'\n'
# shellcheck disable=SC2207
COMPREPLY=($(compgen -W "${choices}" -- "${COMP_WORDS[${COMP_CWORD}]}")) COMPREPLY=($(compgen -W "${choices}" -- "${COMP_WORDS[${COMP_CWORD}]}"))
} }

View File

@ -16,7 +16,7 @@ export TEST_REQUIRES_TOOLS="git shellcheck"
run_lint () { run_lint () {
# Ignoring third-party directories that we don't want to parse # Ignoring third-party directories that we don't want to parse
# shellcheck disable=SC2046 # shellcheck disable=SC2046
shellcheck $(git ls-files '*.sh' | grep -Ev "glib/libcharset|glib/dirent") shellcheck $(git ls-files '*.sh' 'gio/completion/' | grep -Ev "glib/libcharset|glib/dirent|gitignore")
} }
# shellcheck source=tests/lint-common.sh # shellcheck source=tests/lint-common.sh