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
715 stars 110 forks source link

bash coverage problems with for loops and while #387

Closed sschmid closed 3 weeks ago

sschmid commented 1 year ago

Hi @SimonKagstrom,

I hope you're doing well.

I ran into issues where certain lines being reported as not covered when using kcov, bash and bats. I extracted samples to reproduce:

I attached the coverage report to the kcov-coverage GitHub action artifacts:

main() {
  while read -r i; do
    echo -n "-$i"
  done < <(seq 1 3)     # not covered
}

while read -r i; do
  echo -n "-$i"
done < <(seq 1 3)       # covered

main
Screen Shot 2022-10-21 at 16 35 03

The kcov-coverage sample also includes for loops which are covered as expected. In my actual project they are not.

example 1: https://coveralls.io/builds/53486336/source?filename=src%2Fbee-run.bash#L147 example 2: https://coveralls.io/builds/53486336/source?filename=src%2Fbee-run.bash#L155

When scolling through the report you will find multiple locations where for or while loops are not covered.

This is how I use kcov: see GitHub action ci.yml https://github.com/sschmid/kcov-coverage/blob/main/.github/workflows/ci.yml

Do you have an idea what is going on or how I can fix this?

SimonKagstrom commented 1 year ago

Unfortunately, it probably comes down to #145, i.e., a real parser for shell scripts. As to why these two cases differ, I can't say. Seems like strange behavior from bash I'd say.

sschmid commented 3 weeks ago

As a workaround, I excluded those patterns with --exclude-line.

kcov --include-path=src --exclude-line='done <,for' /__w/bee/bee/coverage test/bats/bin/bats test

While not solving the underlying issue, it's good enough for me 👍

SimonKagstrom commented 3 weeks ago

That's an option I didn't even remember existed :-)