Skip to content

Commit

Permalink
- add test case to validate the tags transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
mikepenz authored Feb 29, 2024
1 parent 0ed1caa commit 499e295
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
36 changes: 35 additions & 1 deletion __tests__/tags.test.ts
Original file line number Diff line number Diff line change
@@ -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)

Expand Down Expand Up @@ -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`)
}
})
2 changes: 1 addition & 1 deletion src/pr-collector/tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 499e295

Please sign in to comment.