From 659bb4fff30483f02a7ac748e38e34a26a4ec2a5 Mon Sep 17 00:00:00 2001 From: Michal Dorner Date: Sat, 16 Jan 2021 21:19:40 +0100 Subject: [PATCH] Update interfaces to accept multiple reports to parse --- __tests__/dart-json.test.ts | 10 +++++++--- __tests__/dotnet-trx.test.ts | 10 +++++++--- __tests__/jest-junit.test.ts | 10 +++++++--- src/main.ts | 6 +++--- src/parsers/dart-json/dart-json-parser.ts | 6 +++--- src/parsers/dotnet-trx/dotnet-trx-parser.ts | 6 +++--- src/parsers/jest-junit/jest-junit-parser.ts | 6 +++--- src/parsers/parser-types.ts | 4 +++- 8 files changed, 36 insertions(+), 22 deletions(-) diff --git a/__tests__/dart-json.test.ts b/__tests__/dart-json.test.ts index 99bd7c5..6d2cf82 100644 --- a/__tests__/dart-json.test.ts +++ b/__tests__/dart-json.test.ts @@ -4,8 +4,12 @@ import * as path from 'path' import {parseDartJson} from '../src/parsers/dart-json/dart-json-parser' import {ParseOptions} from '../src/parsers/parser-types' -const xmlFixture = fs.readFileSync(path.join(__dirname, 'fixtures', 'dart-json.json'), {encoding: 'utf8'}) -const outputPath = __dirname + '/__outputs__/dart-json.md' +const fixturePath = path.join(__dirname, 'fixtures', 'dart-json.json') +const outputPath = path.join(__dirname, '__outputs__', 'dart-json.md') +const xmlFixture = { + path: fixturePath, + content: fs.readFileSync(fixturePath, {encoding: 'utf8'}) +} describe('dart-json tests', () => { it('matches report snapshot', async () => { @@ -16,7 +20,7 @@ describe('dart-json tests', () => { workDir: 'C:/Users/Michal/Workspace/dorny/test-check/reports/dart/' } - const result = await parseDartJson(xmlFixture, opts) + const result = await parseDartJson([xmlFixture], opts) fs.mkdirSync(path.dirname(outputPath), {recursive: true}) fs.writeFileSync(outputPath, result?.output?.summary ?? '') diff --git a/__tests__/dotnet-trx.test.ts b/__tests__/dotnet-trx.test.ts index 6e1054b..af9760a 100644 --- a/__tests__/dotnet-trx.test.ts +++ b/__tests__/dotnet-trx.test.ts @@ -4,8 +4,12 @@ import * as path from 'path' import {parseDotnetTrx} from '../src/parsers/dotnet-trx/dotnet-trx-parser' import {ParseOptions} from '../src/parsers/parser-types' -const xmlFixture = fs.readFileSync(path.join(__dirname, 'fixtures', 'dotnet-trx.trx'), {encoding: 'utf8'}) -const outputPath = __dirname + '/__outputs__/dotnet-trx.md' +const fixturePath = path.join(__dirname, 'fixtures', 'dotnet-trx.trx') +const outputPath = path.join(__dirname, '__outputs__', 'dotnet-trx.md') +const xmlFixture = { + path: fixturePath, + content: fs.readFileSync(fixturePath, {encoding: 'utf8'}) +} describe('dotnet-trx tests', () => { it('matches report snapshot', async () => { @@ -16,7 +20,7 @@ describe('dotnet-trx tests', () => { workDir: 'C:/Users/Michal/Workspace/dorny/test-check/reports/dotnet/' } - const result = await parseDotnetTrx(xmlFixture, opts) + const result = await parseDotnetTrx([xmlFixture], opts) fs.mkdirSync(path.dirname(outputPath), {recursive: true}) fs.writeFileSync(outputPath, result?.output?.summary ?? '') diff --git a/__tests__/jest-junit.test.ts b/__tests__/jest-junit.test.ts index 6ae22f1..0c9092f 100644 --- a/__tests__/jest-junit.test.ts +++ b/__tests__/jest-junit.test.ts @@ -4,8 +4,12 @@ import * as path from 'path' import {parseJestJunit} from '../src/parsers/jest-junit/jest-junit-parser' import {ParseOptions} from '../src/parsers/parser-types' -const xmlFixture = fs.readFileSync(path.join(__dirname, 'fixtures', 'jest-junit.xml'), {encoding: 'utf8'}) -const outputPath = __dirname + '/__outputs__/jest-junit.md' +const fixturePath = path.join(__dirname, 'fixtures', 'jest-junit.xml') +const outputPath = path.join(__dirname, '__outputs__', 'jest-junit.md') +const xmlFixture = { + path: fixturePath, + content: fs.readFileSync(fixturePath, {encoding: 'utf8'}) +} describe('jest-junit tests', () => { it('matches report snapshot', async () => { @@ -16,7 +20,7 @@ describe('jest-junit tests', () => { workDir: 'C:/Users/Michal/Workspace/dorny/test-check/reports/jest/' } - const result = await parseJestJunit(xmlFixture, opts) + const result = await parseJestJunit([xmlFixture], opts) fs.mkdirSync(path.dirname(outputPath), {recursive: true}) fs.writeFileSync(outputPath, result?.output?.summary ?? '') diff --git a/src/main.ts b/src/main.ts index a7af0fa..e6af83f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -5,7 +5,7 @@ import glob from 'fast-glob' import {parseDartJson} from './parsers/dart-json/dart-json-parser' import {parseDotnetTrx} from './parsers/dotnet-trx/dotnet-trx-parser' import {parseJestJunit} from './parsers/jest-junit/jest-junit-parser' -import {ParseOptions, ParseTestResult} from './parsers/parser-types' +import {FileContent, ParseOptions, ParseTestResult} from './parsers/parser-types' import {normalizeDirPath} from './utils/file-utils' import {listFiles} from './utils/git' import {getCheckRunSha} from './utils/github-utils' @@ -54,7 +54,7 @@ async function main(): Promise { return } - const result = await parser(files[0].content, opts) + const result = await parser(files, opts) const conclusion = result.success ? 'success' : 'failure' await octokit.checks.create({ @@ -87,7 +87,7 @@ function getParser(reporter: string): ParseTestResult { } } -export async function getFiles(pattern: string): Promise<{path: string; content: string}[]> { +export async function getFiles(pattern: string): Promise { const paths = await glob(pattern, {dot: true}) return Promise.all( paths.map(async path => { diff --git a/src/parsers/dart-json/dart-json-parser.ts b/src/parsers/dart-json/dart-json-parser.ts index 4d1b8e0..7608db0 100644 --- a/src/parsers/dart-json/dart-json-parser.ts +++ b/src/parsers/dart-json/dart-json-parser.ts @@ -1,4 +1,4 @@ -import {Annotation, ParseOptions, TestResult} from '../parser-types' +import {Annotation, FileContent, ParseOptions, TestResult} from '../parser-types' import getReport from '../../report/get-report' import {normalizeFilePath} from '../../utils/file-utils' @@ -68,8 +68,8 @@ class TestCase { } } -export async function parseDartJson(content: string, options: ParseOptions): Promise { - const testRun = getTestRun(content) +export async function parseDartJson(files: FileContent[], options: ParseOptions): Promise { + const testRun = getTestRun(files[0].content) const icon = testRun.success ? Icon.success : Icon.fail return { diff --git a/src/parsers/dotnet-trx/dotnet-trx-parser.ts b/src/parsers/dotnet-trx/dotnet-trx-parser.ts index 3f15c51..95792fd 100644 --- a/src/parsers/dotnet-trx/dotnet-trx-parser.ts +++ b/src/parsers/dotnet-trx/dotnet-trx-parser.ts @@ -1,6 +1,6 @@ import {ErrorInfo, Outcome, TestMethod, TrxReport} from './dotnet-trx-types' -import {Annotation, ParseOptions, TestResult} from '../parser-types' +import {Annotation, FileContent, ParseOptions, TestResult} from '../parser-types' import {parseStringPromise} from 'xml2js' import {normalizeFilePath} from '../../utils/file-utils' @@ -41,8 +41,8 @@ class Test { } } -export async function parseDotnetTrx(content: string, options: ParseOptions): Promise { - const trx = (await parseStringPromise(content, { +export async function parseDotnetTrx(files: FileContent[], options: ParseOptions): Promise { + const trx = (await parseStringPromise(files[0].content, { attrValueProcessors: [parseAttribute] })) as TrxReport diff --git a/src/parsers/jest-junit/jest-junit-parser.ts b/src/parsers/jest-junit/jest-junit-parser.ts index 40b9d2a..fefa625 100644 --- a/src/parsers/jest-junit/jest-junit-parser.ts +++ b/src/parsers/jest-junit/jest-junit-parser.ts @@ -1,4 +1,4 @@ -import {Annotation, ParseOptions, TestResult} from '../parser-types' +import {Annotation, FileContent, ParseOptions, TestResult} from '../parser-types' import {parseStringPromise} from 'xml2js' import {JunitReport, TestCase, TestSuite} from './jest-junit-types' @@ -15,8 +15,8 @@ import { } from '../../report/test-results' import getReport from '../../report/get-report' -export async function parseJestJunit(content: string, options: ParseOptions): Promise { - const junit = (await parseStringPromise(content, { +export async function parseJestJunit(files: FileContent[], options: ParseOptions): Promise { + const junit = (await parseStringPromise(files[0].content, { attrValueProcessors: [parseAttribute] })) as JunitReport const testsuites = junit.testsuites diff --git a/src/parsers/parser-types.ts b/src/parsers/parser-types.ts index 574eca0..9294b39 100644 --- a/src/parsers/parser-types.ts +++ b/src/parsers/parser-types.ts @@ -13,7 +13,9 @@ export type Annotation = { raw_details?: string } -export type ParseTestResult = (content: string, options: ParseOptions) => Promise +export type ParseTestResult = (files: FileContent[], options: ParseOptions) => Promise + +export type FileContent = {path: string; content: string} export interface ParseOptions { name: string