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 1/2] 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 00000000..c76ed0e9 --- /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 a2ee9449..e8111d41 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 a4df6a38..8573873a 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 { From 86d6ec5dd5105dbef239143f548baed8556e7468 Mon Sep 17 00:00:00 2001 From: Michal Dorner Date: Tue, 29 Nov 2022 08:55:49 +0100 Subject: [PATCH 2/2] Update dist/index.js --- dist/index.js | 10 ++++++---- src/parsers/java-junit/java-junit-parser.ts | 3 +-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/dist/index.js b/dist/index.js index f9222d3b..21c0fe8b 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1037,10 +1037,12 @@ class JavaJunitParser { const details = typeof failure === 'object' ? failure._ : failure; 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 { path: filePath, diff --git a/src/parsers/java-junit/java-junit-parser.ts b/src/parsers/java-junit/java-junit-parser.ts index 8573873a..42b7b08b 100644 --- a/src/parsers/java-junit/java-junit-parser.ts +++ b/src/parsers/java-junit/java-junit-parser.ts @@ -128,8 +128,7 @@ export class JavaJunitParser implements TestParser { let filePath let line - if(details != null) - { + if (details != null) { const src = this.exceptionThrowSource(details) if (src) { filePath = src.filePath