-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from mcarvin8/clover
feat: add support for clover format
- Loading branch information
Showing
13 changed files
with
507 additions
and
95 deletions.
There are no files selected for viewing
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
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
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 @@ | ||
export const formatOptions: string[] = ['sonar', 'cobertura', 'clover']; |
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,82 @@ | ||
'use strict'; | ||
|
||
import { | ||
SonarCoverageObject, | ||
CoberturaCoverageObject, | ||
CloverCoverageObject, | ||
CoberturaClass, | ||
CoberturaPackage, | ||
} from './types.js'; | ||
|
||
export function initializeCoverageObject(format: string): { | ||
coverageObj: SonarCoverageObject | CoberturaCoverageObject | CloverCoverageObject; | ||
packageObj: CoberturaPackage | null; | ||
} { | ||
let coverageObj: SonarCoverageObject | CoberturaCoverageObject | CloverCoverageObject; | ||
|
||
if (format === 'sonar') { | ||
coverageObj = { | ||
coverage: { '@version': '1', file: [] }, | ||
} as SonarCoverageObject; | ||
} else if (format === 'cobertura') { | ||
coverageObj = { | ||
coverage: { | ||
'@lines-valid': 0, | ||
'@lines-covered': 0, | ||
'@line-rate': 0, | ||
'@branches-valid': 0, | ||
'@branches-covered': 0, | ||
'@branch-rate': 1, | ||
'@timestamp': Date.now(), | ||
'@complexity': 0, | ||
'@version': '0.1', | ||
sources: { source: ['.'] }, | ||
packages: { package: [] }, | ||
}, | ||
} as CoberturaCoverageObject; | ||
} else { | ||
coverageObj = { | ||
coverage: { | ||
'@generated': Date.now(), | ||
'@clover': '3.2.0', | ||
project: { | ||
'@timestamp': Date.now(), | ||
'@name': 'All files', | ||
metrics: { | ||
'@statements': 0, | ||
'@coveredstatements': 0, | ||
'@conditionals': 0, | ||
'@coveredconditionals': 0, | ||
'@methods': 0, | ||
'@coveredmethods': 0, | ||
'@elements': 0, | ||
'@coveredelements': 0, | ||
'@complexity': 0, | ||
'@loc': 0, | ||
'@ncloc': 0, | ||
'@packages': 1, | ||
'@files': 0, | ||
'@classes': 0, | ||
}, | ||
file: [], | ||
}, | ||
}, | ||
} as CloverCoverageObject; | ||
} | ||
|
||
const packageObj = | ||
format === 'cobertura' | ||
? ({ | ||
'@name': 'main', | ||
'@line-rate': 0, | ||
'@branch-rate': 1, | ||
classes: { class: [] as CoberturaClass[] }, | ||
} as CoberturaPackage) | ||
: null; | ||
|
||
if (packageObj) { | ||
(coverageObj as CoberturaCoverageObject).coverage.packages.package.push(packageObj); | ||
} | ||
|
||
return { coverageObj, packageObj }; | ||
} |
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,55 @@ | ||
'use strict'; | ||
/* eslint-disable no-param-reassign */ | ||
|
||
import { join } from 'node:path'; | ||
import { getTotalLines } from './getTotalLines.js'; | ||
import { CloverFile, CloverLine } from './types.js'; | ||
|
||
export async function setCoveredLinesClover( | ||
coveredLines: number[], | ||
uncoveredLines: number[], | ||
repoRoot: string, | ||
filePath: string, | ||
fileObj: CloverFile | ||
): Promise<void> { | ||
const randomLines: number[] = []; | ||
const totalLines = await getTotalLines(join(repoRoot, filePath)); | ||
|
||
for (const coveredLine of coveredLines) { | ||
if (coveredLine > totalLines) { | ||
for (let randomLineNumber = 1; randomLineNumber <= totalLines; randomLineNumber++) { | ||
if ( | ||
!uncoveredLines.includes(randomLineNumber) && | ||
!coveredLines.includes(randomLineNumber) && | ||
!randomLines.includes(randomLineNumber) | ||
) { | ||
const randomLine: CloverLine = { | ||
'@num': randomLineNumber, | ||
'@count': 1, | ||
'@type': 'stmt', | ||
}; | ||
fileObj.line.push(randomLine); | ||
randomLines.push(randomLineNumber); | ||
break; | ||
} | ||
} | ||
} else { | ||
const coveredLineObj: CloverLine = { | ||
'@num': coveredLine, | ||
'@count': 1, | ||
'@type': 'stmt', | ||
}; | ||
fileObj.line.push(coveredLineObj); | ||
} | ||
} | ||
|
||
// Update Clover file-level metrics | ||
fileObj.metrics['@statements'] += coveredLines.length + uncoveredLines.length; | ||
fileObj.metrics['@coveredstatements'] += coveredLines.length; | ||
|
||
// Optionally calculate derived metrics | ||
fileObj.metrics['@conditionals'] ??= 0; // Add default if missing | ||
fileObj.metrics['@coveredconditionals'] ??= 0; | ||
fileObj.metrics['@methods'] ??= 0; | ||
fileObj.metrics['@coveredmethods'] ??= 0; | ||
} |
Oops, something went wrong.