From a95e814338883bbb4a07d1cc1d28f9a7910fb0f5 Mon Sep 17 00:00:00 2001 From: zhangtao25 Date: Tue, 19 Nov 2024 17:27:42 +0800 Subject: [PATCH] chore: repo overall update --- .../services/coverage-client.service.ts | 48 ++++++++++++++----- packages/canyon-collect/src/utils/coverage.ts | 36 ++------------ .../canyon-collect/src/zod/istanbul.zod.ts | 1 + 3 files changed, 39 insertions(+), 46 deletions(-) diff --git a/packages/canyon-collect/src/apps/collect/services/coverage-client.service.ts b/packages/canyon-collect/src/apps/collect/services/coverage-client.service.ts index a0c913cc..e2fa4365 100755 --- a/packages/canyon-collect/src/apps/collect/services/coverage-client.service.ts +++ b/packages/canyon-collect/src/apps/collect/services/coverage-client.service.ts @@ -1,15 +1,19 @@ import { HttpException, Injectable } from '@nestjs/common'; import { PrismaService } from '../../../prisma/prisma.service'; import { IstanbulHitMapSchema } from '../../../zod/istanbul.zod'; -import { compressedData } from '../../../utils/zstd'; -import { formatReportObject, regularData } from '../../../utils/coverage'; +import { compressedData, decompressedData } from '../../../utils/zstd'; +import { + formatReportObject, + regularData, + remapCoverage, +} from '../../../utils/coverage'; @Injectable() export class CoverageClientService { constructor(private readonly prisma: PrismaService) {} async invoke({ sha, projectID, coverage, instrumentCwd }) { // 1. 检查是否上传map - const coverageMapCount = await this.prisma.coverage.count({ + const coverageMapCount = await this.prisma.coverage.findFirst({ where: { projectID: projectID, sha: sha, @@ -17,14 +21,28 @@ export class CoverageClientService { }, }); - if (coverageMapCount === 0) { + if (!coverageMapCount) { throw new HttpException('coverage map not found', 400); } const oldcoverage = typeof coverage === 'string' ? JSON.parse(coverage) : coverage; + // const sss = regularData(oldcoverage) + + const coverageMapCount111 = await decompressedData(coverageMapCount.map) + .then((r) => r.toString()) + .then((r) => JSON.parse(r)); + // console.log('oldcoverage', coverageMapCount111); + // 流程需要改一下 + // 1. 不反map + // 2. 在消费的时候再反map + + // 测一下有map的 // 暂时解决方案,需要解决sourceMap问题 + // ******** + // map要存,而且build时候的路径要存,不然回不去******** + // ******** const { coverage: formartCOv } = await formatReportObject({ coverage: regularData(oldcoverage), instrumentCwd: instrumentCwd, @@ -32,6 +50,17 @@ export class CoverageClientService { const formatCoverage = IstanbulHitMapSchema.parse(formartCOv); + console.log(coverageMapCount111, formatCoverage); + + const finalRes = {}; + for (const key in coverageMapCount111) { + console.log(key); + finalRes[key] = { + ...coverageMapCount111[key], + ...formatCoverage[key], + path: key, + }; + } // ut_coverage,在基础上扩展,source之类的 // invoke.coverage @@ -42,15 +71,8 @@ export class CoverageClientService { // 先不考虑分布式,直接存储 // this.prisma.coverage.upsert() - return this.prisma.coverage.updateMany({ - where: { - projectID: projectID, - sha: sha, - covType: 'all', - }, - data: { - hit: compressedFormatCoverageStr, - }, + return remapCoverage(finalRes).then((r) => { + return r; }); } // async invoke(invoke) { diff --git a/packages/canyon-collect/src/utils/coverage.ts b/packages/canyon-collect/src/utils/coverage.ts index 3f92a838..74eef154 100644 --- a/packages/canyon-collect/src/utils/coverage.ts +++ b/packages/canyon-collect/src/utils/coverage.ts @@ -1,8 +1,5 @@ import libCoverage from 'istanbul-lib-coverage'; import libSourceMaps from 'istanbul-lib-source-maps'; -import fs from 'node:fs'; -// import { mergeCoverageMap as mergeCoverageMapOfCanyonData } from "canyon-data"; -// import { merge_coverage_json_str } from 'canyon-data'; function parseInstrumentCwd(instrumentCwd) { if (instrumentCwd.includes('=>')) { const instrumentCwdSplit = instrumentCwd.split('=>'); @@ -56,12 +53,13 @@ export async function formatReportObject(c: any) { // 确保修改成istanbul格式,去掉start、end为空的情况 return { - coverage: removeStartEndNull(obj), + coverage: obj, instrumentCwd, }; } + // 覆盖率回溯,在覆盖率存储之前转换 -async function remapCoverage(obj: any) { +export async function remapCoverage(obj: any) { const res = await libSourceMaps .createSourceMapStore() .transformCoverage(libCoverage.createCoverageMap(obj)); @@ -74,34 +72,6 @@ async function remapCoverage(obj: any) { return obj_1; } -function getJsonSize(jsonObj) { - // 创建一个 TextEncoder 实例 - const encoder = new TextEncoder(); - - // 将 JSON 对象转换为字符串 - const jsonString = JSON.stringify(jsonObj); - - // 将 JSON 字符串转换为字节数组 - const encodedJson = encoder.encode(jsonString); - - // 返回字节数组的长度 - return encodedJson.length / 1024 / 1024; -} - -// export const mergeCoverageMap = (cov1: any, cov2: any) => { -// // 超过2M的数据用js合并 -// const size = getJsonSize(cov1); -// if (size > 0) { -// // console.log(`size of cov1: ${size}M`); -// return mergeCoverageMapOfCanyonData(cov1, cov2); -// } else { -// return mergeCoverageMapOfCanyonData(cov1, cov2); -// // return JSON.parse( -// // merge_coverage_json_str(JSON.stringify(cov1), JSON.stringify(cov2)), -// // ); -// } -// }; - export function resetCoverageData(coverageData) { return Object.entries(coverageData).reduce((acc, [key, value]: any) => { acc[key] = { diff --git a/packages/canyon-collect/src/zod/istanbul.zod.ts b/packages/canyon-collect/src/zod/istanbul.zod.ts index 57413545..9dc1a72f 100644 --- a/packages/canyon-collect/src/zod/istanbul.zod.ts +++ b/packages/canyon-collect/src/zod/istanbul.zod.ts @@ -34,6 +34,7 @@ const IstanbulMapSchema = z.object({ line: z.number(), }), ), + inputSourceMap: z.unknown(), }); // map类型的key是文件路径,value是IstanbulDataSchema