@@ -9,13 +9,24 @@ import {
99} from "vitest" ;
1010
1111import { mockLoggingFunctions } from "./test-utils/logging.mock.ts" ;
12- import { getBranchName , logInfoForBranchNameResult , sleep } from "./utils.ts" ;
12+ import {
13+ createDistinctIdRegex ,
14+ escapeRegExp ,
15+ getBranchName ,
16+ logInfoForBranchNameResult ,
17+ sleep ,
18+ } from "./utils.ts" ;
1319
1420vi . mock ( "@actions/core" ) ;
1521
1622describe ( "utils" , ( ) => {
17- const { coreDebugLogMock, coreInfoLogMock, assertOnlyCalled } =
18- mockLoggingFunctions ( ) ;
23+ const {
24+ coreDebugLogMock,
25+ coreInfoLogMock,
26+ coreWarningLogMock,
27+ assertOnlyCalled,
28+ assertNoneCalled,
29+ } = mockLoggingFunctions ( ) ;
1930
2031 afterEach ( ( ) => {
2132 vi . resetAllMocks ( ) ;
@@ -187,6 +198,54 @@ describe("utils", () => {
187198 await vi . advanceTimersByTimeAsync ( 1000 ) ;
188199
189200 await expect ( sleepPromise ) . resolves . toBeUndefined ( ) ;
201+
202+ assertNoneCalled ( ) ;
203+ } ) ;
204+ } ) ;
205+
206+ describe ( "escapeRegExp" , ( ) => {
207+ const escaped = "\\^\\$\\.\\*\\+\\?\\(\\)\\[\\]\\{\\}\\|\\\\" ;
208+ const unescaped = "^$.*+?()[]{}|\\" ;
209+
210+ it ( "should escape values" , ( ) => {
211+ expect ( escapeRegExp ( unescaped + unescaped ) ) . toBe ( escaped + escaped ) ;
212+ assertNoneCalled ( ) ;
213+ } ) ;
214+
215+ it ( "should handle strings with nothing to escape" , ( ) => {
216+ expect ( escapeRegExp ( "abc" ) ) . toBe ( "abc" ) ;
217+ assertNoneCalled ( ) ;
218+ } ) ;
219+
220+ it ( "should return an empty string for empty values" , ( ) => {
221+ expect ( escapeRegExp ( "" ) ) . toEqual ( "" ) ;
222+ assertNoneCalled ( ) ;
223+ } ) ;
224+ } ) ;
225+
226+ describe ( "createDistinctIdRegex" , ( ) => {
227+ it ( "should return a regex without warning if the input is safe" , ( ) => {
228+ expect ( createDistinctIdRegex ( "test-cfg" ) ) . toStrictEqual (
229+ new RegExp ( "test-cfg" ) ,
230+ ) ;
231+ assertNoneCalled ( ) ;
232+ } ) ;
233+
234+ it ( "should return a regex with warning if the input is required escaping" , ( ) => {
235+ const input = "test$.*+?()[]{}|\\cfg" ;
236+ const escapedInput = escapeRegExp ( input ) ;
237+
238+ const distinctId = createDistinctIdRegex ( input ) ;
239+
240+ // Behaviour
241+ expect ( distinctId ) . toStrictEqual ( new RegExp ( escapedInput ) ) ;
242+
243+ // Logging
244+ assertOnlyCalled ( coreWarningLogMock ) ;
245+ expect ( coreWarningLogMock ) . toHaveBeenCalledOnce ( ) ;
246+ expect ( coreWarningLogMock . mock . calls [ 0 ] ?. [ 0 ] ) . toMatchInlineSnapshot (
247+ `"Unescaped characters found in distinctId input, using: test\\$\\.\\*\\+\\?\\(\\)\\[\\]\\{\\}\\|\\\\cfg"` ,
248+ ) ;
190249 } ) ;
191250 } ) ;
192251} ) ;
0 commit comments