Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangtao25 committed Nov 28, 2024
2 parents 810ec75 + b0734b8 commit 6c25ef8
Show file tree
Hide file tree
Showing 15 changed files with 233 additions and 137 deletions.
3 changes: 2 additions & 1 deletion packages/canyon-collect/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"zod": "^3.23.8",
"@nestjs/typeorm": "^10.0.2",
"typeorm": "^0.3.20",
"sqlite3": "^5.1.7"
"sqlite3": "^5.1.7",
"canyon-data2": "workspace:^"
},
"devDependencies": {
"@nestjs/cli": "^10.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
import { Injectable } from '@nestjs/common';
// import { PrismaService } from '../../../prisma/prisma.service';
import {
genSummaryMapByCoverageMap,
getSummaryByPath,
mergeCoverageMap,
} from '../../../../canyon-data/src';
// import { mergeCoverageMap, resetCoverageData } from '../../../utils/coverage';
// import { removeNullKeys, resolveProjectID } from '../../../utils/utils';
// import { PullChangeCodeAndInsertDbService } from '../common/pull-change-code-and-insert-db.service';
// import { logger } from "../../../logger";

import { CoveragediskService } from './coveragedisk.service';
// import { TestExcludeService } from '../common/test-exclude.service';
// import { PullFilePathAndInsertDbService } from '../common/pull-file-path-and-insert-db.service';
// import { compressedData, decompressedData } from '../../../utils/zstd';
// import { logger } from '../../../../logger';
import { PrismaService } from '../../../../prisma/prisma.service';
import { removeNullKeys } from '../../../../utils/utils';
import { compressedData, decompressedData } from '../../../../utils/zstd';
import { reorganizeCompleteCoverageObjects } from '../../../../data/coverage';
import { coverageObj } from '../../models/coverage.model';
import fs from 'node:fs';
import { removeStartEndNull } from '../../../../utils/coverage';

import { resetCoverageDataMap } from '../../../../utils/coverage';
import { mergeCoverageMap } from 'canyon-data';
import {remapCoverageWithInstrumentCwd, reorganizeCompleteCoverageObjects} from "canyon-data2";

const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

Expand Down Expand Up @@ -109,15 +101,21 @@ export class ConsumerCoverageService {
reportID: covType === 'agg' ? queueDataToBeConsumed.reportID : null,
}),
});
const map = await this.prisma.coverage
const { map, instrumentCwd } = await this.prisma.coverage
.findFirst({
where: {
sha: queueDataToBeConsumed.sha,
projectID: queueDataToBeConsumed.projectID,
covType: 'all',
},
})
.then((res) => decompressedData(res.map));
.then(async (res) => {
const map = await decompressedData(res.map);
return {
map: map,
instrumentCwd: res.instrumentCwd,
};
});
const codechanges = [];

