While trying to assert_failure
on a function called some_function
, I'm experiencing some difficulties passing more than 1 argument.
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:
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:
✗ 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?
Edit in response to comment
Though I am grateful for the practical solution provided in the comments by KamilCuk, it seems to allow for increases in specificity. For example, variable_one
might be a variable that is used in multiple functions with different values for different calls to those functions. So ideally, I would not overwrite the "exported" value every time a different function is called. Instead, I think it would be better to pass specific arguments to a specific function.
CodePudding user response:
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:
variable_one=argument a
variable_two=argument B
variable_three=This is c
variable_four=Andy