Skip to content

Commit c796f30

Browse files
committed
fix(plugin-coverage): prevent invalid coverage when lcov has hit > found
1 parent f57c035 commit c796f30

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

packages/plugin-coverage/src/lib/runner/lcov/lcov-runner.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ export async function parseLcovFiles(
8484
typeof result === 'string' || result.pathToProject == null
8585
? record.file
8686
: path.join(result.pathToProject, record.file),
87-
functions: filterOutInvalidLines(record, 'functions'),
88-
branches: filterOutInvalidLines(record, 'branches'),
89-
lines: filterOutInvalidLines(record, 'lines'),
87+
functions: patchInvalidStats(record, 'functions'),
88+
branches: patchInvalidStats(record, 'branches'),
89+
lines: patchInvalidStats(record, 'lines'),
9090
}),
9191
);
9292
}),
@@ -101,21 +101,19 @@ export async function parseLcovFiles(
101101
}
102102

103103
/**
104-
* Filters out invalid line numbers.
105-
*
106-
* Some tools like pytest-cov emit line number 0. https://github.com/nedbat/coveragepy/issues/1846
107-
*
104+
* Filters out invalid `line` numbers, and ensures `hit <= found`.
108105
* @param record LCOV record
109106
* @param type Coverage type
110-
* @returns Coverage output from record without invalid line numbers
107+
* @returns Patched stats for type in record
111108
*/
112-
function filterOutInvalidLines<T extends 'branches' | 'functions' | 'lines'>(
109+
function patchInvalidStats<T extends 'branches' | 'functions' | 'lines'>(
113110
record: LCOVRecord,
114111
type: T,
115112
): LCOVRecord[T] {
116113
const stats = record[type];
117114
return {
118115
...stats,
116+
hit: Math.min(stats.hit, stats.found),
119117
details: stats.details.filter(detail => detail.line > 0),
120118
};
121119
}

packages/plugin-coverage/src/lib/runner/lcov/lcov-runner.unit.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ SF:kw/__init__.py
3939
DA:1,1,gG9L/J2A/IwO9tZM1raZxQ
4040
DA:0,0,gG9L/J2A/IwO9tZM1raZxQ
4141
LF:2
42-
LH:1
42+
LH:3
4343
BRF:0
4444
BRH:0
4545
end_of_record
@@ -125,12 +125,14 @@ end_of_record
125125
parseLcovFiles([path.join('coverage', 'pytest', 'lcov.info')]),
126126
).resolves.toEqual([
127127
expect.objectContaining({
128-
lines: expect.objectContaining({
128+
lines: {
129+
found: 2,
130+
hit: 2, // not 3
129131
details: [
130132
{ hit: 1, line: 1 },
131133
// no { hit: 0, line: 0 },
132134
],
133-
}),
135+
},
134136
}),
135137
]);
136138
});

0 commit comments

Comments
 (0)