From be0af4287aeb373a620fdba428f7d58fab0cabd0 Mon Sep 17 00:00:00 2001 From: Chris Yee Date: Thu, 25 Jul 2019 20:50:17 -0400 Subject: [PATCH 1/3] Add more handling for undefined vars Also ensure tests are run with 'nounset' flag. --- shpy | 6 +++--- t/test_cleanupSpies | 47 +++++++++++++++++++++++++++++++++++++++++++++ t/test_createSpy | 2 ++ 3 files changed, 52 insertions(+), 3 deletions(-) create mode 100755 t/test_cleanupSpies diff --git a/shpy b/shpy index bceaf4e..76c2d4a 100644 --- a/shpy +++ b/shpy @@ -29,7 +29,7 @@ createSpy() { echo "$OPTARG" > "$errors_dir/$error_num" error_num=$((error_num + 1)) ;; - r) val="$val $OPTARG" ;; + r) val="${val:-} $OPTARG" ;; *) shpy_die "Error: Unknown option -$OPTARG" ;; esac done @@ -37,7 +37,7 @@ createSpy() { shift $(( OPTIND - 1 )) OPTIND=1 - [ -n "$1" ] || shpy_die "Error: Missing spy name" + [ $# -eq 0 ] && shpy_die "Error: Missing spy name" _shpyResetSpy "$1" _shpySpySetReturnValue "$1" "${val:-0}" @@ -144,7 +144,7 @@ examineNextSpyCall() { } cleanupSpies() { - if [ -n "$_shpy_spies_dir" ]; then + if [ -n "${_shpy_spies_dir+is_set}" ]; then shpy_remove_dir_tree "$_shpy_spies_dir" || shpy_die "Error: \`shpy_remove_dir_tree '$_shpy_spies_dir'\` failed" fi } diff --git a/t/test_cleanupSpies b/t/test_cleanupSpies new file mode 100755 index 0000000..3574f87 --- /dev/null +++ b/t/test_cleanupSpies @@ -0,0 +1,47 @@ +#!/bin/sh + +readonly TEST_DIR="$(dirname "$0")" + +# shellcheck source=/dev/null +. "$TEST_DIR/../shpy" +# shellcheck source=/dev/null +. "$TEST_DIR/../shpy-shunit2" + +testRemovesDirectory() { + createSpy foo + + assertNotNull "${_shpy_spies_dir}" + [ -d "${_shpy_spies_dir}" ] || fail "Temporary directory does not exist" + + cleanupSpies + + [ ! -d "${_shpy_spies_dir}" ] || fail "Temporary directory should have been removed" +} + +testWorksUnderNoUnsetOption() { + [ -d "${_shpy_spies_dir}" ] && shpy_remove_dir_tree "${_shpy_spies_dir}" + + unset -v _shpy_spies_dir + unset -v _shpy_inited + + set -o nounset + + createSpy foo + + cleanupSpies || fail "Should exit cleanly" +} + +testDoesNotFailIfNoSpiesCreated() { + [ -d "${_shpy_spies_dir}" ] && shpy_remove_dir_tree "${_shpy_spies_dir}" + + unset -v _shpy_spies_dir + unset -v _shpy_inited + + set -o nounset + + cleanupSpies || fail "Should exit cleanly" +} + + +# shellcheck source=/dev/null +. "$TEST_DIR/shunit2" diff --git a/t/test_createSpy b/t/test_createSpy index ee10664..e172de3 100755 --- a/t/test_createSpy +++ b/t/test_createSpy @@ -2,6 +2,8 @@ readonly TEST_DIR="$(dirname "$0")" +set -o nounset + # shellcheck source=/dev/null . "$TEST_DIR/../shpy" From 80df4507aa01d5b84ab3c02a7a230e238bc1e58c Mon Sep 17 00:00:00 2001 From: Chris Yee Date: Thu, 25 Jul 2019 21:12:23 -0400 Subject: [PATCH 2/3] Tell shellcheck to ignore known variable --- t/test_cleanupSpies | 1 + 1 file changed, 1 insertion(+) diff --git a/t/test_cleanupSpies b/t/test_cleanupSpies index 3574f87..a16154b 100755 --- a/t/test_cleanupSpies +++ b/t/test_cleanupSpies @@ -10,6 +10,7 @@ readonly TEST_DIR="$(dirname "$0")" testRemovesDirectory() { createSpy foo + # shellcheck disable=SC2154 assertNotNull "${_shpy_spies_dir}" [ -d "${_shpy_spies_dir}" ] || fail "Temporary directory does not exist" From 03ecde5b3a222990a15070c74217e45609fc257b Mon Sep 17 00:00:00 2001 From: Chris Yee Date: Sun, 4 Aug 2019 10:25:58 -0400 Subject: [PATCH 3/3] Update t/test_cleanupSpies https://github.com/codehearts/shpy/pull/18#discussion_r310367714 Co-Authored-By: Katie --- t/test_cleanupSpies | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/test_cleanupSpies b/t/test_cleanupSpies index a16154b..21f521e 100755 --- a/t/test_cleanupSpies +++ b/t/test_cleanupSpies @@ -7,7 +7,7 @@ readonly TEST_DIR="$(dirname "$0")" # shellcheck source=/dev/null . "$TEST_DIR/../shpy-shunit2" -testRemovesDirectory() { +testCleanupSpies_RemovesTmpDirectory() { createSpy foo # shellcheck disable=SC2154