Created
January 18, 2017 22:39
-
-
Save brianoflan/ca9c789ba4a40dbc272994f4cbb1e6f8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
pre() { | |
local f=$1 ; | |
mkdir test_$f &> /dev/null ; | |
ln -s ../functions.sh test_$f/functions.sh ; | |
cat bash_script.sh.pre > test_$f/bash_script.sh | |
echo "$MAIN" >> test_$f/bash_script.sh ; | |
cat bash_script.sh.post >> test_$f/bash_script.sh | |
} | |
post() { | |
local f=$1 ; | |
{ bash test_$f/bash_script.sh ; echo $? ; } &> test_$f/out.txt ; | |
e=0 ; | |
echo "Testing $f()" | |
diff -dbB test_$f/out.txt test_$f/out.txt.proper > $tmpf || e=$? ; cat $tmpf | perl -ne 'print " $_"' ; | |
if [[ $e -ne 0 ]] ; then | |
echo "ERROR with $f(): '$e'." ; export errors="$errors $f" ; | |
fi ; | |
echo ; | |
} | |
main() { | |
rm -rf demo_bash_scripts &>/dev/null ; | |
[[ -d demo_bash_scripts ]] || mkdir demo_bash_scripts ; | |
cd demo_bash_scripts && { | |
tmpf=`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` ; | |
export errors='' ; | |
cat > functions.sh <<'EOF' | |
#!/bin/bash | |
die() { | |
local exit=$2 ; | |
echo "$exit" | egrep '^[0-9][0-9]*$' &> /dev/null || exit=1 ; | |
echo $1 ; | |
exit $exit ; | |
} | |
execute() { # Notice and say so when a command has a non-zero exit status. | |
local e=0 ; | |
# echo "Executing." 1>&2 ; | |
"$@" || e=$? ; | |
[[ $e -eq 0 ]] || echo "ERROR: $e executing {$@}." 1>&2 ; | |
return $e ; | |
} | |
vexecute() { # Verbose execute (say the command before running it). | |
echo "Executing {$@}." 1>&2 ; | |
execute "$@" ; | |
} | |
[[ $DEBUG -lt 2 ]] || echo "Sourced functions.sh ." 1>&2 ; | |
true ; | |
# | |
EOF | |
cat > bash_script.sh.pre <<'EOF' | |
#!/bin/bash | |
main() { | |
EOF | |
cat > bash_script.sh.post <<'EOF' | |
} | |
final() { # Never define final() in functions.sh - only ever define it in an actual script. | |
[[ $DEBUG -lt 2 ]] || echo "final() { \$?: $? }" ; # Usually shows an exit code of 1 for some mysterious reason. QQQ | |
echo final_in_script ; | |
} | |
trap final EXIT ; | |
cd $(dirname $0) && main "$@" || { e=$? ; echo "ERROR: '$e' with main in $0." ; exit $e ; } ; | |
# | |
EOF | |
# # # | |
f=debug ; | |
export MAIN=" | |
source ./functions.sh ; | |
" ; | |
pre $f ; | |
cat > test_$f/out.txt.proper <<'EOF' | |
Sourced functions.sh . | |
final() { $?: 1 } | |
final_in_script | |
0 | |
EOF | |
export DEBUG=2 ; | |
post $f | |
export DEBUG=0 ; | |
# # # | |
f='return' ; | |
export MAIN=" | |
return 1 ; | |
" ; | |
pre $f ; | |
cat > test_$f/out.txt.proper <<EOF | |
ERROR: '1' with main in test_$f/bash_script.sh. | |
final_in_script | |
1 | |
EOF | |
post $f | |
# # # | |
f=execute ; | |
export MAIN=" | |
source ./functions.sh ; | |
execute bash -c 'exit 1' | |
echo ; | |
" ; | |
pre $f ; | |
cat > test_$f/out.txt.proper <<'EOF' | |
ERROR: 1 executing {bash -c exit 1}. | |
final_in_script | |
0 | |
EOF | |
post $f | |
# # # | |
f=execute_and_die ; | |
export MAIN=" | |
source ./functions.sh ; | |
execute bash -c 'exit 1' || die 'Nay.' | |
" ; | |
pre $f ; | |
cat > test_$f/out.txt.proper <<'EOF' | |
ERROR: 1 executing {bash -c exit 1}. | |
Nay. | |
final_in_script | |
1 | |
EOF | |
post $f | |
# # # | |
f=vexecute ; | |
export MAIN=" | |
source ./functions.sh ; | |
vexecute bash -c 'exit 1' | |
echo | |
" ; | |
pre $f ; | |
cat > test_$f/out.txt.proper <<'EOF' | |
Executing {bash -c exit 1}. | |
ERROR: 1 executing {bash -c exit 1}. | |
final_in_script | |
0 | |
EOF | |
post $f | |
# # # | |
f=vexecute_and_die ; | |
export MAIN=" | |
source ./functions.sh ; | |
execute bash -c 'exit 1' || die 'Nay.' | |
" ; | |
pre $f ; | |
cat > test_$f/out.txt.proper <<'EOF' | |
ERROR: 1 executing {bash -c exit 1}. | |
Nay. | |
final_in_script | |
1 | |
EOF | |
post $f | |
# # # | |
# # # | |
if [[ $errors ]] ; then | |
echo -e "\n\n\nERRORS:$errors.\n" ; | |
return 1 ; | |
fi ; | |
} | |
} | |
final() { | |
cd .. ; rm -rf demo_bash_scripts.old &>/dev/null ; mv demo_bash_scripts demo_bash_scripts.old ; | |
} | |
trap final EXIT ; | |
main || exit $? ; | |
# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment