Skip to content

Commit 4148348

Browse files
committed
Allow diffing of lcov files
1 parent 2ed7f55 commit 4148348

File tree

6 files changed

+89
-8
lines changed

6 files changed

+89
-8
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ action runner, use `${{ secrets.GITHUB_TOKEN }}`.
2323
The location of the lcov file to read the coverage report from. Defaults to
2424
`./coverage/lcov.info`.
2525

26+
##### `lcov-base` (**Optional**)
27+
The location of the lcov file resulting from running the tests in the base
28+
branch. When this is set a diff of the coverage percentages is shown.
29+
2630
## Example usage
2731

2832
```yml

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ inputs:
1111
lcov-file:
1212
description: The location of the lcov.info file
1313
required: false
14+
lcov-base:
15+
description: The location of the lcov file for the base branch
16+
required: false
1417
runs:
1518
using: node12
1619
main: dist/main.js

dist/main.js

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22881,7 +22881,7 @@ function uncovered(file, options) {
2288122881
.join(", ")
2288222882
}
2288322883

22884-
function comment (lcov, options) {
22884+
function comment$1 (lcov, options) {
2288522885
return fragment(
2288622886
`Coverage after merging ${b(options.head)} into ${b(options.base)}`,
2288722887
table(tbody(tr(th(percentage(lcov).toFixed(2), "%")))),
@@ -22890,17 +22890,49 @@ function comment (lcov, options) {
2289022890
)
2289122891
}
2289222892

22893+
function diff(lcov, before, options) {
22894+
if (!before) {
22895+
return comment$1(lcov, options)
22896+
}
22897+
22898+
const pbefore = percentage(before);
22899+
const pafter = percentage(lcov);
22900+
const pdiff = pafter - pbefore;
22901+
const plus = pdiff > 0 ? "+" : "";
22902+
const arrow =
22903+
pdiff === 0
22904+
? ""
22905+
: pdiff < 0
22906+
? "▾"
22907+
: "▴";
22908+
22909+
return fragment(
22910+
`Coverage after merging ${b(options.head)} into ${b(options.base)}`,
22911+
table(tbody(tr(
22912+
th(pafter.toFixed(2), "%"),
22913+
th(arrow, " ", plus, pdiff.toFixed(2), "%"),
22914+
))),
22915+
"\n\n",
22916+
details(summary("Coverage Report"), tabulate(lcov, options)),
22917+
)
22918+
}
22919+
2289322920
async function main$1() {
2289422921
const token = core$1.getInput("github-token");
2289522922
const lcovFile = core$1.getInput("lcov-file") || "./coverage/lcov.info";
22923+
const baseFile = core$1.getInput("lcov-base");
2289622924

2289722925
const raw = await fs.promises.readFile(lcovFile, "utf-8").catch(err => null);
22898-
2289922926
if (!raw) {
2290022927
console.log(`No coverage report found at '${lcovFile}', exiting...`);
2290122928
return
2290222929
}
2290322930

22931+
const baseRaw = baseFile && await fs.promises.readFile(baseFile, "utf-8").catch(err => null);
22932+
if (baseFile && !baseRaw) {
22933+
console.log(`No coverage report found at '${baseFile}', ignoring...`);
22934+
}
22935+
2290422936
const options = {
2290522937
repository: github_1.payload.repository.full_name,
2290622938
commit: github_1.payload.pull_request.head.sha,
@@ -22910,7 +22942,8 @@ async function main$1() {
2291022942
};
2291122943

2291222944
const lcov = await parse$2(raw);
22913-
const body = comment(lcov, options);
22945+
const baselcov = baseRaw && await parse$2(baseRaw);
22946+
const body = diff(lcov, baselcov, options);
2291422947

2291522948
await new github_2(token).issues.createComment({
2291622949
repo: github_1.repo.repo,

src/cli.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,22 @@ import { promises as fs } from "fs"
33
import path from "path"
44

55
import { parse } from "./lcov"
6-
import { comment } from "./comment"
6+
import { diff } from "./comment"
77

88
async function main() {
99
const file = process.argv[2]
10+
const beforeFile = process.argv[3]
1011
const prefix = path.dirname(path.dirname(path.resolve(file))) + "/"
1112

1213
const content = await fs.readFile(file, "utf-8")
1314
const lcov = await parse(content)
15+
16+
let before
17+
if (beforeFile) {
18+
const content = await fs.readFile(beforeFile, "utf-8")
19+
before = await parse(content)
20+
}
21+
1422
const options = {
1523
repository: "example/foo",
1624
commit: "f9d42291812ed03bb197e48050ac38ac6befe4e5",
@@ -19,7 +27,7 @@ async function main() {
1927
base: "master",
2028
}
2129

22-
console.log(comment(lcov, options))
30+
console.log(diff(lcov, before, options))
2331
}
2432

2533
main().catch(function(err) {

src/comment.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,30 @@ export function comment (lcov, options) {
1111
details(summary("Coverage Report"), tabulate(lcov, options)),
1212
)
1313
}
14+
15+
export function diff(lcov, before, options) {
16+
if (!before) {
17+
return comment(lcov, options)
18+
}
19+
20+
const pbefore = percentage(before)
21+
const pafter = percentage(lcov)
22+
const pdiff = pafter - pbefore
23+
const plus = pdiff > 0 ? "+" : ""
24+
const arrow =
25+
pdiff === 0
26+
? ""
27+
: pdiff < 0
28+
? "▾"
29+
: "▴"
30+
31+
return fragment(
32+
`Coverage after merging ${b(options.head)} into ${b(options.base)}`,
33+
table(tbody(tr(
34+
th(pafter.toFixed(2), "%"),
35+
th(arrow, " ", plus, pdiff.toFixed(2), "%"),
36+
))),
37+
"\n\n",
38+
details(summary("Coverage Report"), tabulate(lcov, options)),
39+
)
40+
}

src/index.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,24 @@ import core from "@actions/core"
33
import { GitHub, context } from "@actions/github"
44

55
import { parse } from "./lcov"
6-
import { comment } from "./comment"
6+
import { diff } from "./comment"
77

88
async function main() {
99
const token = core.getInput("github-token")
1010
const lcovFile = core.getInput("lcov-file") || "./coverage/lcov.info"
11+
const baseFile = core.getInput("lcov-base")
1112

1213
const raw = await fs.readFile(lcovFile, "utf-8").catch(err => null)
13-
1414
if (!raw) {
1515
console.log(`No coverage report found at '${lcovFile}', exiting...`)
1616
return
1717
}
1818

19+
const baseRaw = baseFile && await fs.readFile(baseFile, "utf-8").catch(err => null)
20+
if (baseFile && !baseRaw) {
21+
console.log(`No coverage report found at '${baseFile}', ignoring...`)
22+
}
23+
1924
const options = {
2025
repository: context.payload.repository.full_name,
2126
commit: context.payload.pull_request.head.sha,
@@ -25,7 +30,8 @@ async function main() {
2530
}
2631

2732
const lcov = await parse(raw)
28-
const body = comment(lcov, options)
33+
const baselcov = baseRaw && await parse(baseRaw)
34+
const body = diff(lcov, baselcov, options)
2935

3036
await new GitHub(token).issues.createComment({
3137
repo: context.repo.repo,

0 commit comments

Comments
 (0)