I'm trying to unit test some bash scripts. One of my functions returns 1 as a false value, which I can use in if tests like
foo() {
return 0
}
if foo; then
echo "Passed"
fi
This is really handy for writing clean code.
However when trying to unit test this code using shunit2, the assertFalse
assertion needs a string value to test.
I've got around this by writing tests like this:
test_foo() {
local result="$(foo; echo $?)"
assertFalse "$result"
}
but this fails when foo
returns 1
. The echo $?
seems to be echoing an empty string, rather than the actual return code.
Any idea what's going on?
edit
This only seems to occur when set -e
is set. No idea why though.