From a91086638b3ac2440415e0765e25cae26a5dce4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Audren?= Date: Fri, 25 Nov 2022 13:04:24 +0900 Subject: [PATCH] Gracefully handle empty failure tags This commit fixes #137. Some JUnit generators emit an empty failure tag, with only a message property set. In those cases, the parser crashes when trying to match the failure with a source file. Since this feature is optional, the simplest fix is to skip the processing when the failure tag is empty. Also added a test, and the corresponding input file is generated from a reporter within our codebase. --- .../fixtures/external/java/empty_failures.xml | 2 ++ __tests__/java-junit.test.ts | 18 ++++++++++++++++++ src/parsers/java-junit/java-junit-parser.ts | 11 +++++++---- 3 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 __tests__/fixtures/external/java/empty_failures.xml diff --git a/__tests__/fixtures/external/java/empty_failures.xml b/__tests__/fixtures/external/java/empty_failures.xml new file mode 100644 index 0000000..c76ed0e --- /dev/null +++ b/__tests__/fixtures/external/java/empty_failures.xml @@ -0,0 +1,2 @@ + + diff --git a/__tests__/java-junit.test.ts b/__tests__/java-junit.test.ts index a2ee944..e8111d4 100644 --- a/__tests__/java-junit.test.ts +++ b/__tests__/java-junit.test.ts @@ -72,4 +72,22 @@ describe('java-junit tests', () => { fs.mkdirSync(path.dirname(outputPath), {recursive: true}) fs.writeFileSync(outputPath, report) }) + + it('parses empty failures in test results', async () => { + const fixturePath = path.join(__dirname, 'fixtures', 'external', 'java', 'empty_failures.xml') + const filePath = normalizeFilePath(path.relative(__dirname, fixturePath)) + const fileContent = fs.readFileSync(fixturePath, {encoding: 'utf8'}) + + const trackedFiles: string[] = [] + const opts: ParseOptions = { + parseErrors: true, + trackedFiles + } + + const parser = new JavaJunitParser(opts) + const result = await parser.parse(filePath, fileContent) + + expect(result.result === 'failed') + expect(result.failed === 1) + }) }) diff --git a/src/parsers/java-junit/java-junit-parser.ts b/src/parsers/java-junit/java-junit-parser.ts index a4df6a3..8573873 100644 --- a/src/parsers/java-junit/java-junit-parser.ts +++ b/src/parsers/java-junit/java-junit-parser.ts @@ -128,10 +128,13 @@ export class JavaJunitParser implements TestParser { let filePath let line - const src = this.exceptionThrowSource(details) - if (src) { - filePath = src.filePath - line = src.line + if(details != null) + { + const src = this.exceptionThrowSource(details) + if (src) { + filePath = src.filePath + line = src.line + } } return {