-
Notifications
You must be signed in to change notification settings - Fork 517
Support for "unofficial strict mode"? #171
Comments
@felixSchl You're not doing anything wrong. This is a bug. It's not I'll bring it up in the discussion about upcoming versions. This is a serious bug that should get high priority. Bats suffers from a lack of manpower so I have no idea when this may be fixed. Until then check out the workaround below. TL;DRThis is a bug where a test file, i.e. arbitrary user supplied code, has an unintended effect on the execution of Bats. Bats In this case, the Protecting against such errors are notoriously hard, but should be high priority for future releases. Details
The errors can be seen in the test's
The failing test triggers capturing of the stack trace where the first error occours. Since
The failing Example
#!/usr/bin/env bats
set -u
@test '1: failing' {
exit 1
}
@test '2: never executed' {
touch 'this-will-never-appear.file'
}
#!/usr/bin/env bats
set -eo pipefail
IFS=$'\n\t'
@test '3: passing' {
true
} Output:
WorkaroundOne solution is to isolate the effect of The example below puts the problematic code into
#!/usr/bin/env bats
load 'test_helper'
fixtures 'isolated'
@test 'set -u does not mess up Bats' {
run "${TEST_FIXTURE_ROOT}/set-u.bash"
echo "$output"
[ "$status" -eq 1 ]
[[ $output == *'UNSET_VARIABLE: unbound variable' ]] || false
}
@test 'second test runs' {
true
}
#!/usr/bin/env bash
set -u
echo "$UNSET_VARIABLE"
# Set variables for easily accessing sub-directories of `./fixtures'.
#
# Globals:
# BATS_TEST_DIRNAME
# TEST_FIXTURE_ROOT
# TEST_RELATIVE_FIXTURE_ROOT
# Arguments:
# $1 - name of sub-directory
# Returns:
# none
fixtures() {
TEST_FIXTURE_ROOT="${BATS_TEST_DIRNAME}/fixtures/$1"
TEST_RELATIVE_FIXTURE_ROOT="$(bats_trim_filename "${TEST_FIXTURE_ROOT}")"
} Output:
|
Thank you for the detailed explanation. This could sound silly, but could bats itself not work under If we wanted to go down that route, then all variables would need to be checked using |
@felixSchl That's not silly at all. The solution is likely to be along those lines. I haven't looked into it any further than that, but this seems a plausible solution. Bats must source test files, so complete isolation will never be possible. But it can try to cope as best as Bash allows it. This and similar issues will trip up even novice users and should be addressed in coming releases. Regardless, I think using |
Part of #20. Addresses sstephenson/bats#171. Bats is now capable of executing test cases that conform to the "unoffical Bats strict mode" described by: - http://redsymbol.net/articles/unofficial-bash-strict-mode/ While the test fixture is straightforward, it took trial and error to realize a few things. First, `set -u` changed in Bash 4.1-alpha. Chapter and verse from https://tiswww.case.edu/php/chet/bash/CHANGES: This document details the changes between this version, bash-4.1-alpha, and the previous version, bash-4.0-release. n. Fixed the behavior of `set -u' to conform to the latest Posix interpretation: every expansion of an unset variable except $@ and $* will cause the shell to exit. This almost certainly accounts for the fact that referencing `"${FOO[@]}"` when `FOO=()` is defined is OK under the latest Bash 4.4.12(1)-release, but breaks under the Bash 3.2.57(1)-release that ships with macOS. The fix was to do an array length check before each such operation. It probably also accounts for the fact that under Bash 3.2.57, no error output is generated at all for certain unset variable accesses. The workaround there is to run the test using the latest version of Bash. The biggest surprise was realizing that no error output is visible when the unset variable access happens inside a DEBUG trap handler. All versions of Bash seem susceptible to this. As a result of these debugging subtleties, the test case contains a thorough error message to suggest how to proceed when the fixture fails.
We need to change the BATS we use to get around this issue: sstephenson/bats#171
We need to change the BATS we use to get around this issue: sstephenson/bats#171
Hey, I am seeing a strange behaviour when using
load
or directlysource
ing files that set the unofficial strict mode. The behaviour I am seeing is that my test stops failing:test.bats:
test_helper.bash:
Result:
What am I doing wrong?
The text was updated successfully, but these errors were encountered: