Skip to content

Commit

Permalink
- ability to configure exclude sub paths from source lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
mikepenz committed Dec 22, 2021
1 parent 2821502 commit 1b7a260
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 26 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ jobs:
| **Input** | **Description** |
|----------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `report_paths` | **Required**. [Glob](https://github.com/actions/toolkit/tree/master/packages/glob) expression to junit report paths. The default is `**/junit-reports/TEST-*.xml`. |
| `token` | Optional. GitHub token for creating a check run. Set to `${{ github.token }}` by default. |
| `check_name` | Optional. Check name to use when creating a check run. The default is `JUnit Test Report`. |
| `token` | Optional. GitHub token for creating a check run. Set to `${{ github.token }}` by default. |
| `exclude_sources` | Optional. Provide `,` seperated array of folders to ignore for source lookup. Defaults to: `/build/,/__pycache__/` |
| `check_name` | Optional. Check name to use when creating a check run. The default is `JUnit Test Report`. |
| `suite_regex` | Optional. Regular expression for the named test suites. E.g. `Test*` |
| `commit` | Optional. The commit SHA to update the status. This is useful when you run it with `workflow_run`. |
| `fail_on_failure` | Optional. Fail the build in case of a test failure. |
Expand Down
8 changes: 4 additions & 4 deletions __tests__/testParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,19 +124,19 @@ note: run with `RUST_BACKTRACE=1` environment variable to display

describe('resolvePath', () => {
it('should find correct file for Java fileName', async () => {
const path = await resolvePath('EmailAddressTest');
const path = await resolvePath('EmailAddressTest', ['/build/', '/__pycache__/']);
expect(path).toBe(
'test_results/tests/email/src/test/java/action/surefire/report/email/EmailAddressTest.java'
);
});

it('should find correct file for Kotlin fileName', async () => {
const path = await resolvePath('CalcUtilsTest');
const path = await resolvePath('CalcUtilsTest', ['/build/', '/__pycache__/']);
expect(path).toBe('test_results/tests/utils/src/test/java/action/surefire/report/calc/CalcUtilsTest.kt');
});

it('should find correct file with a relative path', async () => {
const path = await resolvePath('./test_results/CalcUtilsTest.kt');
const path = await resolvePath('./test_results/CalcUtilsTest.kt', ['/build/', '/__pycache__/']);
expect(path).toBe('test_results/CalcUtilsTest.kt');
})
});
Expand Down Expand Up @@ -426,7 +426,7 @@ action.surefire.report.email.InvalidEmailAddressException: Invalid email address
});

it('parse mocha test case, custom title template', async () => {
const { count, skipped, annotations } = await parseFile('test_results/mocha/mocha.xml', '*', true, '${{TEST_NAME}}');
const { count, skipped, annotations } = await parseFile('test_results/mocha/mocha.xml', '*', true, ['/build/', '/__pycache__/'], '${{TEST_NAME}}');

expect(count).toBe(1);
expect(skipped).toBe(0);
Expand Down
6 changes: 5 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ inputs:
description: 'Deprecated syntax to specify github token.'
required: true
report_paths:
description: 'JUnit xml report paths in glob format'
description: 'Xml report paths in glob format'
required: false
default: '**/junit-reports/TEST-*.xml'
exclude_sources:
description: 'Comma seperated list of source folders to ignore for lookup'
required: false
default: '/build/,/__pycache__/'
suite_regex:
description: 'Regular expression for the named test suites'
required: false
Expand Down
30 changes: 17 additions & 13 deletions dist/index.js

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

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ export async function run(): Promise<void> {
const failOnFailure = core.getInput('fail_on_failure') === 'true'
const requireTests = core.getInput('require_tests') === 'true'
const includePassed = core.getInput('include_passed') === 'true'
const excludeSources = core.getInput('exclude_sources')
? core.getInput('exclude_sources').split(',')
: []

core.endGroup()
core.startGroup(`📦 Process test results`)
Expand All @@ -34,6 +37,7 @@ export async function run(): Promise<void> {
reportPaths,
suiteRegex,
includePassed,
excludeSources,
checkTitleTemplate
)
const foundResults = testResult.count > 0 || testResult.skipped > 0
Expand Down
32 changes: 27 additions & 5 deletions src/testParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ function safeParseInt(line: string | null): number | null {
* Modification Copyright 2021 Mike Penz
* https://github.com/mikepenz/action-junit-report/
*/
export async function resolvePath(fileName: string): Promise<string> {
export async function resolvePath(
fileName: string,
excludeSources: string[]
): Promise<string> {
core.debug(`Resolving path for ${fileName}`)
const normalizedFilename = fileName.replace(/^\.\//, '') // strip relative prefix (./)
const globber = await glob.create(`**/${normalizedFilename}.*`, {
Expand All @@ -106,7 +109,9 @@ export async function resolvePath(fileName: string): Promise<string> {
const searchPath = globber.getSearchPaths() ? globber.getSearchPaths()[0] : ''
for await (const result of globber.globGenerator()) {
core.debug(`Matched file: ${result}`)
if (!result.includes('/build/')) {

const found = excludeSources.find(v => result.includes(v))
if (!found) {
const path = result.slice(searchPath.length + 1)
core.debug(`Resolved path: ${path}`)
return path
Expand All @@ -126,14 +131,22 @@ export async function parseFile(
file: string,
suiteRegex = '',
includePassed = false,
excludeSources: string[] = ['/build/', '/__pycache__/'],
checkTitleTemplate: string | undefined = undefined
): Promise<TestResult> {
core.debug(`Parsing file ${file}`)

const data: string = fs.readFileSync(file, 'utf8')
const report = JSON.parse(parser.xml2json(data, {compact: true}))

return parseSuite(report, '', suiteRegex, includePassed, checkTitleTemplate)
return parseSuite(
report,
'',
suiteRegex,
includePassed,
excludeSources,
checkTitleTemplate
)
}

async function parseSuite(
Expand All @@ -142,6 +155,7 @@ async function parseSuite(
parentName: string,
suiteRegex: string,
includePassed = false,
excludeSources: string[],
checkTitleTemplate: string | undefined = undefined
): Promise<TestResult> {
let count = 0
Expand Down Expand Up @@ -182,6 +196,7 @@ async function parseSuite(
suiteName,
suiteRegex,
includePassed,
excludeSources,
checkTitleTemplate
)
count += res.count
Expand Down Expand Up @@ -236,7 +251,7 @@ async function parseSuite(
stackTrace
)

let resolvedPath = await resolvePath(pos.fileName)
let resolvedPath = await resolvePath(pos.fileName, excludeSources)

core.debug(`Path prior to stripping: ${resolvedPath}`)

Expand Down Expand Up @@ -296,6 +311,7 @@ export async function parseTestReports(
reportPaths: string,
suiteRegex: string,
includePassed = false,
excludeSources: string[],
checkTitleTemplate: string | undefined = undefined
): Promise<TestResult> {
const globber = await glob.create(reportPaths, {followSymbolicLinks: false})
Expand All @@ -307,7 +323,13 @@ export async function parseTestReports(
count: c,
skipped: s,
annotations: a
} = await parseFile(file, suiteRegex, includePassed, checkTitleTemplate)
} = await parseFile(
file,
suiteRegex,
includePassed,
excludeSources,
checkTitleTemplate
)
if (c === 0) continue
count += c
skipped += s
Expand Down

0 comments on commit 1b7a260

Please sign in to comment.