|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | + |
| 3 | +const mockContext = vi.hoisted(() => ({ |
| 4 | + inputs: {} as Record<string, string>, |
| 5 | + warnings: [] as string[], |
| 6 | +})) |
| 7 | + |
| 8 | +const toolkitMocks = vi.hoisted(() => ({ |
| 9 | + getBooleanInput: vi.fn((name: string) => (mockContext.inputs[name] ?? '') === 'true'), |
| 10 | + getInput: vi.fn((name: string) => mockContext.inputs[name] ?? ''), |
| 11 | + warning: vi.fn((message: string) => { |
| 12 | + mockContext.warnings.push(message) |
| 13 | + }), |
| 14 | +})) |
| 15 | + |
| 16 | +vi.mock('@actions/core', () => ({ |
| 17 | + getBooleanInput: toolkitMocks.getBooleanInput, |
| 18 | + getInput: toolkitMocks.getInput, |
| 19 | + warning: toolkitMocks.warning, |
| 20 | +})) |
| 21 | + |
| 22 | +async function importInputsModule() { |
| 23 | + vi.resetModules() |
| 24 | + return import('../src/inputs') |
| 25 | +} |
| 26 | + |
| 27 | +describe('getInputs', () => { |
| 28 | + beforeEach(() => { |
| 29 | + vi.resetModules() |
| 30 | + vi.clearAllMocks() |
| 31 | + mockContext.inputs = {} |
| 32 | + mockContext.warnings = [] |
| 33 | + }) |
| 34 | + |
| 35 | + it('uses the configured extra-args default when no explicit value is provided', async () => { |
| 36 | + mockContext.inputs['extra-args'] = '--all-files' |
| 37 | + |
| 38 | + const { getInputs } = await importInputsModule() |
| 39 | + |
| 40 | + expect(getInputs().extraArgs).toBe('--all-files') |
| 41 | + }) |
| 42 | + |
| 43 | + it('does not emit a runtime warning for extra_args (deprecationMessage handles it)', async () => { |
| 44 | + mockContext.inputs.extra_args = '--files foo.py' |
| 45 | + |
| 46 | + const { getInputs } = await importInputsModule() |
| 47 | + |
| 48 | + expect(getInputs().extraArgs).toBe('--files foo.py') |
| 49 | + expect(mockContext.warnings).toEqual([]) |
| 50 | + }) |
| 51 | + |
| 52 | + it('preserves an explicit empty extra-args value', async () => { |
| 53 | + mockContext.inputs['extra-args'] = '' |
| 54 | + |
| 55 | + const { getInputs } = await importInputsModule() |
| 56 | + |
| 57 | + expect(getInputs().extraArgs).toBe('') |
| 58 | + }) |
| 59 | + |
| 60 | + it('prefers extra_args over extra-args when both are set', async () => { |
| 61 | + mockContext.inputs.extra_args = '--legacy' |
| 62 | + mockContext.inputs['extra-args'] = '--modern' |
| 63 | + |
| 64 | + const { getInputs } = await importInputsModule() |
| 65 | + |
| 66 | + expect(getInputs().extraArgs).toBe('--legacy') |
| 67 | + }) |
| 68 | + |
| 69 | + it('does not expose the deprecated token value to runtime code', async () => { |
| 70 | + mockContext.inputs.token = 'secret' |
| 71 | + |
| 72 | + const { getInputs } = await importInputsModule() |
| 73 | + const inputs = getInputs() as Record<string, unknown> |
| 74 | + |
| 75 | + expect('token' in inputs).toBe(false) |
| 76 | + }) |
| 77 | + |
| 78 | + it('enables verbose logs by default and allows opting out', async () => { |
| 79 | + let { getInputs } = await importInputsModule() |
| 80 | + expect(getInputs().showVerboseLogs).toBe(true) |
| 81 | + |
| 82 | + mockContext.inputs['show-verbose-logs'] = 'false' |
| 83 | + ;({ getInputs } = await importInputsModule()) |
| 84 | + expect(getInputs().showVerboseLogs).toBe(false) |
| 85 | + }) |
| 86 | +}) |
0 commit comments