infertux / bashcov

Code coverage tool for Bash
MIT License
151 stars 20 forks source link

Error when used with bats #48

Closed pawamoy closed 5 years ago

pawamoy commented 5 years ago
$ bats tests
 ✓ errors without arguments
 ✓ correctly initializes a loop
 ✓ correctly stops a loop
 ✓ correctly pauses a loop
 ✓ correctly resumes a loop
 ✓ a just initialized loop is alive
 ✓ can't initialize an already existing loop
 ✓ can't stop a dead loop
 ✓ can't pause a dead loop
 ✓ can't resume a dead loop
 ✓ pausing a paused loop has no effect
 ✓ resuming an alive loop has no effect
 ✓ control returns 1 with dead loop
 ✓ control correctly breaks a loop
 ✓ control lets loop run when alive

15 tests, 0 failures

$ bashcov bats tests
/home/pawamoy/.basher/cellar/bin/bats: line 44: /media/Data/git/shellm/libexec/bats-core/bats: No such file or directory
Run completed using bashcov 1.8.2 with Bash 4.4, Ruby 2.5.3, and SimpleCov 0.15.1.
Coverage report generated for /bin/bash bats tests to /media/Data/git/shellm/loop/coverage. 0 / 267 LOC (0.0%) covered.

Am I using bashcov correctly? bashcov bats tests

tomeon commented 5 years ago

@pawamoy -- this appears to be a bug in bats itself; namely, the logic it uses for determining the location of its "library" scripts. The line in question (at least, in bats-core release 1.1.0) is this:

https://github.com/bats-core/bats-core/blob/c706d1470dd1376687776bbe985ac22d09780327/bin/bats#L48-L50

The problem is that bats does not properly handle cases in which $0 is not an absolute path. Compare:

$ bash -x bats
+ set -e
+ export BATS_READLINK=true
+ BATS_READLINK=true
+ command -v greadlink
+ command -v readlink
+ BATS_READLINK=readlink
+ export BATS_ROOT
+ bats_resolve_absolute_root_dir bats BATS_ROOT
+ local cwd=/home/matt/src/bash-bats-core
+ local path=bats
+ local result=BATS_ROOT
+ local target_dir
+ local target_name
+ local original_shell_options=ehxB
+ set -P
+ true
+ target_dir=bats
+ target_name=bats
+ [[ bats != \b\a\t\s ]]
+ [[ -L bats ]]
+ printf -v BATS_ROOT -- %s /home/matt/src
+ set +P -ehxB
+ cd /home/matt/src/bash-bats-core
+ return
+ exec /home/matt/src/lib/bats-core/bats
/usr/bin/bats: line 50: /home/matt/src/lib/bats-core/bats: No such file or directory

With:

$ bash -x /usr/bin/bats
+ set -e
+ export BATS_READLINK=true
+ BATS_READLINK=true
+ command -v greadlink
+ command -v readlink
+ BATS_READLINK=readlink
+ export BATS_ROOT
+ bats_resolve_absolute_root_dir /usr/bin/bats BATS_ROOT
+ local cwd=/home/matt/src/bash-bats-core
+ local path=/usr/bin/bats
+ local result=BATS_ROOT
+ local target_dir
+ local target_name
+ local original_shell_options=ehxB
+ set -P
+ true
+ target_dir=/usr/bin
+ target_name=bats
+ [[ /usr/bin != \/\u\s\r\/\b\i\n\/\b\a\t\s ]]
+ cd /usr/bin
+ [[ -L bats ]]
+ printf -v BATS_ROOT -- %s /usr
+ set +P -ehxB
+ cd /home/matt/src/bash-bats-core
+ return
+ exec /usr/lib/bats-core/bats
Error: Must specify at least one <test>
Bats 1.1.0
Usage: bats [-c] [-r] [-p | -t] <test> [<test> ...]

Unless and until this is fixed upstream, I'd suggested using the absolute path to bats when invoking it with bashcov:

$ bashcov /home/pawamoy/.basher/cellar/bin/bats tests
pawamoy commented 5 years ago

Got it, thank you very much @tomeon for the explanation and workaround!

tomeon commented 5 years ago

Sure thing, @pawamoy! Glad to see there's a fix pending over in the bats-core repo.