generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from urcomputeringpal/jnewland/expand-tests
Expand test coverage for required status scenarios
- Loading branch information
Showing
12 changed files
with
368 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
name: Continuous Integration | ||
name: CI | ||
|
||
on: | ||
pull_request: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
name: Lint Codebase | ||
name: Lint | ||
|
||
on: | ||
pull_request: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
import { required } from '../src/required' | ||
import { GitHub } from '@actions/github/lib/utils' | ||
import { Context } from '@actions/github/lib/context' | ||
// eslint-disable-next-line import/no-unresolved | ||
import { WorkflowRunCompletedEvent } from '@octokit/webhooks-types' | ||
|
||
describe('required', () => { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
let mockOctokit: any | ||
let context: Context | ||
let event: WorkflowRunCompletedEvent | ||
|
||
beforeEach(() => { | ||
process.env.GITHUB_REPOSITORY = 'owner/repo' // Setting GITHUB_REPOSITORY environment variable | ||
mockOctokit = { | ||
rest: { | ||
repos: { | ||
createCommitStatus: jest.fn() | ||
}, | ||
actions: { | ||
// eslint-disable-next-line @typescript-eslint/promise-function-async | ||
getWorkflowRun: jest.fn().mockImplementation(() => { | ||
return Promise.resolve({ | ||
data: { | ||
id: 123, | ||
name: 'Test Workflow', | ||
head_sha: 'abc123', | ||
conclusion: 'success', | ||
html_url: 'http://example.com' | ||
} | ||
}) // Simulating a specific workflow run object | ||
}), | ||
// eslint-disable-next-line @typescript-eslint/promise-function-async | ||
listWorkflowRunsForRepo: jest.fn().mockImplementation(() => { | ||
return Promise.resolve({ | ||
data: { | ||
workflow_runs: [ | ||
{ id: 123, name: 'Test Workflow', conclusion: 'success' } | ||
] | ||
} | ||
}) // Simulating a successful response for listWorkflowRunsForRepo | ||
}) | ||
} | ||
} | ||
} | ||
context = new Context() | ||
event = { | ||
workflow_run: { | ||
id: 123, | ||
name: 'Test Workflow', | ||
head_sha: 'abc123', | ||
conclusion: 'success', | ||
html_url: 'http://example.com' | ||
} | ||
} as unknown as WorkflowRunCompletedEvent | ||
}) | ||
|
||
it('should handle all required statuses completing successfully', async () => { | ||
// Mock setup to simulate all required statuses completing successfully | ||
mockOctokit.rest.actions.listWorkflowRunsForRepo.mockResolvedValue({ | ||
data: { | ||
workflow_runs: [ | ||
{ id: 123, name: 'Test Workflow', conclusion: 'success' }, | ||
{ id: 124, name: 'Another Workflow', conclusion: 'success' } | ||
] | ||
} | ||
}) | ||
|
||
await required({ | ||
octokit: mockOctokit as unknown as InstanceType<typeof GitHub>, | ||
context, | ||
event, | ||
workflows: ['Test Workflow', 'Another Workflow'], | ||
statusName: 'Required' | ||
}) | ||
|
||
expect(mockOctokit.rest.repos.createCommitStatus).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
state: 'success', | ||
description: 'All 2 observed required workflows have succeeded' | ||
}) | ||
) | ||
}) | ||
|
||
it('should handle only one of the required statuses reporting success', async () => { | ||
// Mock setup to simulate only one of the required statuses reporting success | ||
mockOctokit.rest.actions.listWorkflowRunsForRepo.mockResolvedValue({ | ||
data: { | ||
workflow_runs: [ | ||
{ id: 123, name: 'Test Workflow', conclusion: 'success' }, | ||
{ id: 124, name: 'Another Workflow', conclusion: null } | ||
] | ||
} | ||
}) | ||
|
||
await required({ | ||
octokit: mockOctokit as unknown as InstanceType<typeof GitHub>, | ||
context, | ||
event, | ||
workflows: ['Test Workflow', 'Another Workflow'], | ||
statusName: 'Required' | ||
}) | ||
|
||
expect(mockOctokit.rest.repos.createCommitStatus).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
state: 'pending', | ||
description: expect.stringContaining( | ||
'required workflows are still pending...' | ||
) | ||
}) | ||
) | ||
}) | ||
|
||
it('should handle one of the required statuses reporting failure', async () => { | ||
// Mock setup to simulate one of the required statuses reporting failure | ||
mockOctokit.rest.actions.listWorkflowRunsForRepo.mockResolvedValue({ | ||
data: { | ||
workflow_runs: [ | ||
{ id: 123, name: 'Test Workflow', conclusion: 'success' }, | ||
{ id: 124, name: 'Another Workflow', conclusion: 'failure' } | ||
] | ||
} | ||
}) | ||
|
||
await required({ | ||
octokit: mockOctokit as unknown as InstanceType<typeof GitHub>, | ||
context, | ||
event, | ||
workflows: ['Test Workflow', 'Another Workflow'], | ||
statusName: 'Required' | ||
}) | ||
|
||
expect(mockOctokit.rest.repos.createCommitStatus).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
state: 'failure', | ||
description: expect.stringContaining( | ||
'required workflows were not successful' | ||
) | ||
}) | ||
) | ||
}) | ||
|
||
it('should handle replication lag scenario', async () => { | ||
// Mock setup to simulate replication lag scenario where "Test workflow" isn't in the results | ||
mockOctokit.rest.actions.listWorkflowRunsForRepo.mockResolvedValue({ | ||
data: { | ||
workflow_runs: [ | ||
{ id: 124, name: 'Another Workflow', conclusion: 'success' }, | ||
{ | ||
id: 125, | ||
name: 'Another Not Required Workflow 2', | ||
conclusion: 'success' | ||
} | ||
] | ||
} | ||
}) | ||
|
||
await required({ | ||
octokit: mockOctokit as unknown as InstanceType<typeof GitHub>, | ||
context, | ||
event, | ||
workflows: ['Test Workflow', 'Another Workflow'], | ||
statusName: 'Required' | ||
}) | ||
|
||
expect(mockOctokit.rest.repos.createCommitStatus).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
state: 'pending', | ||
description: | ||
'Waiting for conclusion to be reported for Test Workflow...' | ||
}) | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.