我有一个bash函数,它封装了一个命令,用于向teamcity报告结果(连续集成测试)。我的问题是我无法通过预先设置!
来否定结果。
function run_as_teamcity_test
{
local name="$1"
shift
echo "##teamcity[testStarted name='$name' captureStandardOutput='true']"
if ! $@; then
ANY_TEST_FAILURE=true
FAILED_TESTS="${FAILED_TESTS} ${name}\n"
echo "##teamcity[testFailed name='$name']"
echo -e "\033[31mTests failed: $name\033[0m"
fi
echo "##teamcity[testFinished name='$name']"
}
这有效:
> run_as_teamcity_test "Should be true" echo "OK"
##teamcity[testStarted name='Should be true' captureStandardOutput='true']
OK
##teamcity[testFinished name='Should be true']
这不起作用:
> run_as_teamcity_test "Should be false" ! echo "Not OK"
##teamcity[testStarted name='Should be false' captureStandardOutput='true']
!: command not found
##teamcity[testFailed name='Should be false']
Tests failed: Should be false
##teamcity[testFinished name='Should be false']
Bash正在寻找命令!
,而不是使用内置命令。有没有办法让它使用内置??
答案 0 :(得分:0)
您遇到的问题是bash
没有一流的功能。最好的办法是定义一个全局函数并将名称作为参数传递。
test_goodtest () { echo "OK"; }
test_badtest () { ! echo "NOT OK"; }
run_as_teamcity_test ()
{
local name=$1
echo "##teamcity[testStarted name='$name' captureStandardOutput='true']"
if ! "$name"; then
ANY_TEST_FAILURE=true
FAILED_TESTS="${FAILED_TESTS} ${name}\n"
echo "##teamcity[testFailed name='$name']"
echo -e "\033[31mTests failed: $name\033[0m"
fi
echo "##teamcity[testFinished name='$name']"
}
run_as_teamcity_test test_goodteset
run_as_teamcity_test test_badtest
我认为函数的名称可以加倍作为测试的名称,但如果您愿意,可以为run_as_teamcity_test
添加单独的参数。
答案 1 :(得分:0)
我最终使用辅助函数而不是!
:
function negate
{
! "$@"
}
并像这样使用它:
> run_as_teamcity_test "Should be false" negate echo "Not OK"
##teamcity[testStarted name='Should be false' captureStandardOutput='true']
Not OK
##teamcity[testFailed name='Should be false']
Tests failed: Should be false
##teamcity[testFinished name='Should be false']