From 1c044b4aefb96c58f802d2555c2b52c701a64696 Mon Sep 17 00:00:00 2001 From: Jozef Izso Date: Wed, 8 Nov 2023 15:59:52 +0100 Subject: [PATCH] Add `SwiftXunitParser` class based on `JavaJunitParser` for `swift-xunit` reporter --- README.md | 7 +++ __tests__/__outputs__/swift-xunit.md | 13 ++++++ .../__snapshots__/swift-xunit.test.ts.snap | 44 +++++++++++++++++++ __tests__/fixtures/swift-xunit.xml | 12 +++++ __tests__/swift-xunit.test.ts | 34 ++++++++++++++ action.yml | 1 + src/main.ts | 3 ++ src/parsers/swift-xunit/swift-xunit-parser.ts | 8 ++++ 8 files changed, 122 insertions(+) create mode 100644 __tests__/__outputs__/swift-xunit.md create mode 100644 __tests__/__snapshots__/swift-xunit.test.ts.snap create mode 100644 __tests__/fixtures/swift-xunit.xml create mode 100644 __tests__/swift-xunit.test.ts create mode 100644 src/parsers/swift-xunit/swift-xunit-parser.ts diff --git a/README.md b/README.md index 0ae7b766..f4be6e05 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ This [Github Action](https://github.com/features/actions) displays test results - Flutter / [test](https://pub.dev/packages/test) - Java / [JUnit](https://junit.org/) - JavaScript / [JEST](https://jestjs.io/) / [Mocha](https://mochajs.org/) +- Swift / xUnit For more information see [Supported formats](#supported-formats) section. @@ -317,6 +318,12 @@ Mocha, unfortunately, doesn't have the option to store `json` output directly to There is a work in progress to fix it: [mocha#4607](https://github.com/mochajs/mocha/pull/4607) +
+ swift-xunit (Experimental) + +Support for Swift test results in xUnit format is experimental - should work but it was not extensively tested. +
+ ## GitHub limitations Unfortunately, there are some known issues and limitations caused by GitHub API: diff --git a/__tests__/__outputs__/swift-xunit.md b/__tests__/__outputs__/swift-xunit.md new file mode 100644 index 00000000..ecafaf9e --- /dev/null +++ b/__tests__/__outputs__/swift-xunit.md @@ -0,0 +1,13 @@ +![Tests failed](https://img.shields.io/badge/tests-2%20passed%2C%201%20failed-critical) +## ❌ fixtures/swift-xunit.xml +**3** tests were completed in **220ms** with **2** passed, **1** failed and **0** skipped. +|Test suite|Passed|Failed|Skipped|Time| +|:---|---:|---:|---:|---:| +|[TestResults](#r0s0)|2✅|1❌||220ms| +### ❌ TestResults +``` +AcmeLibTests.AcmeLibTests + ✅ test_always_pass + ✅ test_always_skip + ❌ test_always_fail +``` \ No newline at end of file diff --git a/__tests__/__snapshots__/swift-xunit.test.ts.snap b/__tests__/__snapshots__/swift-xunit.test.ts.snap new file mode 100644 index 00000000..2a9e81b1 --- /dev/null +++ b/__tests__/__snapshots__/swift-xunit.test.ts.snap @@ -0,0 +1,44 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`swift-xunit tests report from swift test results matches snapshot 1`] = ` +TestRunResult { + "path": "fixtures/swift-xunit.xml", + "suites": Array [ + TestSuiteResult { + "groups": Array [ + TestGroupResult { + "name": "AcmeLibTests.AcmeLibTests", + "tests": Array [ + TestCaseResult { + "error": undefined, + "name": "test_always_pass", + "result": "success", + "time": 36.386333, + }, + TestCaseResult { + "error": undefined, + "name": "test_always_skip", + "result": "success", + "time": 92.039167, + }, + TestCaseResult { + "error": Object { + "details": undefined, + "line": undefined, + "message": undefined, + "path": undefined, + }, + "name": "test_always_fail", + "result": "failed", + "time": 92.05175, + }, + ], + }, + ], + "name": "TestResults", + "totalTime": 220.47725000000003, + }, + ], + "totalTime": undefined, +} +`; diff --git a/__tests__/fixtures/swift-xunit.xml b/__tests__/fixtures/swift-xunit.xml new file mode 100644 index 00000000..1046225c --- /dev/null +++ b/__tests__/fixtures/swift-xunit.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/__tests__/swift-xunit.test.ts b/__tests__/swift-xunit.test.ts new file mode 100644 index 00000000..e0a2cd03 --- /dev/null +++ b/__tests__/swift-xunit.test.ts @@ -0,0 +1,34 @@ +import * as fs from 'fs' +import * as path from 'path' + +import {SwiftXunitParser} from '../src/parsers/swift-xunit/swift-xunit-parser' +import {ParseOptions} from '../src/test-parser' +import {getReport} from '../src/report/get-report' +import {normalizeFilePath} from '../src/utils/path-utils' + +describe('swift-xunit tests', () => { + it('report from swift test results matches snapshot', async () => { + const fixturePath = path.join(__dirname, 'fixtures', 'swift-xunit.xml') + const outputPath = path.join(__dirname, '__outputs__', 'swift-xunit.md') + const filePath = normalizeFilePath(path.relative(__dirname, fixturePath)) + const fileContent = fs.readFileSync(fixturePath, {encoding: 'utf8'}) + + const trackedFiles = [ + 'Package.swift', + 'Sources/AcmeLib/AcmeLib.swift', + 'Tests/AcmeLibTests/AcmeLibTests.swift', + ] + const opts: ParseOptions = { + parseErrors: true, + trackedFiles + } + + const parser = new SwiftXunitParser(opts) + const result = await parser.parse(filePath, fileContent) + expect(result).toMatchSnapshot() + + const report = getReport([result]) + fs.mkdirSync(path.dirname(outputPath), {recursive: true}) + fs.writeFileSync(outputPath, report) + }) +}) diff --git a/action.yml b/action.yml index ee0b64bc..db6d99c2 100644 --- a/action.yml +++ b/action.yml @@ -31,6 +31,7 @@ inputs: - java-junit - jest-junit - mocha-json + - swift-xunit required: true list-suites: description: | diff --git a/src/main.ts b/src/main.ts index 87a1da7e..4fdc3a09 100644 --- a/src/main.ts +++ b/src/main.ts @@ -15,6 +15,7 @@ import {DotnetTrxParser} from './parsers/dotnet-trx/dotnet-trx-parser' import {JavaJunitParser} from './parsers/java-junit/java-junit-parser' import {JestJunitParser} from './parsers/jest-junit/jest-junit-parser' import {MochaJsonParser} from './parsers/mocha-json/mocha-json-parser' +import {SwiftXunitParser} from './parsers/swift-xunit/swift-xunit-parser' import {normalizeDirPath, normalizeFilePath} from './utils/path-utils' import {getCheckRunContext} from './utils/github-utils' @@ -219,6 +220,8 @@ class TestReporter { return new JestJunitParser(options) case 'mocha-json': return new MochaJsonParser(options) + case 'swift-xunit': + return new SwiftXunitParser(options) default: throw new Error(`Input variable 'reporter' is set to invalid value '${reporter}'`) } diff --git a/src/parsers/swift-xunit/swift-xunit-parser.ts b/src/parsers/swift-xunit/swift-xunit-parser.ts new file mode 100644 index 00000000..8a96b733 --- /dev/null +++ b/src/parsers/swift-xunit/swift-xunit-parser.ts @@ -0,0 +1,8 @@ +import {ParseOptions} from '../../test-parser' +import {JavaJunitParser} from '../java-junit/java-junit-parser' + +export class SwiftXunitParser extends JavaJunitParser { + constructor(readonly options: ParseOptions) { + super(options) + } +}