Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions __tests__/context.test.ts
Original file line number Diff line number Diff line change
@@ -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;
}
21 changes: 20 additions & 1 deletion dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,23 @@ export async function getInputs(): Promise<Inputs> {
cleanup: core.getBooleanInput('cleanup')
};
}

export async function getInputList(name: string, ignoreComma?: boolean): Promise<string[]> {
const items = core.getInput(name);
if (items == '') {
return [];
}
return items
.split(/\r?\n/)
.filter(x => x)
.reduce<string[]>(
(acc, line) => acc.concat(!ignoreComma ? line.split(',').filter(x => x) : line).map(pat => pat.trim()),
[]
);
Comment on lines +22 to +32
Copy link
Copy Markdown
Contributor

@joaofnds joaofnds Apr 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const items = core.getInput(name);
if (items == '') {
return [];
}
return items
.split(/\r?\n/)
.filter(x => x)
.reduce<string[]>(
(acc, line) => acc.concat(!ignoreComma ? line.split(',').filter(x => x) : line).map(pat => pat.trim()),
[]
);
const items = core.getInput(name);
if (items == '') {
return [];
}
let splitCharSet = '\r\n'
if (!ignoreComma) splitCharSet += ','
return items
.split(new RegExp(`[${splitCharSet}]`))
.filter(x => x)
.map(pat => pat.trim());

}

export const asyncForEach = async (array, callback) => {
for (const index in array) {
await callback(array[index], index, array);
}
};