Skip to content

Commit

Permalink
Add tests (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
int128 authored Aug 15, 2023
1 parent 09f1a66 commit 541414a
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ A pull request status looks like:

This action waits for the statuses of workflow runs at the current commit SHA.

- If any workflow run is not completed, poll the status.
- If any workflow run is failed, exit with an error.
- If any workflow run is not yet completed, poll the statuses.
- Otherwise, exit successfully.

### Inputs
Expand Down
12 changes: 6 additions & 6 deletions src/checks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import assert from 'assert'
import { ActionsChecksQuery } from './generated/graphql'
import { CheckConclusionState, CheckStatusState, StatusState } from './generated/graphql-types'

type Summary = {
export type Summary = {
state: State
workflowRuns: WorkflowRun[]
}
Expand Down Expand Up @@ -43,22 +43,22 @@ export const summarize = (checks: ActionsChecksQuery, excludeWorkflowNames: stri
}

assert(checks.repository.object.statusCheckRollup != null)
const state = calculateState(checks.repository.object.statusCheckRollup.state, workflowRuns)
const state = rollup(checks.repository.object.statusCheckRollup.state, workflowRuns)
return { state, workflowRuns }
}

const calculateState = (statusCheckRollupState: StatusState, workflowRuns: WorkflowRun[]): State => {
export const rollup = (statusCheckRollupState: StatusState, workflowRuns: WorkflowRun[]): State => {
if (statusCheckRollupState === StatusState.Failure) {
// workflowRuns may be incomplete if the rollup status is failure
return StatusState.Failure
}

if (workflowRuns.some((run) => run.status !== CheckStatusState.Completed)) {
return StatusState.Pending
}
if (workflowRuns.some((run) => run.conclusion === CheckConclusionState.Failure)) {
return StatusState.Failure
}
if (workflowRuns.some((run) => run.status !== CheckStatusState.Completed)) {
return StatusState.Pending
}
return StatusState.Success
}

Expand Down
158 changes: 158 additions & 0 deletions tests/checks.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
import { Summary, rollup, summarize } from '../src/checks'
import { CheckConclusionState, CheckStatusState, StatusState } from '../src/generated/graphql-types'

describe('summarize', () => {
it('should exclude given workflows', () => {
const summary = summarize(
{
rateLimit: {
cost: 1,
},
repository: {
object: {
__typename: 'Commit',
statusCheckRollup: {
state: StatusState.Pending,
},
checkSuites: {
totalCount: 3,
pageInfo: {
hasNextPage: false,
},
nodes: [
{
workflowRun: {
event: 'pull_request_target',
workflow: {
name: 'workflow-1',
},
},
status: CheckStatusState.Completed,
conclusion: CheckConclusionState.Skipped,
},
{
workflowRun: {
event: 'pull_request',
workflow: {
name: 'workflow-2',
},
},
status: CheckStatusState.Completed,
conclusion: CheckConclusionState.Success,
},
{
workflowRun: {
event: 'pull_request',
workflow: {
name: 'workflow-3',
},
},
status: CheckStatusState.InProgress,
conclusion: null,
},
],
},
},
},
},
['workflow-3'],
)
expect(summary).toStrictEqual<Summary>({
state: StatusState.Success,
workflowRuns: [
{
status: CheckStatusState.Completed,
conclusion: CheckConclusionState.Skipped,
event: 'pull_request_target',
workflowName: 'workflow-1',
},
{
status: CheckStatusState.Completed,
conclusion: CheckConclusionState.Success,
event: 'pull_request',
workflowName: 'workflow-2',
},
],
})
})
})

describe('rollup', () => {
it(`should return ${StatusState.Failure} if rollup state is ${StatusState.Failure} regardless of workflow runs`, () => {
const state = rollup(StatusState.Failure, [
{
status: CheckStatusState.InProgress,
conclusion: null,
event: 'pull_request',
workflowName: 'test',
},
])
expect(state).toBe(StatusState.Failure)
})

it(`should return ${StatusState.Success} if no workflow run is given`, () => {
const state = rollup(StatusState.Pending, [])
expect(state).toBe(StatusState.Success)
})

const runSuccess = {
status: CheckStatusState.Completed,
conclusion: CheckConclusionState.Success,
event: 'pull_request',
workflowName: 'test-success',
}
const runFailure = {
status: CheckStatusState.Completed,
conclusion: CheckConclusionState.Failure,
event: 'pull_request',
workflowName: 'test-failure',
}
const runInProgress = {
status: CheckStatusState.InProgress,
conclusion: null,
event: 'pull_request',
workflowName: 'test-in-progress',
}

it.each([
{ workflowRuns: [runSuccess] },
{ workflowRuns: [runSuccess, runSuccess] },
{ workflowRuns: [runSuccess, runSuccess, runSuccess] },
])(
`should return ${StatusState.Success} if all workflow runs are ${CheckConclusionState.Success}`,
({ workflowRuns }) => {
const state = rollup(StatusState.Pending, workflowRuns)
expect(state).toBe(StatusState.Success)
},
)

it.each([
{ workflowRuns: [runFailure] },
{ workflowRuns: [runSuccess, runFailure] },
{ workflowRuns: [runFailure, runFailure] },
{ workflowRuns: [runInProgress, runFailure] },
{ workflowRuns: [runSuccess, runSuccess, runFailure] },
{ workflowRuns: [runInProgress, runSuccess, runFailure] },
])(
`should return ${StatusState.Failure} if any workflow run is ${CheckConclusionState.Failure}`,
({ workflowRuns }) => {
const state = rollup(StatusState.Pending, workflowRuns)
expect(state).toBe(StatusState.Failure)
},
)

it.each([
{ workflowRuns: [runInProgress] },
{ workflowRuns: [runSuccess, runInProgress] },
{ workflowRuns: [runInProgress, runInProgress] },
{ workflowRuns: [runSuccess, runSuccess, runInProgress] },
{ workflowRuns: [runInProgress, runSuccess, runInProgress] },
{ workflowRuns: [runInProgress, runInProgress, runInProgress] },
])(
`should return ${StatusState.Pending} if any workflow run is not ${CheckStatusState.Completed}`,
({ workflowRuns }) => {
const state = rollup(StatusState.Pending, workflowRuns)
expect(state).toBe(StatusState.Pending)
},
)
})
1 change: 0 additions & 1 deletion tests/run.test.ts

This file was deleted.

0 comments on commit 541414a

Please sign in to comment.