通过以下方式传递多个参数:`运行 bash -c ...`
Passing multiple arguments through: `run bash -c ...`
在尝试 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=""
local variable_two=""
local variable_three=""
local variable_four=""
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=
--
如何将 multiple/four 变量传递给函数,同时仍然 运行 assert_failure
在它上面?
根据评论进行编辑
虽然我很感谢 KamilCuk 在评论中提供的实用解决方案,但它似乎允许增加特异性。例如,variable_one
可能是一个在多个函数中使用的变量,对这些函数的不同调用具有不同的值。所以理想情况下,每次调用不同的函数时我都不会覆盖“导出”的值。相反,我认为将特定参数传递给特定函数会更好。
正常传递参数但跳过第一个在此上下文中保留的参数:
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
在尝试 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=""
local variable_two=""
local variable_three=""
local variable_four=""
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=
--
如何将 multiple/four 变量传递给函数,同时仍然 运行 assert_failure
在它上面?
根据评论进行编辑
虽然我很感谢 KamilCuk 在评论中提供的实用解决方案,但它似乎允许增加特异性。例如,variable_one
可能是一个在多个函数中使用的变量,对这些函数的不同调用具有不同的值。所以理想情况下,每次调用不同的函数时我都不会覆盖“导出”的值。相反,我认为将特定参数传递给特定函数会更好。
正常传递参数但跳过第一个在此上下文中保留的参数:
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