aboutsummaryrefslogtreecommitdiff
path: root/src/formats.ts
blob: ff24061a5a56cc965224a23de9722ed577d66197 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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) => Promise<CoverageResult>;
  matchColor: (coverage: number, style: GradientStyle) => string;
  fileName: string;
}

interface FormatList {
  [key: string]: Format;
}

interface FormatObj {
  formats: FormatList;
  listFormats: () => string[];
  getFormat: (format: string) => Format;
}

export interface GradientStyle {
  stage1: number;
  stage2: number;
}

// color is a gradient from green (>=stage_1) -> yellow (stage_2) -> red. Stage values should come from metadata.
export const defaultColorMatches = (
  coverage: number,
  style: GradientStyle
): string => {
  const gradient =
    coverage >= style.stage1
      ? 76
      : coverage >= style.stage2
      ? Math.floor(
          ((style.stage1 - coverage) / (style.stage1 - style.stage2)) * 10
        ) *
          16 +
        76
      : 225 + Math.floor(coverage / (style.stage2 / 11));
  return gradient.toString(16) + "1";
};

const FormatsObj: FormatObj = {
  formats: {
    tarpaulin: {
      parseCoverage: async (contents: string): Promise<CoverageResult> => {
        const file = new JSDOM(contents).window.document;
        const scripts = file.getElementsByTagName("script");
        if (scripts.length === 0) {
          return new InvalidReportDocumentError();
        }
        const data = scripts[0].text;
        const accumFunc = (regex: RegExp): number => {
          let acc = 0;
          while (true) {
            const match = regex.exec(data);
            if (match === null) break;
            acc += Number(match[1]);
          }

          return acc;
        };

        const covered = accumFunc(/"covered":(\d*)/g);
        const coverable = accumFunc(/"coverable":(\d*)/g);

        // do not error if LOC is 0
        if (coverable === 0) {
          return 0.0;
        }
        return (100 * covered) / coverable;
      },
      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",
    },
  },

  listFormats: function () {
    return Object.keys(this.formats);
  },

  getFormat: function (format: string) {
    return this.formats[format];
  },
};

export default FormatsObj;