[英]Passing multiple arguments through: `run bash -c ...`
While trying to assert_failure
on a function called some_function
, I'm experiencing some difficulties passing more than 1 argument.在尝试在名为some_function
的assert_failure
上进行 assert_failure 时,我在传递多个参数时遇到了一些困难。
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"
}
Where the function consists of:其中 function 包括:
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"
}
The output shows only the first variable is passed successfully, whereas, the second to fourth are not: output 仅显示第一个变量成功传递,而第二个到第四个没有:
✗ 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=
--
How can I pass multiple/four variables to the function whilst still running assert_failure
on it?如何在 function 上运行assert_failure
的同时将多个/四个变量传递给它?
Though I am grateful for the practical solution provided in the comments by KamilCuk, it seems to allow for increases in specificity.虽然我很感谢 KamilCuk 评论中提供的实用解决方案,但它似乎可以提高特异性。 For example, variable_one
might be a variable that is used in multiple functions with different values for different calls to those functions.例如, variable_one
可能是在多个函数中使用的变量,对于这些函数的不同调用具有不同的值。 So ideally, I would not overwrite the "exported" value every time a different function is called.所以理想情况下,每次调用不同的 function 时,我都不会覆盖“导出”值。 Instead, I think it would be better to pass specific arguments to a specific function.相反,我认为将特定的 arguments 传递给特定的 function 会更好。
Pass the argument normally but skip first one that is reserved in this context:正常传递参数,但跳过在此上下文中保留的第一个参数:
bash -c 'source src/some_script.sh && some_function "$@"' _ 'argument a' 'argument B' 'This is c' 'Andy'
The output: output:
variable_one=argument a
variable_two=argument B
variable_three=This is c
variable_four=Andy
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.