Skip to content

Coverage Reports

CodeKnife does not run your tests to measure coverage. It reads a coverage report that your own tooling already produced, and uses it for CRAP scores, Diff-CRAP in the PR risk brief, and to skip uncovered lines in mutation testing. Without a report, the Tests blade runs in complexity-only mode.

Produce a report with your usual tool, then run CodeKnife from the same root:

Terminal window
cargo llvm-cov --lcov --output-path lcov.info
codeknife --no-cache --format agent tests

lcov.info in the root is found automatically. To use a report somewhere else, pass it:

Terminal window
codeknife --no-cache --format agent tests --coverage coverage/rust.json

When a report loads, the run prints a note with the detected format and the number of files in the report:

NOTE loaded coverage report (llvm-cov-json, 1 files)
Format Detected when the file content Format name in the note Typical producer
Go coverprofile starts with mode: go-coverprofile go test -coverprofile=coverage.out ./...
llvm-cov JSON starts with { and has a top-level data array llvm-cov-json cargo llvm-cov report --json
Istanbul JSON starts with { and has no top-level data array istanbul-json coverage/coverage-final.json from nyc, c8, Jest or Vitest
Cobertura XML starts with < and contains <coverage, or starts with < and the file name ends in .xml cobertura-xml coverage xml (coverage.py), pytest --cov --cov-report=xml
LCOV anything else lcov cargo llvm-cov --lcov, coverage lcov, most JavaScript tools

Detection uses the content (after leading whitespace), checked in the order of this table. Only the Cobertura check also looks at the file name.

  • LCOV: SF: starts a file, DA:<line>,<hits> records a line, end_of_record ends a file. All other records (FN, BRDA and so on) are ignored.
  • Go coverprofile: each file:startLine.col,endLine.col statements count line marks every line from start to end with count. If go.mod in the root has a module line, that module path is stripped from the front of each file name.
  • llvm-cov JSON: for each entry in data[].files[], the filename and the segments. A segment counts when it has at least 4 elements and its fourth element (has_count) is true. The first element is the line and the third is the count.
  • Istanbul JSON: each top-level key is a file path. For each statement in statementMap, the lines from start.line to end.line get that statement’s count from s. Function and branch maps are ignored.
  • Cobertura XML: each <class filename="..."> starts a file, and each <line number="..." hits="..."> inside it records a line. <sources> is ignored.

When the same line appears more than once, the highest hit count wins.

A text file with no LCOV records still counts as a loaded report:

Terminal window
codeknife --no-cache --path covrs --format agent tests --coverage covstash/garbage.txt
CODEKNIFE tests status=fail files=1 errors=1 warnings=0 infos=0 elapsed_ms=4
[1] ERROR CK_TEST_HIGH_CRAP src/lib.rs:1-23 sym=covrs::grade impact=72 | `grade` CRAP score is 72.0 (CC=8, cov=0%, limit 15.0) | fix: Reduce complexity below 4 or add tests covering `grade` to bring CRAP below 15.0.
NOTE loaded coverage report (lcov, 0 files)

Check the file count in the loaded coverage report note. If it is 0, or much smaller than you expect, CodeKnife did not understand your report.

Each demo has one function grade with several branches and one test that covers only the first branch.

LCOV and llvm-cov JSON (Rust, cargo llvm-cov):

Terminal window
codeknife --no-cache --format agent tests --coverage ../covstash/lcov.info
CODEKNIFE tests status=pass files=1 errors=0 warnings=0 infos=0 elapsed_ms=7
NOTE loaded coverage report (lcov, 1 files)
Terminal window
codeknife --no-cache --format agent tests --coverage ../covstash/llvm-cov.json
CODEKNIFE tests status=pass files=1 errors=0 warnings=0 infos=0 elapsed_ms=21
NOTE loaded coverage report (llvm-cov-json, 1 files)

The two reports describe the same test run but mark different lines, so the per-function numbers differ. From .codeknife/metrics.json:

covrs::grade 8 52.38095238095239 14.910700788251809
covrs::double 1 100.0 1.0
covrs::grade 8 61.111111111111114 11.764060356652948
covrs::double 1 100.0 1.0

(columns: function, cyclomatic complexity, coverage %, CRAP; LCOV first, then llvm-cov JSON).

Go coverprofile, auto-discovered as coverage.out:

Terminal window
go test -coverprofile=coverage.out ./...
codeknife --no-cache --format agent tests
CODEKNIFE tests status=fail files=2 errors=0 warnings=1 infos=0 elapsed_ms=11
[1] WARNING CK_TEST_HIGH_CRAP pkg/calc/calc.go:3-21 sym=example.com/covgo/pkg/calc/Grade impact=20 | `Grade` CRAP score is 20.3 (CC=7, cov=35%, limit 15.0) | fix: Reduce complexity below 3 or add tests covering `Grade` to bring CRAP below 15.0.
NOTE loaded coverage report (go-coverprofile, 1 files)

The report names the file example.com/covgo/pkg/calc/calc.go. CodeKnife strips the module path from go.mod and matches it to pkg/calc/calc.go.

Cobertura XML, auto-discovered as coverage.xml (coverage.py):

Terminal window
coverage run -m unittest -q
coverage xml -o coverage.xml
codeknife --no-cache --format agent tests
CODEKNIFE tests status=fail files=4 errors=0 warnings=1 infos=0 elapsed_ms=10
[1] WARNING CK_TEST_HIGH_CRAP app/calc.py:1-15 sym=app.calc.grade impact=18 | `grade` CRAP score is 17.6 (CC=7, cov=40%, limit 15.0) | fix: Reduce complexity below 3 or add tests covering `grade` to bring CRAP below 15.0.
NOTE loaded coverage report (cobertura-xml, 2 files)

Istanbul JSON, auto-discovered as coverage/coverage-final.json (c8):

Terminal window
npx c8 --reporter=json --reporter=text node --test src/
codeknife --no-cache --format agent tests
CODEKNIFE tests status=fail files=2 errors=0 warnings=1 infos=0 elapsed_ms=3
[1] WARNING CK_TEST_HIGH_CRAP src/calc.js:1-19 sym=src/calc/grade impact=17 | `grade` CRAP score is 16.5 (CC=7, cov=42%, limit 15.0) | fix: Reduce complexity below 3 or add tests covering `grade` to bring CRAP below 15.0.
NOTE loaded coverage report (istanbul-json, 1 files)

codeknife tests and codeknife mutate pick a report like this:

  1. If you pass --coverage <file>, that file is loaded. If it cannot be read or parsed, the command fails.
  2. Otherwise each entry in [coverage] paths from codeknife.toml is tried, relative to the root. Missing entries are skipped.
  3. Then these paths are tried, relative to the root, in this order. The first one that exists is loaded:
Order Path
1 lcov.info
2 coverage/lcov.info
3 coverage/coverage-final.json
4 coverage/coverage.json
5 coverage.out
6 cover.out
7 coverage.xml
8 cobertura.xml
9 target/llvm-cov/lcov.info
10 target/llvm-cov.json
  1. If none exists, there is no report.

Only one report is used per run. A report is not merged with others. In the Rust demo, with only target/llvm-cov.json present, codeknife tests prints:

NOTE loaded coverage report (llvm-cov-json, 1 files)

After also writing lcov.info to the root, lcov.info wins because it is earlier in the list:

NOTE loaded coverage report (lcov, 1 files)

After adding paths = ["reports/rust.json"] (an llvm-cov JSON file) under [coverage], the configured path wins over both:

NOTE loaded coverage report (llvm-cov-json, 1 files)

check, plan, score, dossier, verify, map and the baseline commands go through the shared static suite. There, the report is chosen like this:

  1. --coverage <file> if given (map and the baseline commands have no --coverage flag).
  2. Otherwise the first entry in [coverage] paths, loaded as given. If it cannot be read, the command fails.
  3. Otherwise the auto-discovery list above.
Command Flag
codeknife check --coverage <path>; also passed to mutation testing with --tier full
codeknife plan --coverage <path>
codeknife tests --coverage <path>
codeknife mutate --coverage <path>
codeknife dossier --coverage <path>
codeknife verify --coverage <path>
codeknife score --coverage <path>

The MCP tools have no coverage argument. They use [coverage] paths and auto-discovery.

CodeKnife works with paths relative to the root, such as src/lib.rs. Paths in a report are normalized first:

  1. Backslashes become / and a leading ./ is removed.
  2. If the path starts with the root directory, that prefix is removed.
  3. For Go, the module path from go.mod is removed.

Then, to find coverage for a source file:

  1. An exact match on the normalized path wins.
  2. Otherwise, a report path matches when one path ends with / plus the other. If several match, the longest report path wins.

The suffix rule means a report produced in a different checkout still matches. Rewrite the SF: line of the Rust demo’s LCOV file to a path from another machine:

Terminal window
sed "s#^SF:.*#SF:/ci/build/checkout/covrs/src/lib.rs#" ../covstash/lcov.info > ../covstash/moved.info
codeknife --no-cache --format agent tests --coverage ../covstash/moved.info | grep NOTE
NOTE loaded coverage report (lcov, 1 files)

src/lib.rs gets the same per-function numbers as with the original report (function, coverage %, CRAP, read from .codeknife/metrics.json after a check with the same report):

covrs::grade 52.38095238095239 14.910700788251809
covrs::double 100.0 1.0

For each function, coverage is the share of instrumented lines between the function’s first and last line that have a hit count above 0. Lines the report does not mention are not counted.

When a report is loaded but has no instrumented lines for a function (the file is missing from the report, or no lines in the range), the function’s coverage is treated as 0%. The CRAP score is then CC^2 + CC. When no report is loaded at all, there is no coverage value and no CRAP score.

CRAP is CC^2 * (1 - coverage)^3 + CC, where CC is cyclomatic complexity. The CK_TEST_HIGH_CRAP rule and its thresholds are on 7. Tests & CRAP.

The Tests blade falls back to complexity-only mode and adds a note that suggests a command for each language in the repository:

NOTE no coverage file found (running complexity-only mode); generate one with: cargo llvm-cov --lcov --output-path lcov.info

The suggestions are cargo llvm-cov --lcov --output-path lcov.info (Rust), npx vitest run --coverage (TypeScript or JavaScript), pytest --cov --cov-report=xml:coverage.xml (Python) and go test -coverprofile=coverage.out ./... (Go), joined with |. With no files in a supported language, the note is no coverage file found; pass `--coverage <path>` to enable CRAP scoring.

In this mode CK_TEST_HIGH_CRAP cannot fire. Functions with cyclomatic complexity of 10 or more get an info finding, CK_TEST_UNCOVERED_COMPLEXITY, instead.

Situation Result Exit code
--coverage file does not exist codeknife error: reading coverage report <path>: No such file or directory (os error 2) 2
--coverage file starts with { but is not valid JSON codeknife error: parsing JSON coverage: ... 2
Auto-discovered file is unreadable or invalid JSON Same errors 2
File in an unrecognized format Loaded as LCOV with 0 files, no error normal
No report anywhere Complexity-only note, no error normal

[coverage] paths in codeknife.toml. The key coverage.command is parsed but never used, so CodeKnife never runs a coverage command for you.