Skip to content

Commit

Permalink
Report skipped tests and improve types
Browse files Browse the repository at this point in the history
  • Loading branch information
GaelGirodon committed May 23, 2024
1 parent a3b1d30 commit fc45199
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 35 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ jobs:
node-version: 20
- run: npm ci
- run: npm run lint
- run: npm run build
- run: npm run test:ci
env:
GIST_TOKEN: ${{ secrets.GIST_TOKEN }}
- run: rm -rf test/data
- run: npm run build
- run: |
sed -i '0,/index.js/s//dist\/index.js/' action.yml package.json
rm -rf test/data
- uses: ./
with:
gist-id: ${{ env.GIST_ID }}
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ advantage of Shields customization features (through the query string).
![tests](https://img.shields.io/endpoint?style=flat-square&url=https%3A%2F%2Fgist.githubusercontent.com%2FGaelGirodon%2F715c62717519f634185af0ebde234992%2Fraw%2Frepo-go-tests.json)
![tests](https://img.shields.io/endpoint?style=flat-square&url=https%3A%2F%2Fgist.githubusercontent.com%2FGaelGirodon%2F715c62717519f634185af0ebde234992%2Fraw%2Frepo-junit-tests.json)

This badge displays the number of passed and failed tests extracted from a test
report.
This badge displays the number of passed, failed and skipped tests extracted
from test report(s).

```json
{"schemaVersion":1,"label":"tests","message":"3 passed","color":"brightgreen"}
Expand Down Expand Up @@ -89,7 +89,7 @@ Only matched report formats will get a file uploaded to the Gist.
Write the verbose test output (`>` or `tee`) with coverage enabled to a single
`test*.{out,txt}` file next to the `go.mod` file:

- `RUN`, `PASS` and `FAIL` flags will be used to count tests
- `RUN`, `PASS`, `FAIL` and `SKIP` flags will be used to count tests
- The last percentage will be used as the coverage value

`go tool cover -func=cover.out` output may be appended to the above file to make
Expand Down Expand Up @@ -118,8 +118,8 @@ support this format too, natively or using an additional reporter:
- **Deno**: `deno test --junit-path=report.xml`
- **PHPUnit**: `phpunit --log-junit report.xml`

The number of tests and failures will be extracted from top-level `<testsuite>`
tags, from all matching and valid report files.
The number of tests (total, failed and skipped) will be extracted from
top-level `<testsuite>` tags, from all matching and valid report files.

➡️ `{repo}-[{ref}-]junit-tests.json`

Expand Down
2 changes: 1 addition & 1 deletion src/badges/coverage.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Build a coverage badge.
* @param {*} data Badge data
* @param {import('../reports/index.js').CoverageReportData} data Coverage report data
* @returns {import('./index.js').Badge} Badge content
*/
export function buildBadge(data) {
Expand Down
8 changes: 6 additions & 2 deletions src/badges/tests.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
/**
* Build a tests badge.
* @param {*} data Badge data
* @param {import('../reports/index.js').TestReportData} data Test report data
* @returns {import('./index.js').Badge} Badge content
*/
export function buildBadge(data) {
const content = {};
content.message = `${data.passed} passed`;
content.color = data.passed > 0 ? 'brightgreen' : 'lightgrey';
if (data.failed > 0) {
content.message += `, ${data.failed} failed`;
content.color = 'red';
}
if (data.skipped > 0) {
content.message += `, ${data.skipped} skipped`;
}
content.color = data.failed === 0 ? 'brightgreen' : 'red';
return content;
}
10 changes: 7 additions & 3 deletions src/badges/tests.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import { buildBadge } from './tests.js';
describe('badges/tests', () => {
describe('#buildBadge()', () => {
const tests = [
{ data: { passed: 5, failed: 0 }, expected: { message: '5 passed', color: 'brightgreen' } },
{ data: { passed: 4, failed: 1 }, expected: { message: '4 passed, 1 failed', color: 'red' } }
{ data: { passed: 0, failed: 0, skipped: 0 }, expected: { message: '0 passed', color: 'lightgrey' } },
{ data: { passed: 0, failed: 0, skipped: 1 }, expected: { message: '0 passed, 1 skipped', color: 'lightgrey' } },
{ data: { passed: 5, failed: 0, skipped: 0 }, expected: { message: '5 passed', color: 'brightgreen' } },
{ data: { passed: 4, failed: 0, skipped: 1 }, expected: { message: '4 passed, 1 skipped', color: 'brightgreen' } },
{ data: { passed: 4, failed: 1, skipped: 0 }, expected: { message: '4 passed, 1 failed', color: 'red' } },
{ data: { passed: 3, failed: 1, skipped: 1 }, expected: { message: '3 passed, 1 failed, 1 skipped', color: 'red' } }
];
for (const { data, expected } of tests) {
it(`should return a ${expected.color} "${expected.message}" badge for ${data.passed}/${data.failed} tests`, () => {
it(`should return a ${expected.color} "${expected.message}" badge for ${data.passed}/${data.failed}/${data.skipped} tests`, () => {
const actual = buildBadge(data);
assert.deepEqual(actual, expected);
});
Expand Down
3 changes: 2 additions & 1 deletion src/reports/go.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export async function getReports(root) {
}
const passed = (report.match(/--- PASS/g) || []).length;
const failed = (report.match(/--- FAIL/g) || []).length;
badges.push({ type: 'tests', data: { passed, failed, tests } });
const skipped = (report.match(/--- SKIP/g) || []).length;
badges.push({ type: 'tests', data: { tests, passed, failed, skipped } });
const percentages = report.match(/(?<=\s)[0-9.]+(?=%)/g);
if (percentages && percentages.length >= 1) {
const coverage = parseFloat(percentages.slice(-1)[0]);
Expand Down
27 changes: 20 additions & 7 deletions src/reports/go.test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
import assert from 'assert/strict';
import { mkdir, writeFile, copyFile, rm } from 'fs/promises';
import { join } from 'path';
import { getReports } from './go.js';

describe('reports/go', () => {
describe('#getReports()', () => {
it('should return tests and coverage reports', async () => {
const reports = await getReports(join(process.cwd(), 'test/data/go'));
assert.equal(reports.length, 2);
assert.deepEqual(reports, [
{ type: 'tests', data: { passed: 3, failed: 0, tests: 3 } },
{ type: 'coverage', data: { coverage: 96.5 } }
]);
before(async () => {
await mkdir('test/data/go/project');
await writeFile('test/data/go/project/go.mod', '');
});
const coverage = { type: 'coverage', data: { coverage: 96.5 } };
const tests = [
{ report: 'test.out', expected: [{ type: 'tests', data: { tests: 3, passed: 3, failed: 0, skipped: 0 } }, coverage] },
{ report: 'failed-test.out', expected: [{ type: 'tests', data: { tests: 6, passed: 3, failed: 2, skipped: 1 } }, coverage] }
];
for (const { report, expected } of tests) {
it(`should return tests and coverage reports for file ${report}`, async () => {
await copyFile(join('test/data/go', report), 'test/data/go/project/test.out');
const reports = await getReports(join(process.cwd(), 'test/data/go/project'));
assert.equal(reports.length, 2);
assert.deepEqual(reports, expected);
});
}
after(async () => {
await rm('test/data/go/project', { recursive: true });
});
});
});
7 changes: 5 additions & 2 deletions src/reports/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import * as cobertura from './cobertura.js';
import * as jacoco from './jacoco.js';

/**
* @typedef {{ type: string, format?: string, data: any }} Report A loaded report
* @typedef {{ passed: number, failed: number, skipped: number }} TestReportData Test report data
* @typedef {{ coverage: number }} CoverageReportData Coverage report data
* @typedef {TestReportData|CoverageReportData} ReportData Report data
* @typedef {{ type: string, format?: string, data: ReportData }} Report A loaded report
* @typedef {(root: string) => Promise<Report[]>} ReportsLoader A reports loader
*/

/**
* Available report loaders
* @type {{[key: string]: {getReports: ReportsLoader}}}
* @type {{ [key: string]: { getReports: ReportsLoader } }}
*/
const loaders = { go, junit, cobertura, jacoco };

Expand Down
7 changes: 4 additions & 3 deletions src/reports/junit.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export async function getReports(root) {
join(root, '**/*junit*.xml')
];
const reports = await globNearest(patterns);
const data = { passed: 0, failed: 0, tests: 0 };
const data = { tests: 0, passed: 0, failed: 0, skipped: 0 };
let count = 0;
for (const r of reports) {
core.info(`Load JUnit report '${r}'`);
Expand All @@ -27,13 +27,14 @@ export async function getReports(root) {
continue; // Invalid report file, trying the next one
}
for (const ts of testSuites) {
data.tests += parseInt(ts.match(/tests="([0-9]+)"/)?.[1] ?? '0');
data.failed += parseInt(ts.match(/failures="([0-9]+)"/)?.[1] ?? '0');
data.failed += parseInt(ts.match(/errors="([0-9]+)"/)?.[1] ?? '0');
data.tests += parseInt(ts.match(/tests="([0-9]+)"/)?.[1] ?? '0');
data.skipped += parseInt(ts.match(/skipped="([0-9]+)"/)?.[1] ?? '0');
}
count++;
}
data.passed = data.tests - data.failed;
data.passed = data.tests - (data.failed + data.skipped);
core.info(`Loaded ${count} JUnit report(s)`);
return [{ type: 'tests', data }];
}
Expand Down
2 changes: 1 addition & 1 deletion src/reports/junit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('reports/junit', () => {
const reports = await getReports(join(process.cwd(), 'test/data/junit'));
assert.equal(reports.length, 1);
assert.deepEqual(reports, [
{ type: 'tests', data: { passed: 8, failed: 4, tests: 12 } }
{ type: 'tests', data: { tests: 12, passed: 7, failed: 4, skipped: 1 } }
]);
});
});
Expand Down
15 changes: 15 additions & 0 deletions test/data/go/failed-test.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
=== RUN Test
=== RUN Test/example/1
=== RUN Test/example/2
=== RUN Test/example/3
=== RUN Test/example/4
=== RUN Test/example/5
--- FAIL: Test (0.01s)
--- PASS: Test/example/1 (0.00s)
--- PASS: Test/example/2 (0.01s)
--- SKIP: Test/example/3 (0.00s)
--- FAIL: Test/example/4 (0.01s)
--- PASS: Test/example/5 (0.01s)
FAIL
coverage: 96.5% of statements in ./...
ok github.com/example/test 0.111s coverage: 96.5% of statements in ./...
10 changes: 5 additions & 5 deletions test/data/junit/test-results-1.xml
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuites name="Suites" time="2.563" tests="10" failures="3" errors="1">
<testsuite name="Suite 0" tests="0" time="0.000" failures="0" errors="0">
<testsuites name="Suites">
<testsuite name="Suite 0" tests="0" failures="0" errors="0" skipped="0" time="0.000">
</testsuite>
<testsuite name="Suite 1" tests="2" time="0.502" failures="0" errors="0">
<testsuite name="Suite 1" tests="2" failures="0" errors="0" skipped="0" time="0.502">
<testcase name="Case 1.1" time="0.284"></testcase>
<testcase name="Case 1.2" time="0.217"></testcase>
</testsuite>
<testsuite name="Suite 2" tests="5" time="1.297" failures="2" errors="1">
<testsuite name="Suite 2" tests="5" failures="2" errors="1" skipped="1" time="1.297">
<testcase name="Case 2.1" time="0.285"></testcase>
<testcase name="Case 2.2" time="0.271"></testcase>
<testcase name="Case 2.3" time="0.224"></testcase>
<testcase name="Case 2.4" time="0.297"></testcase>
<testcase name="Case 2.5" time="0.219"></testcase>
</testsuite>
<testsuite name="Suite 3" tests="3" time="0.763" failures="1" errors="0">
<testsuite name="Suite 3" tests="3" failures="1" errors="0" skipped="0" time="0.763">
<testcase name="Case 3.1" time="0.264"></testcase>
<testcase name="Case 3.2" time="0.261"></testcase>
<testcase name="Case 3.3" time="0.237"></testcase>
Expand Down
6 changes: 3 additions & 3 deletions test/data/junit/test-results-2.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuites name="More suites" time="0.398" tests="2" failures="0" errors="0">
<testsuite name="Suite 4" tests="2" time="0.397" failures="0" errors="0">
<testsuite name="Nested suite 4.1" tests="2" time="0.397" failures="0" errors="0">
<testsuites name="More suites" tests="2" failures="0" errors="0" skipped="0" time="0.398">
<testsuite name="Suite 4" tests="2" failures="0" errors="0" skipped="0" time="0.397">
<testsuite name="Nested suite 4.1" tests="2" failures="0" errors="0" skipped="0" time="0.397">
<testcase name="Case 4.1.1" time="0.192"></testcase>
<testcase name="Case 4.1.2" time="0.204"></testcase>
</testsuite>
Expand Down

0 comments on commit fc45199

Please sign in to comment.