SimonKagstrom / kcov

Code coverage tool for compiled programs, Python and Bash which uses debugging information to collect and report data without special compilation options
http://simonkagstrom.github.io/kcov/
GNU General Public License v2.0
710 stars 109 forks source link

Bash lines missed (or incorrectly marked as no-code lines) #364

Closed jasonk closed 2 years ago

jasonk commented 2 years ago

Found a couple of odd cases when using kcov to test some bash scripts and thought I'd report it. Hopefully I'll find the time to put together a PR, but I don't know if my C++ is up to the task..

Single line bash functions, like these two that I use a lot, don't even get reported as code in the HTML output, they get rendered exactly the same as comments:

warn() { echo "$@" 1>&2; } 
die() { warn "$@"; exit 1; }

I suspect it's because bash doesn't report the first and last lines of a function as being executed, so whatever is deciding which lines are executable or not has excluded lines that are the start of a function, but in this case that line is the whole function. I was able to work around this one by just making sure the body of the function was on a separate line.

The one I wasn't able to find a workaround for though, was this kind of construct:

for IFS='' read -r LINE; do
  echo "$LINE"
done < <(seq 1 10)

I tried a couple of variations of that last line, but I could not find a way to get it to be marked as covered, even though the first two lines were, so clearly the last line had to have run too..

SimonKagstrom commented 2 years ago

Yes, you are right about the functions. There might be two reasons. The first with functions is as you guessed, that the line with the function definition is skipped (incorrectly here). The second example might be due to how bash reports executed lines.

The parser is not good, and writing a real one is covered in #145 . It's a large undertaking though.

There is an alternative and more basic parser, which will give you more false negatives, but sometimes is not led astray as often as the regular one. This one can be used with

kcov --configure=bash-use-basic-parser=1 [regular options]

I believe the first of your two cases would be covered by this.

SimonKagstrom commented 2 years ago

Closing, as #145 will be required to fix this in a proper way.