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 3cfb91b commit ba92c85
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/canyon-collect/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ model Coverage {
sha String
branch String
compareTarget String @map("compare_target")
instrumentCwd String @map("instrument_cwd") //是build时的路径,因为要与sourceMap对应
provider String
buildProvider String @map("build_provider") // 通过侦测CI环境变量来判断
buildID String @map("build_id")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const coverageObj = {
summary: '',
hit: '',
map: '',
instrumentCwd: '',
// createdAt: new Date(),
// updatedAt: new Date(),
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { compressedData, decompressedData } from '../../../utils/zstd';
import {
formatReportObject,
regularData,
remapCoverage,
} from '../../../utils/coverage';
import { mergeCoverageMap } from 'canyon-data';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export class CoverageMapClientService {
summary: Buffer.from([]),
hit: Buffer.from([]),
map: compressedFormatCoverageStr,
instrumentCwd: instrumentCwd,
},
})
.then((r) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/canyon-collect/src/utils/coverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function convertInstrumentCwd({ path, instrumentCwd, projectInstrumentCwd }) {
}
}
// 格式化上报的覆盖率对象
export async function formatReportObject(c: any) {
export function formatReportObject(c: any) {
// 去除斜杠\\
const removeSlash = (x: any) =>
JSON.parse(JSON.stringify(x).replace(/\\\\/g, '/'));
Expand Down
28 changes: 24 additions & 4 deletions packages/canyon-platform/app/api/cov/map/route.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
// export const dynamic = 'force-static'
import prisma from "@/lib/prisma";
import { compressedData, decompressedData } from "@/utils/zstd";
import { formatReportObject, remapCoverage } from "@/utils/coverage";
// import { decompressedData } from "@/utils/zstd";

// import { compress, decompress } from "@mongodb-js/zstd";

export async function GET() {
const data = await prisma.coverage.findFirst({
where: {
projectID: "118075",
sha: "91a12fc016b8f0c61dd605a67b38b6c5999fed74",
projectID: "490316875",
sha: "0cacb37b236c9507004505662a0d6bfb018aaec4",
},
});

// console.log(data.instrumentCwd);

const d = await decompressedData(data.map)
.then((r) => r.toString())
.then((r) => JSON.parse(r));
Expand All @@ -26,8 +29,25 @@ export async function GET() {
obj[key] = {
...d[key],
...c[key],
path: key,
};
}

return Response.json(obj);
// console.log(data,'')
function addInstrumentCwd(cov) {
const o = {};
for (const key in cov) {
o[data.instrumentCwd + "/" + key] = {
...cov[key],
path: data.instrumentCwd + "/" + key,
};
}
return o;
}
const r = await remapCoverage(addInstrumentCwd(obj)).then((r) =>
formatReportObject({
coverage: r,
instrumentCwd: data.instrumentCwd,
}),
);
return Response.json(r.coverage);
}
2 changes: 2 additions & 0 deletions packages/canyon-platform/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
"eslint-config-next": "15.0.3",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.2.1",
"istanbul-lib-coverage": "^3.2.2",
"istanbul-lib-source-maps": "^5.0.6",
"postcss": "^8",
"tailwindcss": "^3.4.1",
"tailwindcss-animate": "^1.0.7",
Expand Down
1 change: 1 addition & 0 deletions packages/canyon-platform/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ model Coverage {
hit Bytes
// 特殊逻辑,必须要有map数据,才能上报hit,并且存在cov_type上。
map Bytes
instrumentCwd String @map("instrument_cwd") //是build时的路径,因为要与sourceMap对应
// 通用
createdAt DateTime @default(now()) @map("created_at") @db.Timestamp(3)
updatedAt DateTime @default(now()) @map("updated_at") @db.Timestamp(3)
Expand Down
74 changes: 74 additions & 0 deletions packages/canyon-platform/utils/coverage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import libCoverage from "istanbul-lib-coverage";
import libSourceMaps from "istanbul-lib-source-maps";

// 覆盖率回溯,在覆盖率存储之前转换
export async function remapCoverage(obj: any) {
const res = await libSourceMaps
.createSourceMapStore()
.transformCoverage(libCoverage.createCoverageMap(obj));
const { data: data_1 } = res;
const obj_1: any = {};
for (const dataKey in data_1) {
const x = data_1[dataKey]["data"];
obj_1[x.path] = x;
}
return obj_1;
}

function parseInstrumentCwd(instrumentCwd) {
if (instrumentCwd.includes("=>")) {
const instrumentCwdSplit = instrumentCwd.split("=>");
return [instrumentCwdSplit[0], instrumentCwdSplit[1]];
} else {
return [instrumentCwd, ""];
}
}
function convertInstrumentCwd({ path, instrumentCwd, projectInstrumentCwd }) {
if (!projectInstrumentCwd) {
return path.replace(instrumentCwd, "");
} else {
// 这里需要解析一下instrumentCwd,如果包含"=>",则需要替换。
const [leftInstrumentCwd, rightInstrumentCwd] =
parseInstrumentCwd(projectInstrumentCwd);
return path
.replace(instrumentCwd, "")
.replace(leftInstrumentCwd, rightInstrumentCwd);
}
}
// 格式化上报的覆盖率对象
export function formatReportObject(c: any) {
// 去除斜杠\\
const removeSlash = (x: any) =>
JSON.parse(JSON.stringify(x).replace(/\\\\/g, "/"));
// 暂时解决方案,需要解决sourceMap问题
const coverage = removeSlash(c.coverage);
const instrumentCwd = removeSlash(c.instrumentCwd);
const projectInstrumentCwd = removeSlash(c.projectInstrumentCwd || "");
const reversePath = (p: string) => {
const a = convertInstrumentCwd({
path: p,
instrumentCwd,
projectInstrumentCwd,
});
let b = "";
// 从第二个字符开始
for (let i = 1; i < a.length; i++) {
b += a[i];
}
return "" + b;
};
const obj: any = {};
for (const coverageKey in coverage) {
obj[reversePath(coverageKey)] = {
...coverage[coverageKey],
path: reversePath(coverageKey),
};
}

// 确保修改成istanbul格式,去掉start、end为空的情况

return {
coverage: obj,
instrumentCwd,
};
}
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ba92c85

Please sign in to comment.