diff --git a/packages/canyon-backend/src/coverage/services/core/consumer-coverage.service.ts b/packages/canyon-backend/src/coverage/services/core/consumer-coverage.service.ts index 43499a98..4f1182e9 100644 --- a/packages/canyon-backend/src/coverage/services/core/consumer-coverage.service.ts +++ b/packages/canyon-backend/src/coverage/services/core/consumer-coverage.service.ts @@ -131,7 +131,51 @@ export class ConsumerCoverageService { } })(); - const newcoverage = mergeCoverageMap(queueDataToBeConsumed.coverage, cov); + const hit1 = mergeCoverageMap(queueDataToBeConsumed.coverage, cov); + + + + const coverageMaps = await this.prisma.coverageMap.findMany({ + where: { + sha:queueDataToBeConsumed.sha, + projectID:queueDataToBeConsumed.projectID, + }, + }); + + + const coverageJsonMaps = await Promise.all( + coverageMaps.map((coverageMap) => { + return decompressedData(coverageMap.map).then((map) => { + return { + ...coverageMap, + ...JSON.parse(map), + }; + }); + }), + ); + const obj = {}; + coverageJsonMaps.forEach((item) => { + if (hit1[item.path]) { + const o = { + ...hit1[item.path], + ...item, + path: item.path, + }; + obj[item.path] = { + path: o.path, + b: o.b || {}, + f: o.f || {}, + s: o.s || {}, + branchMap: o.branchMap || {}, + fnMap: o.fnMap || {}, + statementMap: o.statementMap || {}, + }; + } + }); + + const newcoverage = obj; + + const summary = genSummaryMapByCoverageMap( await this.testExcludeService.invoke( queueDataToBeConsumed.projectID, diff --git a/packages/canyon-data/package.json b/packages/canyon-data/package.json index 9483430e..9560f456 100755 --- a/packages/canyon-data/package.json +++ b/packages/canyon-data/package.json @@ -1,6 +1,6 @@ { "name": "canyon-data", - "version": "0.1.1-alpha.15", + "version": "0.1.1-alpha.16", "type": "module", "main": "dist/canyon-data.cjs", "module": "dist/canyon-data.js", @@ -27,4 +27,4 @@ "istanbul-lib-coverage": "^3.2.2", "istanbul-lib-source-maps": "^5.0.6" } -} \ No newline at end of file +} diff --git a/packages/canyon-data/src/utils/line.ts b/packages/canyon-data/src/utils/line.ts index 1547005b..562135e5 100755 --- a/packages/canyon-data/src/utils/line.ts +++ b/packages/canyon-data/src/utils/line.ts @@ -1,6 +1,17 @@ import {FileCoverageData, Range} from "istanbul-lib-coverage"; import {percent} from "./percent.ts"; +function getLinesFromRanges(ranges) { + const lines = []; + for (const range of ranges) { + for (let lineNumber = range.startLine; lineNumber <= range.endLine; lineNumber++) { + lines.push(lineNumber); + } + } + return [...new Set(lines)]; +} + + /** * returns computed line coverage from statement coverage. * This is a map of hits keyed by line number in the source. @@ -26,67 +37,46 @@ function getLineCoverage(statementMap:{ [key: string]: Range },s:{ [key: string] export function calculateNewLineCoverageForSingleFile(coverage:FileCoverageData, newLine:number[]) { /* 变更行覆盖率计算 -1. 遍历变更行,判断是否在s、f、b中 -2. 如果在其中,只统计这部分行的覆盖率 -3. 在其中,且他们的hit大于0,就是覆盖的 +1. 遍历s,f,b,找出没有覆盖的行 +2. 看哪些新增行在没有覆盖的行中 */ + const noCovered: { startLine: number; endLine: number; }[] = [] + Object.entries(coverage.s).forEach(([key, count]) => { + if (coverage.statementMap[key] && count===0){ + noCovered.push({ + startLine: coverage.statementMap[key].start.line, + endLine: coverage.statementMap[key].end.line + }) + } + }) + Object.entries(coverage.f).forEach(([key, count]) => { + if (coverage.fnMap[key] && count===0){ + noCovered.push({ + startLine: coverage.fnMap[key].decl.start.line, + endLine: coverage.fnMap[key].decl.end.line + }) + } + }) - const newLineResult = [] - for (let i = 0; i < newLine.length; i++) { - const line = newLine[i]; - - let isCovered = false - let isLand = false - - Object.keys(coverage.statementMap).forEach((key) => { - const statementRange = coverage.statementMap[key]; - if (statementRange.start.line <= line && (statementRange.end?.line||statementRange.start.line) >= line) { - isLand = true - if ( coverage.s[key] > 0){ - isCovered = true - } - } - }); - - Object.keys(coverage.fnMap).forEach((key) => { - const fnRange = coverage.fnMap[key]; - if (fnRange.decl.start.line <= line && (fnRange.decl.end?.line||fnRange.decl.start.line) >= line) { - isLand = true - if (coverage.f[key] > 0) { - isCovered = true - } + Object.keys(coverage.branchMap).forEach((key) => { + const branchRange = coverage.branchMap[key]; + branchRange.locations.forEach((location,index) => { + if (coverage.b[key][index] === 0){ + noCovered.push({ + startLine: location.start.line, + endLine: location.end.line + }) } }); + }); - Object.keys(coverage.branchMap).forEach((key) => { - const branchRange = coverage.branchMap[key]; - branchRange.locations.forEach((location,index) => { - if (location.start.line <= line && (location.end?.line||location.start.line) >= line) { - isLand = true - if (coverage.b[key][index] > 0){ - isCovered = true - } - } - }); - }); - newLineResult.push({ - line, - covered: isCovered, - isLand - }) - } + const noCoveredLines = getLinesFromRanges(noCovered); - const newLineResultIsLand = newLineResult.filter((l) => l.isLand) - if(coverage.path ==='src/pages/flightList/index.tsx'){ - console.log(newLine) - console.log(newLineResultIsLand) - } - const result = { - total: newLineResultIsLand.length, - covered: newLineResultIsLand.filter((l) => l.covered).length, + return { + total: newLine.length, + covered: newLine.filter((line) => !noCoveredLines.includes(line)).length, skipped: 0, - pct: percent(newLineResultIsLand.filter((l) => l.covered).length, newLineResultIsLand.length) + pct: percent(newLine.filter((line) => !noCoveredLines.includes(line)).length, newLine.length) } - return result } diff --git a/packages/canyon-data/src/utils/test/utils.test.ts b/packages/canyon-data/src/utils/test/utils.test.ts index 29b039f4..e99c9c15 100644 --- a/packages/canyon-data/src/utils/test/utils.test.ts +++ b/packages/canyon-data/src/utils/test/utils.test.ts @@ -3,5 +3,5 @@ import {calculateNewLineCoverageForSingleFile} from "../line.ts"; import mock from './mock.json' test('测试变更行覆盖率', () => { // @ts-ignore - expect(calculateNewLineCoverageForSingleFile(mock,[1,2,77,78])).toMatchObject({ total: 2, covered: 1, skipped: 0, pct: 50 }) + expect(calculateNewLineCoverageForSingleFile(mock,[1,2,77,78])).toMatchObject({ total: 4, covered: 2, skipped: 0, pct: 50 }) })