diff --git a/__tests__/context.test.ts b/__tests__/context.test.ts new file mode 100644 index 0000000..f24e00e --- /dev/null +++ b/__tests__/context.test.ts @@ -0,0 +1,79 @@ +import * as context from '../src/context'; + +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']); + }); + + it('remove empty lines correctly', async () => { + setInput('foo', 'bar\n\nbaz'); + const res = await context.getInputList('foo'); + expect(res).toEqual(['bar', 'baz']); + }); + + it('handles comma correctly', async () => { + setInput('foo', 'bar,baz'); + const res = await context.getInputList('foo'); + expect(res).toEqual(['bar', 'baz']); + }); + + it('remove empty result correctly', async () => { + setInput('foo', 'bar,baz,'); + const res = await context.getInputList('foo'); + expect(res).toEqual(['bar', 'baz']); + }); + + 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', 'foo,bar\nbaz'); + const res = await context.getInputList('driver-opts', true); + expect(res).toEqual(['foo,bar', 'baz']); + }); + + it('handles different new lines and ignoring comma correctly', async () => { + setInput('driver-opts', 'foo,bar\r\nbaz'); + const res = await context.getInputList('driver-opts', true); + expect(res).toEqual(['foo,bar', 'baz']); + }); +}); + +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); + }); +}); + +// 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; +} diff --git a/dist/index.js b/dist/index.js index 72012b7..1c39295 100644 --- a/dist/index.js +++ b/dist/index.js @@ -36,7 +36,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge }); }; Object.defineProperty(exports, "__esModule", ({ value: true })); -exports.getInputs = void 0; +exports.asyncForEach = exports.getInputList = exports.getInputs = void 0; const core = __importStar(__webpack_require__(186)); function getInputs() { return __awaiter(this, void 0, void 0, function* () { @@ -50,6 +50,25 @@ function getInputs() { }); } exports.getInputs = getInputs; +function getInputList(name, ignoreComma) { + return __awaiter(this, void 0, void 0, function* () { + const items = core.getInput(name); + if (items == '') { + return []; + } + return items + .split(/\r?\n/) + .filter(x => x) + .reduce((acc, line) => acc.concat(!ignoreComma ? line.split(',').filter(x => x) : line).map(pat => pat.trim()), []); + }); +} +exports.getInputList = getInputList; +const asyncForEach = (array, callback) => __awaiter(void 0, void 0, void 0, function* () { + for (const index in array) { + yield callback(array[index], index, array); + } +}); +exports.asyncForEach = asyncForEach; //# sourceMappingURL=context.js.map /***/ }), diff --git a/src/context.ts b/src/context.ts index a19b33a..6b9e417 100644 --- a/src/context.ts +++ b/src/context.ts @@ -17,3 +17,23 @@ export async function getInputs(): Promise { cleanup: core.getBooleanInput('cleanup') }; } + +export async function getInputList(name: string, ignoreComma?: boolean): Promise { + const items = core.getInput(name); + if (items == '') { + return []; + } + return items + .split(/\r?\n/) + .filter(x => x) + .reduce( + (acc, line) => acc.concat(!ignoreComma ? line.split(',').filter(x => x) : line).map(pat => pat.trim()), + [] + ); +} + +export const asyncForEach = async (array, callback) => { + for (const index in array) { + await callback(array[index], index, array); + } +};