|
| 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 | +} |
0 commit comments