// TODO cov应该是全量的,应该是find出来的hit,因为已经合并过了,避免重复
Expand All @@ -131,11 +129,14 @@ export class ConsumerCoverageService {

// map不参与exclude过滤,需要保留完整的

const newCoverage = removeStartEndNull(
reorganizeCompleteCoverageObjects(
map, //
mergedHit,
),
const reMapMap = await remapCoverageWithInstrumentCwd(
resetCoverageDataMap(map),
instrumentCwd,
);

const newCoverage = reorganizeCompleteCoverageObjects(
reMapMap, //
mergedHit,
);

const summary = genSummaryMapByCoverageMap(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { decompressedData } from '../../../utils/zstd';
import { formatReportObject, regularData } from '../../../utils/coverage';
import { CoveragediskService } from './core/coveragedisk.service';
import {
remapCoverage123,
remapCoverageWithInstrumentCwd,
reorganizeCompleteCoverageObjects,
} from '../../../data/coverage';
} from 'canyon-data2';

// 此代码重中之重、核心中的核心!!!
@Injectable()
Expand Down Expand Up @@ -60,7 +60,7 @@ export class CoverageClientService {
const chongzu = reorganizeCompleteCoverageObjects(map, originalHit);

// #region == Step x: 覆盖率回溯,在覆盖率存储之前转换(这里一定要用数据库里的instrumentCwd,因为和map是对应的!!!)
const hit = await remapCoverage123(
const hit = await remapCoverageWithInstrumentCwd(
chongzu,
coverageFromDatabase.instrumentCwd,
);
Expand Down
2 changes: 1 addition & 1 deletion packages/canyon-collect/src/canyon-data/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './coverage';
// export * from './coverage';
export * from './summary';
1 change: 0 additions & 1 deletion packages/canyon-collect/src/data/README.md

This file was deleted.

53 changes: 0 additions & 53 deletions packages/canyon-collect/src/data/coverage.ts

This file was deleted.

38 changes: 24 additions & 14 deletions packages/canyon-collect/src/utils/coverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,6 @@ export function formatReportObject(c: any) {
};
}

// 覆盖率回溯,在覆盖率存储之前转换
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;
}

export function resetCoverageData(coverageData) {
return Object.entries(coverageData).reduce((acc, [key, value]: any) => {
acc[key] = {
Expand All @@ -96,6 +82,30 @@ export function resetCoverageData(coverageData) {
}, {});
}

export function resetCoverageDataMap(coverageData) {
return Object.entries(coverageData).reduce((acc, [key, value]: any) => {
acc[key] = {
...value,
s: Object.entries(value.statementMap).reduce((accInside, [keyInside]) => {
accInside[keyInside] = 0;
return accInside;
}, {}),
f: Object.entries(value.fnMap).reduce((accInside, [keyInside]) => {
accInside[keyInside] = 0;
return accInside;
}, {}),
b: Object.entries(value.branchMap).reduce(
(accInside, [keyInside, valueInside]: any) => {
accInside[keyInside] = Array(valueInside.length).fill(0);
return accInside;
},
{},
),
};
return acc;
}, {});
}

export function regularData(data: any) {
const obj = {};
const coverage = data;
Expand Down
36 changes: 36 additions & 0 deletions packages/canyon-data2/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "canyon-data2",
"version": "1.0.0",
"description": "",
"type": "module",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js",
"require": "./dist/index.cjs"
}
},
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "rslib build",
"preinstall": "rslib build"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@rslib/core": "^0.0.18",
"@types/istanbul-lib-coverage": "^2.0.6",
"@types/istanbul-lib-source-maps": "^4.0.4"
},
"dependencies": {
"istanbul-lib-coverage": "^3.2.2",
"istanbul-lib-source-maps": "^5.0.6"
}
}
15 changes: 15 additions & 0 deletions packages/canyon-data2/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { defineConfig } from '@rslib/core';

export default defineConfig({
lib: [
{
format: 'esm',
syntax: 'es2021',
dts: true,
},
{
format: 'cjs',
syntax: 'es2021',
},
],
});
70 changes: 70 additions & 0 deletions packages/canyon-data2/src/coverage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
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) {
// @ts-ignore
const x = data_1[dataKey]["data"];
obj_1[x.path] = x;
}
return obj_1;
}

export const reorganizeCompleteCoverageObjects = (
map: {
[key: string]: object;
},
hit: {
[key: string]: object;
},
) => {
// istanbul数据结构
const obj = {};
for (const objKey in hit) {
const item = hit[objKey];
const mapItem = map[objKey];
// @ts-ignore
obj[objKey] = {
...mapItem,
// 一定要在下面!!!
...item,
path: objKey,
};
}
return obj;
// return {};
};

// 回溯未经过reMapCoverage的数据,但是必须得传入插装路径,因为这里的noReMap是没有插装路径的
export const remapCoverageWithInstrumentCwd = async (noReMap:any, inser:string) => {
// 如果来自的插桩路径不同,要预处理!!!
const obj = {};
for (const key in noReMap) {
const newKey = inser + "/" + key;
const item = noReMap[key];
// @ts-ignore
obj[newKey] = {
...item,
path: newKey,
};
}

const reMapedCov = await remapCoverage(obj);

const obj222: any = {};
for (const coverageKey in reMapedCov) {
const newKey = coverageKey.replace(inser + "/", "");
obj222[newKey] = {
...reMapedCov[coverageKey],
path: newKey,
};
}

// 再把inser去掉
return obj222;
};
1 change: 1 addition & 0 deletions packages/canyon-data2/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './coverage';
15 changes: 15 additions & 0 deletions packages/canyon-data2/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"lib": ["ES2021"],
"module": "ESNext",
"noEmit": true,
"strict": true,
"skipLibCheck": true,
"isolatedModules": true,
"resolveJsonModule": true,
"moduleResolution": "bundler",
"useDefineForClassFields": true,
"allowImportingTsExtensions": true
},
"include": ["src"]
}
Loading

0 comments on commit 6c25ef8

Please sign in to comment.