我想测试这个functions.sh
:
#!/usr/bin/env bash
function func {
false # bats should ignore this
return 0
}
使用这个单元测试:
#!/usr/bin/env bats
@test "test func" {
source ./functions.sh
func
}
bat失败
✗ test func
(from function `func' in file ./functions.sh, line 4,
in test file test.sh, line 5)
`func' failed
,尽管函数返回0。
我怎么能源一个脚本,其中有功能与行,其中失败?可以用grep来代替false
。
我找到了一个解决方案,这里有一个相关的问题。
由于bat使用-e
运行,如果源脚本中的一行失败,则源立即失败。
要解决这个问题,您可以用if ...;then
包裹失败的命令,或者用F=$(...) || true
分配它。
functions.sh
现在的样子:
#!/usr/bin/env bash
function func {
if ! false; then
echo "false"
fi
F=$(false) || true
return 0
}
文档:https://manpages.org/set
感谢larsks提示的解决方案!