From 499e2957c496e9e336ff4b705d9905471b9b46b3 Mon Sep 17 00:00:00 2001 From: Mike Penz Date: Thu, 29 Feb 2024 20:19:37 +0000 Subject: [PATCH] - add test case to validate the tags transformer --- __tests__/tags.test.ts | 36 +++++++++++++++++++++++++++++++++++- src/pr-collector/tags.ts | 2 +- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/__tests__/tags.test.ts b/__tests__/tags.test.ts index d0b3ab9..cb76d8d 100644 --- a/__tests__/tags.test.ts +++ b/__tests__/tags.test.ts @@ -1,4 +1,5 @@ -import {filterTags, prepareAndSortTags, TagInfo} from '../src/pr-collector/tags' +import { validateTransformer } from '../src/pr-collector/regexUtils' +import {filterTags, prepareAndSortTags, TagInfo, transformTags} from '../src/pr-collector/tags' jest.setTimeout(180000) @@ -145,3 +146,36 @@ it('Should filter tags correctly using the regex (inverse)', async () => { expect(filtered).toStrictEqual(`0.1.0-b01,1.0.0,1.0.0-a01,2.0.0,10.1.0,20.0.2`) }) + + +it('Should transform tags correctly using the regex', async () => { + const tags: TagInfo[] = [ + {name: 'api-0.0.1', commit: ''}, + {name: 'api-0.0.1-rc01', commit: ''}, + {name: 'config-0.1.0', commit: ''}, + {name: '0.1.0-b01', commit: ''}, + {name: '2.0.0', commit: ''}, + {name: '10.1.0', commit: ''}, + {name: 'api-10.1.0-2', commit: ''}, + {name: '20.0.2', commit: ''} + ] + + const tagResolver = { + method: 'non-existing-method', + transformer: { + pattern: '(api\-)?(.+)', + target: "$2" + } + } + + const transformer = validateTransformer(tagResolver.transformer) + if(transformer != null) { + const transformed = transformTags(tags, transformer) + .map(function (tag) { + return tag.name + }) + .join(',') + + expect(transformed).toStrictEqual(`0.0.1,0.0.1-rc01,config-0.1.0,0.1.0-b01,2.0.0,10.1.0,10.1.0-2,20.0.2`) + } +}) diff --git a/src/pr-collector/tags.ts b/src/pr-collector/tags.ts index 913a7c9..7038c46 100644 --- a/src/pr-collector/tags.ts +++ b/src/pr-collector/tags.ts @@ -211,7 +211,7 @@ export function filterTags(tags: TagInfo[], tagResolver: TagResolver): TagInfo[] /** * Helper function to transform the tag name given the transformer */ -function transformTags(tags: TagInfo[], transformer: RegexTransformer): TagInfo[] { +export function transformTags(tags: TagInfo[], transformer: RegexTransformer): TagInfo[] { return tags.map(function (tag) { if (transformer.pattern) { const transformedName = tag.name.replace(transformer.pattern, transformer.target)