1
0
Files
metadata-action/__tests__/context.test.ts
T

209 lines
5.7 KiB
TypeScript
Raw Normal View History

2022-03-22 21:09:00 +01:00
import {beforeEach, describe, expect, it, jest} from '@jest/globals';
2020-12-24 04:13:41 +01:00
import * as fs from 'fs';
2021-04-24 00:44:38 +02:00
import * as os from 'os';
2020-12-24 04:13:41 +01:00
import * as path from 'path';
2020-10-25 02:25:23 +01:00
import * as context from '../src/context';
2020-12-24 04:13:41 +01:00
jest.spyOn(context, 'tmpDir').mockImplementation((): string => {
2021-05-10 15:54:35 +02:00
const tmpDir = path.join('/tmp/.docker-metadata-action-jest').split(path.sep).join(path.posix.sep);
2020-12-24 04:13:41 +01:00
if (!fs.existsSync(tmpDir)) {
fs.mkdirSync(tmpDir, {recursive: true});
}
return tmpDir;
});
2020-10-25 02:25:23 +01:00
describe('getInputList', () => {
2020-12-23 22:09:38 +01:00
it('single line correctly', async () => {
2020-10-25 02:25:23 +01:00
await setInput('foo', 'bar');
2022-03-07 07:37:59 +01:00
const res = context.getInputList('foo');
2020-10-25 02:25:23 +01:00
expect(res).toEqual(['bar']);
});
2020-12-23 22:09:38 +01:00
it('multiline correctly', async () => {
2020-10-25 02:25:23 +01:00
setInput('foo', 'bar\nbaz');
2022-03-07 07:37:59 +01:00
const res = context.getInputList('foo');
2020-10-25 02:25:23 +01:00
expect(res).toEqual(['bar', 'baz']);
});
2020-12-23 22:09:38 +01:00
it('empty lines correctly', async () => {
2020-10-25 02:25:23 +01:00
setInput('foo', 'bar\n\nbaz');
2022-03-07 07:37:59 +01:00
const res = context.getInputList('foo');
2020-10-25 02:25:23 +01:00
expect(res).toEqual(['bar', 'baz']);
});
2022-03-07 07:37:59 +01:00
it('comment correctly', async () => {
setInput('foo', 'bar\n#com\n"#taken"\nhello#comment\nbaz');
const res = context.getInputList('foo');
expect(res).toEqual(['bar', '#taken', 'hello', 'baz']);
});
2020-12-23 22:09:38 +01:00
it('comma correctly', async () => {
2020-10-25 02:25:23 +01:00
setInput('foo', 'bar,baz');
2022-03-07 07:37:59 +01:00
const res = context.getInputList('foo');
2020-10-25 02:25:23 +01:00
expect(res).toEqual(['bar', 'baz']);
});
2020-12-23 22:09:38 +01:00
it('empty result correctly', async () => {
2020-10-25 02:25:23 +01:00
setInput('foo', 'bar,baz,');
2022-03-07 07:37:59 +01:00
const res = context.getInputList('foo');
2020-10-25 02:25:23 +01:00
expect(res).toEqual(['bar', 'baz']);
});
2020-12-23 22:09:38 +01:00
it('different new lines correctly', async () => {
2020-10-25 02:25:23 +01:00
setInput('foo', 'bar\r\nbaz');
2022-03-07 07:37:59 +01:00
const res = context.getInputList('foo');
2020-10-25 02:25:23 +01:00
expect(res).toEqual(['bar', 'baz']);
});
2020-12-23 22:09:38 +01:00
it('different new lines and comma correctly', async () => {
2020-10-25 02:25:23 +01:00
setInput('foo', 'bar\r\nbaz,bat');
2022-03-07 07:37:59 +01:00
const res = context.getInputList('foo');
2020-10-25 02:25:23 +01:00
expect(res).toEqual(['bar', 'baz', 'bat']);
});
2020-12-23 22:09:38 +01:00
it('multiline and ignoring comma correctly', async () => {
setInput('cache-from', 'user/app:cache\ntype=local,src=path/to/dir');
2022-03-07 07:37:59 +01:00
const res = context.getInputList('cache-from', true);
2020-12-23 22:09:38 +01:00
expect(res).toEqual(['user/app:cache', 'type=local,src=path/to/dir']);
});
it('different new lines and ignoring comma correctly', async () => {
setInput('cache-from', 'user/app:cache\r\ntype=local,src=path/to/dir');
2022-03-07 07:37:59 +01:00
const res = context.getInputList('cache-from', true);
2020-12-23 22:09:38 +01:00
expect(res).toEqual(['user/app:cache', 'type=local,src=path/to/dir']);
});
it('multiline values', async () => {
setInput(
'secrets',
`GIT_AUTH_TOKEN=abcdefgh,ijklmno=0123456789
"MYSECRET=aaaaaaaa
bbbbbbb
ccccccccc"
FOO=bar`
);
2022-03-07 07:37:59 +01:00
const res = context.getInputList('secrets', true);
2020-12-23 22:09:38 +01:00
expect(res).toEqual([
'GIT_AUTH_TOKEN=abcdefgh,ijklmno=0123456789',
`MYSECRET=aaaaaaaa
bbbbbbb
ccccccccc`,
'FOO=bar'
]);
});
it('multiline values with empty lines', async () => {
setInput(
'secrets',
`GIT_AUTH_TOKEN=abcdefgh,ijklmno=0123456789
"MYSECRET=aaaaaaaa
bbbbbbb
ccccccccc"
FOO=bar
"EMPTYLINE=aaaa
bbbb
ccc"`
);
2022-03-07 07:37:59 +01:00
const res = context.getInputList('secrets', true);
2020-12-23 22:09:38 +01:00
expect(res).toEqual([
'GIT_AUTH_TOKEN=abcdefgh,ijklmno=0123456789',
`MYSECRET=aaaaaaaa
bbbbbbb
ccccccccc`,
'FOO=bar',
`EMPTYLINE=aaaa
bbbb
ccc`
]);
});
it('multiline values without quotes', async () => {
setInput(
'secrets',
`GIT_AUTH_TOKEN=abcdefgh,ijklmno=0123456789
MYSECRET=aaaaaaaa
bbbbbbb
ccccccccc
FOO=bar`
);
2022-03-07 07:37:59 +01:00
const res = context.getInputList('secrets', true);
2020-12-23 22:09:38 +01:00
expect(res).toEqual(['GIT_AUTH_TOKEN=abcdefgh,ijklmno=0123456789', 'MYSECRET=aaaaaaaa', 'bbbbbbb', 'ccccccccc', 'FOO=bar']);
});
it('multiline values escape quotes', async () => {
setInput(
'secrets',
`GIT_AUTH_TOKEN=abcdefgh,ijklmno=0123456789
"MYSECRET=aaaaaaaa
bbbb""bbb
ccccccccc"
FOO=bar`
);
2022-03-07 07:37:59 +01:00
const res = context.getInputList('secrets', true);
2020-12-23 22:09:38 +01:00
expect(res).toEqual([
'GIT_AUTH_TOKEN=abcdefgh,ijklmno=0123456789',
`MYSECRET=aaaaaaaa
2022-03-22 21:09:00 +01:00
bbbb"bbb
2020-12-23 22:09:38 +01:00
ccccccccc`,
'FOO=bar'
]);
});
2020-10-25 02:25:23 +01:00
});
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-24 00:44:38 +02:00
describe('setOutput', () => {
beforeEach(() => {
2022-03-22 21:09:00 +01:00
process.stdout.write = jest.fn() as typeof process.stdout.write;
2021-04-24 00:44:38 +02:00
});
2022-03-22 21:09:00 +01:00
// eslint-disable-next-line jest/expect-expect
2021-04-24 00:44:38 +02: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-22 21:09:00 +01:00
// eslint-disable-next-line jest/expect-expect
2021-04-24 00:44:38 +02:00
it('setOutput handles bools', () => {
context.setOutput('some output', false);
assertWriteCalls([`::set-output name=some output::false${os.EOL}`]);
});
2022-03-22 21:09:00 +01:00
// eslint-disable-next-line jest/expect-expect
2021-04-24 00:44:38 +02:00
it('setOutput handles numbers', () => {
context.setOutput('some output', 1.01);
assertWriteCalls([`::set-output name=some output::1.01${os.EOL}`]);
});
});
2020-10-25 02:25:23 +01: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-24 00:44:38 +02: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]);
}
}