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.
Quick start
Section titled “Quick start”Produce a report with your usual tool, then run CodeKnife from the same root:
cargo llvm-cov --lcov --output-path lcov.infocodeknife --no-cache --format agent testslcov.info in the root is found automatically. To use a report somewhere else, pass it:
codeknife --no-cache --format agent tests --coverage coverage/rust.jsonWhen 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)Supported formats
Section titled “Supported formats”| 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.
What each parser reads
Section titled “What each parser reads”- LCOV:
SF:starts a file,DA:<line>,<hits>records a line,end_of_recordends a file. All other records (FN,BRDAand so on) are ignored. - Go coverprofile: each
file:startLine.col,endLine.col statements countline marks every line from start to end withcount. Ifgo.modin the root has amoduleline, that module path is stripped from the front of each file name. - llvm-cov JSON: for each entry in
data[].files[], thefilenameand thesegments. A segment counts when it has at least 4 elements and its fourth element (has_count) istrue. 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 fromstart.linetoend.lineget that statement’s count froms. 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.
Formats that look supported but are not
Section titled “Formats that look supported but are not”A text file with no LCOV records still counts as a loaded report:
codeknife --no-cache --path covrs --format agent tests --coverage covstash/garbage.txtCODEKNIFE 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.
Real runs, one per format
Section titled “Real runs, one per format”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):
codeknife --no-cache --format agent tests --coverage ../covstash/lcov.infoCODEKNIFE tests status=pass files=1 errors=0 warnings=0 infos=0 elapsed_ms=7NOTE loaded coverage report (lcov, 1 files)codeknife --no-cache --format agent tests --coverage ../covstash/llvm-cov.jsonCODEKNIFE tests status=pass files=1 errors=0 warnings=0 infos=0 elapsed_ms=21NOTE 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.910700788251809covrs::double 1 100.0 1.0covrs::grade 8 61.111111111111114 11.764060356652948covrs::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:
go test -coverprofile=coverage.out ./...codeknife --no-cache --format agent testsCODEKNIFE 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):
coverage run -m unittest -qcoverage xml -o coverage.xmlcodeknife --no-cache --format agent testsCODEKNIFE 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):
npx c8 --reporter=json --reporter=text node --test src/codeknife --no-cache --format agent testsCODEKNIFE 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)How a report is chosen
Section titled “How a report is chosen”Tests and Mutation blades
Section titled “Tests and Mutation blades”codeknife tests and codeknife mutate pick a report like this:
- If you pass
--coverage <file>, that file is loaded. If it cannot be read or parsed, the command fails. - Otherwise each entry in
[coverage] pathsfrom codeknife.toml is tried, relative to the root. Missing entries are skipped. - 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 |
- 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 and the other suite commands
Section titled “check and the other suite commands”check, plan, score, dossier, verify, map and the baseline commands go through the shared static suite. There, the report is chosen like this:
--coverage <file>if given (mapand the baseline commands have no--coverageflag).- Otherwise the first entry in
[coverage] paths, loaded as given. If it cannot be read, the command fails. - Otherwise the auto-discovery list above.
Commands that take --coverage
Section titled “Commands that take --coverage”| 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.
How report paths match your files
Section titled “How report paths match your files”CodeKnife works with paths relative to the root, such as src/lib.rs. Paths in a report are normalized first:
- Backslashes become
/and a leading./is removed. - If the path starts with the root directory, that prefix is removed.
- For Go, the module path from
go.modis removed.
Then, to find coverage for a source file:
- An exact match on the normalized path wins.
- 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:
sed "s#^SF:.*#SF:/ci/build/checkout/covrs/src/lib.rs#" ../covstash/lcov.info > ../covstash/moved.infocodeknife --no-cache --format agent tests --coverage ../covstash/moved.info | grep NOTENOTE 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.910700788251809covrs::double 100.0 1.0How coverage becomes a number
Section titled “How coverage becomes a number”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.
When no report is found
Section titled “When no report is found”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.infoThe 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.
Errors
Section titled “Errors”| 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 |
Also available via
Section titled “Also available via”[coverage] paths in codeknife.toml. The key coverage.command is parsed but never used, so CodeKnife never runs a coverage command for you.