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 18, 2024
1 parent 68af219 commit 80235cd
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class CoverageMapClientService {
.create({
data: {
...coverageObj,
branch: branch,
branch: branch || '-',
compareTarget: sha,
provider: 'github',
buildProvider: 'github',
Expand Down
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"
Expand All @@ -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;
}
Expand All @@ -69,6 +69,6 @@ export class IsValidCoverage implements ValidatorConstraintInterface {
}

defaultMessage() {
return "coverageMap格式不正确";
return 'coverageMap格式不正确';
}
}
78 changes: 63 additions & 15 deletions packages/canyon-collect/src/utils/coverage.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,49 @@
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);
}
}
// 格式化上报的覆盖率对象
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) {
Expand All @@ -51,8 +52,11 @@ export async function formatReportObject(c: any) {
path: reversePath(coverageKey),
};
}

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

return {
coverage: obj,
coverage: removeStartEndNull(obj),
instrumentCwd,
};
}
Expand All @@ -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;
Expand Down Expand Up @@ -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;
};
4 changes: 2 additions & 2 deletions tools/canyon-uploader/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion tools/canyon-uploader/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "canyon-uploader",
"version": "0.0.1-beta.11",
"version": "0.0.1-beta.12",
"files": [
"bin",
"dist"
Expand Down
4 changes: 2 additions & 2 deletions tools/canyon-uploader/src/commands/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ 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,
instrumentCwd: instrumentCwd || process.cwd(),
coverage: JSON.stringify(data),
}).catch((e) => {
// 打印错误
console.log(e.response.data);
// console.log(e.message,e.name,e.errors,e.response);
return e;
});
}

0 comments on commit 80235cd

Please sign in to comment.