Skip to content

Commit

Permalink
feat: add code for getting coverage per req (#108)
Browse files Browse the repository at this point in the history
Signed-off-by: Sarthak Shyngle <[email protected]>
  • Loading branch information
Sarthak160 authored May 8, 2024
1 parent abd393d commit c8fd07a
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 11 deletions.
7 changes: 0 additions & 7 deletions .vscode/settings.json

This file was deleted.

57 changes: 54 additions & 3 deletions package-lock.json

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

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@
"@types/express": "^4.17.21",
"@types/node": "^20.11.16",
"axios": "^1.6.7",
"cors": "^2.8.5",
"merge-descriptors": "^2.0.0",
"tree-kill": "^1.2.2",
"typescript": "^5.3.3"
},
"devDependencies": {
"@types/axios": "^0.14.0"
"@types/axios": "^0.14.0",
"@types/cors": "^2.8.17"
}
}
116 changes: 116 additions & 0 deletions v2/dedup/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Request, Response, NextFunction } from "express";
const fs = require('fs');
const yaml = require('js-yaml');


// middleware
export default function middleware(

): (req: Request, res: Response, next: NextFunction) => void {
// console.log("Inside middleware...");
return (req: Request, res: Response, next: NextFunction) => {
res.on("finish", () => {

afterMiddleware(req, res);
});
next();

};
}


export function afterMiddleware(req: Request, res: Response) {
let id = req.get("KEPLOY-TEST-ID");
if (!id) {
console.error("No test ID found in the request headers");
return;
}
let executedLinesByFile = GetCoverage();

let currentData = {
id: id,
executedLinesByFile: executedLinesByFile
};

const filePath = 'dedupData.yaml';

let existingData = [];

try {
const fileContent = fs.readFileSync(filePath, 'utf-8');
existingData = yaml.load(fileContent) || [];
} catch (error) {
// Handle the case where the file doesn't exist or is not valid YAML
// console.error("Error reading existing file:", error);
}



// Add or update the entry for the current id
existingData.push(currentData);

// Convert the array to YAML format
const yamlData = yaml.dump(existingData);

// Write the updated YAML data back to the file
fs.writeFileSync(filePath, yamlData, 'utf-8');

// Log to the console
// console.log("Executed lines by file:", executedLinesByFile);
// console.log("Data has been appended and logged to", filePath);
}

// isJsonValid checks whether o is a valid JSON or not

let count = 0;
const executedLinebyEachTest = new Array();
function GetCoverage() {
// console.log("Inside GetCoverage");
count++;
let executedLinesByFile = {};
// iterate over global.__coverage__
// @ts-ignore
for (const filename in global.__coverage__) {
// console.log("FIlenamae", filename);
// while (1) {
// @ts-ignore
let coverageData = global.__coverage__[filename];
// console.log("Inside GetCoverage " + count);
// console.log(coverageData);


// for (const filePath of Object.keys(coverageData)) {
const executedLines = new Set();
const fileCoverage = coverageData;
const statementMap = fileCoverage.statementMap;
const hitCounts = fileCoverage.s;
if (count > 1) {
// iterate over hitcounts and subtract the previous hitcounts
// @ts-ignore
var prevHitCounts = executedLinebyEachTest[count - 2];

for (const statementId in hitCounts) {
hitCounts[statementId] = Math.abs(
hitCounts[statementId] - prevHitCounts[statementId]
);
}
}

for (const statementId in statementMap) {
if (hitCounts[statementId] > 0) {
const executedLine = statementMap[statementId].start.line;
executedLines.add(executedLine);
}
}
// @ts-ignore
executedLinesByFile[filename] = Array.from(executedLines).sort((a, b) => a - b);
// }
// @ts-ignore
executedLinebyEachTest.push({ ...hitCounts });

// console.log("Executed lines by file:", executedLinesByFile);
// extract s from the coverage data
}
return executedLinesByFile;
}
28 changes: 28 additions & 0 deletions v2/dedup/register.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// @ts-ignore
import Hook from "require-in-the-middle";
import expressMiddleware from "./middleware";
import bodyParser from "body-parser";
import cors from "cors";
import mixin from "merge-descriptors";


// @ts-ignore
Hook(["express"], function (exports) {
const expressApp = exports;
function keployWrappedExpress() {
const keployApp = expressApp();

keployApp.use(bodyParser.json());
keployApp.use(cors());
keployApp.use(expressMiddleware());
keployApp.appliedMiddleware = true;
return keployApp;
}

// copy the properties and methods of exported Function object into wrapped Funtion(keployWrappedExpress).
// In order to prevent "express._Method_ or express._Field_ is not declared" error.
mixin(keployWrappedExpress, expressApp, false);
exports = keployWrappedExpress;
return exports;
});
export {};

0 comments on commit c8fd07a

Please sign in to comment.