Skip to content

Commit

Permalink
*: avoid more errors in nounset mode
Browse files Browse the repository at this point in the history
Test suite has no more related errors in my local setup.

Closes #44
  • Loading branch information
scop committed May 22, 2020
1 parent d0a0eb5 commit 505fb2c
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 33 deletions.
2 changes: 1 addition & 1 deletion bash_completion
Original file line number Diff line number Diff line change
Expand Up @@ -1603,7 +1603,7 @@ _known_hosts_real()
{
local configfile flag prefix="" ifs=$IFS
local cur suffix="" aliases i host ipv4 ipv6
local -a kh tmpkh khd=() config
local -a kh tmpkh khd=() config=()

# TODO remove trailing %foo from entries

Expand Down
4 changes: 2 additions & 2 deletions completions/_udevadm
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ _udevadm()

$split && return

if [[ -z $udevcmd ]]; then
if [[ ! -v udevcmd ]]; then
case $cur in
-*)
COMPREPLY=($(compgen -W '--help --version --debug' -- "$cur"))
Expand All @@ -68,7 +68,7 @@ _udevadm()

if [[ $cur == -* ]]; then
COMPREPLY=($(compgen -W \
'$("$1" $udevcmd --help 2>/dev/null | _parse_help -)' -- "$cur"))
'$("$1" ${udevcmd-} --help 2>/dev/null | _parse_help -)' -- "$cur"))
[[ ${COMPREPLY-} == *= ]] && compopt -o nospace
fi
} &&
Expand Down
2 changes: 1 addition & 1 deletion completions/_umount.linux
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ _linux_fstab()
local ifs="$IFS"
while read -r fs_spec fs_file fs_other; do
if [[ $fs_spec == [#]* ]]; then continue; fi
if [[ $1 == -L ]]; then
if [[ ${1-} == -L ]]; then
local fs_label=${fs_spec/#LABEL=/}
if [[ $fs_label != "$fs_spec" ]]; then
__linux_fstab_unescape fs_label
Expand Down
25 changes: 13 additions & 12 deletions completions/ssh
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ _ssh_suboption()
fi
;;
proxyjump)
_known_hosts_real -a -F "$configfile" -- "$cur"
_known_hosts_real -a ${configfile:+-F "$configfile"} -- "$cur"
;;
proxycommand | remotecommand | localcommand)
COMPREPLY=($(compgen -c -- "$cur"))
Expand Down Expand Up @@ -219,7 +219,7 @@ _ssh_configfile()
configfile="$(dequote "${1:2}")"
else
shift
[[ $1 ]] && configfile="$(dequote "$1")"
[[ ${1-} ]] && configfile="$(dequote "$1")"
fi
break
fi
Expand Down Expand Up @@ -282,7 +282,7 @@ _ssh()
return
;;
-*J)
_known_hosts_real -a -F "$configfile" -- "$cur"
_known_hosts_real -a ${configfile:+-F "$configfile"} -- "$cur"
return
;;
-*l)
Expand Down Expand Up @@ -320,7 +320,7 @@ _ssh()
elif [[ $cur == -* ]]; then
COMPREPLY=($(compgen -W '$(_parse_usage "$1")' -- "$cur"))
else
_known_hosts_real $ipvx -a -F "$configfile" -- "$cur"
_known_hosts_real ${ipvx-} -a ${configfile:+-F "$configfile"} -- "$cur"

local args
_count_args
Expand Down Expand Up @@ -369,7 +369,7 @@ _sftp()
return
;;
-*J)
_known_hosts_real -a -F "$configfile" -- "$cur"
_known_hosts_real -a ${configfile:+-F "$configfile"} -- "$cur"
return
;;
-*o)
Expand All @@ -392,7 +392,7 @@ _sftp()
elif [[ $cur == -* ]]; then
COMPREPLY=($(compgen -W '$(_parse_usage "$1")' -- "$cur"))
else
_known_hosts_real $ipvx -a -F "$configfile" -- "$cur"
_known_hosts_real ${ipvx-} -a ${configfile:+-F "$configfile"} -- "$cur"
fi
} &&
shopt -u hostcomplete && complete -F _sftp sftp
Expand Down Expand Up @@ -451,18 +451,18 @@ _scp_local_files()
local IFS=$'\n'

