koalaman / shellcheck

ShellCheck, a static analysis tool for shell scripts
https://www.shellcheck.net
GNU General Public License v3.0
35.62k stars 1.75k forks source link

bats: bogus SC2034 (no support for bats' load?) #2041

Open kolyshkin opened 3 years ago

kolyshkin commented 3 years ago

For bugs

Here's a snippet or screenshot that shows the problem:

helpers.bash:

#!/bin/bash

FOO=bar

function prepare() {
    echo $FOO
}

a.bats:

#!/usr/bin/env bats

load helpers

@test default {
    prepare | grep -w bar
}

@test override {
    FOO=whatever
    prepare | grep -w whatever
}

Here's what shellcheck currently says:

$ shellcheck -x a.bats
In a.bats line 10:
    FOO=whatever
        ^-^ SC2034: FOO appears unused. Verify use (or export if used externally).

For more information:
  https://www.shellcheck.net/wiki/SC2034 -- FOO appears unused. Verify use (o...

Here's what I wanted or expected to see:

No errors, like in the following example using pure bash:

helpers.bash:

#!/bin/bash

FOO=bar

function prepare() {
    echo $FOO
}

a.sh:

#!/bin/bash

set -e
. helpers.bash

function test_default() {
    prepare | grep -w bar
}

function test_override() {
    FOO=whatever
    prepare | grep -w whatever
}

test_default
test_override

Test execution to see if it works:

$ bash ./a.sh 
bar
whatever
$ echo $?
0

shellcheck run (shows no warnings):

$ shellcheck -x ./a.sh 
$ echo $?
0

Additional information

In case I move function prepare() from helpers.bash to the main a.bats file, the warning goes away.

The very same repro using pure bash (see above) does not result in a warning.

This probably means that bats' load statement is not recognized in a way similar to bash' source.

kolyshkin commented 3 years ago

@koalaman PTAL :pray: