Clarification on `oneTimeSetUp()` in `shunit2`
adamgranthendry opened this issue · comments
I'm trying to understand when and how oneTimeSetUp()
is called in shunit2
. At the end (lines 1371-1374), I see it gets called like so:
# Execute the oneTimeSetUp function (if it exists).
if ! oneTimeSetUp; then
_shunit_fatal "oneTimeSetUp() returned non-zero return code."
fi
but wouldn't this also fail if oneTimeSetUp()
doesn't exist? If it doesn't, oneTimeSetUp would be ''
(i.e. NULL) and ! ''
evaluates to true (i.e. test ''
returns a non-zero error code).
I would have expected this to be
# Execute the oneTimeSetUp function (if it exists).
if type oneTimeSetUp >/dev/null 2>&1 && ! oneTimeSetUp; then
_shunit_fatal "oneTimeSetUp() returned non-zero return code."
fi
How does the code work without erroring when no oneTimeSetUp()
exists? The same question applies to the segment of code in shunit2
that calls oneTimeTearDown()
.
The _shunit_mktempFunc()
function generates stub functions that are written to disk as executables.
https://github.com/kward/shunit2/blob/master/shunit2#L939
It is called as part of the main of shUnit2 during startup so that those functions exist, even if the script doesn't have them.
https://github.com/kward/shunit2/blob/master/shunit2#L1302
If the functions exist in the script, shells will use the local function over any function in the path, which is what I'm taking advantage of here.