local dirsonly=false
if [[ $1 == -d ]]; then
if [[ ${1-} == -d ]]; then
dirsonly=true
shift
fi

if $dirsonly; then
COMPREPLY+=($(command ls -aF1dL $cur* 2>/dev/null |
command sed -e "s/$_scp_path_esc/\\\\&/g" -e '/[^\/]$/d' -e "s/^/$1/"))
command sed -e "s/$_scp_path_esc/\\\\&/g" -e '/[^\/]$/d' -e "s/^/${1-}/"))
else
COMPREPLY+=($(command ls -aF1dL $cur* 2>/dev/null |
command sed -e "s/$_scp_path_esc/\\\\&/g" -e 's/[*@|=]$//g' \
-e 's/[^\/]$/& /g' -e "s/^/$1/"))
-e 's/[^\/]$/& /g' -e "s/^/${1-}/"))
fi
}

Expand Down Expand Up @@ -506,7 +506,7 @@ _scp()
return
;;
-*J)
_known_hosts_real -a -F "$configfile" -- "$cur"
_known_hosts_real -a ${configfile:+-F "$configfile"} -- "$cur"
return
;;
-*[lP])
Expand Down Expand Up @@ -550,12 +550,13 @@ _scp()
# not a known host, pass through
;;
*)
_known_hosts_real $ipvx -c -a -F "$configfile" -- "$cur"
_known_hosts_real ${ipvx-} -c -a \
${configfile:+-F "$configfile"} -- "$cur"
;;
esac
fi

_scp_local_files "$prefix"
_scp_local_files "${prefix-}"
} &&
complete -F _scp -o nospace scp

Expand Down
2 changes: 1 addition & 1 deletion completions/svcadm
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ _svcadm()
fi
done

if [[ -z $command ]]; then
if [[ ! -v command ]]; then
if [[ ${cur} == -* ]]; then
COMPREPLY=($(compgen -W "-v" -- ${cur}))
else
Expand Down
4 changes: 2 additions & 2 deletions completions/sysbench
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,12 @@ _sysbench()
done

local opts=$(_parse_help "$1")
if [[ $test ]]; then
if [[ -v test ]]; then
local help=($(_parse_help "$1" "--test=$test help"))
opts="${opts/--test=/} ${help[*]} prepare run cleanup help version"
fi

if [[ $cur == -* || ! $test ]]; then
if [[ $cur == -* || ! -v test ]]; then
COMPREPLY=($(compgen -W "$opts" -- "$cur"))
[[ ${COMPREPLY-} == *= ]] && compopt -o nospace
else
Expand Down
18 changes: 9 additions & 9 deletions completions/tar
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ __tar_complete_mode()
filler i

short_modes="ctx"
[[ -z $basic_tar ]] && short_modes="ctxurdA"
[[ ! -v basic_tar ]] && short_modes="ctxurdA"

# Remove prefix when needed
rawopt=${cur#-}
Expand All @@ -270,7 +270,7 @@ __tar_complete_mode()

# when user passed something like 'tar cf' do not put the '-' before
filler=
if [[ -z $cur && -z $basic_tar ]]; then
if [[ -z $cur && ! -v basic_tar ]]; then
filler=-
fi

Expand Down Expand Up @@ -338,14 +338,14 @@ __tar_try_mode()
case "$cur" in
--*)
# posix tar does not support long opts
[[ -n $basic_tar ]] && return 0
[[ -v basic_tar ]] && return 0
__gtar_complete_lopts
return $?
;;

-*)
# posix tar does not support short optios
[[ -n $basic_tar ]] && return 0
[[ -v basic_tar ]] && return 0

