From b0e0efadbaaa0352ee312f4d1fe2890e9d083fed Mon Sep 17 00:00:00 2001 From: Nikolai Laevskii Date: Thu, 12 Oct 2023 11:18:54 +0200 Subject: [PATCH] Add no-mock testing --- .../helpers/__tests__/command-runner.test.ts | 79 +++++++++++++++++++ packages/helpers/src/command-runner/core.ts | 3 + .../helpers/src/command-runner/middleware.ts | 6 +- 3 files changed, 85 insertions(+), 3 deletions(-) diff --git a/packages/helpers/__tests__/command-runner.test.ts b/packages/helpers/__tests__/command-runner.test.ts index 255a5916..2941bf50 100644 --- a/packages/helpers/__tests__/command-runner.test.ts +++ b/packages/helpers/__tests__/command-runner.test.ts @@ -1,5 +1,6 @@ import * as exec from '@actions/exec' import * as core from '@actions/core' +import * as io from '@actions/io' import {CommandRunner, createCommandRunner} from '../src/helpers' describe('command-runner', () => { @@ -425,4 +426,82 @@ describe('command-runner', () => { }) }) }) + + const IS_WINDOWS = process.platform === 'win32' + + describe('no-mock testing', () => { + beforeEach(() => { + jest.restoreAllMocks() + }) + + it('creates a command object', async () => { + let toolpath: string + let args: string[] + if (IS_WINDOWS) { + toolpath = await io.which('cmd', true) + args = ['/c', 'echo', 'hello'] + } else { + toolpath = await io.which('echo', true) + args = ['hello'] + } + const command = createCommandRunner(`"${toolpath}"`, args) + expect(command).toBeDefined() + expect(command).toBeInstanceOf(CommandRunner) + }) + + it('runs a command with non-zero exit code', async () => { + const runner = createCommandRunner() + + runner.setOptions({ + silent: true + }) + + if (IS_WINDOWS) { + runner.setCommand(await io.which('cmd', true)) + runner.setArgs(['/c', 'dir']) + } else { + runner.setCommand(await io.which('ls', true)) + runner.setArgs(['-l']) + } + + runner.setArgs((args: string[]) => [...args, 'non-existent-dir']) + + const cmdPromise = runner.onError('throw').run() + + await expect(cmdPromise).rejects.toThrow() + }) + + it('runs a command with zero exit code', async () => { + const runner = createCommandRunner() + + if (IS_WINDOWS) { + runner.setCommand(await io.which('cmd', true)) + runner.setArgs(['/c', 'echo']) + } else { + runner.setCommand(await io.which('echo', true)) + } + + runner.setArgs((args: string[]) => [...args, 'hello']) + + const result = await runner.run() + + expect(result.stdout).toContain('hello') + expect(result.exitCode).toEqual(0) + }) + + it('runs a command with empty output', async () => { + const runner = createCommandRunner() + + if (IS_WINDOWS) { + runner.setCommand(await io.which('cmd', true)) + runner.setArgs(['/c', 'echo.']) + } else { + runner.setCommand(await io.which('echo', true)) + } + + const cmdPromise = runner.onEmptyOutput('throw').run() + + await expect(cmdPromise).rejects.toThrow() + }) + }) }) diff --git a/packages/helpers/src/command-runner/core.ts b/packages/helpers/src/command-runner/core.ts index 7989693a..0e4a8390 100644 --- a/packages/helpers/src/command-runner/core.ts +++ b/packages/helpers/src/command-runner/core.ts @@ -103,6 +103,9 @@ export class CommandRunnerBase { } } ) + + context.stdout = (context.stdout ?? '') + stdoutDecoder.end() + context.stderr = (context.stderr ?? '') + stderrDecoder.end() } catch (error) { context.execerr = error as Error } diff --git a/packages/helpers/src/command-runner/middleware.ts b/packages/helpers/src/command-runner/middleware.ts index 8814c2e6..3707e3e7 100644 --- a/packages/helpers/src/command-runner/middleware.ts +++ b/packages/helpers/src/command-runner/middleware.ts @@ -248,7 +248,7 @@ export const matchEvent = ( } if (shouldRun) { - composedMiddleware(ctx, next) + await composedMiddleware(ctx, next) return } @@ -291,7 +291,7 @@ export const matchOutput = ( return } - composedMiddleware(ctx, next) + await composedMiddleware(ctx, next) } } @@ -396,6 +396,6 @@ export const matchSpecificError = ( return } - composedMiddleware(ctx, next) + await composedMiddleware(ctx, next) } }