-
Notifications
You must be signed in to change notification settings - Fork 518
set -o nounset make exit trap fail. #88
Comments
I guess if no tests have been skipped then In # 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:
..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 |
Fix for: set -o nounset make exit trap fail. · Issue sstephenson#88 · sstephenson/bats sstephenson#88
I can confirm that just adding I've made a fork with the line added, here: https://github.com/textarcana/bats/blob/eeeae6f35cd4213dac88de0b3c3132f7ce7ba355/libexec/bats-exec-test#L6 |
Why: More restrict and fail-safe See: [Best Practices for Writing Bash Scripts](http://kvz.io/blog/2013/11/21/bash-best-practices/) How: set -e/set -o errexit: script exit when a command fails set -u/set -o nounset: script exit when undeclared variable is used `set -u` causes bats test running failed, becasue BATS_TEST_SKIPPED is unbound variable. Ignore BATS_TEST_SKIPPED in bahn.bats See: [set -o nounset make exit trap fail.](sstephenson/bats#88)
Closes sstephenson#90. Closes sstephenson#83. Closes sstephenson#56. Closes sstephenson#55. Closes sstephenson#53. Resolves outstanding issues from sstephenson#32. Per all these earlier issues and PRs: - The original bin/bats symlink was causing problems on Windows. - The attempted fix from sstephenson#32 created problems for install.sh. - Per sstephenson#90, changing bin/bats to a one-line script in sstephenson#88 broke some Bats installations that rely on symlink schemes (such as when installed via https://github.com/basherpm/basher). For an idea of why I wanted to keep bin/bats as a script due to how symlinks complicate things on Windows, see the following results I discovered during a search for "git symlink windows": - https://github.com/git-for-windows/git/wiki/Symbolic-Links - https://blogs.windows.com/buildingapps/2016/12/02/symlinks-windows-10/ - libgit2/libgit2#4107 - https://stackoverflow.com/q/5917249 In the course of applying these changes, I realized libexec/bats computed some extraneous variables, so I removed them, eliminating a few external processes and subshells. I also cleaned up other small bits of logic. On top of making install.sh, bin/bats, and libexec/bats more resilient and compact, the existing test suite (before adding the new test/installer.bats file) sped up significantly, running 0.6s-0.7s faster on my MacBook Pro (2.9 GHz Intel Core i5, 8GB RAM): Bash 3.2.57(1)-release before: 58 tests, 0 failures real 0m4.924s user 0m3.045s sys 0m1.798s Bash 3.2.57(1)-release after: 58 tests, 0 failures real 0m4.341s user 0m2.808s sys 0m1.540s Bash 4.4.23(1)-release before: 58 tests, 0 failures real 0m5.228s user 0m3.046s sys 0m1.952s Bash 4.4.23(1)-release after: 58 tests, 0 failures real 0m4.582s user 0m2.791s sys 0m1.643s Also tweaks the Dockerfile to update the symlink to point to bin/bats, not libexec/bats.
While running my mbland/go-script-bash tests with v1.0.0 (which I should've tried _before_ v1.0.0), I noticed some failed due to the last line of output missing. This happened because the `while` loop used to replace `sed` in sstephenson#88 would break the loop when the last line didn't end with a newline. As it turns out, the fix was pretty easy, thanks to the hints from: - https://stackoverflow.com/a/14547230 - https://unix.stackexchange.com/a/418067 However, there was a slight performance hit for the existing tests when the `[[ -n "$line" ]]` conditional appeared as part of the `while` expression. Instead, this change emits the last line in an `if` statement after the `while` loop. I got my mbland/go-script-bash tests to pass by updating the code to always emit a newline, which was the right thing to do regardless. However, it seemed still worth handling this condition in Bats itself, to maintain behavioral parity with v0.4.0.
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?!
The text was updated successfully, but these errors were encountered: