|
| 1 | +import {beforeEach, describe, expect, test} from '@jest/globals'; |
| 2 | + |
| 3 | +import * as context from '../src/context'; |
| 4 | + |
| 5 | +describe('getInputs', () => { |
| 6 | + beforeEach(() => { |
| 7 | + process.env = Object.keys(process.env).reduce((object, key) => { |
| 8 | + if (!key.startsWith('INPUT_')) { |
| 9 | + object[key] = process.env[key]; |
| 10 | + } |
| 11 | + return object; |
| 12 | + }, {}); |
| 13 | + }); |
| 14 | + |
| 15 | + // prettier-ignore |
| 16 | + test.each([ |
| 17 | + [ |
| 18 | + 0, |
| 19 | + new Map<string, string>([]), |
| 20 | + { |
| 21 | + image: 'tonistiigi/binfmt:latest', |
| 22 | + platforms: 'all', |
| 23 | + } as context.Inputs |
| 24 | + ], |
| 25 | + [ |
| 26 | + 1, |
| 27 | + new Map<string, string>([ |
| 28 | + ['image', 'docker/binfmt:latest'], |
| 29 | + ['platforms', 'arm64,riscv64,arm'], |
| 30 | + ]), |
| 31 | + { |
| 32 | + image: 'docker/binfmt:latest', |
| 33 | + platforms: 'arm64,riscv64,arm', |
| 34 | + } as context.Inputs |
| 35 | + ], |
| 36 | + [ |
| 37 | + 2, |
| 38 | + new Map<string, string>([ |
| 39 | + ['platforms', 'arm64, riscv64, arm '], |
| 40 | + ]), |
| 41 | + { |
| 42 | + image: 'tonistiigi/binfmt:latest', |
| 43 | + platforms: 'arm64,riscv64,arm', |
| 44 | + } as context.Inputs |
| 45 | + ] |
| 46 | + ])( |
| 47 | + '[%d] given %p as inputs, returns %p', |
| 48 | + async (num: number, inputs: Map<string, string>, expected: context.Inputs) => { |
| 49 | + inputs.forEach((value: string, name: string) => { |
| 50 | + setInput(name, value); |
| 51 | + }); |
| 52 | + const res = await context.getInputs(); |
| 53 | + expect(res).toEqual(expected); |
| 54 | + } |
| 55 | + ); |
| 56 | +}); |
| 57 | + |
| 58 | +// See: https://github.com/actions/toolkit/blob/master/packages/core/src/core.ts#L67 |
| 59 | +function getInputName(name: string): string { |
| 60 | + return `INPUT_${name.replace(/ /g, '_').toUpperCase()}`; |
| 61 | +} |
| 62 | + |
| 63 | +function setInput(name: string, value: string): void { |
| 64 | + process.env[getInputName(name)] = value; |
| 65 | +} |
0 commit comments