sstephenson / bats

Bash Automated Testing System
MIT License
7.12k stars 518 forks source link

Error from bats: `# /tmp/bats.7466.src: line 5: [: : integer expression expected` #206

Open sputtene opened 7 years ago

sputtene commented 7 years ago

Hi,

I'm trying to test a script that reads from STDIN.

I'm refactoring the script for easy testability (extracting logic to functions, implementing the modulino paradigm, so the script can be either sourced from another script or run directly from the command line), but I'm not there yet.

When I run my script through the test, I get this output:

$ bats --tap t/00_smoke.bats
1..2
not ok 1 Loading the script must succeed
# (in test file t/00_smoke.bats, line 5)
#   `[ "$status" -eq 0 ]' failed with status 2
# TMP_DIR: /tmp/foobar.vQeXgFe3/
# /tmp/bats.8003.src: line 5: [: : integer expression expected
ok 2 ... and not return any output

The TMP_DIR: /tmp/foobar.vQeXgFe3/ is output from my script, so that's expected.

I was wondering about 2 things:

File t/00_smoke.bats:

#!/usr/bin/env bats

@test 'Loading the script must succeed' {
        load "$BATS_TEST_DIRNAME/../foobar.sh" < /dev/null
        [ "$status" -eq 0 ]
}

@test '... and not return any output' {
        load "$BATS_TEST_DIRNAME/../foobar.sh" < /dev/null
        [ -z "$output" ]
}
sputtene commented 7 years ago

I just found that if I change the load statements to run in the bats file, Everything works as expected:

$ bats --tap t/
1..2
ok 1 Loading the script must succeed
not ok 2 ... and not return any output
# (in test file t/00_smoke.bats, line 10)
#   `[ -z "$output" ]' failed

I'm still wondering about the integer expression expected error though.

giosh94mhz commented 5 years ago

I know this is an old thread, but I just had the same issue.

The message [: : integer expression expected comes from this expression in bash: [ "" -eq 0 ]

It means that $status is unset because you have to use run not load.

So, this issue can be safely closed. :wink: