|
| 1 | +import { writeFileSync } from 'fs' |
| 2 | +import { tmpdir } from 'os' |
| 3 | +import { resolve } from 'path' |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +type CoreMock = { |
| 7 | + getInput: ReturnType<typeof vi.fn> |
| 8 | + info: ReturnType<typeof vi.fn> |
| 9 | + notice: ReturnType<typeof vi.fn> |
| 10 | + setFailed: ReturnType<typeof vi.fn> |
| 11 | +} |
| 12 | + |
| 13 | +const core: CoreMock = { |
| 14 | + getInput: vi.fn(), |
| 15 | + info: vi.fn(), |
| 16 | + notice: vi.fn(), |
| 17 | + setFailed: vi.fn(), |
| 18 | +} |
| 19 | + |
| 20 | +vi.mock('@actions/core', () => core) |
| 21 | + |
| 22 | +const createTempEventFile = (payload: unknown) => { |
| 23 | + const filePath = resolve(tmpdir(), `lint-and-unit-tests-pass-${Date.now()}-${Math.random().toString(16).slice(2)}.json`) |
| 24 | + writeFileSync(filePath, JSON.stringify(payload), 'utf8') |
| 25 | + return filePath |
| 26 | +} |
| 27 | + |
| 28 | +const createMockResponse = (params: { ok: boolean; status: number; json?: unknown }) => { |
| 29 | + return { |
| 30 | + ok: params.ok, |
| 31 | + status: params.status, |
| 32 | + json: async () => params.json, |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +describe('lint-and-unit-tests-pass action', () => { |
| 37 | + const originalEnv = process.env |
| 38 | + |
| 39 | + beforeEach(() => { |
| 40 | + vi.clearAllMocks() |
| 41 | + process.env = { ...originalEnv } |
| 42 | + delete process.env['GITHUB_EVENT_PATH'] |
| 43 | + |
| 44 | + core.getInput = vi.fn() |
| 45 | + core.info = vi.fn() |
| 46 | + core.notice = vi.fn() |
| 47 | + core.setFailed = vi.fn() |
| 48 | + }) |
| 49 | + |
| 50 | + afterEach(() => { |
| 51 | + process.env = originalEnv |
| 52 | + vi.unstubAllGlobals() |
| 53 | + }) |
| 54 | + |
| 55 | + it('skips verification for hotfix pull_request workflow_run', async () => { |
| 56 | + const { run } = await import('../src/index') |
| 57 | + |
| 58 | + const eventPath = createTempEventFile({ |
| 59 | + 'workflow_run': { |
| 60 | + 'id': 123, |
| 61 | + 'head_branch': 'hotfix/something', |
| 62 | + 'event': 'pull_request', |
| 63 | + }, |
| 64 | + repository: { owner: { login: 'webstackdev' }, name: 'astro.webstackbuilders.com' }, |
| 65 | + }) |
| 66 | + |
| 67 | + process.env['GITHUB_EVENT_PATH'] = eventPath |
| 68 | + |
| 69 | + core.getInput.mockImplementation((name: string, options?: { required?: boolean }) => { |
| 70 | + if (name === 'github-token') return 'ghs_test' |
| 71 | + if (options?.required) throw new Error(`Missing required input: ${name}`) |
| 72 | + return '' |
| 73 | + }) |
| 74 | + |
| 75 | + const fetchMock = vi.fn(async () => createMockResponse({ ok: true, status: 200, json: {} })) |
| 76 | + vi.stubGlobal('fetch', fetchMock as unknown as typeof fetch) |
| 77 | + |
| 78 | + await run() |
| 79 | + |
| 80 | + expect(core.notice).toHaveBeenCalledWith('Hotfix branch: skipping CI verification requirements.') |
| 81 | + expect(fetchMock).not.toHaveBeenCalled() |
| 82 | + expect(core.setFailed).not.toHaveBeenCalled() |
| 83 | + }) |
| 84 | + |
| 85 | + it('fails when required jobs are missing or not successful', async () => { |
| 86 | + const { run } = await import('../src/index') |
| 87 | + |
| 88 | + const eventPath = createTempEventFile({ |
| 89 | + 'workflow_run': { |
| 90 | + 'id': 123, |
| 91 | + 'head_branch': 'feature/thing', |
| 92 | + 'event': 'pull_request', |
| 93 | + }, |
| 94 | + repository: { owner: { login: 'webstackdev' }, name: 'astro.webstackbuilders.com' }, |
| 95 | + }) |
| 96 | + |
| 97 | + process.env['GITHUB_EVENT_PATH'] = eventPath |
| 98 | + |
| 99 | + core.getInput.mockImplementation((name: string, options?: { required?: boolean }) => { |
| 100 | + if (name === 'github-token') return 'ghs_test' |
| 101 | + if (options?.required) throw new Error(`Missing required input: ${name}`) |
| 102 | + return '' |
| 103 | + }) |
| 104 | + |
| 105 | + const fetchMock = vi.fn(async (url: string) => { |
| 106 | + if (String(url).includes('/actions/runs/123/jobs')) { |
| 107 | + return createMockResponse({ |
| 108 | + ok: true, |
| 109 | + status: 200, |
| 110 | + json: { |
| 111 | + jobs: [{ name: 'Lint', conclusion: 'success' }], |
| 112 | + }, |
| 113 | + }) |
| 114 | + } |
| 115 | + return createMockResponse({ ok: false, status: 404 }) |
| 116 | + }) |
| 117 | + vi.stubGlobal('fetch', fetchMock as unknown as typeof fetch) |
| 118 | + |
| 119 | + await run() |
| 120 | + |
| 121 | + expect(core.setFailed).toHaveBeenCalledWith('Required CI jobs missing or failed: Unit Tests') |
| 122 | + }) |
| 123 | + |
| 124 | + it('passes when required jobs succeeded', async () => { |
| 125 | + const { run } = await import('../src/index') |
| 126 | + |
| 127 | + const eventPath = createTempEventFile({ |
| 128 | + 'workflow_run': { |
| 129 | + 'id': 123, |
| 130 | + 'head_branch': 'feature/thing', |
| 131 | + 'event': 'pull_request', |
| 132 | + }, |
| 133 | + repository: { owner: { login: 'webstackdev' }, name: 'astro.webstackbuilders.com' }, |
| 134 | + }) |
| 135 | + |
| 136 | + process.env['GITHUB_EVENT_PATH'] = eventPath |
| 137 | + |
| 138 | + core.getInput.mockImplementation((name: string, options?: { required?: boolean }) => { |
| 139 | + if (name === 'github-token') return 'ghs_test' |
| 140 | + if (options?.required) throw new Error(`Missing required input: ${name}`) |
| 141 | + return '' |
| 142 | + }) |
| 143 | + |
| 144 | + const fetchMock = vi.fn(async (url: string) => { |
| 145 | + if (String(url).includes('/actions/runs/123/jobs')) { |
| 146 | + return createMockResponse({ |
| 147 | + ok: true, |
| 148 | + status: 200, |
| 149 | + json: { |
| 150 | + jobs: [ |
| 151 | + { name: 'Lint', conclusion: 'success' }, |
| 152 | + { name: 'Unit Tests', conclusion: 'success' }, |
| 153 | + ], |
| 154 | + }, |
| 155 | + }) |
| 156 | + } |
| 157 | + return createMockResponse({ ok: false, status: 404 }) |
| 158 | + }) |
| 159 | + vi.stubGlobal('fetch', fetchMock as unknown as typeof fetch) |
| 160 | + |
| 161 | + await run() |
| 162 | + |
| 163 | + expect(core.setFailed).not.toHaveBeenCalled() |
| 164 | + expect(core.info).toHaveBeenCalledWith('Required CI jobs succeeded.') |
| 165 | + }) |
| 166 | +}) |
0 commit comments