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

Enable glob support on path #19

Merged
merged 3 commits into from
Sep 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -26,7 +26,7 @@ The GITHUB_TOKEN. See [details](https://help.github.com/en/articles/virtual-envi

### `path`

The to the cobertura report. Defaults to `coverage.xml`.
The to the cobertura report. Defaults to `coverage.xml`. If a glob pattern is provided it will take the first match.

### `skip_covered`

Expand Down
22 changes: 22 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -29,6 +29,7 @@
"dependencies": {
"@actions/core": "^1.2.2",
"@actions/github": "^2.1.0",
"glob-promise": "^3.4.0",
"xml2js": "^0.4.23"
},
"devDependencies": {
Expand Down
7 changes: 7 additions & 0 deletions src/cobertura.js
@@ -1,10 +1,17 @@
const fs = require("fs").promises;
const xml2js = require("xml2js");
const util = require("util");
const glob = require("glob-promise");
const parseString = util.promisify(xml2js.parseString);

async function processCoverage(path, options) {
options = options || { skipCovered: false };

if (glob.hasMagic(path)) {
const paths = await glob(path);
path = paths[0];
}

const xml = await fs.readFile(path, "utf-8");
const { coverage } = await parseString(xml, {
explicitArray: false,
Expand Down
31 changes: 31 additions & 0 deletions src/cobertura.test.js
Expand Up @@ -147,3 +147,34 @@ test("processCoverage(test-python.xml, {skipCovered: false})", async () => {
expect(files[0].filename).toBe("source.py");
expect(files[0].name).toBe("source.py");
});

test("processCoverage(glob-test-branch.xml, {skipCovered: false})", async () => {
const report = await processCoverage("./src/**/test-branch.xml");
expect(report.total).toBe(82.5);
const files = report.files;
expect(files.length).toBe(4);

expect(files[0].total).toBe(100);
expect(files[0].branch).toBe(100);
expect(files[0].line).toBe(100);
expect(files[0].filename).toBe("Main.java");
expect(files[0].name).toBe("Main");

expect(files[1].total).toBe(87.5);
expect(files[1].branch).toBe(83.33333333333334);
expect(files[1].line).toBe(91.66666666666666);
expect(files[1].filename).toBe("search/BinarySearch.java");
expect(files[1].name).toBe("search.BinarySearch");

expect(files[2].total).toBe(100);
expect(files[2].branch).toBe(100);
expect(files[2].line).toBe(100);
expect(files[2].filename).toBe("search/ISortedArraySearch.java");
expect(files[2].name).toBe("search.ISortedArraySearch");

expect(files[3].total).toBe(69.04761904761904);
expect(files[3].branch).toBe(66.66666666666666);
expect(files[3].line).toBe(71.42857142857143);
expect(files[3].filename).toBe("search/LinearSearch.java");
expect(files[3].name).toBe("search.LinearSearch");
});