Skip to content

Fix NaN coverage percentage when no resources are collected#46

Open
tas50 wants to merge 1 commit into
chef:mainfrom
tas50:fix/coverage-nan-zero-resources
Open

Fix NaN coverage percentage when no resources are collected#46
tas50 wants to merge 1 commit into
chef:mainfrom
tas50:fix/coverage-nan-zero-resources

Conversation

@tas50

@tas50 tas50 commented Jun 28, 2026

Copy link
Copy Markdown

Summary

ChefSpec::Coverage#report! computed the coverage percentage as:

h[:coverage] = ((h[:touched] / h[:total].to_f) * 100).round(2)

When a run collects no resources (total == 0), that's 0 / 0.0, which evaluates to NaN. The bundled human ERB template guards display with if @total == 0, so it's invisible there — but the NaN still lands in the report hash and is handed to every registered output, including JSON and any programmatic consumer.

Fix

Extract the calculation into a private coverage_percentage(touched, total) helper that returns 0.0 when there are no resources, and use it in report!. 0.0 (rather than 100.0) is the least-surprising value for "nothing was collected."

Testing

  • Added spec/unit/coverage_spec.rb (TDD — verified it failed first) covering the zero-resources case (0.0, not NaN), a normal percentage, and rounding.
  • bundle exec rspec spec/unit/165 examples, 0 failures.
  • cookstyle --chefstyle -c .rubocop.yml → no offenses.

Note

Adds spec/unit/coverage_spec.rb, which #44 also adds (there it covers Coverage::ResourceWrapper). Whichever merges first, the other just needs the two describe blocks combined into the one file.

Coverage#report! computed the percentage as
`(touched / total.to_f * 100).round(2)`. When a run collects no resources
(total == 0), that is `0 / 0.0` which evaluates to NaN. The human ERB
template hides this, but the value still flows into the report hash and
any programmatic/JSON consumer sees NaN.

Extract the math into a coverage_percentage helper that returns 0.0 when
there are no resources, and unit test it.

Signed-off-by: Tim Smith <tsmith84@proton.me>
@tas50
tas50 requested review from a team and jaymzh as code owners June 28, 2026 06:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant