-
Notifications
You must be signed in to change notification settings - Fork 610
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4544 from iclanton/refactor
[rush] Refactor tests for CacheEntryId.
- Loading branch information
Showing
3 changed files
with
102 additions
and
121 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
common/changes/@microsoft/rush/refactor_2024-02-26-08-20.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"changes": [ | ||
{ | ||
"packageName": "@microsoft/rush", | ||
"comment": "", | ||
"type": "none" | ||
} | ||
], | ||
"packageName": "@microsoft/rush" | ||
} |
127 changes: 49 additions & 78 deletions
127
libraries/rush-lib/src/logic/buildCache/test/CacheEntryId.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,61 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. | ||
// See LICENSE in the project root for license information. | ||
|
||
import { | ||
CacheEntryId, | ||
type GetCacheEntryIdFunction, | ||
type IGenerateCacheEntryIdOptions | ||
} from '../CacheEntryId'; | ||
import { CacheEntryId, type GetCacheEntryIdFunction } from '../CacheEntryId'; | ||
|
||
describe(CacheEntryId.name, () => { | ||
describe('Valid pattern names', () => { | ||
function validatePatternMatchesSnapshot( | ||
projectName: string, | ||
pattern?: string, | ||
generateCacheEntryIdOptions?: Partial<IGenerateCacheEntryIdOptions> | ||
): void { | ||
const getCacheEntryId: GetCacheEntryIdFunction = CacheEntryId.parsePattern(pattern); | ||
expect( | ||
getCacheEntryId({ | ||
projectName, | ||
projectStateHash: '09d1ecee6d5f888fa6c35ca804b5dac7c3735ce3', | ||
phaseName: '_phase:compile', | ||
...generateCacheEntryIdOptions | ||
}) | ||
).toMatchSnapshot(pattern || 'no pattern'); | ||
} | ||
|
||
// prettier-ignore | ||
it('Handles a cache entry name for a project name without a scope', () => { | ||
const projectName: string = 'project+name'; | ||
validatePatternMatchesSnapshot(projectName); | ||
validatePatternMatchesSnapshot(projectName, '[hash]'); | ||
validatePatternMatchesSnapshot(projectName, '[projectName]_[hash]'); | ||
validatePatternMatchesSnapshot(projectName, '[phaseName:normalize]_[hash]'); | ||
validatePatternMatchesSnapshot(projectName, '[phaseName:trimPrefix]_[hash]'); | ||
validatePatternMatchesSnapshot(projectName, '[projectName:normalize]_[hash]'); | ||
validatePatternMatchesSnapshot(projectName, '[projectName:normalize]_[phaseName:normalize]_[hash]'); | ||
validatePatternMatchesSnapshot(projectName, '[projectName:normalize]_[phaseName:trimPrefix]_[hash]'); | ||
validatePatternMatchesSnapshot(projectName, 'prefix/[projectName:normalize]_[hash]'); | ||
validatePatternMatchesSnapshot(projectName, 'prefix/[projectName:normalize]_[phaseName:normalize]_[hash]'); | ||
validatePatternMatchesSnapshot(projectName, 'prefix/[projectName:normalize]_[phaseName:trimPrefix]_[hash]'); | ||
validatePatternMatchesSnapshot(projectName, 'prefix/[projectName]_[hash]'); | ||
validatePatternMatchesSnapshot(projectName, 'prefix/[projectName]_[phaseName:normalize]_[hash]'); | ||
validatePatternMatchesSnapshot(projectName, 'prefix/[projectName]_[phaseName:trimPrefix]_[hash]'); | ||
}); | ||
|
||
// prettier-ignore | ||
it('Handles a cache entry name for a project name with a scope', () => { | ||
const projectName: string = '@scope/project+name'; | ||
validatePatternMatchesSnapshot(projectName); | ||
validatePatternMatchesSnapshot(projectName, '[hash]'); | ||
validatePatternMatchesSnapshot(projectName, '[projectName]_[hash]'); | ||
validatePatternMatchesSnapshot(projectName, '[phaseName:normalize]_[hash]'); | ||
validatePatternMatchesSnapshot(projectName, '[phaseName:trimPrefix]_[hash]'); | ||
validatePatternMatchesSnapshot(projectName, '[projectName:normalize]_[hash]'); | ||
validatePatternMatchesSnapshot(projectName, '[projectName:normalize]_[phaseName:normalize]_[hash]'); | ||
validatePatternMatchesSnapshot(projectName, '[projectName:normalize]_[phaseName:trimPrefix]_[hash]'); | ||
validatePatternMatchesSnapshot(projectName, 'prefix/[projectName:normalize]_[hash]'); | ||
validatePatternMatchesSnapshot(projectName, 'prefix/[projectName:normalize]_[phaseName:normalize]_[hash]'); | ||
validatePatternMatchesSnapshot(projectName, 'prefix/[projectName:normalize]_[phaseName:trimPrefix]_[hash]'); | ||
validatePatternMatchesSnapshot(projectName, 'prefix/[projectName]_[hash]'); | ||
validatePatternMatchesSnapshot(projectName, 'prefix/[projectName]_[phaseName:normalize]_[hash]'); | ||
validatePatternMatchesSnapshot(projectName, 'prefix/[projectName]_[phaseName:trimPrefix]_[hash]'); | ||
}); | ||
describe.each([ | ||
{ projectName: 'project+name', note: 'without a scope' }, | ||
{ projectName: '@scope/project+name', note: 'with a scope' } | ||
])('For a project name $note', ({ projectName }) => | ||
it.each([ | ||
undefined, | ||
'[hash]', | ||
'[projectName]_[hash]', | ||
'[phaseName:normalize]_[hash]', | ||
'[phaseName:trimPrefix]_[hash]', | ||
'[projectName:normalize]_[hash]', | ||
'[projectName:normalize]_[phaseName:normalize]_[hash]', | ||
'[projectName:normalize]_[phaseName:trimPrefix]_[hash]', | ||
'prefix/[projectName:normalize]_[hash]', | ||
'prefix/[projectName:normalize]_[phaseName:normalize]_[hash]', | ||
'prefix/[projectName:normalize]_[phaseName:trimPrefix]_[hash]', | ||
'prefix/[projectName]_[hash]', | ||
'prefix/[projectName]_[phaseName:normalize]_[hash]', | ||
'prefix/[projectName]_[phaseName:trimPrefix]_[hash]' | ||
])('Handles pattern %s', (pattern) => { | ||
const getCacheEntryId: GetCacheEntryIdFunction = CacheEntryId.parsePattern(pattern); | ||
expect( | ||
getCacheEntryId({ | ||
projectName, | ||
projectStateHash: '09d1ecee6d5f888fa6c35ca804b5dac7c3735ce3', | ||
phaseName: '_phase:compile' | ||
}) | ||
).toMatchSnapshot(); | ||
}) | ||
); | ||
}); | ||
|
||
describe('Invalid pattern names', () => { | ||
async function validateInvalidPatternErrorMatchesSnapshotAsync(pattern: string): Promise<void> { | ||
await expect(() => CacheEntryId.parsePattern(pattern)).toThrowErrorMatchingSnapshot(); | ||
} | ||
|
||
it('Throws an exception for an invalid pattern', async () => { | ||
await validateInvalidPatternErrorMatchesSnapshotAsync('x'); | ||
await validateInvalidPatternErrorMatchesSnapshotAsync('[invalidTag]'); | ||
await validateInvalidPatternErrorMatchesSnapshotAsync('unstartedTag]'); | ||
await validateInvalidPatternErrorMatchesSnapshotAsync('[incompleteTag'); | ||
await validateInvalidPatternErrorMatchesSnapshotAsync('[hash:badAttribute]'); | ||
await validateInvalidPatternErrorMatchesSnapshotAsync('[hash:badAttribute:attr2]'); | ||
await validateInvalidPatternErrorMatchesSnapshotAsync('[projectName:badAttribute]'); | ||
await validateInvalidPatternErrorMatchesSnapshotAsync('[projectName:]'); | ||
await validateInvalidPatternErrorMatchesSnapshotAsync('[phaseName]'); | ||
await validateInvalidPatternErrorMatchesSnapshotAsync('[phaseName:]'); | ||
await validateInvalidPatternErrorMatchesSnapshotAsync('[phaseName:badAttribute]'); | ||
await validateInvalidPatternErrorMatchesSnapshotAsync('[:attr1]'); | ||
await validateInvalidPatternErrorMatchesSnapshotAsync('[projectName:attr1:attr2]'); | ||
await validateInvalidPatternErrorMatchesSnapshotAsync('/[hash]'); | ||
await validateInvalidPatternErrorMatchesSnapshotAsync('~'); | ||
it.each([ | ||
'x', | ||
'[invalidTag]', | ||
'unstartedTag]', | ||
'[incompleteTag', | ||
'[hash:badAttribute]', | ||
'[hash:badAttribute:attr2]', | ||
'[projectName:badAttribute]', | ||
'[projectName:]', | ||
'[phaseName]', | ||
'[phaseName:]', | ||
'[phaseName:badAttribute]', | ||
'[:attr1]', | ||
'[projectName:attr1:attr2]', | ||
'/[hash]', | ||
'~' | ||
])('Throws an exception for an invalid pattern (%s)', (pattern) => { | ||
expect(() => CacheEntryId.parsePattern(pattern)).toThrowErrorMatchingSnapshot(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.