mfridman/tparse

Cover displays as 0% when -cover is not provided

Closed this issue · 6 comments

I noticed that when I forget to include the -cover option on the go test command, the COVER column is displayed with a 0.0% value.

┌─────────────────────────────────────────────────────────────────────┐
│  STATUS │ ELAPSED │      PACKAGE      │ COVER │ PASS │ FAIL │ SKIP  │
│─────────┼─────────┼───────────────────┼───────┼──────┼──────┼───────│
│  PASS   │ 0.11s   │ 4d63.com/optional │ 0.0%  │   21 │    0 │    0  │
└─────────────────────────────────────────────────────────────────────┘

tparse could hide that column when go test has not provided any coverage information.

From best I can tell the go test -json output omits the coverage outputs when -cover isn't used, and explicitly states 0.0% when -cover is used but no coverage exists. So it should be possible to distinguish between these situations.

I'd be happy to contribute this if you're open to contributions.

Good catch, let's fix this!

If you're up for it, contributions welcome. Otherwise I can fix this up fairly quickly. Need to improve the parsing between these 2 lines:

with -cover

{"Time":"2022-05-26T00:46:03.790079-04:00","Action":"output","Package":"fmt","Output":"ok  \tfmt\t0.159s\tcoverage: 95.2% of statements\n"}

no -cover

{"Time":"2022-05-26T00:46:12.520973-04:00","Action":"output","Package":"fmt","Output":"ok  \tfmt\t0.161s\n"}

Oh, I think it already returns a pkg.Cover .. so can probably modify the default coverage string here:

coverage := fmt.Sprintf("%.1f%%", pkg.Coverage)
if pkg.Summary.Action != parse.ActionFail {
switch cover := pkg.Coverage; {
case cover > 0.0 && cover <= 50.0:
coverage = c.red(coverage, false)
case pkg.Coverage > 50.0 && pkg.Coverage < 80.0:
coverage = c.yellow(coverage, false)
case pkg.Coverage >= 80.0:
coverage = c.green(coverage, false)
}
}

I'd be happy to contribute this if you're open to contributions.

Sorry, I was too eager and can't take this now. Still loving the tool 🎉. Thank you!

Haha, no worries. I'll fix this up today!

Jotting this down for brevity.

Previously

┌────────────────────────────────────────────────────────────────────────────────────┐
│  STATUS │ ELAPSED │             PACKAGE              │ COVER │ PASS │ FAIL │ SKIP  │
│─────────┼─────────┼──────────────────────────────────┼───────┼──────┼──────┼───────│
│  PASS   │ 0.10s   │ github.com/mfridman/tparse/parse │ 0.0%  │   42 │    0 │    0  │
│  PASS   │ 0.16s   │ github.com/mfridman/tparse/tests │ 0.0%  │   85 │    0 │    0  │
└────────────────────────────────────────────────────────────────────────────────────┘

But, as pointed out there were not coverage statements, so let's display -- instead of 0.0%. Notably:

coverage: [no statements]
ok      github.com/mfridman/tparse/tests        0.151s  coverage: [no statements]

Expected without coverage

┌────────────────────────────────────────────────────────────────────────────────────┐
│  STATUS │ ELAPSED │             PACKAGE              │ COVER │ PASS │ FAIL │ SKIP  │
│─────────┼─────────┼──────────────────────────────────┼───────┼──────┼──────┼───────│
│  PASS   │  0.17s  │ github.com/mfridman/tparse/parse │  --   │  42  │  0   │  0    │
│  PASS   │  0.24s  │ github.com/mfridman/tparse/tests │  --   │  85  │  0   │  0    │
└────────────────────────────────────────────────────────────────────────────────────┘

Expected with coverage

┌────────────────────────────────────────────────────────────────────────────────────┐
│  STATUS │ ELAPSED │             PACKAGE              │ COVER │ PASS │ FAIL │ SKIP  │
│─────────┼─────────┼──────────────────────────────────┼───────┼──────┼──────┼───────│
│  PASS   │  0.12s  │ github.com/mfridman/tparse/parse │ 14.0% │  42  │  0   │  0    │
│  PASS   │  0.19s  │ github.com/mfridman/tparse/tests │ 89.8% │  85  │  0   │  0    │
└────────────────────────────────────────────────────────────────────────────────────┘