Skip to content

Commit c8fd07a

Browse files
authored
feat: add code for getting coverage per req (#108)
Signed-off-by: Sarthak Shyngle <50234097+Sarthak160@users.noreply.github.com>
1 parent abd393d commit c8fd07a

File tree

5 files changed

+202
-11
lines changed

5 files changed

+202
-11
lines changed

.vscode/settings.json

-7
This file was deleted.

package-lock.json

+54-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@
2323
"@types/express": "^4.17.21",
2424
"@types/node": "^20.11.16",
2525
"axios": "^1.6.7",
26+
"cors": "^2.8.5",
27+
"merge-descriptors": "^2.0.0",
2628
"tree-kill": "^1.2.2",
2729
"typescript": "^5.3.3"
2830
},
2931
"devDependencies": {
30-
"@types/axios": "^0.14.0"
32+
"@types/axios": "^0.14.0",
33+
"@types/cors": "^2.8.17"
3134
}
3235
}

v2/dedup/middleware.ts

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/* eslint-disable @typescript-eslint/no-explicit-any */
2+
import { Request, Response, NextFunction } from "express";
3+
const fs = require('fs');
4+
const yaml = require('js-yaml');
5+
6+
7+
// middleware
8+
export default function middleware(
9+
10+
): (req: Request, res: Response, next: NextFunction) => void {
11+
// console.log("Inside middleware...");
12+
return (req: Request, res: Response, next: NextFunction) => {
13+
res.on("finish", () => {
14+
15+
afterMiddleware(req, res);
16+
});
17+
next();
18+
19+
};
20+
}
21+
22+
23+
export function afterMiddleware(req: Request, res: Response) {
24+
let id = req.get("KEPLOY-TEST-ID");
25+
if (!id) {
26+
console.error("No test ID found in the request headers");
27+
return;
28+
}
29+
let executedLinesByFile = GetCoverage();
30+
31+
let currentData = {
32+
id: id,
33+
executedLinesByFile: executedLinesByFile
34+
};
35+
36+
const filePath = 'dedupData.yaml';
37+
38+
let existingData = [];
39+
40+
try {
41+
const fileContent = fs.readFileSync(filePath, 'utf-8');
42+
existingData = yaml.load(fileContent) || [];
43+
} catch (error) {
44+
// Handle the case where the file doesn't exist or is not valid YAML
45+
// console.error("Error reading existing file:", error);
46+
}
47+
48+
49+
50+
// Add or update the entry for the current id
51+
existingData.push(currentData);
52+
53+
// Convert the array to YAML format
54+
const yamlData = yaml.dump(existingData);
55+
56+
// Write the updated YAML data back to the file
57+
fs.writeFileSync(filePath, yamlData, 'utf-8');
58+
59+
// Log to the console
60+
// console.log("Executed lines by file:", executedLinesByFile);
61+
// console.log("Data has been appended and logged to", filePath);
62+
}
63+
64+
// isJsonValid checks whether o is a valid JSON or not
65+
66+
let count = 0;
67+
const executedLinebyEachTest = new Array();
68+
function GetCoverage() {
69+
// console.log("Inside GetCoverage");
70+
count++;
71+
let executedLinesByFile = {};
72+
// iterate over global.__coverage__
73+
// @ts-ignore
74+
for (const filename in global.__coverage__) {
75+
// console.log("FIlenamae", filename);
76+
// while (1) {
77+
// @ts-ignore
78+
let coverageData = global.__coverage__[filename];
79+
// console.log("Inside GetCoverage " + count);
80+
// console.log(coverageData);
81+
82+
83+
// for (const filePath of Object.keys(coverageData)) {
84+
const executedLines = new Set();
85+
const fileCoverage = coverageData;
86+
const statementMap = fileCoverage.statementMap;
87+
const hitCounts = fileCoverage.s;
88+
if (count > 1) {
89+
// iterate over hitcounts and subtract the previous hitcounts
90+
// @ts-ignore
91+
var prevHitCounts = executedLinebyEachTest[count - 2];
92+
93+
for (const statementId in hitCounts) {
94+
hitCounts[statementId] = Math.abs(
95+
hitCounts[statementId] - prevHitCounts[statementId]
96+
);
97+
}
98+
}
99+
100+
for (const statementId in statementMap) {
101+
if (hitCounts[statementId] > 0) {
102+
const executedLine = statementMap[statementId].start.line;
103+
executedLines.add(executedLine);
104+
}
105+
}
106+
// @ts-ignore
107+
executedLinesByFile[filename] = Array.from(executedLines).sort((a, b) => a - b);
108+
// }
109+
// @ts-ignore
110+
executedLinebyEachTest.push({ ...hitCounts });
111+
112+
// console.log("Executed lines by file:", executedLinesByFile);
113+
// extract s from the coverage data
114+
}
115+
return executedLinesByFile;
116+
}

v2/dedup/register.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// @ts-ignore
2+
import Hook from "require-in-the-middle";
3+
import expressMiddleware from "./middleware";
4+
import bodyParser from "body-parser";
5+
import cors from "cors";
6+
import mixin from "merge-descriptors";
7+
8+
9+
// @ts-ignore
10+
Hook(["express"], function (exports) {
11+
const expressApp = exports;
12+
function keployWrappedExpress() {
13+
const keployApp = expressApp();
14+
15+
keployApp.use(bodyParser.json());
16+
keployApp.use(cors());
17+
keployApp.use(expressMiddleware());
18+
keployApp.appliedMiddleware = true;
19+
return keployApp;
20+
}
21+
22+
// copy the properties and methods of exported Function object into wrapped Funtion(keployWrappedExpress).
23+
// In order to prevent "express._Method_ or express._Field_ is not declared" error.
24+
mixin(keployWrappedExpress, expressApp, false);
25+
exports = keployWrappedExpress;
26+
return exports;
27+
});
28+
export {};

0 commit comments

Comments
 (0)