From b171e9c8ea08b2f5432d485d2944d3ea57201eb5 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Sat, 27 Mar 2021 12:47:35 +0100 Subject: [PATCH] Tests --- __tests__/meta.test.ts | 214 ++++++++++++++++++++++++++++++++++++++++- __tests__/tag.test.ts | 30 ++++-- dist/index.js | 29 +++--- src/meta.ts | 21 ++-- src/tag.ts | 4 +- 5 files changed, 252 insertions(+), 46 deletions(-) diff --git a/__tests__/meta.test.ts b/__tests__/meta.test.ts index 897a322..b77fec1 100644 --- a/__tests__/meta.test.ts +++ b/__tests__/meta.test.ts @@ -88,6 +88,9 @@ describe('null', () => { 'event_empty.env', { images: ['user/app'], + tags: [ + `type=sha` + ] } as Inputs, { main: undefined, @@ -462,7 +465,37 @@ describe('push', () => { "org.opencontainers.image.revision=90dd6032fac8bda1b6c4436a2e65de27961ed071", "org.opencontainers.image.licenses=MIT" ] - ] + ], + [ + 'push13', + 'event_push_defbranch.env', + { + images: ['user/app'], + tags: [ + `type=ref,priority=2000,event=branch`, + `type=edge` + ], + } as Inputs, + { + main: 'master', + partial: ['edge'], + latest: false + } as Version, + [ + 'user/app:master', + 'user/app:edge' + ], + [ + "org.opencontainers.image.title=Hello-World", + "org.opencontainers.image.description=This your first repo!", + "org.opencontainers.image.url=https://github.com/octocat/Hello-World", + "org.opencontainers.image.source=https://github.com/octocat/Hello-World", + "org.opencontainers.image.version=master", + "org.opencontainers.image.created=2020-01-10T00:30:00.000Z", + "org.opencontainers.image.revision=90dd6032fac8bda1b6c4436a2e65de27961ed071", + "org.opencontainers.image.licenses=MIT" + ] + ], ])('given %p with %p event', tagsLabelsTest); }); @@ -713,7 +746,7 @@ describe('tag', () => { { images: ['org/app', 'ghcr.io/user/app'], tags: [ - `type=match,"pattern=^v(\\d{1,3}.\\d{1,3}.\\d{1,3})$",group=1` + `type=match,"pattern=/^v(\\d{1,3}.\\d{1,3}.\\d{1,3})$/ig",group=1`, ] } as Inputs, { @@ -897,6 +930,74 @@ describe('tag', () => { "org.opencontainers.image.revision=90dd6032fac8bda1b6c4436a2e65de27961ed071", "org.opencontainers.image.licenses=MIT" ] + ], + [ + 'tag15', + 'event_tag_v2.0.8-beta.67.env', + { + images: ['org/app', 'ghcr.io/user/app'], + tags: [ + `type=raw,priority=2000,foo`, + `type=semver,pattern={{version}}`, + `type=match,"pattern=\\d{1,3}.\\d{1,3}"` + ] + } as Inputs, + { + main: 'foo', + partial: ['2.0.8-beta.67', '2.0'], + latest: false + } as Version, + [ + 'org/app:foo', + 'org/app:2.0.8-beta.67', + 'org/app:2.0', + 'ghcr.io/user/app:foo', + 'ghcr.io/user/app:2.0.8-beta.67', + 'ghcr.io/user/app:2.0' + ], + [ + "org.opencontainers.image.title=Hello-World", + "org.opencontainers.image.description=This your first repo!", + "org.opencontainers.image.url=https://github.com/octocat/Hello-World", + "org.opencontainers.image.source=https://github.com/octocat/Hello-World", + "org.opencontainers.image.version=foo", + "org.opencontainers.image.created=2020-01-10T00:30:00.000Z", + "org.opencontainers.image.revision=90dd6032fac8bda1b6c4436a2e65de27961ed071", + "org.opencontainers.image.licenses=MIT" + ] + ], + [ + 'tag16', + 'event_tag_v1.1.1.env', + { + images: ['org/app', 'ghcr.io/user/app'], + tags: [ + `type=raw,priority=2000,foo`, + `type=ref,event=tag`, + `type=edge` + ] + } as Inputs, + { + main: 'foo', + partial: ['v1.1.1'], + latest: false + } as Version, + [ + 'org/app:foo', + 'org/app:v1.1.1', + 'ghcr.io/user/app:foo', + 'ghcr.io/user/app:v1.1.1', + ], + [ + "org.opencontainers.image.title=Hello-World", + "org.opencontainers.image.description=This your first repo!", + "org.opencontainers.image.url=https://github.com/octocat/Hello-World", + "org.opencontainers.image.source=https://github.com/octocat/Hello-World", + "org.opencontainers.image.version=foo", + "org.opencontainers.image.created=2020-01-10T00:30:00.000Z", + "org.opencontainers.image.revision=90dd6032fac8bda1b6c4436a2e65de27961ed071", + "org.opencontainers.image.licenses=MIT" + ] ] ])('given %p with %p event', tagsLabelsTest); }); @@ -1274,6 +1375,38 @@ describe('pr', () => { "org.opencontainers.image.licenses=MIT" ] ], + [ + 'pr04', + 'event_pull_request.env', + { + images: ['org/app', 'ghcr.io/user/app'], + tags: [ + `type=sha,priority=2000`, + `type=ref,event=pr` + ] + } as Inputs, + { + main: 'sha-1e9249f', + partial: ['pr-2'], + latest: false + } as Version, + [ + 'org/app:sha-1e9249f', + 'org/app:pr-2', + 'ghcr.io/user/app:sha-1e9249f', + 'ghcr.io/user/app:pr-2' + ], + [ + "org.opencontainers.image.title=Hello-World", + "org.opencontainers.image.description=This your first repo!", + "org.opencontainers.image.url=https://github.com/octocat/Hello-World", + "org.opencontainers.image.source=https://github.com/octocat/Hello-World", + "org.opencontainers.image.version=sha-1e9249f", + "org.opencontainers.image.created=2020-01-10T00:30:00.000Z", + "org.opencontainers.image.revision=1e9249f05bfc090e0688b8fb9c1b347586add504", + "org.opencontainers.image.licenses=MIT" + ] + ], ])('given %p with %p event', tagsLabelsTest); }); @@ -1746,6 +1879,80 @@ describe('raw', () => { "org.opencontainers.image.revision=90dd6032fac8bda1b6c4436a2e65de27961ed071", "org.opencontainers.image.licenses=MIT" ] + ], + [ + 'raw08', + 'event_push.env', + { + images: ['user/app'], + tags: [ + `type=match,pattern=\\d{8}`, + `type=raw,my`, + `type=raw,custom`, + `type=raw,tags` + ], + flavor: [ + `latest=false` + ] + } as Inputs, + { + main: 'my', + partial: ['custom', 'tags'], + latest: false + } as Version, + [ + 'user/app:my', + 'user/app:custom', + 'user/app:tags' + ], + [ + "org.opencontainers.image.title=Hello-World", + "org.opencontainers.image.description=This your first repo!", + "org.opencontainers.image.url=https://github.com/octocat/Hello-World", + "org.opencontainers.image.source=https://github.com/octocat/Hello-World", + "org.opencontainers.image.version=my", + "org.opencontainers.image.created=2020-01-10T00:30:00.000Z", + "org.opencontainers.image.revision=90dd6032fac8bda1b6c4436a2e65de27961ed071", + "org.opencontainers.image.licenses=MIT" + ] + ], + [ + 'raw09', + 'event_push.env', + { + images: ['user/app'], + tags: [ + `type=match,pattern=\\d{8}`, + `type=raw,my,prefix=foo-,suffix=-bar`, + `type=raw,custom`, + `type=raw,tags` + ], + flavor: [ + `latest=false`, + `prefix=glo-`, + `suffix=-bal` + ] + } as Inputs, + { + main: 'foo-my-bar', + partial: ['glo-custom-bal', 'glo-tags-bal'], + latest: false + } as Version, + [ + 'user/app:foo-my-bar', + 'user/app:glo-custom-bal', + 'user/app:glo-tags-bal' + ], + [ + "org.opencontainers.image.title=Hello-World", + "org.opencontainers.image.description=This your first repo!", + "org.opencontainers.image.url=https://github.com/octocat/Hello-World", + "org.opencontainers.image.source=https://github.com/octocat/Hello-World", + "org.opencontainers.image.version=foo-my-bar", + "org.opencontainers.image.created=2020-01-10T00:30:00.000Z", + "org.opencontainers.image.revision=90dd6032fac8bda1b6c4436a2e65de27961ed071", + "org.opencontainers.image.licenses=MIT" + ] ] ])('given %p wth %p event', tagsLabelsTest); }); @@ -1763,6 +1970,9 @@ describe('bake', () => { `type=raw,my`, `type=raw,custom`, `type=raw,tags` + ], + labels: [ + "invalid" ] } as Inputs, { diff --git a/__tests__/tag.test.ts b/__tests__/tag.test.ts index 299b25b..bb2d827 100644 --- a/__tests__/tag.test.ts +++ b/__tests__/tag.test.ts @@ -140,11 +140,6 @@ describe('parse', () => { } as Tag, false ], - [ - `type=semver`, - {} as Tag, - true - ], [ `type=semver,enable=true,pattern={{version}}`, { @@ -173,11 +168,6 @@ describe('parse', () => { } as Tag, false ], - [ - `type=match`, - {} as Tag, - true - ], [ `type=match,enable=true,pattern=v(.*),group=1`, { @@ -407,6 +397,26 @@ describe('parse', () => { } } as Tag, false + ], + [ + `type=semver`, + {} as Tag, + true + ], + [ + `type=match`, + {} as Tag, + true + ], + [ + `type=foo`, + {} as Tag, + true + ], + [ + `type=sha,enable=foo`, + {} as Tag, + true ] ])('given %p event ', async (s: string, expected: Tag, invalid: boolean) => { try { diff --git a/dist/index.js b/dist/index.js index 56aa382..140f1ae 100644 --- a/dist/index.js +++ b/dist/index.js @@ -365,7 +365,15 @@ class Meta { break; } case tcl.Type.Ref: { - version = this.procRef(version, tag); + if (tag.attrs['event'] == tcl.RefEvent.Branch) { + version = this.procRefBranch(version, tag); + } + else if (tag.attrs['event'] == tcl.RefEvent.Tag) { + version = this.procRefTag(version, tag); + } + else if (tag.attrs['event'] == tcl.RefEvent.PR) { + version = this.procRefPr(version, tag); + } break; } case tcl.Type.Edge: { @@ -475,18 +483,6 @@ class Meta { } return version; } - procRef(version, tag) { - if (tag.attrs['event'] == tcl.RefEvent.Branch) { - return this.procRefBranch(version, tag); - } - else if (tag.attrs['event'] == tcl.RefEvent.Tag) { - return this.procRefTag(version, tag); - } - else if (tag.attrs['event'] == tcl.RefEvent.PR) { - return this.procRefPr(version, tag); - } - return version; - } procRefBranch(version, tag) { if (!/^refs\/heads\//.test(this.context.ref)) { return version; @@ -598,7 +594,7 @@ class Meta { val = `${val}${tag.attrs['suffix']}`; } else if (this.flavor.suffix.length > 0) { - val = `${this.flavor.suffix}${val}`; + val = `${val}${this.flavor.suffix}`; } return val; } @@ -738,7 +734,7 @@ function Parse(s) { if (parts.length == 1) { tag.attrs['value'] = parts[0].trim(); } - else if (parts.length == 2) { + else { const key = parts[0].trim().toLowerCase(); const value = parts[1].trim(); switch (key) { @@ -755,9 +751,6 @@ function Parse(s) { } } } - else { - throw new Error(`Invalid entry: ${field}`); - } } if (tag.type == undefined) { tag.type = Type.Raw; diff --git a/src/meta.ts b/src/meta.ts index e8e85c3..018fbe8 100644 --- a/src/meta.ts +++ b/src/meta.ts @@ -58,7 +58,13 @@ export class Meta { break; } case tcl.Type.Ref: { - version = this.procRef(version, tag); + if (tag.attrs['event'] == tcl.RefEvent.Branch) { + version = this.procRefBranch(version, tag); + } else if (tag.attrs['event'] == tcl.RefEvent.Tag) { + version = this.procRefTag(version, tag); + } else if (tag.attrs['event'] == tcl.RefEvent.PR) { + version = this.procRefPr(version, tag); + } break; } case tcl.Type.Edge: { @@ -176,17 +182,6 @@ export class Meta { return version; } - private procRef(version: Version, tag: tcl.Tag): Version { - if (tag.attrs['event'] == tcl.RefEvent.Branch) { - return this.procRefBranch(version, tag); - } else if (tag.attrs['event'] == tcl.RefEvent.Tag) { - return this.procRefTag(version, tag); - } else if (tag.attrs['event'] == tcl.RefEvent.PR) { - return this.procRefPr(version, tag); - } - return version; - } - private procRefBranch(version: Version, tag: tcl.Tag): Version { if (!/^refs\/heads\//.test(this.context.ref)) { return version; @@ -308,7 +303,7 @@ export class Meta { if (tag.attrs['suffix'].length > 0) { val = `${val}${tag.attrs['suffix']}`; } else if (this.flavor.suffix.length > 0) { - val = `${this.flavor.suffix}${val}`; + val = `${val}${this.flavor.suffix}`; } return val; } diff --git a/src/tag.ts b/src/tag.ts index 8560146..88ebe4e 100644 --- a/src/tag.ts +++ b/src/tag.ts @@ -70,7 +70,7 @@ export function Parse(s: string): Tag { const parts = field.toString().split('=', 2); if (parts.length == 1) { tag.attrs['value'] = parts[0].trim(); - } else if (parts.length == 2) { + } else { const key = parts[0].trim().toLowerCase(); const value = parts[1].trim(); switch (key) { @@ -86,8 +86,6 @@ export function Parse(s: string): Tag { break; } } - } else { - throw new Error(`Invalid entry: ${field}`); } }