__tar_complete_mode && return 0
;;
Expand Down Expand Up @@ -457,8 +457,8 @@ __tar_detect_ext()
_gtar()
{
local long_opts short_opts \
long_arg_none long_arg_opt long_arg_req \
short_arg_none short_arg_opt short_arg_req \
long_arg_none="" long_arg_opt="" long_arg_req="" \
short_arg_none="" short_arg_opt="" short_arg_req="" \
tar_mode tar_mode_arg old_opt_progress=0 \
old_opt_used=0 old_opt_parsed=()

Expand All @@ -469,7 +469,7 @@ _gtar()
# FIXME: handle long options
local tar_mode_arg=

if [[ $_TAR_OPT_DEBUG == 1 ]]; then
if [[ -v BASHCOMP_TAR_OPT_DEBUG ]]; then
set -x
PS4='$BASH_SOURCE:$LINENO: '
fi
Expand Down Expand Up @@ -607,7 +607,7 @@ _gtar()
break
done # just-for-easy-break while

if [[ $_TAR_OPT_DEBUG == 1 ]]; then
if [[ -v BASHCOMP_TAR_OPT_DEBUG ]]; then
set +x
unset PS4
fi
Expand All @@ -631,7 +631,7 @@ __tar_posix_prev_handle()
_posix_tar()
{
local long_opts short_opts basic_tar \
long_arg_none long_arg_opt long_arg_req \
long_arg_none="" long_arg_opt long_arg_req="" \
short_arg_none short_arg_opt short_arg_req \
tar_mode tar_mode_arg old_opt_progress=0 \
old_opt_used=1 old_opt_parsed=()
Expand Down
2 changes: 1 addition & 1 deletion completions/tracepath
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ _tracepath()

local ipvx
[[ $1 == *6 ]] && ipvx=-6
_known_hosts_real $ipvx -- "$cur"
_known_hosts_real ${ipvx-} -- "$cur"
} &&
complete -F _tracepath tracepath tracepath6

Expand Down
5 changes: 3 additions & 2 deletions completions/tshark
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ _tshark()
cur=${cur#*:}
_filedir
else
[ -n "$_tshark_prefs" ] ||
[ -v _tshark_prefs ] ||
_tshark_prefs="$("$1" -G defaultprefs 2>/dev/null | command sed -ne 's/^#\{0,1\}\([a-z0-9_.-]\{1,\}:\).*/\1/p' |
tr '\n' ' ')"
: ${prefix:=}
COMPREPLY=($(compgen -P "$prefix" -W "$_tshark_prefs" \
-- "${cur:${#prefix}}"))
[[ ${COMPREPLY-} == *: ]] && compopt -o nospace
Expand Down Expand Up @@ -78,7 +79,7 @@ _tshark()
-*O)
local prefix=
[[ $cur == *,* ]] && prefix="${cur%,*},"
[ -n "$_tshark_protocols" ] ||
[ -v _tshark_protocols ] ||
_tshark_protocols="$("$1" -G protocols 2>/dev/null |
cut -f 3 | tr '\n' ' ')"
COMPREPLY=($(compgen -W "$_tshark_protocols" -- "${cur##*,}"))
Expand Down
2 changes: 1 addition & 1 deletion completions/ulimit
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ _ulimit()
;;
esac

if [[ -z $mode ]]; then
if [[ ! -v mode ]]; then
local word
for word in "${words[@]}"; do
[[ $word == -*a* ]] && return
Expand Down
2 changes: 1 addition & 1 deletion completions/update-alternatives
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ _update_alternatives()
fi
done

case $mode in
case ${mode-} in
--install)
case $args in
1 | 3)
Expand Down
4 changes: 4 additions & 0 deletions test/t/test_ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ def test_capital_f_without_space(self, completion):
"option requires an argument -- F" in x for x in completion
)

@pytest.mark.complete("ssh -F nonexistent ")
def test_capital_f_nonexistent(self, completion):
assert completion

def test_partial_hostname(self, bash, known_hosts):
first_char, partial_hosts = partialize(bash, known_hosts)
completion = assert_complete(bash, "ssh %s" % first_char)
Expand Down

0 comments on commit 505fb2c

Please sign in to comment.