Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for exclude files #48

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ branch. When this is set a diff of the coverage percentages is shown.
##### `filter-changed-files` (**Default: false**)
If set to true, only changed files will be included in the report. Total percentage will still include all files.

##### `excluded-files` (**Default: []**)
A list of contains string for files without unit testing. Total percentage will be impacted.

##### `delete-old-comments` (**Default: false**)
If set to true, old comments will be deleted before a new comment is posted

Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ inputs:
description: Set to true to only comment with coverage on files changed in this commit
required: false
default: false
excluded-files:
description: List of files to be excluded from coverage
required: false
default: []
delete-old-comments:
description: Set to true to delete old Coverage Report comments
required: false
Expand Down
43 changes: 33 additions & 10 deletions dist/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -22749,17 +22749,28 @@ function parse$2(data) {
}

// Get the total coverage percentage from the lcov data.
function percentage(lcov) {
function percentage(lcov, options) {
let hit = 0;
let found = 0;
for (const entry of lcov) {
hit += entry.lines.hit;
found += entry.lines.found;
if (shouldBeIncluded(entry.file, options)) {
hit += entry.lines.hit;
found += entry.lines.found;
}
}

return (hit / found) * 100
}

function shouldBeIncluded(fileName, options) {
for (let i in options.excludedFiles) {
if (fileName.startsWith(options.excludedFiles[i])) {
return false
}
}
return true
}

function tag(name) {
return function(...children) {
const props =
Expand Down Expand Up @@ -22833,10 +22844,15 @@ function filterAndNormaliseLcov(lcov, options) {
...file,
file: normalisePath(file.file),
}))
.filter(file => shouldBeIncluded(file.file, options))
.filter(file => shouldBeIncluded$1(file.file, options))
}

