According to a trick from this question is-there-a-way-to-write-a-bash-function-which-aborts-the-whole-execution...
My example code (example.sh):
trap "exit 0" TERM
top_pid=$$
evalInput(){
cmd=$1
if [[ $cmd =~ ^\ *exit\ *$ ]]; then
kill -s TERM $top_pid
elif [another command]
...
fi
}
If I type evalInput "exit"
then this process will be killed with exit status zero.
Test file:
testExitCmd(){
. ./example.sh
...
evalInput "exit"
ps [PID of `evalInput` function]
# `ps` command is failed if evalInput is killed.
assertNotEquals "0" "$?"
}
. shunit2
My idea to test evalInput
function is very simple, just use ps
command to make sure that evalInput
function is killed but the problem is how I can done this? The important issue here is when you try to execute evalInput
that mean you also kill testExitCmd
function.
I've tried many ways already e.g. using &
to put evalInput
to another process and bla bla bla. but I still get an error like shunit2:WARN trapped and now handling the (TERM) signal
. As I understand, this indecate that I try to kill my test function process.
Please carefully test it, I don't think just only your imagination can solve this problem but please test a code. If you are on OSX
you can simply install shUnit2
via brew
and simply run it by ./your_test_script.sh