在嘗試assert_failure呼叫一個名為 的函式時some_function,我在傳遞超過 1 個引數時遇到了一些困難。
load 'libs/bats-support/load'
load 'libs/bats-assert/load'
# https://github.com/bats-core/bats-file#Index-of-all-functions
load 'libs/bats-file/load'
# https://github.com/bats-core/bats-assert#usage
load 'assert_utils'
@test "Perform some test." {
variable_one="one"
variable_two="two"
variable_three="three"
variable_four="four"
run bash -c 'source src/some_script.sh && some_function
"$variable_one" "$variable_two" "$variable_three"'
assert_failure
assert_output "$expected_error_message"
}
其中函式包括:
some_function() {
local variable_one="$1"
local variable_two="$2"
local variable_three="$3"
local variable_four="$4"
echo "variable_one=$variable_one"
echo "variable_two=$variable_two"
echo "variable_three=$variable_three"
echo "variable_four=$variable_four"
}
輸出顯示只有第一個變數成功傳遞,而第二個到第四個沒有:
? Verify an error is thrown, if something.
(from function `assert_failure' in file test/libs/bats-assert/src/assert.bash, line 140,
in test file test/test_something.bats, line 89)
`assert_failure' failed
-- command succeeded, but it was expected to fail --
output (3 lines):
variable_one=one
variable_two=
variable_three=
variable_four=
--
如何在仍然運行的同時將多個/四個變數傳遞給函式assert_failure?
編輯以回應評論
雖然我很感激 KamilCuk 在評論中提供的實用解決方案,但它似乎允許增加特異性。例如,variable_one可能是在多個函式中使用的變數,對于這些函式的不同呼叫具有不同的值。因此,理想情況下,每次呼叫不同的函式時,我都不會覆寫“匯出”值。相反,我認為將特定引數傳遞給特定函式會更好。
uj5u.com熱心網友回復:
正常傳遞引數,但跳過在此背景關系中保留的第一個引數:
bash -c 'source src/some_script.sh && some_function "$@"' _ 'argument a' 'argument B' 'This is c' 'Andy'
輸出:
variable_one=argument a
variable_two=argument B
variable_three=This is c
variable_four=Andy
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/363001.html
上一篇:腳本作為變數值
下一篇:多行逐行添加到vim