function shouldBeIncluded(fileName, options) {
function shouldBeIncluded$1(fileName, options) {
for (let i in options.excludedFiles) {
if (fileName.startsWith(options.excludedFiles[i])) {
return false
}
}
if (!options.shouldFilterChangedFiles) {
return true
}
Expand Down Expand Up @@ -22966,7 +22982,7 @@ function comment(lcov, options) {
options.base,
)} will be`
: `Coverage for this commit`,
table(tbody(tr(th(percentage(lcov).toFixed(2), "%")))),
table(tbody(tr(th(percentage(lcov, options).toFixed(2), "%")))),
"\n\n",
details(
summary(
Expand All @@ -22984,8 +23000,8 @@ function diff(lcov, before, options) {
return comment(lcov, options)
}

const pbefore = percentage(before);
const pafter = percentage(lcov);
const pbefore = percentage(before, options);
const pafter = percentage(lcov, options);
const pdiff = pafter - pbefore;
const plus = pdiff > 0 ? "+" : "";
const arrow = pdiff === 0 ? "" : pdiff < 0 ? "▾" : "▴";
Expand Down Expand Up @@ -23094,8 +23110,14 @@ async function main$1() {
const githubClient = new github_2(token);
const lcovFile = core$1.getInput("lcov-file") || "./coverage/lcov.info";
const baseFile = core$1.getInput("lcov-base");
const shouldFilterChangedFiles = core$1.getInput("filter-changed-files").toLowerCase() === 'true';
const shouldDeleteOldComments = core$1.getInput("delete-old-comments").toLowerCase() === 'true';
const shouldFilterChangedFiles =
core$1.getInput("filter-changed-files").toLowerCase() === "true";
const excludedFiles = core$1
.getInput("excluded-files")
.split("\n")
.filter(x => x !== "");
const shouldDeleteOldComments =
core$1.getInput("delete-old-comments").toLowerCase() === "true";
const title = core$1.getInput("title");

const raw = await fs.promises.readFile(lcovFile, "utf-8").catch(err => null);
Expand Down Expand Up @@ -23127,6 +23149,7 @@ async function main$1() {
}

options.shouldFilterChangedFiles = shouldFilterChangedFiles;
options.excludedFiles = excludedFiles;
options.title = title;

if (shouldFilterChangedFiles) {
Expand Down
6 changes: 3 additions & 3 deletions src/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function comment(lcov, options) {
options.base,
)} will be`
: `Coverage for this commit`,
table(tbody(tr(th(percentage(lcov).toFixed(2), "%")))),
table(tbody(tr(th(percentage(lcov, options).toFixed(2), "%")))),
"\n\n",
details(
summary(
Expand All @@ -29,8 +29,8 @@ export function diff(lcov, before, options) {
return comment(lcov, options)
}

const pbefore = percentage(before)
const pafter = percentage(lcov)
const pbefore = percentage(before, options)
const pafter = percentage(lcov, options)
const pdiff = pafter - pbefore
const plus = pdiff > 0 ? "+" : ""
const arrow = pdiff === 0 ? "" : pdiff < 0 ? "▾" : "▴"
Expand Down
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ async function main() {
const baseFile = core.getInput("lcov-base")
const shouldFilterChangedFiles =
core.getInput("filter-changed-files").toLowerCase() === "true"
const excludedFiles = core
.getInput("excluded-files")
.split("\n")
.filter(x => x !== "")
const shouldDeleteOldComments =
core.getInput("delete-old-comments").toLowerCase() === "true"
const title = core.getInput("title")
Expand Down Expand Up @@ -50,6 +54,7 @@ async function main() {
}

options.shouldFilterChangedFiles = shouldFilterChangedFiles
options.excludedFiles = excludedFiles
options.title = title

if (shouldFilterChangedFiles) {
Expand Down
17 changes: 14 additions & 3 deletions src/lcov.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,24 @@ export function parse(data) {
}

// Get the total coverage percentage from the lcov data.
export function percentage(lcov) {
export function percentage(lcov, options) {
let hit = 0
let found = 0
for (const entry of lcov) {
hit += entry.lines.hit
found += entry.lines.found
if (shouldBeIncluded(entry.file, options)) {
hit += entry.lines.hit
found += entry.lines.found
}
}

return (hit / found) * 100
}

function shouldBeIncluded(fileName, options) {
for (let i in options.excludedFiles) {
if (fileName.startsWith(options.excludedFiles[i])) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use an actual glob here instead of a prefix match.

return false
}
}
return true
}
15 changes: 11 additions & 4 deletions src/lcov_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,17 @@ test("parse should fail on invalid lcov", async function() {
})

test("percentage should calculate the correct percentage", function() {
const options = {
excludedFiles: ["lib/ignore"],
}
expect(
percentage([
{ lines: { hit: 20, found: 25 } },
{ lines: { hit: 10, found: 15 } },
]),
percentage(
[
{ lines: { hit: 20, found: 25 }, file: "" },
{ lines: { hit: 10, found: 15 }, file: "" },
{ lines: { hit: 0, found: 15 }, file: "lib/ignore" },
],
options,
),
).toBe(75)
})
5 changes: 5 additions & 0 deletions src/tabulate.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ function filterAndNormaliseLcov(lcov, options) {
}

function shouldBeIncluded(fileName, options) {
for (let i in options.excludedFiles) {
if (fileName.startsWith(options.excludedFiles[i])) {
return false
}
}
if (!options.shouldFilterChangedFiles) {
return true
}
Expand Down
175 changes: 175 additions & 0 deletions src/tabulate_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,181 @@ test("filtered tabulate should generate a correct table with only changed files"
expect(tabulate(data, options)).toBe(html)
})

test("filtered tabulate should generate a correct table with excluded files", function() {
const data = [
{
file: "/files/project/index.js",
functions: {
found: 0,
hit: 0,
details: [],
},
},
{
file: "/files/project/src/foo.js",
lines: {
found: 23,
hit: 21,
details: [
{
line: 20,
hit: 3,
},
{
line: 21,
hit: 3,
},
{
line: 22,
hit: 3,
},
],
},
functions: {
hit: 2,
found: 3,
details: [
{
name: "foo",
line: 19,
},
{
name: "bar",
line: 33,
},
{
name: "baz",
line: 54,
},
],
},
branches: {
hit: 3,
found: 3,
details: [
{
line: 21,
block: 0,
branch: 0,
taken: 1,
},
{
line: 21,
block: 0,
branch: 1,
taken: 2,
},
{
line: 37,
block: 1,
branch: 0,
taken: 0,
},
],
},
},
{
file: "/files/project/src/bar/baz.js",
lines: {
found: 10,
hit: 5,
details: [
{
line: 20,
hit: 0,
},
{
line: 21,
hit: 0,
},
{
line: 27,
hit: 0,
},
],
},
functions: {
hit: 2,
found: 3,
details: [
{
name: "foo",
line: 19,
},
{
name: "bar",
line: 33,
},
{
name: "baz",
line: 54,
},
],
},
},
]

const options = {
repository: "example/foo",
commit: "2e15bee6fe0df5003389aa5ec894ec0fea2d874a",
prefix: "/files/project/",
excludedFiles: ["/files/project/src/bar"],
}

const html = table(
tbody(
tr(
th("File"),
th("Stmts"),
th("Branches"),
th("Funcs"),
th("Lines"),
th("Uncovered Lines"),
),
tr(
td(
a(
{
href: `https://github.com/${options.repository}/blob/${options.commit}/index.js`,
},
"index.js",
),
),
td("100%"),
td("N/A"),
td("100%"),
td("N/A"),
td(),
),
tr(td({ colspan: 6 }, b("src"))),
tr(
td(
"&nbsp; &nbsp;",
a(
{
href: `https://github.com/${options.repository}/blob/${options.commit}/src/foo.js`,
},
"foo.js",
),
),
td(b("89.66%")),
td("100%"),
td(b("66.67%")),
td(b("91.30%")),
td(
a(
{
href: `https://github.com/${options.repository}/blob/${options.commit}/src/foo.js#L37`,
},
37,
),
),
),
),
)
expect(tabulate(data, options)).toBe(html)
})

test("filtered tabulate should fix backwards slashes in filenames", function() {
const data = [
{
Expand Down