-
-
Notifications
You must be signed in to change notification settings - Fork 177
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
mrdannael
wants to merge
10
commits into
webpro-nl:main
Choose a base branch
from
mrdannael:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add JUnit reporter #586
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
23dc510
feat: add junit reporter
mrdannael 979f0d0
Merge branch 'webpro:main' into main
mrdannael 648f9a6
use fast-xml-parser for building xml file, rewrite reporter logic
mrdannael 73cbad3
Merge branch 'webpro:main' into main
mrdannael d0b7c00
improve and format code
mrdannael 7637a06
update docs with junit reporter entry
mrdannael c1c7b99
Merge remote-tracking branch 'upstream/main'
mrdannael bc2760d
Merge remote-tracking branch 'upstream/main'
mrdannael 51ef8f2
Update fast-xml-parser to the latest version
mrdannael 4c5f232
Merge remote-tracking branch 'upstream/main'
mrdannael File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
</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()); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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)?
There was a problem hiding this comment.
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.