diff --git a/packages/toolkit/__tests__/exit.test.ts b/packages/toolkit/__tests__/exit.test.ts index 6318f62d..d3901977 100644 --- a/packages/toolkit/__tests__/exit.test.ts +++ b/packages/toolkit/__tests__/exit.test.ts @@ -1,36 +1,37 @@ +import * as exitPkg from '@actions/exit' import {Signale} from 'signale' -import {Exit, ExitCode} from '../src/exit' +import {Exit} from '../src/exit' -describe('Exit', () => { - const tests: [keyof Exit, keyof Signale, ExitCode][] = [ - ['success', 'success', ExitCode.Success], - ['neutral', 'info', ExitCode.Neutral], - ['failure', 'fatal', ExitCode.Failure] - ] +jest.mock('@actions/exit') - describe.each(tests)('%s', (method, log, code) => { - let logger: Signale - let exit: Exit +const tests: [keyof Exit, keyof Signale][] = [ + ['success', 'success'], + ['neutral', 'info'], + ['failure', 'fatal'] +] - beforeEach(() => { - // Create a logger to mock - logger = new Signale() - logger.success = jest.fn() - logger.info = jest.fn() - logger.fatal = jest.fn() +describe.each(tests)('%s', (method, log) => { + let logger: Signale + let exit: Exit - process.exit = jest.fn() - exit = new Exit(logger) - }) + beforeEach(() => { + // Create a logger to mock + logger = new Signale() + logger.success = jest.fn() + logger.info = jest.fn() + logger.fatal = jest.fn() - it('exits with the expected code', () => { - exit[method]() - expect(process.exit).toHaveBeenCalledWith(code) - }) + process.exit = jest.fn() + exit = new Exit(logger) + }) - it('logs the expected message', () => { - exit[method]('hello') - expect(logger[log]).toHaveBeenCalledWith('hello') - }) + it('exits with the expected method', () => { + exit[method]() + expect(exitPkg[method]).toHaveBeenCalled() + }) + + it('logs the expected message', () => { + exit[method]('hello') + expect(logger[log]).toHaveBeenCalledWith('hello') }) }) diff --git a/packages/toolkit/__tests__/toolkit.test.ts b/packages/toolkit/__tests__/toolkit.test.ts index 4a7b233f..d8dd3254 100644 --- a/packages/toolkit/__tests__/toolkit.test.ts +++ b/packages/toolkit/__tests__/toolkit.test.ts @@ -1,50 +1,50 @@ +import * as exitPkg from '@actions/exit' import {Signale} from 'signale' -import {ExitCode} from '../src/exit' import {Toolkit} from '../src/toolkit' -describe('Toolkit', () => { - describe('.run', () => { - it('runs a sync function', async () => { - const cb = jest.fn(() => true) - const value = await Toolkit.run(cb) - expect(cb).toHaveBeenCalledWith(expect.any(Toolkit)) - expect(value).toBe(true) - }) +jest.mock('@actions/exit') - it('runs an async function', async () => { - const cb = jest.fn(async () => true) - const value = await Toolkit.run(cb) - expect(cb).toHaveBeenCalledWith(expect.any(Toolkit)) - expect(value).toBe(true) - }) - - it('logs and fails when an error occurs', async () => { - const err = new Error() - const exitFailure = jest.fn() - - await Toolkit.run(async tk => { - tk.exit.failure = exitFailure - throw err - }) - - expect(exitFailure).toHaveBeenCalledWith(err) - }) +describe('.run', () => { + it('runs a sync function', async () => { + const cb = jest.fn(() => true) + const value = await Toolkit.run(cb) + expect(cb).toHaveBeenCalledWith(expect.any(Toolkit)) + expect(value).toBe(true) }) - it('asserts required keys are present', async () => { - const missingKey = '__DOES_NOT_EXIST__' + it('runs an async function', async () => { + const cb = jest.fn(async () => true) + const value = await Toolkit.run(cb) + expect(cb).toHaveBeenCalledWith(expect.any(Toolkit)) + expect(value).toBe(true) + }) - Reflect.deleteProperty(process.env, missingKey) + it('logs and fails when an error occurs', async () => { + const err = new Error() + const exitFailure = jest.fn() - const logger = new Signale() - logger.fatal = jest.fn() - jest.spyOn(process, 'exit').mockImplementation() + await Toolkit.run(async tk => { + tk.exit.failure = exitFailure + throw err + }) - new Toolkit({logger, requiredEnv: [missingKey]}) - - expect(process.exit).toHaveBeenCalledWith(ExitCode.Failure) - expect(logger.fatal) - .toHaveBeenCalledWith(`The following environment variables are required for this action to run: -- __DOES_NOT_EXIST__`) + expect(exitFailure).toHaveBeenCalledWith(err) }) }) + +it('asserts required keys are present', async () => { + const missingKey = '__DOES_NOT_EXIST__' + + Reflect.deleteProperty(process.env, missingKey) + + const logger = new Signale() + logger.fatal = jest.fn() + jest.spyOn(process, 'exit').mockImplementation() + + new Toolkit({logger, requiredEnv: [missingKey]}) + + expect(exitPkg.failure).toHaveBeenCalled() + expect(logger.fatal) + .toHaveBeenCalledWith(`The following environment variables are required for this action to run: +- __DOES_NOT_EXIST__`) +}) diff --git a/packages/toolkit/package.json b/packages/toolkit/package.json index 874b01cd..45d350c5 100644 --- a/packages/toolkit/package.json +++ b/packages/toolkit/package.json @@ -31,8 +31,8 @@ "url": "https://github.com/actions/toolkit/issues" }, "dependencies": { + "@actions/exit": "^0.0.0", "@octokit/rest": "^16.25.0", "signale": "^1.4.0" - }, - "devDependencies": {} + } } diff --git a/packages/toolkit/src/exit.ts b/packages/toolkit/src/exit.ts index 4d1d5169..69000aac 100644 --- a/packages/toolkit/src/exit.ts +++ b/packages/toolkit/src/exit.ts @@ -1,17 +1,6 @@ +import * as exit from '@actions/exit' 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 */ @@ -23,7 +12,7 @@ export class Exit { */ success(message?: string) { if (message) this.logger.success(message) - process.exit(ExitCode.Success) + exit.success() } /** @@ -31,7 +20,7 @@ export class Exit { */ neutral(message?: string) { if (message) this.logger.info(message) - process.exit(ExitCode.Neutral) + exit.neutral() } /** @@ -39,6 +28,6 @@ export class Exit { */ failure(message?: string) { if (message) this.logger.fatal(message) - process.exit(ExitCode.Failure) + exit.failure() } }