Skip to content

Commit

Permalink
Merge pull request #80 from mcarvin8/covered-lines
Browse files Browse the repository at this point in the history
  • Loading branch information
mcarvin8 authored Jan 9, 2025
2 parents f8cfd6f + 34bb533 commit 60db4c6
Show file tree
Hide file tree
Showing 17 changed files with 145 additions and 325 deletions.
28 changes: 9 additions & 19 deletions src/handlers/cloverCoverageHandler.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict';

import { CloverCoverageObject, CloverFile, CoverageHandler } from '../helpers/types.js';
import { setCoveredLinesClover } from '../helpers/setCoveredLinesClover.js';
import { normalizePathToUnix } from '../helpers/normalizePathToUnix.js';

export class CloverCoverageHandler implements CoverageHandler {
private readonly coverageObj: CloverCoverageObject;
Expand Down Expand Up @@ -37,18 +35,16 @@ export class CloverCoverageHandler implements CoverageHandler {
};
}

public async processFile(
public processFile(
filePath: string,
fileName: string,
lines: Record<string, number>,
uncoveredLines: number[],
coveredLines: number[],
repoRoot: string,
reportType: 'test' | 'deploy'
): Promise<void> {
): void {
const fileObj: CloverFile = {
'@name': fileName,
'@path': normalizePathToUnix(filePath),
'@path': filePath,
metrics: {
'@statements': uncoveredLines.length + coveredLines.length,
'@coveredstatements': coveredLines.length,
Expand All @@ -59,18 +55,12 @@ export class CloverCoverageHandler implements CoverageHandler {
},
line: [],
};

if (reportType === 'test') {
for (const [lineNumber, isCovered] of Object.entries(lines)) {
fileObj.line.push({
'@num': Number(lineNumber),
'@count': isCovered === 1 ? 1 : 0,
'@type': 'stmt',
});
}
} else if (reportType === 'deploy') {
fileObj.line = [...uncoveredLines.map((lineNumber) => ({ '@num': lineNumber, '@count': 0, '@type': 'stmt' }))];
await setCoveredLinesClover(coveredLines, uncoveredLines, repoRoot, filePath, fileObj);
for (const [lineNumber, isCovered] of Object.entries(lines)) {
fileObj.line.push({
'@num': Number(lineNumber),
'@count': isCovered === 1 ? 1 : 0,
'@type': 'stmt',
});
}
this.coverageObj.coverage.project.file.push(fileObj);
const projectMetrics = this.coverageObj.coverage.project.metrics;
Expand Down
30 changes: 8 additions & 22 deletions src/handlers/coberturaCoverageHandler.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict';

import { CoberturaCoverageObject, CoberturaPackage, CoberturaClass, CoverageHandler } from '../helpers/types.js';
import { setCoveredLinesCobertura } from '../helpers/setCoveredLinesCobertura.js';
import { normalizePathToUnix } from '../helpers/normalizePathToUnix.js';

export class CoberturaCoverageHandler implements CoverageHandler {
private readonly coverageObj: CoberturaCoverageObject;
Expand Down Expand Up @@ -33,18 +31,16 @@ export class CoberturaCoverageHandler implements CoverageHandler {
this.coverageObj.coverage.packages.package.push(this.packageObj);
}

public async processFile(
public processFile(
filePath: string,
fileName: string,
lines: Record<string, number>,
uncoveredLines: number[],
coveredLines: number[],
repoRoot: string,
reportType: 'test' | 'deploy'
): Promise<void> {
): void {
const classObj: CoberturaClass = {
'@name': fileName,
'@filename': normalizePathToUnix(filePath),
'@filename': filePath,
'@line-rate': '0',
'@branch-rate': '1',
methods: {},
Expand All @@ -54,22 +50,12 @@ export class CoberturaCoverageHandler implements CoverageHandler {
const totalLines = uncoveredLines.length + coveredLines.length;
const coveredLineCount = coveredLines.length;

// Process lines for 'test' or 'deploy' reports
if (reportType === 'test') {
for (const [lineNumber, isCovered] of Object.entries(lines)) {
classObj.lines.line.push({
'@number': Number(lineNumber),
'@hits': isCovered === 1 ? 1 : 0,
'@branch': 'false',
});
}
} else if (reportType === 'deploy') {
classObj.lines.line = uncoveredLines.map((lineNumber) => ({
'@number': lineNumber,
'@hits': 0,
for (const [lineNumber, isCovered] of Object.entries(lines)) {
classObj.lines.line.push({
'@number': Number(lineNumber),
'@hits': isCovered === 1 ? 1 : 0,
'@branch': 'false',
}));
await setCoveredLinesCobertura(coveredLines, uncoveredLines, repoRoot, filePath, classObj);
});
}

// Calculate and set the line rate for this class
Expand Down
26 changes: 7 additions & 19 deletions src/handlers/lcovCoverageHandler.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict';

import { CoverageHandler, LcovCoverageObject, LcovFile } from '../helpers/types.js';
import { setCoveredLinesLcov } from '../helpers/setCoveredLinesLcov.js';
import { normalizePathToUnix } from '../helpers/normalizePathToUnix.js';

export class LcovCoverageHandler implements CoverageHandler {
private readonly coverageObj: LcovCoverageObject;
Expand All @@ -11,35 +9,25 @@ export class LcovCoverageHandler implements CoverageHandler {
this.coverageObj = { files: [] };
}

public async processFile(
public processFile(
filePath: string,
fileName: string,
lines: Record<string, number>,
uncoveredLines: number[],
coveredLines: number[],
repoRoot: string,
reportType: 'test' | 'deploy'
): Promise<void> {
): void {
const lcovFile: LcovFile = {
sourceFile: normalizePathToUnix(filePath),
sourceFile: filePath,
lines: [],
totalLines: uncoveredLines.length + coveredLines.length,
coveredLines: coveredLines.length,
};

if (reportType === 'test') {
for (const [lineNumber, isCovered] of Object.entries(lines)) {
lcovFile.lines.push({
lineNumber: Number(lineNumber),
hitCount: isCovered === 1 ? 1 : 0,
});
}
} else if (reportType === 'deploy') {
lcovFile.lines = uncoveredLines.map((lineNumber) => ({
for (const [lineNumber, isCovered] of Object.entries(lines)) {
lcovFile.lines.push({
lineNumber: Number(lineNumber),
hitCount: 0,
}));
await setCoveredLinesLcov(coveredLines, uncoveredLines, repoRoot, filePath, lcovFile);
hitCount: isCovered === 1 ? 1 : 0,
});
}

this.coverageObj.files.push(lcovFile);
Expand Down
35 changes: 10 additions & 25 deletions src/handlers/sonarCoverageHandler.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict';

import { SonarCoverageObject, SonarClass, CoverageHandler } from '../helpers/types.js';
import { setCoveredLinesSonar } from '../helpers/setCoveredLinesSonar.js';
import { normalizePathToUnix } from '../helpers/normalizePathToUnix.js';

export class SonarCoverageHandler implements CoverageHandler {
private readonly coverageObj: SonarCoverageObject;
Expand All @@ -11,35 +9,22 @@ export class SonarCoverageHandler implements CoverageHandler {
this.coverageObj = { coverage: { '@version': '1', file: [] } };
}

public async processFile(
public processFile(
filePath: string,
_fileName: string,
lines: Record<string, number>,
uncoveredLines: number[],
coveredLines: number[],
repoRoot: string,
reportType: 'test' | 'deploy'
): Promise<void> {
): void {
const fileObj: SonarClass = {
'@path': normalizePathToUnix(filePath),
'@path': filePath,
lineToCover: [],
};

if (reportType === 'test') {
for (const lineNumberString in lines) {
if (!Object.hasOwn(lines, lineNumberString)) continue;
const covered = lines[lineNumberString] === 1 ? 'true' : 'false';
fileObj.lineToCover.push({
'@lineNumber': Number(lineNumberString),
'@covered': covered,
});
}
} else if (reportType === 'deploy') {
fileObj.lineToCover = uncoveredLines.map((lineNumber) => ({
'@lineNumber': lineNumber,
'@covered': 'false',
}));
await setCoveredLinesSonar(coveredLines, uncoveredLines, repoRoot, filePath, fileObj);
for (const lineNumberString in lines) {
if (!Object.hasOwn(lines, lineNumberString)) continue;
const covered = lines[lineNumberString] === 1 ? 'true' : 'false';
fileObj.lineToCover.push({
'@lineNumber': Number(lineNumberString),
'@covered': covered,
});
}

this.coverageObj.coverage.file.push(fileObj);
Expand Down
2 changes: 2 additions & 0 deletions src/helpers/findFilePath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import { readdir, stat } from 'node:fs/promises';
import { join, relative } from 'node:path';
import { normalizePathToUnix } from './normalizePathToUnix.js';

export async function findFilePath(
fileName: string,
Expand All @@ -13,6 +14,7 @@ export async function findFilePath(
for (const directory of packageDirectories) {
relativeFilePath = await findFilePathinDirectory(fileName, directory, repoRoot);
if (relativeFilePath !== undefined) {
relativeFilePath = normalizePathToUnix(relativeFilePath)
break;
}
}
Expand Down
34 changes: 34 additions & 0 deletions src/helpers/setCoveredLines.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';

import { join } from 'node:path';
import { getTotalLines } from './getTotalLines.js';

export async function renumberLines(
filePath: string,
repoRoot: string,
lines: Record<string, number>
): Promise<Record<string, number>> {
const totalLines = await getTotalLines(join(repoRoot, filePath));
const updatedLines: Record<string, number> = {};
const usedLines = new Set<number>();

for (const [line, status] of Object.entries(lines)) {
const lineNumber = parseInt(line, 10);

if (status === 1 && lineNumber > totalLines) {
// Find the first valid line number not already used
for (let randomLine = 1; randomLine <= totalLines; randomLine++) {
if (!usedLines.has(randomLine)) {
updatedLines[randomLine.toString()] = status;
usedLines.add(randomLine);
break;
}
}
} else {
updatedLines[line] = status;
usedLines.add(lineNumber);
}
}

return updatedLines;
}
44 changes: 0 additions & 44 deletions src/helpers/setCoveredLinesClover.ts

This file was deleted.

45 changes: 0 additions & 45 deletions src/helpers/setCoveredLinesCobertura.ts

This file was deleted.

35 changes: 0 additions & 35 deletions src/helpers/setCoveredLinesLcov.ts

This file was deleted.

Loading

0 comments on commit 60db4c6

Please sign in to comment.