From 656ede0390a5df74200529c1319039564ed10962 Mon Sep 17 00:00:00 2001 From: Michal Dorner Date: Sat, 16 Jan 2021 21:07:12 +0100 Subject: [PATCH] Support path pattern to match test report files --- src/main.ts | 29 ++++++++++++++++++++++++----- src/utils/file-utils.ts | 18 ++---------------- 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/src/main.ts b/src/main.ts index a95ad50..a7af0fa 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,10 +1,12 @@ -import * as core from '@actions/core' -import * as github from '@actions/github' +import core from '@actions/core' +import github from '@actions/github' +import fs from 'fs' +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 {getFileContent, normalizeDirPath} from './utils/file-utils' +import {normalizeDirPath} from './utils/file-utils' import {listFiles} from './utils/git' import {getCheckRunSha} from './utils/github-utils' @@ -26,6 +28,7 @@ async function main(): Promise { const workDirInput = core.getInput('working-directory', {required: false}) if (workDirInput) { + core.info(`Changing directory to ${workDirInput}`) process.chdir(workDirInput) } @@ -44,8 +47,14 @@ async function main(): Promise { } const parser = getParser(reporter) - const content = getFileContent(path) - const result = await parser(content, opts) + const files = await getFiles(path) + + if (files.length === 0) { + core.setFailed(`No file matches path ${path}`) + return + } + + const result = await parser(files[0].content, opts) const conclusion = result.success ? 'success' : 'failure' await octokit.checks.create({ @@ -78,4 +87,14 @@ function getParser(reporter: string): ParseTestResult { } } +export async function getFiles(pattern: string): Promise<{path: string; content: string}[]> { + const paths = await glob(pattern, {dot: true}) + return Promise.all( + paths.map(async path => { + const content = await fs.promises.readFile(path, {encoding: 'utf8'}) + return {path, content} + }) + ) +} + run() diff --git a/src/utils/file-utils.ts b/src/utils/file-utils.ts index ffc5a50..657b1e7 100644 --- a/src/utils/file-utils.ts +++ b/src/utils/file-utils.ts @@ -1,24 +1,10 @@ -import * as fs from 'fs' - -export function getFileContent(path: string): string { - if (!fs.existsSync(path)) { - throw new Error(`File '${path}' not found`) - } - - if (!fs.lstatSync(path).isFile()) { - throw new Error(`'${path}' is not a file`) - } - - return fs.readFileSync(path, {encoding: 'utf8'}) -} - -export function normalizeDirPath(path: string, trailingSeparator: boolean): string { +export function normalizeDirPath(path: string, addTrailingSlash: boolean): string { if (!path) { return path } path = normalizeFilePath(path) - if (trailingSeparator && !path.endsWith('/')) { + if (addTrailingSlash && !path.endsWith('/')) { path += '/' } return path