From 7964d8e7c8ec2ed7f1bcb19f3efe33f83ab7970a Mon Sep 17 00:00:00 2001 From: Mike Penz Date: Fri, 20 Oct 2023 10:02:39 +0000 Subject: [PATCH 1/2] - gracefully handle corrupt test export files - RELATES TO https://github.com/mikepenz/action-junit-report/issues/952 --- __tests__/testParser.test.ts | 28 +++++++++++++++++++ src/testParser.ts | 14 +++++++++- .../TEST-test.CorruptTest.xml | 18 ++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 test_results/corrupt-junit/e2e-tests/corrupt/target/surefire-reports/TEST-test.CorruptTest.xml diff --git a/__tests__/testParser.test.ts b/__tests__/testParser.test.ts index d234f130..7ea96305 100644 --- a/__tests__/testParser.test.ts +++ b/__tests__/testParser.test.ts @@ -1085,4 +1085,32 @@ action.surefire.report.email.InvalidEmailAddressException: Invalid email address } ]) }) + + it('parse corrupt test output', async () => { + const result = await parseTestReports( + '', + '', + 'test_results/corrupt-junit/**/target/surefire-reports/TEST-*.xml', + '', + false, + false, + [], + '', + '', + undefined, + false, + undefined + ) + + expect(result).toStrictEqual({ + checkName: "", + summary: "", + totalCount: 0, + skipped: 0, + failed: 0, + passed: 0, + annotations: [ + ], + }) + }) }) diff --git a/src/testParser.ts b/src/testParser.ts index a6cbea73..0e915390 100644 --- a/src/testParser.ts +++ b/src/testParser.ts @@ -151,7 +151,19 @@ export async function parseFile( core.debug(`Parsing file ${file}`) const data: string = fs.readFileSync(file, 'utf8') - const report = JSON.parse(parser.xml2json(data, {compact: true})) + + // eslint-disable-next-line @typescript-eslint/no-explicit-any + let report: any + try { + report = JSON.parse(parser.xml2json(data, {compact: true})) + } catch (error) { + core.error(`⚠️ Failed to parse file (${file}) with error ${error}`) + return { + totalCount: 0, + skipped: 0, + annotations: [] + } + } return parseSuite( report, diff --git a/test_results/corrupt-junit/e2e-tests/corrupt/target/surefire-reports/TEST-test.CorruptTest.xml b/test_results/corrupt-junit/e2e-tests/corrupt/target/surefire-reports/TEST-test.CorruptTest.xml new file mode 100644 index 00000000..038eb55e --- /dev/null +++ b/test_results/corrupt-junit/e2e-tests/corrupt/target/surefire-reports/TEST-test.CorruptTest.xml @@ -0,0 +1,18 @@ + + + + + + + + + + 26 + at corrupt.core.Main$Companion.initAndRun(Main.kt:244) + at corrupt.core.Main.initAndRun(Main.kt) + at java.util.concurrent.FutureTask.run(FutureTask.java:266) + at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) + at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) + at java.lang.Thread.run(Thread.java:750) +]]> + Date: Fri, 20 Oct 2023 10:05:10 +0000 Subject: [PATCH 2/2] - change corrupt test file folder --- __tests__/testParser.test.ts | 2 +- .../{surefire-reports => sf-reports}/TEST-test.CorruptTest.xml | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename test_results/corrupt-junit/e2e-tests/corrupt/target/{surefire-reports => sf-reports}/TEST-test.CorruptTest.xml (100%) diff --git a/__tests__/testParser.test.ts b/__tests__/testParser.test.ts index 7ea96305..0465c36e 100644 --- a/__tests__/testParser.test.ts +++ b/__tests__/testParser.test.ts @@ -1090,7 +1090,7 @@ action.surefire.report.email.InvalidEmailAddressException: Invalid email address const result = await parseTestReports( '', '', - 'test_results/corrupt-junit/**/target/surefire-reports/TEST-*.xml', + 'test_results/corrupt-junit/**/target/sf-reports/TEST-*.xml', '', false, false, diff --git a/test_results/corrupt-junit/e2e-tests/corrupt/target/surefire-reports/TEST-test.CorruptTest.xml b/test_results/corrupt-junit/e2e-tests/corrupt/target/sf-reports/TEST-test.CorruptTest.xml similarity index 100% rename from test_results/corrupt-junit/e2e-tests/corrupt/target/surefire-reports/TEST-test.CorruptTest.xml rename to test_results/corrupt-junit/e2e-tests/corrupt/target/sf-reports/TEST-test.CorruptTest.xml