2022-03-21 08:43:41 -04:00
|
|
|
import {beforeEach, describe, expect, it, jest} from '@jest/globals';
|
2021-07-02 01:02:22 -04:00
|
|
|
import * as fs from 'fs';
|
2021-04-23 12:14:38 -04:00
|
|
|
import * as os from 'os';
|
2021-07-02 01:02:22 -04:00
|
|
|
import * as path from 'path';
|
2020-09-03 10:23:15 -04:00
|
|
|
import * as context from '../src/context';
|
|
|
|
|
2022-09-21 20:48:11 -04:00
|
|
|
const tmpdir = fs.mkdtempSync(path.join(os.tmpdir(), 'docker-setup-buildx-')).split(path.sep).join(path.posix.sep);
|
2021-07-02 01:02:22 -04:00
|
|
|
jest.spyOn(context, 'tmpDir').mockImplementation((): string => {
|
2022-09-21 20:48:11 -04:00
|
|
|
return tmpdir;
|
2021-07-02 01:02:22 -04:00
|
|
|
});
|
|
|
|
|
2021-09-03 16:21:20 -04:00
|
|
|
jest.spyOn(context, 'tmpNameSync').mockImplementation((): string => {
|
2022-09-21 20:48:11 -04:00
|
|
|
return path.join(tmpdir, '.tmpname').split(path.sep).join(path.posix.sep);
|
2021-09-03 16:21:20 -04:00
|
|
|
});
|
|
|
|
|
2020-09-03 10:23:15 -04:00
|
|
|
describe('getInputList', () => {
|
|
|
|
it('handles single line correctly', async () => {
|
|
|
|
await setInput('foo', 'bar');
|
|
|
|
const res = await context.getInputList('foo');
|
|
|
|
expect(res).toEqual(['bar']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handles multiple lines correctly', async () => {
|
|
|
|
setInput('foo', 'bar\nbaz');
|
|
|
|
const res = await context.getInputList('foo');
|
|
|
|
expect(res).toEqual(['bar', 'baz']);
|
|
|
|
});
|
|
|
|
|
2020-09-12 17:34:53 -04:00
|
|
|
it('remove empty lines correctly', async () => {
|
|
|
|
setInput('foo', 'bar\n\nbaz');
|
|
|
|
const res = await context.getInputList('foo');
|
|
|
|
expect(res).toEqual(['bar', 'baz']);
|
|
|
|
});
|
|
|
|
|
2020-09-03 10:23:15 -04:00
|
|
|
it('handles comma correctly', async () => {
|
|
|
|
setInput('foo', 'bar,baz');
|
|
|
|
const res = await context.getInputList('foo');
|
|
|
|
expect(res).toEqual(['bar', 'baz']);
|
|
|
|
});
|
|
|
|
|
2020-09-12 17:34:53 -04:00
|
|
|
it('remove empty result correctly', async () => {
|
|
|
|
setInput('foo', 'bar,baz,');
|
|
|
|
const res = await context.getInputList('foo');
|
|
|
|
expect(res).toEqual(['bar', 'baz']);
|
|
|
|
});
|
|
|
|
|
2020-09-03 10:23:15 -04:00
|
|
|
it('handles different new lines correctly', async () => {
|
|
|
|
setInput('foo', 'bar\r\nbaz');
|
|
|
|
const res = await context.getInputList('foo');
|
|
|
|
expect(res).toEqual(['bar', 'baz']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handles different new lines and comma correctly', async () => {
|
|
|
|
setInput('foo', 'bar\r\nbaz,bat');
|
|
|
|
const res = await context.getInputList('foo');
|
|
|
|
expect(res).toEqual(['bar', 'baz', 'bat']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handles multiple lines and ignoring comma correctly', async () => {
|
|
|
|
setInput('driver-opts', 'image=moby/buildkit:master\nnetwork=host');
|
|
|
|
const res = await context.getInputList('driver-opts', true);
|
|
|
|
expect(res).toEqual(['image=moby/buildkit:master', 'network=host']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handles different new lines and ignoring comma correctly', async () => {
|
|
|
|
setInput('driver-opts', 'image=moby/buildkit:master\r\nnetwork=host');
|
|
|
|
const res = await context.getInputList('driver-opts', true);
|
|
|
|
expect(res).toEqual(['image=moby/buildkit:master', 'network=host']);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('asyncForEach', () => {
|
|
|
|
it('executes async tasks sequentially', async () => {
|
|
|
|
const testValues = [1, 2, 3, 4, 5];
|
|
|
|
const results: number[] = [];
|
|
|
|
|
|
|
|
await context.asyncForEach(testValues, async value => {
|
|
|
|
results.push(value);
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(results).toEqual(testValues);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-04-23 12:14:38 -04:00
|
|
|
describe('setOutput', () => {
|
|
|
|
beforeEach(() => {
|
2022-03-21 08:43:41 -04:00
|
|
|
process.stdout.write = jest.fn() as typeof process.stdout.write;
|
2021-04-23 12:14:38 -04:00
|
|
|
});
|
|
|
|
|
2022-03-21 08:43:41 -04:00
|
|
|
// eslint-disable-next-line jest/expect-expect
|
2021-04-23 12:14:38 -04:00
|
|
|
it('setOutput produces the correct command', () => {
|
|
|
|
context.setOutput('some output', 'some value');
|
|
|
|
assertWriteCalls([`::set-output name=some output::some value${os.EOL}`]);
|
|
|
|
});
|
|
|
|
|
2022-03-21 08:43:41 -04:00
|
|
|
// eslint-disable-next-line jest/expect-expect
|
2021-04-23 12:14:38 -04:00
|
|
|
it('setOutput handles bools', () => {
|
|
|
|
context.setOutput('some output', false);
|
|
|
|
assertWriteCalls([`::set-output name=some output::false${os.EOL}`]);
|
|
|
|
});
|
|
|
|
|
2022-03-21 08:43:41 -04:00
|
|
|
// eslint-disable-next-line jest/expect-expect
|
2021-04-23 12:14:38 -04:00
|
|
|
it('setOutput handles numbers', () => {
|
|
|
|
context.setOutput('some output', 1.01);
|
|
|
|
assertWriteCalls([`::set-output name=some output::1.01${os.EOL}`]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-09-03 10:23:15 -04:00
|
|
|
// 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;
|
|
|
|
}
|
2021-04-23 12:14:38 -04:00
|
|
|
|
|
|
|
// Assert that process.stdout.write calls called only with the given arguments.
|
|
|
|
function assertWriteCalls(calls: string[]): void {
|
|
|
|
expect(process.stdout.write).toHaveBeenCalledTimes(calls.length);
|
|
|
|
for (let i = 0; i < calls.length; i++) {
|
|
|
|
expect(process.stdout.write).toHaveBeenNthCalledWith(i + 1, calls[i]);
|
|
|
|
}
|
|
|
|
}
|