Skip to content

Commit

Permalink
Merge pull request #19 from ahjephson/feature/path-glob-support
Browse files Browse the repository at this point in the history
Enable glob support on path
  • Loading branch information
hannseman committed Sep 14, 2020
2 parents 927668b + 979fd53 commit 22a3849
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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");
});

0 comments on commit 22a3849

Please sign in to comment.