Skip to content
Joachim Ansorg edited this page Nov 15, 2021 · 3 revisions

In bats, ! does not cause a test failure. Fold the ! into the conditional!

Problematic code:

#!/usr/bin/env bats

@test "test" {
    # ... code
    ! [ $status == 0 ]
    # ... more code
}

Correct code:

#!/usr/bin/env bats

@test "test" {
    # ... code
    [ $status != 0 ]
    # ... more code
}

Rationale:

Bats uses set -e and trap ERR to catch test failures as early as possible. Although the return code of a ! negated command is inverted, they will never trigger errexit, due to a bash design decision (see Related Resources). This means that tests which use ! can never fail.

Exceptions:

The return code of the last command in the test will be the exit code of the test function. This means that you can use ! <command> on the last line of the test and it will still fail appropriately. However, you are encouraged to still transform the code in this case for consistency.

Related resources:

ShellCheck

Each individual ShellCheck warning has its own wiki page like SC1000. Use GitHub Wiki's "Pages" feature above to find a specific one, or see Checks.

Clone this wiki locally