From 80235cd3154d85a17f8b9f3ce856ac38dd7b2051 Mon Sep 17 00:00:00 2001 From: zhangtao25 Date: Mon, 18 Nov 2024 17:46:47 +0800 Subject: [PATCH] chore: repo overall update --- .../services/coverage-map-client.service.ts | 2 +- .../collect/valids/is-valid-coverage-map.ts | 26 +++---- packages/canyon-collect/src/utils/coverage.ts | 78 +++++++++++++++---- tools/canyon-uploader/README.md | 4 +- tools/canyon-uploader/package.json | 2 +- tools/canyon-uploader/src/commands/map.ts | 4 +- 6 files changed, 82 insertions(+), 34 deletions(-) diff --git a/packages/canyon-collect/src/apps/collect/services/coverage-map-client.service.ts b/packages/canyon-collect/src/apps/collect/services/coverage-map-client.service.ts index 925c5bbe..d8e4acbd 100755 --- a/packages/canyon-collect/src/apps/collect/services/coverage-map-client.service.ts +++ b/packages/canyon-collect/src/apps/collect/services/coverage-map-client.service.ts @@ -46,7 +46,7 @@ export class CoverageMapClientService { .create({ data: { ...coverageObj, - branch: branch, + branch: branch || '-', compareTarget: sha, provider: 'github', buildProvider: 'github', diff --git a/packages/canyon-collect/src/apps/collect/valids/is-valid-coverage-map.ts b/packages/canyon-collect/src/apps/collect/valids/is-valid-coverage-map.ts index ad7982e5..54dbd190 100755 --- a/packages/canyon-collect/src/apps/collect/valids/is-valid-coverage-map.ts +++ b/packages/canyon-collect/src/apps/collect/valids/is-valid-coverage-map.ts @@ -1,25 +1,24 @@ import { ValidatorConstraint, ValidatorConstraintInterface, -} from "class-validator"; +} from 'class-validator'; /*// 验证流水线上报的覆盖率map对象, 只需要包含map // */ - function isValidCoverageMap(coverage) { // 检查是否是对象 - if (typeof coverage !== "object" || coverage === null) { + if (typeof coverage !== 'object' || coverage === null) { return false; } // 检查是否有必须的属性 const requiredProperties = [ // "path", - "statementMap", - "fnMap", - "branchMap", + 'statementMap', + 'fnMap', + 'branchMap', // "s", // "f", // "b", @@ -32,9 +31,9 @@ function isValidCoverageMap(coverage) { // 检查属性的类型和结构 if ( // typeof coverage.path !== "string" || - typeof coverage.statementMap !== "object" || - typeof coverage.fnMap !== "object" || - typeof coverage.branchMap !== "object" + typeof coverage.statementMap !== 'object' || + typeof coverage.fnMap !== 'object' || + typeof coverage.branchMap !== 'object' // typeof coverage.s !== "object" || // typeof coverage.f !== "object" || // typeof coverage.b !== "object" @@ -53,13 +52,14 @@ function safeParseJSON(json) { return {}; } } -@ValidatorConstraint({ name: "isValidCoverage", async: false }) +@ValidatorConstraint({ name: 'isValidCoverage', async: false }) export class IsValidCoverage implements ValidatorConstraintInterface { validate(_coverage: unknown) { - if (_coverage === null|| _coverage === undefined) { + if (_coverage === null || _coverage === undefined) { return false; } - const coverage = (typeof _coverage === "string") ? safeParseJSON(_coverage) : _coverage; + const coverage = + typeof _coverage === 'string' ? safeParseJSON(_coverage) : _coverage; if (Object.keys(coverage).length === 0) { return false; } @@ -69,6 +69,6 @@ export class IsValidCoverage implements ValidatorConstraintInterface { } defaultMessage() { - return "coverageMap格式不正确"; + return 'coverageMap格式不正确'; } } diff --git a/packages/canyon-collect/src/utils/coverage.ts b/packages/canyon-collect/src/utils/coverage.ts index e436458d..1303b2b6 100644 --- a/packages/canyon-collect/src/utils/coverage.ts +++ b/packages/canyon-collect/src/utils/coverage.ts @@ -1,24 +1,25 @@ -import libCoverage from "istanbul-lib-coverage"; -import libSourceMaps from "istanbul-lib-source-maps"; +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("=>"); + if (instrumentCwd.includes('=>')) { + const instrumentCwdSplit = instrumentCwd.split('=>'); return [instrumentCwdSplit[0], instrumentCwdSplit[1]]; } else { - return [instrumentCwd, ""]; + return [instrumentCwd, '']; } } function convertInstrumentCwd({ path, instrumentCwd, projectInstrumentCwd }) { if (!projectInstrumentCwd) { - return path.replace(instrumentCwd, ""); + return path.replace(instrumentCwd, ''); } else { // 这里需要解析一下instrumentCwd,如果包含"=>",则需要替换。 const [leftInstrumentCwd, rightInstrumentCwd] = parseInstrumentCwd(projectInstrumentCwd); return path - .replace(instrumentCwd, "") + .replace(instrumentCwd, '') .replace(leftInstrumentCwd, rightInstrumentCwd); } } @@ -26,23 +27,23 @@ function convertInstrumentCwd({ path, instrumentCwd, projectInstrumentCwd }) { export async function formatReportObject(c: any) { // 去除斜杠\\ const removeSlash = (x: any) => - JSON.parse(JSON.stringify(x).replace(/\\\\/g, "/")); + JSON.parse(JSON.stringify(x).replace(/\\\\/g, '/')); // 暂时解决方案,需要解决sourceMap问题 const coverage = removeSlash(c.coverage); const instrumentCwd = removeSlash(c.instrumentCwd); - const projectInstrumentCwd = removeSlash(c.projectInstrumentCwd || ""); + const projectInstrumentCwd = removeSlash(c.projectInstrumentCwd || ''); const reversePath = (p: string) => { const a = convertInstrumentCwd({ path: p, instrumentCwd, projectInstrumentCwd, }); - let b = ""; + let b = ''; // 从第二个字符开始 for (let i = 1; i < a.length; i++) { b += a[i]; } - return "" + b; + return '' + b; }; const obj: any = {}; for (const coverageKey in coverage) { @@ -51,8 +52,11 @@ export async function formatReportObject(c: any) { path: reversePath(coverageKey), }; } + + // 确保修改成istanbul格式,去掉start、end为空的情况 + return { - coverage: obj, + coverage: removeStartEndNull(obj), instrumentCwd, }; } @@ -64,7 +68,7 @@ async function remapCoverage(obj: any) { const { data: data_1 } = res; const obj_1: any = {}; for (const dataKey in data_1) { - const x = data_1[dataKey]["data"]; + const x = data_1[dataKey]['data']; obj_1[x.path] = x; } return obj_1; @@ -122,16 +126,60 @@ export function resetCoverageData(coverageData) { }, {}); } - export function regularData(data: any) { const obj = {}; const coverage = data; // 针对windows电脑,把反斜杠替换成正斜杠 // 做数据过滤,去除 \u0000 字符 for (const coverageKey in coverage) { - if (!coverageKey.includes("\u0000")) { + if (!coverageKey.includes('\u0000')) { obj[coverageKey] = coverage[coverageKey]; } } return obj; } + +const removeStartEndNull = (coverage) => { + const obj = {}; + Object.keys(coverage).forEach((key) => { + const item = coverage[key]; + + // 创建一个新的branchMap,用于存储处理后的结果 + const newBranchMap = {}; + + Object.keys(item.branchMap).forEach((statementKey) => { + const locations = item.branchMap[statementKey].locations; + const newLocations = []; + + for (let i = 0; i < locations.length; i++) { + const location = locations[i]; + + // 如果start和end都不为空对象,则保留该位置信息 + if ( + Object.keys(location.start).length !== 0 || + Object.keys(location.end).length !== 0 + ) { + newLocations.push(location); + } + } + + // 将处理后的新位置信息存入新的branchMap + if (newLocations.length > 0) { + newBranchMap[statementKey] = { + ...item.branchMap[statementKey], + locations: newLocations, + }; + } + }); + + // 如果新的branchMap有数据,则将其存入处理后的对象 + if (Object.keys(newBranchMap).length > 0) { + obj[key] = { + ...item, + branchMap: newBranchMap, + }; + } + }); + // fs.writeFileSync('./coverage.json', JSON.stringify(obj)); + return obj; +}; diff --git a/tools/canyon-uploader/README.md b/tools/canyon-uploader/README.md index 3b461189..d67213be 100644 --- a/tools/canyon-uploader/README.md +++ b/tools/canyon-uploader/README.md @@ -22,6 +22,6 @@ map是支持playwright等自动化工具生成的覆盖率数据 canyon-uploader map --dsn=http://localhost:8080/coverage/client ``` -canyon-uploader map --dsn=http://localhost:8080/coverage/client --project_id=1 --instrument_cwd=/Users/xxx/xxx --branch=master --report_id=1 +canyon-uploader map --dsn=http://localhost:8080/coverage/client --project_id=1 --instrument_cwd=/Users/xxx/xxx --sha=0521a99225c799f2e62439a6c1f3c884fbdb65cc -./bin/canyon-uploader map --dsn=http://localhost:8080/coverage/client --project_id=1 --instrument_cwd=/Users/xxx/xxx --sha=xxxxx +./bin/canyon-uploader map --dsn=http://localhost:8080/coverage/map/client --project_id=1 --instrument_cwd=/Users/xxx/xxx --sha=8ae63737150959dd7a1517735e1069adb4a0b6fc diff --git a/tools/canyon-uploader/package.json b/tools/canyon-uploader/package.json index e964bb33..e643a8dd 100644 --- a/tools/canyon-uploader/package.json +++ b/tools/canyon-uploader/package.json @@ -1,6 +1,6 @@ { "name": "canyon-uploader", - "version": "0.0.1-beta.11", + "version": "0.0.1-beta.12", "files": [ "bin", "dist" diff --git a/tools/canyon-uploader/src/commands/map.ts b/tools/canyon-uploader/src/commands/map.ts index c08a7dbb..4d68ecb9 100644 --- a/tools/canyon-uploader/src/commands/map.ts +++ b/tools/canyon-uploader/src/commands/map.ts @@ -35,7 +35,7 @@ export async function mapCommand(params, options) { instrumentCwd: instrumentCwd || process.cwd(), // coverage: JSON.stringify(data), } - console.log(reqData,Object.keys(data).length); + console.log(reqData); await axios.post(dsn, { projectID: projectID||process.env.CI_PROJECT_ID||process.env.GITHUB_REPOSITORY, sha: sha||process.env.CI_COMMIT_SHA || process.env.GITHUB_SHA, @@ -43,7 +43,7 @@ export async function mapCommand(params, options) { coverage: JSON.stringify(data), }).catch((e) => { // 打印错误 - console.log(e.response.data); + // console.log(e.message,e.name,e.errors,e.response); return e; }); }