Skip to content

Commit

Permalink
replace all usages of exit
Browse files Browse the repository at this point in the history
- those skipped the ERR trap
- add die function to exit with line number and message
  • Loading branch information
MartinNowak committed Jul 4, 2017
1 parent 3317ad8 commit 6e9de34
Show file tree
Hide file tree
Showing 34 changed files with 84 additions and 119 deletions.
5 changes: 2 additions & 3 deletions test/0-init-fail-json.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ packname="0-init-fail-pack"
deps="logger PACKAGE_DONT_EXIST" # would be very unlucky if it does exist...

if $$DUB init -n $packname $deps -f json 2>/dev/null; then
>&2 echo 'Init with unknown non-existing dependency expected to fail'
die $LINENO 'Init with unknown non-existing dependency expected to fail'
fi


Expand All @@ -15,6 +15,5 @@ function cleanup {

if [ -e $packname/dub.json ]; then # package is there, it should have failed
cleanup
exit 1
die $LINENO "$packname/dub.json was not created"
fi
exit 0
5 changes: 2 additions & 3 deletions test/0-init-fail.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ packname="0-init-fail-pack"
deps="logger PACKAGE_DONT_EXIST" # would be very unlucky if it does exist...

if $DUB init -n $packname $deps 2>/dev/null; then
>&2 echo 'Init with unknown non-existing dependency expected to fail'
die $LINENO 'Init with unknown non-existing dependency expected to fail'
fi

function cleanup {
Expand All @@ -14,6 +14,5 @@ function cleanup {

if [ -e $packname/dub.sdl ]; then # package is there, it should have failed
cleanup
exit 1
die $LINENO "$packname/dub.sdl was not created"
fi
exit 0
9 changes: 3 additions & 6 deletions test/0-init-interactive.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@ function cleanup {
}

if [ ! -e $packname/dub.sdl ]; then # it failed
echo "No dub.sdl file has been generated."
cleanup
exit 1
die $LINENO 'No dub.sdl file has been generated.'
fi

if ! diff $packname/dub.sdl "$CURR_DIR"/0-init-interactive.dub.sdl; then
echo "Contents of generated dub.sdl not as expected."
cleanup
exit 1
cleanup
die $LINENO 'Contents of generated dub.sdl not as expected.'
fi

cleanup
exit 0
9 changes: 3 additions & 6 deletions test/0-init-multi-json.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,17 @@ function cleanup {
rm -rf $packname
}

if [ ! -e $packname/dub.json ]; then # it failed, exit 1
exit 1
if [ ! -e $packname/dub.json ]; then
die $LINENO '$packname/dub.json not created'
else # check if resulting dub.json has all dependencies in tow
deps="$deps vibe-d";
IFS=" " read -a arr <<< "$deps"
for ele in "${arr[@]}"
do
if [ `grep -c "$ele" $packname/dub.json` -ne 1 ]; then #something went wrong
echo "$ele not in $packname/dub.json"
cleanup
exit 1
die $LINENO "$ele not in $packname/dub.json"
fi
done
cleanup
exit 0

fi
10 changes: 3 additions & 7 deletions test/0-init-multi.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,18 @@ function cleanup {
rm -rf $packname
}

if [ ! -e $packname/dub.sdl ]; then # it failed, exit 1
echo "No dub.sdl file has been generated."
if [ ! -e $packname/dub.sdl ]; then
cleanup
exit 1
die $LINENO 'No dub.sdl file has been generated.'
else # check if resulting dub.sdl has all dependencies in tow
deps="$deps vibe-d";
IFS=" " read -a arr <<< "$deps"
for ele in "${arr[@]}"
do
if [ `grep -c "$ele" $packname/dub.sdl` -ne 1 ]; then #something went wrong
echo "$ele not in $packname/dub.sdl"
cleanup
exit 1
die $LINENO "$ele not in $packname/dub.sdl"
fi
done
cleanup
exit 0

fi
5 changes: 2 additions & 3 deletions test/0-init-simple-json.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ function cleanup {
rm -rf $packname
}

if [ ! -e $packname/dub.json ]; then # it failed
if [ ! -e $packname/dub.json ]; then
cleanup
exit 1
die $LINENO 'No dub.json file has been generated.'
fi
cleanup
exit 0
4 changes: 1 addition & 3 deletions test/0-init-simple.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ function cleanup {
}

if [ ! -e $packname/dub.sdl ]; then # it failed
echo "No dub.sdl file has been generated."
cleanup
exit 1
die $LINENO 'No dub.sdl file has been generated.'
fi
cleanup
exit 0
4 changes: 2 additions & 2 deletions test/4-describe-data-1-list.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ if ! $DUB describe --compiler=$DC --data-list \
--data=post-build-commands \
'--data=requirements, options' \
> "$temp_file"; then
die 'Printing project data failed!'
die $LINENO 'Printing project data failed!'
fi

# Create the expected output path file to compare against.
Expand Down Expand Up @@ -134,6 +134,6 @@ echo "warnings" >> "$expected_file"
#echo "stackStomping" >> "$expected_file" # Not sure if this (from a sourceLib dependency) should be missing from the result

if ! diff "$expected_file" "$temp_file"; then
die 'The project data did not match the expected output!'
die $LINENO 'The project data did not match the expected output!'
fi

9 changes: 3 additions & 6 deletions test/5-convert-stdout.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@ targetType \"executable\""
RESULT=`${DUB} convert -s -f sdl`

if [ ! -f dub.json ]; then
echo "Package recipe got modified!"
exit 1
die $LINENO 'Package recipe got modified!'
fi

if [ -f dub.sdl ]; then
echo "An SDL recipe got written."
exit 2
die $LINENO 'An SDL recipe got written.'
fi

if [ "$RESULT" != "$EXPECTED" ]; then
echo "Unexpected SDLang output."
exit 3
die $LINENO 'Unexpected SDLang output.'
fi
18 changes: 5 additions & 13 deletions test/5-convert.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,23 @@ temp_file=$(mktemp $(basename $0).XXXXXX)
function cleanup {
rm $temp_file
}

function die {
echo "$@" 1>&2
exit 1
}

trap cleanup EXIT

cp dub.sdl dub.sdl.ref

$DUB convert -f json

if [ -f "dub.sdl" ]; then die 'Old recipe file not removed.'; fi
if [ ! -f "dub.json" ]; then die 'New recipe file not created.'; fi
if [ -f "dub.sdl" ]; then die $LINENO 'Old recipe file not removed.'; fi
if [ ! -f "dub.json" ]; then die $LINENO 'New recipe file not created.'; fi

$DUB convert -f sdl

if [ -f "dub.json" ]; then die 'Old recipe file not removed.'; fi
if [ ! -f "dub.sdl" ]; then die 'New recipe file not created.'; fi
if [ -f "dub.json" ]; then die $LINENO 'Old recipe file not removed.'; fi
if [ ! -f "dub.sdl" ]; then die $LINENO 'New recipe file not created.'; fi

if ! diff "dub.sdl" "dub.sdl.ref"; then
die 'The project data did not match the expected output!'
die $LINENO 'The project data did not match the expected output!'
fi

rm dub.sdl.ref

echo OK

11 changes: 8 additions & 3 deletions test/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ SOURCE_FILE=$_

set -ueEo pipefail

function error {
>&2 echo "Error: $SOURCE_FILE failed at line $1"
# lineno[, msg]
function die() {
local line=$1
local msg=${2:-command failed}
local rc=${3:-1}
>&2 echo "$SOURCE_FILE:$1 Error: $msg"
exit $rc
}
trap 'error $LINENO' ERR
trap 'die $LINENO' ERR
12 changes: 9 additions & 3 deletions test/feat663-search.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
#!/usr/bin/env bash

. $(dirname "${BASH_SOURCE[0]}")/common.sh
${DUB} search 2>/dev/null && exit 1
${DUB} search nonexistent123456789package 2>/dev/null && exit 1
${DUB} search dub | grep -q '^dub' || exit 1
if ${DUB} search 2>/dev/null; then
die $LINENO '`dub search` succeeded'
fi
if ${DUB} search nonexistent123456789package 2>/dev/null; then
die $LINENO '`dub search nonexistent123456789package` succeeded'
fi
if ! ${DUB} search dub | grep -q '^dub'; then
die $LINENO '`dub search dub` failed'
fi
15 changes: 5 additions & 10 deletions test/interactive-remove.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,27 @@
$DUB fetch dub --version=0.9.20 && [ -d $HOME/.dub/packages/dub-0.9.20/dub ]
$DUB fetch dub --version=0.9.21 && [ -d $HOME/.dub/packages/dub-0.9.21/dub ]
if $DUB remove dub --non-interactive 2>/dev/null; then
echo "Non-interactive remove should fail" 1>&2
exit 1
die $LINENO 'Non-interactive remove should fail'
fi
echo 1 | $DUB remove dub | tr --delete '\n' | grep --ignore-case 'select.*0\.9\.20.*0\.9\.21.*'
if [ -d $HOME/.dub/packages/dub-0.9.20/dub ]; then
echo "Failed to remove dub-0.9.20" 1>&2
exit 1
die $LINENO 'Failed to remove dub-0.9.20'
fi
$DUB fetch dub --version=0.9.20 && [ -d $HOME/.dub/packages/dub-0.9.20/dub ]
# EOF aborts remove
echo -xn '' | $DUB remove dub
if [ ! -d $HOME/.dub/packages/dub-0.9.20/dub ] || [ ! -d $HOME/.dub/packages/dub-0.9.21/dub ]; then
echo "Aborted dub still removed a package" 1>&2
exit 1
die $LINENO 'Aborted dub still removed a package'
fi
# validates input
echo -e 'abc\n4\n-1\n3' | $DUB remove dub
if [ -d $HOME/.dub/packages/dub-0.9.20/dub ] || [ -d $HOME/.dub/packages/dub-0.9.21/dub ]; then
echo "Failed to remove all version of dub" 1>&2
exit 1
die $LINENO 'Failed to remove all version of dub'
fi
$DUB fetch dub --version=0.9.20 && [ -d $HOME/.dub/packages/dub-0.9.20/dub ]
$DUB fetch dub --version=0.9.21 && [ -d $HOME/.dub/packages/dub-0.9.21/dub ]
# is non-interactive with --version=
$DUB remove dub --version=\*
if [ -d $HOME/.dub/packages/dub-0.9.20/dub ] || [ -d $HOME/.dub/packages/dub-0.9.21/dub ]; then
echo 'Failed to non-interactively remove specified versions' 1>&2
exit 1
die $LINENO 'Failed to non-interactively remove specified versions'
fi
2 changes: 1 addition & 1 deletion test/issue1004-override-config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

. $(dirname "${BASH_SOURCE[0]}")/common.sh
cd ${CURR_DIR}/issue1004-override-config
${DUB} build --bare main --override-config a/success || exit 1
${DUB} build --bare main --override-config a/success
2 changes: 1 addition & 1 deletion test/issue1005-configuration-resolution.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

. $(dirname "${BASH_SOURCE[0]}")/common.sh
cd ${CURR_DIR}/issue1005-configuration-resolution
${DUB} build --bare main || exit 1
${DUB} build --bare main
8 changes: 3 additions & 5 deletions test/issue1024-selective-upgrade.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
. $(dirname "${BASH_SOURCE[0]}")/common.sh
cd ${CURR_DIR}/issue1024-selective-upgrade
echo "{\"fileVersion\": 1,\"versions\": {\"a\": \"1.0.0\", \"b\": \"1.0.0\"}}" > main/dub.selections.json
$DUB upgrade --bare --root=main a || exit 1
$DUB upgrade --bare --root=main a

if ! grep -c -e "\"a\": \"1.0.1\"" main/dub.selections.json; then
echo "Specified dependency was not upgraded."
exit 1
die $LINENO "Specified dependency was not upgraded."
fi

if grep -c -e "\"b\": \"1.0.1\"" main/dub.selections.json; then
echo "Non-specified dependency got upgraded."
exit 1
die $LINENO "Non-specified dependency got upgraded."
fi
6 changes: 2 additions & 4 deletions test/issue103-single-file-package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ rm -f single-file-test

${DUB} run --single issue103-single-file-package-json.d --compiler=${DC}
if [ ! -f single-file-test ]; then
echo "Normal invocation did not produce a binary in the current directory"
exit 1
die $LINENO 'Normal invocation did not produce a binary in the current directory'
fi
rm single-file-test

Expand All @@ -15,6 +14,5 @@ rm single-file-test
${DUB} issue103-single-file-package-w-dep.d

if [ -f single-file-test ]; then
echo "Shebang invocation produced binary in current directory"
exit 1
die $LINENO 'Shebang invocation produced binary in current directory'
fi
4 changes: 2 additions & 2 deletions test/issue1091-bogus-rebuild.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

cd ${CURR_DIR}/1-exec-simple
rm -f dub.selections.json
${DUB} build --compiler=${DC} 2>&1 | grep -e "building configuration" -c || exit 1
${DUB} build --compiler=${DC} 2>&1 | grep -e "building configuration" -c && exit 1 || exit 0
${DUB} build --compiler=${DC} 2>&1 | grep -e 'building configuration' -c
${DUB} build --compiler=${DC} 2>&1 | { ! grep -e 'building configuration' -c; }
2 changes: 1 addition & 1 deletion test/issue346-redundant-flags.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

. $(dirname "${BASH_SOURCE[0]}")/common.sh
cd ${CURR_DIR}/issue346-redundant-flags
${DUB} build --bare --force --compiler=${DC} -a x86_64 -v main 2>&1 | grep -e "-m64 -m64" -c && exit 1 || exit 0
${DUB} build --bare --force --compiler=${DC} -a x86_64 -v main 2>&1 | { ! grep -e '-m64 -m64' -c; }
8 changes: 4 additions & 4 deletions test/issue361-optional-deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ rm -rf main1/.dub
rm -rf main2/.dub
rm -f main1/dub.selections.json

${DUB} build --bare --compiler=${DC} main1 || exit 1
${DUB} build --bare --compiler=${DC} main1
echo "{" > cmp.tmp
echo " \"fileVersion\": 1," >> cmp.tmp
echo " \"versions\": {" >> cmp.tmp
echo " \"b\": \"~master\"" >> cmp.tmp
echo " }" >> cmp.tmp
echo "}" >> cmp.tmp
diff cmp.tmp main1/dub.selections.json || exit 1
diff cmp.tmp main1/dub.selections.json

${DUB} build --bare --compiler=${DC} main2 || exit 1
${DUB} build --bare --compiler=${DC} main2
echo "{" > cmp.tmp
echo " \"fileVersion\": 1," >> cmp.tmp
echo " \"versions\": {" >> cmp.tmp
echo " \"a\": \"~master\"" >> cmp.tmp
echo " }" >> cmp.tmp
echo "}" >> cmp.tmp
diff cmp.tmp main2/dub.selections.json || exit 1
diff cmp.tmp main2/dub.selections.json
2 changes: 1 addition & 1 deletion test/issue564-invalid-upgrade-dependency.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ cd ${CURR_DIR}/issue564-invalid-upgrade-dependency
rm -rf a-1.0.0/.dub
rm -rf a-1.1.0/.dub
rm -rf main/.dub
${DUB} build --bare --compiler=${DC} main || exit 1
${DUB} build --bare --compiler=${DC} main
4 changes: 2 additions & 2 deletions test/issue586-subpack-dep.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ cd ${CURR_DIR}/issue586-subpack-dep
rm -rf a/.dub
rm -rf a/b/.dub
rm -rf main/.dub
${DUB} build --bare --compiler=${DC} main || exit 1
${DUB} run --bare --compiler=${DC} main || exit 1
${DUB} build --bare --compiler=${DC} main
${DUB} run --bare --compiler=${DC} main
3 changes: 1 addition & 2 deletions test/issue613-dynlib-pic.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
cd ${CURR_DIR}/issue613-dynlib-pic
rm -rf .dub
if [ "${DC}" = "dmd" ]; then
${DUB} build --compiler=${DC} || exit 1
${DUB} build --compiler=${DC}
else
echo "Skipping shared library test for ${DC}..."
fi

5 changes: 2 additions & 3 deletions test/issue672-upgrade-optional.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
cd ${CURR_DIR}/issue672-upgrade-optional
rm -rf b/.dub
echo "{\"fileVersion\": 1,\"versions\": {\"dub\": \"1.0.0\"}}" > dub.selections.json
${DUB} upgrade || exit 1
${DUB} upgrade

if ! grep -c -e "\"dub\": \"1.1.0\"" dub.selections.json; then
echo "Dependency not upgraded."
exit 1
die $LINENO 'Dependency not upgraded.'
fi
Loading

0 comments on commit 6e9de34

Please sign in to comment.