mirror of https://github.com/actions/toolkit
Make use of @actions/exit in @actions/toolkit
parent
cca9523c73
commit
b3e79eb868
|
@ -1,14 +1,16 @@
|
||||||
|
import * as exitPkg from '@actions/exit'
|
||||||
import {Signale} from 'signale'
|
import {Signale} from 'signale'
|
||||||
import {Exit, ExitCode} from '../src/exit'
|
import {Exit} from '../src/exit'
|
||||||
|
|
||||||
describe('Exit', () => {
|
jest.mock('@actions/exit')
|
||||||
const tests: [keyof Exit, keyof Signale, ExitCode][] = [
|
|
||||||
['success', 'success', ExitCode.Success],
|
const tests: [keyof Exit, keyof Signale][] = [
|
||||||
['neutral', 'info', ExitCode.Neutral],
|
['success', 'success'],
|
||||||
['failure', 'fatal', ExitCode.Failure]
|
['neutral', 'info'],
|
||||||
|
['failure', 'fatal']
|
||||||
]
|
]
|
||||||
|
|
||||||
describe.each(tests)('%s', (method, log, code) => {
|
describe.each(tests)('%s', (method, log) => {
|
||||||
let logger: Signale
|
let logger: Signale
|
||||||
let exit: Exit
|
let exit: Exit
|
||||||
|
|
||||||
|
@ -23,9 +25,9 @@ describe('Exit', () => {
|
||||||
exit = new Exit(logger)
|
exit = new Exit(logger)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('exits with the expected code', () => {
|
it('exits with the expected method', () => {
|
||||||
exit[method]()
|
exit[method]()
|
||||||
expect(process.exit).toHaveBeenCalledWith(code)
|
expect(exitPkg[method]).toHaveBeenCalled()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('logs the expected message', () => {
|
it('logs the expected message', () => {
|
||||||
|
@ -33,4 +35,3 @@ describe('Exit', () => {
|
||||||
expect(logger[log]).toHaveBeenCalledWith('hello')
|
expect(logger[log]).toHaveBeenCalledWith('hello')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
|
import * as exitPkg from '@actions/exit'
|
||||||
import {Signale} from 'signale'
|
import {Signale} from 'signale'
|
||||||
import {ExitCode} from '../src/exit'
|
|
||||||
import {Toolkit} from '../src/toolkit'
|
import {Toolkit} from '../src/toolkit'
|
||||||
|
|
||||||
describe('Toolkit', () => {
|
jest.mock('@actions/exit')
|
||||||
|
|
||||||
describe('.run', () => {
|
describe('.run', () => {
|
||||||
it('runs a sync function', async () => {
|
it('runs a sync function', async () => {
|
||||||
const cb = jest.fn(() => true)
|
const cb = jest.fn(() => true)
|
||||||
|
@ -42,9 +43,8 @@ describe('Toolkit', () => {
|
||||||
|
|
||||||
new Toolkit({logger, requiredEnv: [missingKey]})
|
new Toolkit({logger, requiredEnv: [missingKey]})
|
||||||
|
|
||||||
expect(process.exit).toHaveBeenCalledWith(ExitCode.Failure)
|
expect(exitPkg.failure).toHaveBeenCalled()
|
||||||
expect(logger.fatal)
|
expect(logger.fatal)
|
||||||
.toHaveBeenCalledWith(`The following environment variables are required for this action to run:
|
.toHaveBeenCalledWith(`The following environment variables are required for this action to run:
|
||||||
- __DOES_NOT_EXIST__`)
|
- __DOES_NOT_EXIST__`)
|
||||||
})
|
})
|
||||||
})
|
|
||||||
|
|
|
@ -31,8 +31,8 @@
|
||||||
"url": "https://github.com/actions/toolkit/issues"
|
"url": "https://github.com/actions/toolkit/issues"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@actions/exit": "^0.0.0",
|
||||||
"@octokit/rest": "^16.25.0",
|
"@octokit/rest": "^16.25.0",
|
||||||
"signale": "^1.4.0"
|
"signale": "^1.4.0"
|
||||||
},
|
}
|
||||||
"devDependencies": {}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,17 +1,6 @@
|
||||||
|
import * as exit from '@actions/exit'
|
||||||
import {Signale} from 'signale'
|
import {Signale} from 'signale'
|
||||||
|
|
||||||
/**
|
|
||||||
* The code to exit an action
|
|
||||||
*/
|
|
||||||
export enum ExitCode {
|
|
||||||
Success = 0,
|
|
||||||
Failure = 1,
|
|
||||||
Neutral = 78
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: These exit codes may not behave as expected on the new runtime, due to
|
|
||||||
// complexities of async logging and sync exiting.
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A class that wraps some basic methods of exiting from an action
|
* A class that wraps some basic methods of exiting from an action
|
||||||
*/
|
*/
|
||||||
|
@ -23,7 +12,7 @@ export class Exit {
|
||||||
*/
|
*/
|
||||||
success(message?: string) {
|
success(message?: string) {
|
||||||
if (message) this.logger.success(message)
|
if (message) this.logger.success(message)
|
||||||
process.exit(ExitCode.Success)
|
exit.success()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -31,7 +20,7 @@ export class Exit {
|
||||||
*/
|
*/
|
||||||
neutral(message?: string) {
|
neutral(message?: string) {
|
||||||
if (message) this.logger.info(message)
|
if (message) this.logger.info(message)
|
||||||
process.exit(ExitCode.Neutral)
|
exit.neutral()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -39,6 +28,6 @@ export class Exit {
|
||||||
*/
|
*/
|
||||||
failure(message?: string) {
|
failure(message?: string) {
|
||||||
if (message) this.logger.fatal(message)
|
if (message) this.logger.fatal(message)
|
||||||
process.exit(ExitCode.Failure)
|
exit.failure()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue