Skip to content

Instantly share code, notes, and snippets.

@daveadams
Last active September 27, 2016 22:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save daveadams/91f926e9553c95810da2bb0fd21fafe5 to your computer and use it in GitHub Desktop.
Save daveadams/91f926e9553c95810da2bb0fd21fafe5 to your computer and use it in GitHub Desktop.
#!/bin/bash
echo "Failure mode test suite for bash"
echo " running bash $BASH_VERSION"
try() {
trap 'echo FAILS' EXIT
case $1 in
simple)
printf " %-34s" "simple failed command"
/bin/false
;;
cmdsub)
printf " %-34s" "failed command substitution"
local x=$( /bin/false )
;;
pipe)
printf " %-34s" "failed pipe command input"
/bin/false |cat
;;
pipecmdsub)
printf " %-34s" "failed pipe command substitution"
local x=$( /bin/false |cat )
;;
esac
echo "[ $? ]"
trap '' EXIT
}
for flags in "" "-e" "-o pipefail" "-eo pipefail"; do
echo
[[ -z $flags ]] \
&& echo "Running with no error-catching:" \
|| echo "Running with \`set $flags\`:"
for failure in simple cmdsub pipe pipecmdsub; do
(
[[ -n $flags ]] && eval "set $flags"
try $failure
)
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment