Skip to content

Commit

Permalink
chore: repo overall update
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangtao25 committed Nov 19, 2024
1 parent 0cacb37 commit a95e814
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -1,37 +1,66 @@
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,
covType: 'all',
},
});

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,
});

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
Expand All @@ -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) {
Expand Down
36 changes: 3 additions & 33 deletions packages/canyon-collect/src/utils/coverage.ts
Original file line number Diff line number Diff line change
@@ -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('=>');
Expand Down Expand Up @@ -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));
Expand All @@ -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] = {
Expand Down
1 change: 1 addition & 0 deletions packages/canyon-collect/src/zod/istanbul.zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const IstanbulMapSchema = z.object({
line: z.number(),
}),
),
inputSourceMap: z.unknown(),
});

// map类型的key是文件路径,value是IstanbulDataSchema
Expand Down

0 comments on commit a95e814

Please sign in to comment.