通过:`run bash-c..`传递多个参数



在一个名为some_function的函数上尝试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"
}

其中功能包括:

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可能是在多个函数中使用的变量,这些函数的不同调用具有不同的值。因此,理想情况下,我不会覆盖";导出";值。相反,我认为最好将特定的参数传递给特定的函数。

正常传递参数,但跳过此上下文中保留的第一个参数:

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

相关内容

  • 没有找到相关文章

最新更新