diff --git a/__fixtures__/core.ts b/__fixtures__/core.ts index ac354d81f..38c0ed228 100644 --- a/__fixtures__/core.ts +++ b/__fixtures__/core.ts @@ -5,6 +5,7 @@ export const debug = jest.fn() export const error = jest.fn() export const info = jest.fn() export const getInput = jest.fn() +export const isDebug = jest.fn() export const setOutput = jest.fn() export const setFailed = jest.fn() export const warning = jest.fn() diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index e87d3dffd..895720a88 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -21,6 +21,7 @@ describe('main.ts', () => { beforeEach(() => { // Set the action's inputs as return values from core.getInput(). core.getInput.mockImplementation(() => '500') + core.isDebug.mockReturnValue(false) // Mock the wait function so that it does not actually wait. wait.mockImplementation(() => Promise.resolve('done!')) @@ -42,6 +43,40 @@ describe('main.ts', () => { ) }) + it('Fails fast when input is missing', async () => { + core.getInput.mockClear().mockReturnValueOnce('') + await run() + expect(core.setFailed).toHaveBeenNthCalledWith( + 1, + 'Input required and not supplied: milliseconds' + ) + expect(wait).not.toHaveBeenCalled() + }) + + it('Logs debug info in debug mode', async () => { + core.isDebug.mockReturnValue(true) + process.env.GITHUB_ACTIONS = 'true' + process.env.GITHUB_WORKFLOW = 'ci' + process.env.GITHUB_RUN_ID = '123' + process.env.GITHUB_SHA = 'deadbeef' + process.env.GITHUB_REF = 'refs/heads/main' + process.env.RUNNER_OS = 'Linux' + + await run() + + const messages = core.debug.mock.calls.map(call => call[0]) + expect(messages.some(msg => msg.includes('Node.js version:'))).toBe(true) + expect(messages.some(msg => msg.includes('Environment:'))).toBe(true) + }) + + it('Does not log debug env info without debug mode', async () => { + core.isDebug.mockReturnValue(false) + await run() + const messages = core.debug.mock.calls.map(call => call[0]) + expect(messages.some(msg => msg.includes('Node.js version:'))).toBe(false) + expect(messages.some(msg => msg.includes('Environment:'))).toBe(false) + }) + it('Sets a failed status', async () => { // Clear the getInput mock and return an invalid value. core.getInput.mockClear().mockReturnValueOnce('this is not a number') diff --git a/src/main.ts b/src/main.ts index d0ee6d394..a53392372 100644 --- a/src/main.ts +++ b/src/main.ts @@ -10,6 +10,23 @@ export async function run(): Promise { try { const ms: string = core.getInput('milliseconds') + if (!ms.trim()) { + throw new Error('Input required and not supplied: milliseconds') + } + + if (core.isDebug()) { + core.debug(`Node.js version: ${process.version}`) + const envSummary = { + GITHUB_ACTIONS: process.env.GITHUB_ACTIONS, + GITHUB_WORKFLOW: process.env.GITHUB_WORKFLOW, + GITHUB_RUN_ID: process.env.GITHUB_RUN_ID, + GITHUB_SHA: process.env.GITHUB_SHA, + GITHUB_REF: process.env.GITHUB_REF, + RUNNER_OS: process.env.RUNNER_OS + } + core.debug(`Environment: ${JSON.stringify(envSummary)}`) + } + // Debug logs are only output if the `ACTIONS_STEP_DEBUG` secret is true core.debug(`Waiting ${ms} milliseconds ...`)