|
| 1 | +import { Console } from 'node:console' |
| 2 | +import { Writable } from 'node:stream' |
| 3 | +import { getTasks } from '@vitest/runner/utils' |
| 4 | +import stripAnsi from 'strip-ansi' |
| 5 | +import type { File, Reporter, Vitest } from '../../types' |
| 6 | +import { getFullName } from '../../utils' |
| 7 | +import { printError } from '../error' |
| 8 | +import { Logger } from '../logger' |
| 9 | +import type { WorkspaceProject } from '../workspace' |
| 10 | + |
| 11 | +export class GithubActionsReporter implements Reporter { |
| 12 | + ctx: Vitest = undefined! |
| 13 | + |
| 14 | + onInit(ctx: Vitest) { |
| 15 | + this.ctx = ctx |
| 16 | + } |
| 17 | + |
| 18 | + async onFinished(files: File[] = [], errors: unknown[] = []) { |
| 19 | + // collect all errors and associate them with projects |
| 20 | + const projectErrors = new Array<{ project: WorkspaceProject; title: string; error: unknown }>() |
| 21 | + for (const error of errors) { |
| 22 | + projectErrors.push({ |
| 23 | + project: this.ctx.getCoreWorkspaceProject(), |
| 24 | + title: 'Unhandled error', |
| 25 | + error, |
| 26 | + }) |
| 27 | + } |
| 28 | + for (const file of files) { |
| 29 | + const tasks = getTasks(file) |
| 30 | + const project = this.ctx.getProjectByTaskId(file.id) |
| 31 | + for (const task of tasks) { |
| 32 | + if (task.result?.state !== 'fail') |
| 33 | + continue |
| 34 | + |
| 35 | + const title = getFullName(task, ' > ') |
| 36 | + for (const error of task.result?.errors ?? []) { |
| 37 | + projectErrors.push({ |
| 38 | + project, |
| 39 | + title, |
| 40 | + error, |
| 41 | + }) |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + // format errors via `printError` |
| 47 | + for (const { project, title, error } of projectErrors) { |
| 48 | + const result = await printErrorWrapper(error, this.ctx, project) |
| 49 | + const stack = result?.nearest |
| 50 | + if (!stack) |
| 51 | + continue |
| 52 | + const formatted = formatMessage({ |
| 53 | + command: 'error', |
| 54 | + properties: { |
| 55 | + file: stack.file, |
| 56 | + title, |
| 57 | + line: String(stack.line), |
| 58 | + column: String(stack.column), |
| 59 | + }, |
| 60 | + message: stripAnsi(result.output), |
| 61 | + }) |
| 62 | + this.ctx.logger.log(`\n${formatted}`) |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +// use Logger with custom Console to extract messgage from `processError` util |
| 68 | +// TODO: maybe refactor `processError` to require single function `(message: string) => void` instead of full Logger? |
| 69 | +async function printErrorWrapper(error: unknown, ctx: Vitest, project: WorkspaceProject) { |
| 70 | + let output = '' |
| 71 | + const writable = new Writable({ |
| 72 | + write(chunk, _encoding, callback) { |
| 73 | + output += String(chunk) |
| 74 | + callback() |
| 75 | + }, |
| 76 | + }) |
| 77 | + const result = await printError(error, project, { |
| 78 | + showCodeFrame: false, |
| 79 | + logger: new Logger(ctx, new Console(writable, writable)), |
| 80 | + }) |
| 81 | + return { nearest: result?.nearest, output } |
| 82 | +} |
| 83 | + |
| 84 | +// workflow command formatting based on |
| 85 | +// https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-error-message |
| 86 | +// https://github.com/actions/toolkit/blob/f1d9b4b985e6f0f728b4b766db73498403fd5ca3/packages/core/src/command.ts#L80-L85 |
| 87 | +function formatMessage({ |
| 88 | + command, |
| 89 | + properties, |
| 90 | + message, |
| 91 | +}: { |
| 92 | + command: string |
| 93 | + properties: Record<string, string> |
| 94 | + message: string |
| 95 | +}): string { |
| 96 | + let result = `::${command}` |
| 97 | + Object.entries(properties).forEach(([k, v], i) => { |
| 98 | + result += i === 0 ? ' ' : ',' |
| 99 | + result += `${k}=${escapeProperty(v)}` |
| 100 | + }) |
| 101 | + result += `::${escapeData(message)}` |
| 102 | + return result |
| 103 | +} |
| 104 | + |
| 105 | +function escapeData(s: string): string { |
| 106 | + return s.replace(/%/g, '%25').replace(/\r/g, '%0D').replace(/\n/g, '%0A') |
| 107 | +} |
| 108 | + |
| 109 | +function escapeProperty(s: string): string { |
| 110 | + return s |
| 111 | + .replace(/%/g, '%25') |
| 112 | + .replace(/\r/g, '%0D') |
| 113 | + .replace(/\n/g, '%0A') |
| 114 | + .replace(/:/g, '%3A') |
| 115 | + .replace(/,/g, '%2C') |
| 116 | +} |
0 commit comments