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

Add JUnit reporter #586

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Binary file modified bun.lockb
Binary file not shown.
38 changes: 38 additions & 0 deletions packages/docs/src/content/docs/features/reporters.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Knip provides the following built-in reporters:
- `codeowners`
- `compact`
- `json`
- `junit`
- `markdown`
- `symbol` (default)

Expand Down Expand Up @@ -76,6 +77,43 @@ The keys match the [reported issue types][1]. Example usage:
knip --reporter json
```

### JUnit

The built-in `junit` reporter output is meant to be saved to an XML file. This
can be achieved by providing the `path` value using `--reporter-options`. It
reports issues in JUnit XML format, which should be readable by CI tools such as
Bitbucket, Jenkins, etc. For example:

```xml
<?xml version="1.0" encoding="UTF-8"?>
<testsuites name="Knip report" tests="4" failures="4">
<testsuite name="Unused files" tests="1" failures="1">
<testcase tests="1" failures="1" name="Unused files" classname="src/unused.ts">
<failure message="Unused files" type="Unused files">Unused files: src/unused.ts</failure>
</testcase>
</testsuite>
<testsuite name="Unlisted dependencies" tests="2" failures="2">
<testcase tests="1" failures="1" name="Unlisted dependencies" classname="src/index.ts">
<failure message="Unlisted dependencies - unresolved" type="Unlisted dependencies">Unlisted dependencies: "unresolved" inside src/index.ts</failure>
</testcase>
<testcase tests="1" failures="1" name="Unlisted dependencies" classname="src/index.ts">
<failure message="Unlisted dependencies - @org/unresolved" type="Unlisted dependencies">Unlisted dependencies: "@org/unresolved" inside src/index.ts</failure>
</testcase>
</testsuite>
<testsuite name="Unresolved imports" tests="1" failures="1">
<testcase tests="1" failures="1" name="Unresolved imports" classname="src/index.ts:8:23">
<failure message="Unresolved imports - ./unresolved" type="Unresolved imports">Unresolved imports: "./unresolved" inside src/index.ts:8:23</failure>
</testcase>
</testsuite>
</testsuites>
```

Example usage:

```sh
knip --reporter json --reporter-options '{"path": "/test-results/knip.xml"}'
```

### Markdown

The built-in `markdown` reporter output is meant to be saved to a Markdown file.
Expand Down
1 change: 1 addition & 0 deletions packages/knip/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"easy-table": "1.2.0",
"enhanced-resolve": "^5.17.1",
"fast-glob": "^3.3.2",
"fast-xml-parser": "^4.5.0",
"jiti": "^2.3.3",
"js-yaml": "^4.1.0",
"minimist": "^1.2.8",
Expand Down
2 changes: 2 additions & 0 deletions packages/knip/src/reporters/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import codeowners from './codeowners.js';
import compact from './compact.js';
import json from './json.js';
import junit from './junit.js';
import markdown from './markdown.js';
import symbols from './symbols.js';

Expand All @@ -10,4 +11,5 @@ export default {
codeowners,
json,
markdown,
junit,
};
154 changes: 154 additions & 0 deletions packages/knip/src/reporters/junit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import fs from 'node:fs';
import { XMLBuilder } from 'fast-xml-parser';
import { resolve, dirname, toRelative } from '../util/path.js';
import { getTitle } from './util.js';
import type { ReporterOptions, IssueSet, IssueRecords, Issue } from '../types/issues.js';
import type { Entries } from 'type-fest';

interface IssuesEntry {
entry: string;
issues: Issue[];
}

type ExtraReporterOptions = {
path?: string;
};

type Failure = {
'@_message': string;
'@_type': string;
'#text': string;
};

type TestCase = {
'@_tests': number;
'@_failures': number;
'@_name': string;
'@_classname': string;
failure: Failure;
};

type TestSuite = {
'@_name': string;
'@_tests': number;
'@_failures': number;
testcase: TestCase[];
};

export default ({ report, issues, counters, options }: ReporterOptions) => {
let totalIssues = 0;
let testSuite: TestSuite[] = [];

for (const [reportType, isReportType] of Object.entries(report) as Entries<typeof report>) {
if (isReportType) {
const title = getTitle(reportType);
const count = counters[reportType];
const isSet = issues[reportType] instanceof Set;
const issuesForType = isSet
? Array.from(issues[reportType] as IssueSet)
: Object.entries(issues[reportType] as IssueRecords).map(([entry, errors]) => {
const items = Object.values(errors);
const issues = items.flatMap(item => (item.symbols ? item.symbols : { ...item }));
return { entry, issues };
});

if (issuesForType.length > 0) {
let testCase: TestCase[] = [];
if (isSet) {
const setTestCases = (issuesForType as string[]).map(issue => ({
'@_tests': 1,
'@_failures': 1,
'@_name': title,
'@_classname': toRelative(issue),
failure: {
'@_message': title,
'@_type': title,
'#text': `${title}: ${toRelative(issue)}`,
},
}));
testCase.push(...setTestCases);
} else {
const entriesTestCases = (issuesForType as IssuesEntry[]).flatMap(typeIssues =>
typeIssues.issues.map(issue => {
let entry = typeIssues.entry;
if ('line' in issue && 'col' in issue) {
entry = `${typeIssues.entry}:${issue.line}:${issue.col}`;
}
return {
'@_tests': 1,
'@_failures': 1,
'@_name': title,
'@_classname': entry,
failure: {
'@_message': `${title} - ${issue.symbol}`,
'@_type': title,
'#text': `${title}: "${issue.symbol}" inside ${entry}`,
},
};
})
);
testCase.push(...entriesTestCases);
}

if (testCase.length > 0) {
testSuite.push({
'@_name': title,
'@_tests': count,
'@_failures': count,
testcase: testCase,
});

totalIssues += count;
}
}
}
}

if (totalIssues > 0) {
let opts: ExtraReporterOptions = {};

try {
opts = options ? JSON.parse(options) : opts;
} catch (err) {
console.error('Error occured while parsing options:', err);
}

const xml = {
'?xml': {
'@_version': '1.0',
'@_encoding': 'UTF-8',
},
testsuites: {
'@_name': 'Knip report',
'@_tests': totalIssues,
'@_failures': totalIssues,
testsuite: testSuite,
},
};

const builder = new XMLBuilder({
attributeNamePrefix: '@_',
format: true,
processEntities: false,
ignoreAttributes: false,
});
const outputXML = builder.build(xml);

if (opts.path) {
const dir = dirname(opts.path);

if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}

try {
fs.writeFileSync(resolve(opts.path), outputXML, 'utf-8');
console.log('Knip results file successfully written.');
} catch (err) {
console.error('Error occured while writing knip results file: ', err);
}
} else {
console.log(outputXML);
}
}
};
36 changes: 36 additions & 0 deletions packages/knip/test/cli-reporter-junit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { resolve } from '../src/util/path.js';
import { execFactory } from './helpers/exec.js';

const cwd = resolve('fixtures/module-resolution-non-std');

const exec = execFactory(cwd);

test('knip --reporter junit', () => {
const xml = `
<?xml version="1.0" encoding="UTF-8"?>
<testsuites name="Knip report" tests="4" failures="4">
<testsuite name="Unused files" tests="1" failures="1">
<testcase tests="1" failures="1" name="Unused files" classname="src/unused.ts">
<failure message="Unused files" type="Unused files">Unused files: src/unused.ts</failure>
</testcase>
</testsuite>
<testsuite name="Unlisted dependencies" tests="2" failures="2">
<testcase tests="1" failures="1" name="Unlisted dependencies" classname="src/index.ts">
<failure message="Unlisted dependencies - unresolved" type="Unlisted dependencies">Unlisted dependencies: "unresolved" inside src/index.ts</failure>
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't know anything about this format, but will each issue be in its own node, or should it be? This example gives me the impression that all unlisted dependencies will end up in this single node? Or will each one have the "Unlisted dependencies: " prefix?

Maybe you could show an example of how it looks like in the CI environment (eg. Bitbucket)?

Copy link
Author

@mrdannael mrdannael Apr 21, 2024

Choose a reason for hiding this comment

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

Sure thing. I've already used this junit reporter during development and tested it with configured Bitbucket. I'll gather some real-life data and post it here for further discussion.

</testcase>
<testcase tests="1" failures="1" name="Unlisted dependencies" classname="src/index.ts">
<failure message="Unlisted dependencies - @org/unresolved" type="Unlisted dependencies">Unlisted dependencies: "@org/unresolved" inside src/index.ts</failure>
</testcase>
</testsuite>
<testsuite name="Unresolved imports" tests="1" failures="1">
<testcase tests="1" failures="1" name="Unresolved imports" classname="src/index.ts:8:23">
<failure message="Unresolved imports - ./unresolved" type="Unresolved imports">Unresolved imports: "./unresolved" inside src/index.ts:8:23</failure>
</testcase>
</testsuite>
</testsuites>`;

const out = exec('knip --reporter junit').stdout;
assert.equal(out.trim(), xml.trim());
});