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(Junit Report): Adding option for Junit report format #395

Open
wants to merge 2 commits into
base: main
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ $ linkinator LOCATIONS [ --arguments ]
Defaults to 'false'.

--format, -f
Return the data in CSV or JSON format.
Return the data in CSV, JSON or JUNIT format.

--help
Show this command.
Expand Down Expand Up @@ -132,7 +132,7 @@ The `--skip` parameter will accept any regex! You can do more complex matching,
linkinator http://jbeckwith.com --skip '^(?!http://jbeckwith.com)'
```

Maybe you're going to pipe the output to another program. Use the `--format` option to get JSON or CSV!
Maybe you're going to pipe the output to another program. Use the `--format` option to get JSON, JUnit Report Format or CSV!

```sh
linkinator ./docs --format CSV
Expand Down
86 changes: 85 additions & 1 deletion package-lock.json

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

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"@types/server-destroy": "^1.0.1",
"@types/sinon": "^10.0.6",
"@types/update-notifier": "^5.1.0",
"@types/xml2js": "^0.4.11",
"c8": "^7.10.0",
"chai": "^4.3.4",
"codecov": "^3.8.2",
Expand All @@ -58,7 +59,9 @@
"semantic-release": "^19.0.0",
"sinon": "^13.0.0",
"strip-ansi": "^7.0.1",
"typescript": "^4.4.4"
"typescript": "^4.4.4",
"validate-with-xmllint": "^1.2.0",
"xml2js": "^0.4.23"
},
"engines": {
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
Expand Down
8 changes: 7 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import fs from 'fs';
import {URL} from 'url';
import {Flags, getConfig} from './config.js';
import {Format, Logger, LogLevel} from './logger.js';
import {createReport} from './junit-report.js';
import {
LinkChecker,
LinkState,
Expand Down Expand Up @@ -46,7 +47,7 @@ const cli = meow(
Defaults to 'false'.

--format, -f
Return the data in CSV or JSON format.
Return the data in CSV, JSON or JUNIT format.

--help
Show this command.
Expand Down Expand Up @@ -213,6 +214,11 @@ async function main() {
result.links = filteredResults;
console.log(JSON.stringify(result, null, 2));
return;
} else if (format === Format.JUNIT) {
result.links = filteredResults;
const junitReport = createReport(result);
console.log(junitReport);
return;
} else if (format === Format.CSV) {
result.links = filteredResults;
const csv = await jsonexport(result.links);
Expand Down
47 changes: 47 additions & 0 deletions src/junit-report.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {LinkResult, LinkState} from './index.js';

type Result = {
links: LinkResult[];
passed: boolean;
};
export function createReport(result: Result): string {
const id = Math.floor(Date.now() / 1000);
const failureCount = result.links
? result.links.filter(link => link.state === LinkState.BROKEN).length
: 0;
const skippedCount = result.links
? result.links.filter(link => link.state === LinkState.SKIPPED).length
: 0;
const allCount = result.links ? result.links.length : 0;
let testCases = '';
result.links.forEach(link => {
switch (link.state) {
case LinkState.BROKEN:
testCases += `<testcase classname="Linkinator" name="Link ${
link.url
} on ${link.parent} is not correct (${link.status})." time="0" >
<failure message="${
link.failureDetails
? JSON.stringify(link.failureDetails)
: ''
}"></failure>
</testcase>`;
break;
case LinkState.SKIPPED:
testCases += `<testcase classname="Linkinator" name="Link ${link.url} on ${link.parent} is skipped." time="0" >
<skipped />
</testcase>`;
break;
default:
testCases += `<testcase classname="Linkinator" name="Link ${link.url} on ${link.parent} is ok." time="0" />`;
break;
}
testCases += '\n';
});
return `<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite id="${id}" name="Linkinator" failures="0" skipped="${skippedCount}" tests="${allCount}" errors="${failureCount}">
${testCases}
</testsuite>
</testsuites>`;
}
1 change: 1 addition & 0 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export enum LogLevel {
export enum Format {
TEXT,
JSON,
JUNIT,
CSV,
}

Expand Down
80 changes: 80 additions & 0 deletions test/test.junit-report.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import assert from 'assert';
import {describe, it} from 'mocha';
import {validateXML} from 'validate-with-xmllint';
import xml2js from 'xml2js';
import {LinkState} from '../src/index.js';
import {createReport} from '../src/junit-report.js';

describe('junit report', () => {
const parser = new xml2js.Parser();

it('should work with no reults', async () => {
const report = createReport({links: [], passed: true});
await validateXML(report);
const result = await parser.parseStringPromise(report);
assert(report !== null);
assert(result.testsuites !== null);
assert(result.testsuites.testsuite !== null);
const testSuite = result.testsuites.testsuite[0];
assert(testSuite !== null);
assert(testSuite.id !== null);
assert(testSuite.name !== null);
assert.equal(testSuite.$.failures, '0');
assert.equal(testSuite.$.skipped, '0');
assert.equal(testSuite.$.tests, '0');
assert.equal(testSuite.$.errors, '0');
});

it('should work with reults', async () => {
const report = createReport({
links: [
{
state: LinkState.OK,
url: 'a',
parent: 'b',
status: 200,
},
{
state: LinkState.BROKEN,
url: 'c',
failureDetails: [{}],
parent: 'd',
status: 404,
},
{
state: LinkState.SKIPPED,
url: 'e',
parent: 'f',
status: 0,
},
],
passed: false,
});
await validateXML(report);
const result = await parser.parseStringPromise(report);
assert(report !== null);
assert(result.testsuites !== null);
assert(result.testsuites.testsuite !== null);
const testSuite = result.testsuites.testsuite[0];
assert(testSuite !== null);
assert(testSuite.id !== null);
assert(testSuite.name !== null);
assert.equal(testSuite.$.failures, '0');
assert.equal(testSuite.$.skipped, '1');
assert.equal(testSuite.$.tests, '3');
assert.equal(testSuite.$.errors, '1');
assert.equal(testSuite.testcase[0].$.classname, 'Linkinator');
assert.equal(testSuite.testcase[0].$.name, 'Link a on b is ok.');
assert.equal(testSuite.testcase[0].$.time, '0');
assert.equal(testSuite.testcase[1].$.classname, 'Linkinator');
assert.equal(
testSuite.testcase[1].$.name,
'Link c on d is not correct (404).'
);
assert(testSuite.testcase[1].failure !== null);
assert(testSuite.testcase[1].failure[0].$.message !== null);
assert.equal(testSuite.testcase[2].$.classname, 'Linkinator');
assert.equal(testSuite.testcase[2].$.name, 'Link e on f is skipped.');
assert(testSuite.testcase[2].skipped !== null);
});
});