mirror of https://github.com/actions/toolkit
Add no-mock testing
parent
0d75606364
commit
b0e0efadba
|
@ -1,5 +1,6 @@
|
||||||
import * as exec from '@actions/exec'
|
import * as exec from '@actions/exec'
|
||||||
import * as core from '@actions/core'
|
import * as core from '@actions/core'
|
||||||
|
import * as io from '@actions/io'
|
||||||
import {CommandRunner, createCommandRunner} from '../src/helpers'
|
import {CommandRunner, createCommandRunner} from '../src/helpers'
|
||||||
|
|
||||||
describe('command-runner', () => {
|
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()
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -103,6 +103,9 @@ export class CommandRunnerBase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
context.stdout = (context.stdout ?? '') + stdoutDecoder.end()
|
||||||
|
context.stderr = (context.stderr ?? '') + stderrDecoder.end()
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
context.execerr = error as Error
|
context.execerr = error as Error
|
||||||
}
|
}
|
||||||
|
|
|
@ -248,7 +248,7 @@ export const matchEvent = (
|
||||||
}
|
}
|
||||||
|
|
||||||
if (shouldRun) {
|
if (shouldRun) {
|
||||||
composedMiddleware(ctx, next)
|
await composedMiddleware(ctx, next)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -291,7 +291,7 @@ export const matchOutput = (
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
composedMiddleware(ctx, next)
|
await composedMiddleware(ctx, next)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -396,6 +396,6 @@ export const matchSpecificError = (
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
composedMiddleware(ctx, next)
|
await composedMiddleware(ctx, next)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue