shellcheck icon indicating copy to clipboard operation
shellcheck copied to clipboard

use of helper functions with bats causes spurious SC2030 SC2031 warnings

Open cyphar opened this issue 2 years ago • 0 comments

For bugs

  • Rule Id (if any, e.g. SC1000): SC2030/SC2031
  • My shellcheck version (shellcheck --version or "online"): 0.9.0
  • [x] The rule's wiki page does not already cover this (e.g. https://shellcheck.net/wiki/SC2086)
  • [x] I tried on https://www.shellcheck.net/ and verified that this is still a problem on the latest commit

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

Creating helper functions to do tests currently results in spurious errors.

#!/usr/bin/env bats

@test "foo" {
	do_test foo
}

@test "bar" {
	do_test bar
}

function do_test() {
	run echo "doing test $*"
	[ "$status" -eq 0 ]
	[[ "$output" == *"doing test $*"* ]]
}

Here's what shellcheck currently says:


In foo.bats line 7:
@test "bar" {
^-- SC2030 (info): Modification of output is local (to subshell caused by @bats test).
^-- SC2030 (info): Modification of status is local (to subshell caused by @bats test).


In foo.bats line 13:
        [ "$status" -eq 0 ]
           ^-----^ SC2031 (info): status was modified in a subshell. That change might be lost.


In foo.bats line 14:
        [[ "$output" == *"doing test $*"* ]]
            ^-----^ SC2031 (info): output was modified in a subshell. That change might be lost.

For more information:
  https://www.shellcheck.net/wiki/SC2030 -- Modification of output is local (...
  https://www.shellcheck.net/wiki/SC2031 -- output was modified in a subshell...

If you re-arrange the function definition to be before the tests, the warnings all disappear.

Here's what I wanted or expected to see:

I would expect to not see either error regardless of when the helper functions are defined, though fixing the SC2030 alone would also work (we can ignore SC2031 much more neatly than SC2031 because the latter is affecting a random other test rather than the helper function) but it seems the warnings are connected (since only the special bats variables used in the helper function get warnings on the @test).

Given that shellcheck assumes that each @test section references output, lines and status, I wonder if this issue has a similar cause to #2217.

cyphar avatar Dec 03 '23 05:12 cyphar