Skip to content
Merged
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
111 changes: 111 additions & 0 deletions __tests__/check-run.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import * as path from 'path'

// Variables prefixed with 'mock' are accessible inside jest.mock factories due to hoisting
const mockCreate = jest.fn()
const mockUpdate = jest.fn()
const mockListForRef = jest.fn()
const mockSetOutput = jest.fn()
const mockGetInput = jest.fn()
const mockInfo = jest.fn()
const mockWarning = jest.fn()
const mockSetFailed = jest.fn()
const mockFindResults = jest.fn()

jest.mock('@actions/core', () => ({
getInput: mockGetInput,
setOutput: mockSetOutput,
info: mockInfo,
warning: mockWarning,
setFailed: mockSetFailed,
debug: jest.fn(),
error: jest.fn()
}))

jest.mock('@actions/github', () => ({
context: {
repo: {owner: 'owner', repo: 'repo'},
sha: 'abc123',
payload: {}
},
getOctokit: jest.fn(() => ({
rest: {
checks: {
create: mockCreate,
update: mockUpdate,
listForRef: mockListForRef
}
}
}))
}))

jest.mock('../src/search', () => ({
findResults: mockFindResults
}))

import {run} from '../src/main'

const reportPath = path.resolve(
__dirname,
'..',
'reports',
'checkstyle-result.xml'
)

function setupInputs(checkRun: string): void {
mockGetInput.mockImplementation((name: string) => {
const inputs: Record<string, string> = {
path: reportPath,
name: 'Checkstyle',
title: 'Checkstyle Report',
token: 'fake-token',
check_run: checkRun
}
return inputs[name] ?? ''
})
}

beforeEach(() => {
jest.clearAllMocks()
mockListForRef.mockResolvedValue({data: {check_runs: []}})
mockCreate.mockResolvedValue({
data: {html_url: 'https://github.com/owner/repo/runs/1'}
})
mockFindResults.mockResolvedValue({
filesToUpload: [reportPath],
rootDirectory: path.dirname(reportPath)
})
})

test('skips check run creation when check_run is false', async () => {
setupInputs('false')

await run()

expect(mockCreate).not.toHaveBeenCalled()
expect(mockUpdate).not.toHaveBeenCalled()
expect(mockInfo).toHaveBeenCalledWith(
'Check run creation is disabled via check_run input'
)
})

test('creates check run when check_run is true', async () => {
setupInputs('true')

await run()

expect(mockCreate).toHaveBeenCalled()
expect(mockInfo).not.toHaveBeenCalledWith(
'Check run creation is disabled via check_run input'
)
})

test('creates check run when check_run defaults to empty string', async () => {
setupInputs('')

await run()

expect(mockCreate).toHaveBeenCalled()
expect(mockInfo).not.toHaveBeenCalledWith(
'Check run creation is disabled via check_run input'
)
})
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ inputs:
Also when generating a new PAT, select the least scopes necessary.
[Learn more about creating and using encrypted secrets](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/creating-and-using-encrypted-secrets)
default: ${{ github.token }}
check_run:
description: 'Whether to create a GitHub Check Run with the results. Set to false to disable check run creation.'
default: 'true'
outputs:
conclusion:
description: "The conclusion of the check run: 'success', 'failure', or 'neutral'"
Expand Down
Loading
Loading