2023-01-24 07:44:31 +08:00
|
|
|
import {beforeEach, describe, expect, it, jest, test} from '@jest/globals';
|
2023-01-17 18:53:57 +08:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as path from 'path';
|
|
|
|
|
2023-01-23 17:07:14 +08:00
|
|
|
import {Util} from '../src/util';
|
2023-01-17 18:53:57 +08:00
|
|
|
|
2023-01-24 07:44:31 +08:00
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
});
|
|
|
|
|
2023-01-17 18:53:57 +08:00
|
|
|
describe('getInputList', () => {
|
|
|
|
it('single line correctly', async () => {
|
|
|
|
await setInput('foo', 'bar');
|
2023-01-23 17:07:14 +08:00
|
|
|
const res = Util.getInputList('foo');
|
2023-01-17 18:53:57 +08:00
|
|
|
expect(res).toEqual(['bar']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('multiline correctly', async () => {
|
|
|
|
setInput('foo', 'bar\nbaz');
|
2023-01-23 17:07:14 +08:00
|
|
|
const res = Util.getInputList('foo');
|
2023-01-17 18:53:57 +08:00
|
|
|
expect(res).toEqual(['bar', 'baz']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('empty lines correctly', async () => {
|
|
|
|
setInput('foo', 'bar\n\nbaz');
|
2023-01-23 17:07:14 +08:00
|
|
|
const res = Util.getInputList('foo');
|
2023-01-17 18:53:57 +08:00
|
|
|
expect(res).toEqual(['bar', 'baz']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('comma correctly', async () => {
|
|
|
|
setInput('foo', 'bar,baz');
|
2023-01-23 17:07:14 +08:00
|
|
|
const res = Util.getInputList('foo');
|
2023-01-17 18:53:57 +08:00
|
|
|
expect(res).toEqual(['bar', 'baz']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('empty result correctly', async () => {
|
|
|
|
setInput('foo', 'bar,baz,');
|
2023-01-23 17:07:14 +08:00
|
|
|
const res = Util.getInputList('foo');
|
2023-01-17 18:53:57 +08:00
|
|
|
expect(res).toEqual(['bar', 'baz']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('different new lines correctly', async () => {
|
|
|
|
setInput('foo', 'bar\r\nbaz');
|
2023-01-23 17:07:14 +08:00
|
|
|
const res = Util.getInputList('foo');
|
2023-01-17 18:53:57 +08:00
|
|
|
expect(res).toEqual(['bar', 'baz']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('different new lines and comma correctly', async () => {
|
|
|
|
setInput('foo', 'bar\r\nbaz,bat');
|
2023-01-23 17:07:14 +08:00
|
|
|
const res = Util.getInputList('foo');
|
2023-01-17 18:53:57 +08:00
|
|
|
expect(res).toEqual(['bar', 'baz', 'bat']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('multiline and ignoring comma correctly', async () => {
|
|
|
|
setInput('cache-from', 'user/app:cache\ntype=local,src=path/to/dir');
|
2023-01-23 17:07:14 +08:00
|
|
|
const res = Util.getInputList('cache-from', true);
|
2023-01-17 18:53:57 +08: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');
|
2023-01-23 17:07:14 +08:00
|
|
|
const res = Util.getInputList('cache-from', true);
|
2023-01-17 18:53:57 +08: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`
|
|
|
|
);
|
2023-01-23 17:07:14 +08:00
|
|
|
const res = Util.getInputList('secrets', true);
|
2023-01-17 18:53:57 +08: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"`
|
|
|
|
);
|
2023-01-23 17:07:14 +08:00
|
|
|
const res = Util.getInputList('secrets', true);
|
2023-01-17 18:53:57 +08: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`
|
|
|
|
);
|
2023-01-23 17:07:14 +08:00
|
|
|
const res = Util.getInputList('secrets', true);
|
2023-01-17 18:53:57 +08:00
|
|
|
expect(res).toEqual(['GIT_AUTH_TOKEN=abcdefgh,ijklmno=0123456789', 'MYSECRET=aaaaaaaa', 'bbbbbbb', 'ccccccccc', 'FOO=bar']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('large multiline values', async () => {
|
|
|
|
const pgp = fs.readFileSync(path.join(__dirname, 'fixtures', 'pgp.txt'), {encoding: 'utf-8'});
|
|
|
|
setInput(
|
|
|
|
'secrets',
|
|
|
|
`"GPG_KEY=${pgp}"
|
|
|
|
FOO=bar`
|
|
|
|
);
|
2023-01-23 17:07:14 +08:00
|
|
|
const res = Util.getInputList('secrets', true);
|
2023-01-17 18:53:57 +08:00
|
|
|
expect(res).toEqual([`GPG_KEY=${pgp}`, 'FOO=bar']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('multiline values escape quotes', async () => {
|
|
|
|
setInput(
|
|
|
|
'secrets',
|
|
|
|
`GIT_AUTH_TOKEN=abcdefgh,ijklmno=0123456789
|
|
|
|
"MYSECRET=aaaaaaaa
|
|
|
|
bbbb""bbb
|
|
|
|
ccccccccc"
|
|
|
|
FOO=bar`
|
|
|
|
);
|
2023-01-23 17:07:14 +08:00
|
|
|
const res = Util.getInputList('secrets', true);
|
2023-01-17 18:53:57 +08:00
|
|
|
expect(res).toEqual([
|
|
|
|
'GIT_AUTH_TOKEN=abcdefgh,ijklmno=0123456789',
|
|
|
|
`MYSECRET=aaaaaaaa
|
|
|
|
bbbb"bbb
|
|
|
|
ccccccccc`,
|
|
|
|
'FOO=bar'
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('asyncForEach', () => {
|
|
|
|
it('executes async tasks sequentially', async () => {
|
|
|
|
const testValues = [1, 2, 3, 4, 5];
|
|
|
|
const results: number[] = [];
|
|
|
|
|
2023-01-23 17:07:14 +08:00
|
|
|
await Util.asyncForEach(testValues, async value => {
|
2023-01-17 18:53:57 +08:00
|
|
|
results.push(value);
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(results).toEqual(testValues);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('isValidUrl', () => {
|
|
|
|
test.each([
|
|
|
|
['https://github.com/docker/buildx.git', true],
|
|
|
|
['https://github.com/docker/buildx.git#refs/pull/648/head', true],
|
|
|
|
['v0.4.1', false]
|
|
|
|
])('given %p', async (url, expected) => {
|
2023-01-23 17:07:14 +08:00
|
|
|
expect(Util.isValidUrl(url)).toEqual(expected);
|
2023-01-17 18:53:57 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// See: https://github.com/actions/toolkit/blob/a1b068ec31a042ff1e10a522d8fdf0b8869d53ca/packages/core/src/core.ts#L89
|
|
|
|
function getInputName(name: string): string {
|
|
|
|
return `INPUT_${name.replace(/ /g, '_').toUpperCase()}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setInput(name: string, value: string): void {
|
|
|
|
process.env[getInputName(name)] = value;
|
|
|
|
}
|