【发布时间】:2015-08-13 17:12:42
【问题描述】:
根据这个问题is-there-a-way-to-write-a-bash-function-which-aborts-the-whole-execution...的一个技巧
我的示例代码(example.sh):
trap "exit 0" TERM
top_pid=$$
evalInput(){
cmd=$1
if [[ $cmd =~ ^\ *exit\ *$ ]]; then
kill -s TERM $top_pid
elif [another command]
...
fi
}
如果我输入evalInput "exit",那么这个进程将被终止,退出状态为零。
测试文件:
testExitCmd(){
. ./example.sh
...
evalInput "exit"
ps [PID of `evalInput` function]
# `ps` command is failed if evalInput is killed.
assertNotEquals "0" "$?"
}
. shunit2
我测试evalInput函数的想法很简单,只需使用ps命令来确保evalInput函数被杀死但问题是我怎么能做到这一点?这里的重要问题是当你尝试执行evalInput 时,这意味着你也杀死了testExitCmd 函数。
我已经尝试了很多方法,例如使用& 将evalInput 放入另一个进程,然后bla bla bla。但我仍然收到类似shunit2:WARN trapped and now handling the (TERM) signal 的错误。据我了解,这表明我试图杀死我的测试功能进程。
请仔细测试,我不认为只有你的想象力可以解决这个问题,但请测试代码。如果您使用的是OSX,您可以通过brew 安装shUnit2,然后通过./your_test_script.sh 运行它
【问题讨论】:
标签: bash shell unit-testing shunit2