diff options
| author | Kevin J Hoerr <kjhoerr@protonmail.com> | 2019-11-29 18:28:54 -0500 |
|---|---|---|
| committer | Kevin J Hoerr <kjhoerr@protonmail.com> | 2019-11-29 18:28:54 -0500 |
| commit | 1158e558606034b681f4663d62677e798cf5ec23 (patch) | |
| tree | 35e9ead895b0b0d2008d4c622f5d010a4098fc7c /src | |
| parent | 5964ec16673bd1474d32ffe87a2009b823c36f12 (diff) | |
| download | ao-coverage-1158e558606034b681f4663d62677e798cf5ec23.tar.gz ao-coverage-1158e558606034b681f4663d62677e798cf5ec23.tar.bz2 ao-coverage-1158e558606034b681f4663d62677e798cf5ec23.zip | |
Implement report parsing for Tarpaulin
The generated Tarpaulin HTML report does not have stats to parse via the
DOM - the data is injected via a script tag, which only has line-by-line
coverage, though it includes statistics for each file. The total coverage is
counted by summing the covered/coverable stats reported for each file.
Also, the bad version for express is fixed, and messages are
included for errors that occur for the POST endpoint.
Diffstat (limited to 'src')
| -rw-r--r-- | src/formats.ts | 26 | ||||
| -rw-r--r-- | src/index.ts | 11 |
2 files changed, 32 insertions, 5 deletions
diff --git a/src/formats.ts b/src/formats.ts index 34f97df..5d389b4 100644 --- a/src/formats.ts +++ b/src/formats.ts @@ -33,8 +33,30 @@ const FormatsObj: FormatObj = { formats: { tarpaulin: { parse_coverage: (file: Document) => { - //TODO parse coverage from file (example?) - return 0.0; + const scripts = file.getElementsByTagName("script"); + if (scripts.length == 0) { + throw new Error("Invalid report document"); + } + const data = scripts[0].text; + const accumFunc = (regex: RegExp) => { + let acc: number = 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; }, match_color: default_color_matches } diff --git a/src/index.ts b/src/index.ts index 4fe80a9..d592405 100644 --- a/src/index.ts +++ b/src/index.ts @@ -36,12 +36,12 @@ app.post("/v1/:org/:repo/:branch/:commit.html", (req, res) => { const { token, format } = req.query; //TODO @Metadata token should come from metadata if (token != TOKEN) { - return res.status(401).send(); + return res.status(401).send("Invalid token"); } const reporter = format || "tarpaulin"; if (!formats.list_formats().includes(reporter)) { - return res.status(406).send(); + return res.status(406).send("Report format unknown"); } //TODO acquire file, verify file size/content type (HTML) @@ -51,7 +51,12 @@ app.post("/v1/:org/:repo/:branch/:commit.html", (req, res) => { const doc = new DOMParser().parseFromString(contents, "text/html"); const formatter = formats.get_format(reporter); - const coverage = formatter.parse_coverage(doc); + let coverage: number; + try { + coverage = formatter.parse_coverage(doc); + } catch { + return res.status(400).send("Invalid report document"); + } const badge = badgen({ label: "coverage", |
