aboutsummaryrefslogtreecommitdiff
path: root/src/formats.ts
diff options
context:
space:
mode:
authorKevin J Hoerr <kjhoerr@protonmail.com>2021-09-16 15:45:34 -0400
committerKevin J Hoerr <kjhoerr@protonmail.com>2021-09-16 15:45:34 -0400
commit6bbd3f03104e6dcd9da89a8ec5dcb5d992ee3ed5 (patch)
tree6f44b79750ad00267c8840b3aa472ee64aefc66b /src/formats.ts
parent77575aab559f058d886a691eefe262cf0f306710 (diff)
downloadao-coverage-6bbd3f03104e6dcd9da89a8ec5dcb5d992ee3ed5.tar.gz
ao-coverage-6bbd3f03104e6dcd9da89a8ec5dcb5d992ee3ed5.tar.bz2
ao-coverage-6bbd3f03104e6dcd9da89a8ec5dcb5d992ee3ed5.zip
#7 Add Cobertura format
Diffstat (limited to 'src/formats.ts')
-rw-r--r--src/formats.ts28
1 files changed, 26 insertions, 2 deletions
diff --git a/src/formats.ts b/src/formats.ts
index 11113f0..db13fa6 100644
--- a/src/formats.ts
+++ b/src/formats.ts
@@ -1,11 +1,12 @@
import { JSDOM } from "jsdom";
+import { Parser } from "xml2js";
import { InvalidReportDocumentError } from "./errors";
type CoverageResult = number | InvalidReportDocumentError;
export interface Format {
// returns the coverage value as %: Number(90.0), Number(100.0), Number(89.5)
- parseCoverage: (contents: string) => CoverageResult;
+ parseCoverage: (contents: string) => Promise<CoverageResult>;
matchColor: (coverage: number, style: GradientStyle) => string;
fileName: string;
}
@@ -46,7 +47,7 @@ export const defaultColorMatches = (
const FormatsObj: FormatObj = {
formats: {
tarpaulin: {
- parseCoverage: (contents: string): CoverageResult => {
+ parseCoverage: async (contents: string): Promise<CoverageResult> => {
const file = new JSDOM(contents).window.document;
const scripts = file.getElementsByTagName("script");
if (scripts.length === 0) {
@@ -75,6 +76,29 @@ const FormatsObj: FormatObj = {
},
matchColor: defaultColorMatches,
fileName: "index.html"
+ },
+ cobertura: {
+ parseCoverage: async (contents: string): Promise<CoverageResult> => {
+ try {
+ const document = await new Parser().parseStringPromise(contents);
+
+ if (document.coverage === undefined) {
+ return new InvalidReportDocumentError();
+ } else {
+ const validLines = Number(document.coverage.$["lines-valid"]);
+ const coveredLines = Number(document.coverage.$["lines-covered"]);
+ // do not error if LOC is 0
+ if (validLines === 0) {
+ return 0.0;
+ }
+ return (100 * coveredLines) / validLines;
+ }
+ } catch (err) {
+ return new InvalidReportDocumentError();
+ }
+ },
+ matchColor: defaultColorMatches,
+ fileName: "index.xml"
}
},