3
0
Fork 0
mirror of https://gitea.com/docker/setup-qemu-action.git synced 2024-11-22 17:39:31 +01:00
docker-setup-qemu/__tests__/context.test.ts
CrazyMax 4f3cb031e7
cache-image input to enable/disable caching of binfmt image
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
2024-04-12 19:21:11 +02:00

72 lines
1.8 KiB
TypeScript

import {beforeEach, describe, expect, test} from '@jest/globals';
import * as context from '../src/context';
describe('getInputs', () => {
beforeEach(() => {
process.env = Object.keys(process.env).reduce((object, key) => {
if (!key.startsWith('INPUT_')) {
object[key] = process.env[key];
}
return object;
}, {});
});
// prettier-ignore
test.each([
[
0,
new Map<string, string>([
['cache-image', 'true'],
]),
{
image: 'tonistiigi/binfmt:latest',
platforms: 'all',
cacheImage: true,
} as context.Inputs
],
[
1,
new Map<string, string>([
['image', 'docker/binfmt:latest'],
['platforms', 'arm64,riscv64,arm'],
['cache-image', 'false'],
]),
{
image: 'docker/binfmt:latest',
platforms: 'arm64,riscv64,arm',
cacheImage: false,
} as context.Inputs
],
[
2,
new Map<string, string>([
['platforms', 'arm64, riscv64, arm '],
['cache-image', 'true'],
]),
{
image: 'tonistiigi/binfmt:latest',
platforms: 'arm64,riscv64,arm',
cacheImage: true,
} as context.Inputs
]
])(
'[%d] given %p as inputs, returns %p',
async (num: number, inputs: Map<string, string>, expected: context.Inputs) => {
inputs.forEach((value: string, name: string) => {
setInput(name, value);
});
const res = await context.getInputs();
expect(res).toEqual(expected);
}
);
});
// See: https://github.com/actions/toolkit/blob/master/packages/core/src/core.ts#L67
function getInputName(name: string): string {
return `INPUT_${name.replace(/ /g, '_').toUpperCase()}`;
}
function setInput(name: string, value: string): void {
process.env[getInputName(name)] = value;
}