Skip to content

Commit

Permalink
Support path pattern to match test report files
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal Dorner committed Jan 16, 2021
1 parent dfddea6 commit 656ede0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
29 changes: 24 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -26,6 +28,7 @@ async function main(): Promise<void> {
const workDirInput = core.getInput('working-directory', {required: false})

if (workDirInput) {
core.info(`Changing directory to ${workDirInput}`)
process.chdir(workDirInput)
}

Expand All @@ -44,8 +47,14 @@ async function main(): Promise<void> {
}

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({
Expand Down Expand Up @@ -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()
18 changes: 2 additions & 16 deletions src/utils/file-utils.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 656ede0

Please sign in to comment.