Open choffee opened 9 years ago
I guess if no tests have been skipped then $BATS_TEST_SKIPPED
will never be set, which is exactly what set -o nounset
is for. When sourcing a file (instead of running it), it affects the current environment (using cd
in a sourced file actually changes the terminal directory, but running with ./
only changes the subshell). Anyway, setting -u
globally in your my_functs
file will trigger this and prevent you from running all of the tests, but I found that using set -u
at the top of functions will allow all of the tests to run (any test with unset variables will fail).
In my_functs
:
# Don't `set -u` here, it will trigger the error.
function my_code {
# It's okay to do it here:
set -u
# This will cause a test failure.
x="${nonexistent}"
echo "pass?"
}
In the bats file:
setup() {
. ./my_functs
}
@test "Test sourced code" {
[ "$(my_code)" == "pass?" ]
}
..and the output:
✗ Test sourced code
(in test file my_tests.bats, line 10)
`[ "$(my_code)" == "pass?" ]' failed
my_functs: line 5: nonexistent: unbound variable
..pay no attention to those line numbers. I had other stuff in those files so they don't match my example's line numbers.
It seems like bats-exec-test
should explicitly initialize all variables it uses, e.g., by adding a line BATS_TEST_SKIPPED=
somewhere close to the beginning, to play nice with set -u
.
I can confirm that just adding export BATS_TEST_SKIPPED=
at the top of bats-exec-test
fixes the problem.
I've made a fork with the line added, here: https://github.com/textarcana/bats/blob/eeeae6f35cd4213dac88de0b3c3132f7ce7ba355/libexec/bats-exec-test#L6
Any chance to fix this 4 year old bug? Testing bash scripts with strict mode is key for making the reliable and this bug prevents that.
@ssbarnea, do you know about https://github.com/bats-core/bats-core? This bug could be fixed there.
I am sourcing my functions in the test as part of setup so:
setup() { . ./my_functs }
I like to run my bash scripts with "set -o nounset" but get this error on exit.
/usr/lib/bats/bats-exec-test: line 246: BATS_TEST_SKIPPED: unbound variable
Is this a bug or am I just doing it wrong?!