generated from keploy/template
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add code for getting coverage per req (#108)
Signed-off-by: Sarthak Shyngle <[email protected]>
- Loading branch information
1 parent
abd393d
commit c8fd07a
Showing
5 changed files
with
202 additions
and
11 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {}; |