Skip to content

Add more handling for undefined vars #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions shpy
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ 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

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}"
Expand Down Expand Up @@ -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
}
Expand Down
48 changes: 48 additions & 0 deletions t/test_cleanupSpies
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/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

# shellcheck disable=SC2154
assertNotNull "${_shpy_spies_dir}"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Private variables (those starting with an underscore, which also isn't documented anywhere…) shouldn't be used directly in testing. Instead, you could try:

  • Stubbing the mktmp call to return a known directory and verifying that directory is removed
  • Do a pattern match for a directory named shpy.[0-9]+, keeping in mind that zsh doesn't glob like other shells
  • Stub all the i/o functions to verify a call for a tmp directory was made and a call to rm it was made

It's likely that $_shpy_spies_dir reflects the directory on disk, but it's best to check for the directory itself because that's what the end user would see 🔍

[ -d "${_shpy_spies_dir}" ] || fail "Temporary directory does not exist"

cleanupSpies

[ ! -d "${_shpy_spies_dir}" ] || fail "Temporary directory should have been removed"
}

testWorksUnderNoUnsetOption() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be removed, my upcoming PR runs all tests with nounset to ensure shpy runs under those stricter conditions

[ -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"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is good, but you should remove the nounset and private variable references. You shouldn't have to remove the dir tree every time, if you add a teardown function you can more-or-less ensure the test slate is clean

tearDown() {
    cleanupSpies
}

}


# shellcheck source=/dev/null
. "$TEST_DIR/shunit2"
2 changes: 2 additions & 0 deletions t/test_createSpy
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

readonly TEST_DIR="$(dirname "$0")"

set -o nounset
Copy link
Owner

@codehearts codehearts Aug 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be removed, I set it globally for all tests in my upcoming PR


# shellcheck source=/dev/null
. "$TEST_DIR/../shpy"

Expand Down