From 1068b75f65a4ceb5d775837cb23e3216c4525d50 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Mon, 7 Mar 2022 07:37:59 +0100 Subject: [PATCH] handle comments for multi-line inputs Signed-off-by: CrazyMax --- README.md | 8 ++++---- __tests__/context.test.ts | 32 +++++++++++++++++++------------- dist/index.js | 1 + src/context.ts | 1 + 4 files changed, 25 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 092ad3c..ee5522a 100644 --- a/README.md +++ b/README.md @@ -359,7 +359,7 @@ tags: | Will be used on [schedule event](https://docs.github.com/en/actions/reference/events-that-trigger-workflows#schedule). -`pattern` is a specially crafted attribute to support [Handlebars template](https://handlebarsjs.com/guide/) with +`pattern` is a specially crafted attribute to support [Handlebars' template](https://handlebarsjs.com/guide/) with the following expressions: * `date 'format'` ; render date by its [moment format](https://momentjs.com/docs/#/displaying/format/) @@ -478,7 +478,7 @@ tags: | ``` Can create a regular expression for matching Git tag with a pattern and capturing group. Will be used on a -[push tag event](https://docs.github.com/en/actions/reference/events-that-trigger-workflows#push) but you can also use +[push tag event](https://docs.github.com/en/actions/reference/events-that-trigger-workflows#push) but, you can also use a custom value through `value` attribute. | Git tag | Pattern | Group | Output | @@ -615,7 +615,7 @@ tags: | ### Global expressions -The following [Handlebars template](https://handlebarsjs.com/guide/) expressions for `prefix`, `suffix` and `value` +The following [Handlebars' template](https://handlebarsjs.com/guide/) expressions for `prefix`, `suffix` and `value` attributes are available: | Expression | Output | @@ -679,7 +679,7 @@ workflow using the [`fromJSON` function](https://docs.github.com/en/actions/lear ### Overwrite labels -If some of the [OCI Image Format Specification](https://github.com/opencontainers/image-spec/blob/master/annotations.md) +If some [OCI Image Format Specification](https://github.com/opencontainers/image-spec/blob/master/annotations.md) labels generated are not suitable, you can overwrite them like this: ```yaml diff --git a/__tests__/context.test.ts b/__tests__/context.test.ts index 89fca48..52fcd12 100644 --- a/__tests__/context.test.ts +++ b/__tests__/context.test.ts @@ -15,55 +15,61 @@ jest.spyOn(context, 'tmpDir').mockImplementation((): string => { describe('getInputList', () => { it('single line correctly', async () => { await setInput('foo', 'bar'); - const res = await context.getInputList('foo'); + const res = context.getInputList('foo'); expect(res).toEqual(['bar']); }); it('multiline correctly', async () => { setInput('foo', 'bar\nbaz'); - const res = await context.getInputList('foo'); + const res = context.getInputList('foo'); expect(res).toEqual(['bar', 'baz']); }); it('empty lines correctly', async () => { setInput('foo', 'bar\n\nbaz'); - const res = await context.getInputList('foo'); + const res = context.getInputList('foo'); expect(res).toEqual(['bar', 'baz']); }); + 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']); + }); + it('comma correctly', async () => { setInput('foo', 'bar,baz'); - const res = await context.getInputList('foo'); + const res = context.getInputList('foo'); expect(res).toEqual(['bar', 'baz']); }); it('empty result correctly', async () => { setInput('foo', 'bar,baz,'); - const res = await context.getInputList('foo'); + const res = context.getInputList('foo'); expect(res).toEqual(['bar', 'baz']); }); it('different new lines correctly', async () => { setInput('foo', 'bar\r\nbaz'); - const res = await context.getInputList('foo'); + const res = context.getInputList('foo'); expect(res).toEqual(['bar', 'baz']); }); it('different new lines and comma correctly', async () => { setInput('foo', 'bar\r\nbaz,bat'); - const res = await context.getInputList('foo'); + const res = context.getInputList('foo'); 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'); - const res = await context.getInputList('cache-from', true); + const res = context.getInputList('cache-from', true); 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'); - const res = await context.getInputList('cache-from', true); + const res = context.getInputList('cache-from', true); expect(res).toEqual(['user/app:cache', 'type=local,src=path/to/dir']); }); @@ -76,7 +82,7 @@ bbbbbbb ccccccccc" FOO=bar` ); - const res = await context.getInputList('secrets', true); + const res = context.getInputList('secrets', true); expect(res).toEqual([ 'GIT_AUTH_TOKEN=abcdefgh,ijklmno=0123456789', `MYSECRET=aaaaaaaa @@ -99,7 +105,7 @@ FOO=bar bbbb ccc"` ); - const res = await context.getInputList('secrets', true); + const res = context.getInputList('secrets', true); expect(res).toEqual([ 'GIT_AUTH_TOKEN=abcdefgh,ijklmno=0123456789', `MYSECRET=aaaaaaaa @@ -122,7 +128,7 @@ bbbbbbb ccccccccc FOO=bar` ); - const res = await context.getInputList('secrets', true); + const res = context.getInputList('secrets', true); expect(res).toEqual(['GIT_AUTH_TOKEN=abcdefgh,ijklmno=0123456789', 'MYSECRET=aaaaaaaa', 'bbbbbbb', 'ccccccccc', 'FOO=bar']); }); @@ -135,7 +141,7 @@ bbbb""bbb ccccccccc" FOO=bar` ); - const res = await context.getInputList('secrets', true); + const res = context.getInputList('secrets', true); expect(res).toEqual([ 'GIT_AUTH_TOKEN=abcdefgh,ijklmno=0123456789', `MYSECRET=aaaaaaaa diff --git a/dist/index.js b/dist/index.js index de6d333..77ff8e5 100644 --- a/dist/index.js +++ b/dist/index.js @@ -75,6 +75,7 @@ function getInputList(name, ignoreComma) { for (let output of sync_1.default(items, { columns: false, relax: true, + comment: '#', relaxColumnCount: true, skipLinesWithEmptyValues: true })) { diff --git a/src/context.ts b/src/context.ts index 4fd5e54..7bde054 100644 --- a/src/context.ts +++ b/src/context.ts @@ -49,6 +49,7 @@ export function getInputList(name: string, ignoreComma?: boolean): string[] { for (let output of csvparse(items, { columns: false, relax: true, + comment: '#', relaxColumnCount: true, skipLinesWithEmptyValues: true }) as Array) {