Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions __fixtures__/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export const debug = jest.fn<typeof core.debug>()
export const error = jest.fn<typeof core.error>()
export const info = jest.fn<typeof core.info>()
export const getInput = jest.fn<typeof core.getInput>()
export const isDebug = jest.fn<typeof core.isDebug>()
export const setOutput = jest.fn<typeof core.setOutput>()
export const setFailed = jest.fn<typeof core.setFailed>()
export const warning = jest.fn<typeof core.warning>()
35 changes: 35 additions & 0 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!'))
Expand All @@ -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')
Expand Down
17 changes: 17 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@ export async function run(): Promise<void> {
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 ...`)

Expand Down