1- import * as cache from "@actions/cache" ;
2- import * as core from "@actions/core" ;
3-
4- import { Events , RefKey } from "../src/constants" ;
5- import * as actionUtils from "../src/utils/actionUtils" ;
6- import * as testUtils from "../src/utils/testUtils" ;
1+ import { jest , test , expect , beforeEach , afterAll } from "@jest/globals" ;
2+
3+ // Mock @actions/core
4+ jest . unstable_mockModule ( "@actions/core" , ( ) => ( {
5+ getInput : jest . fn ( ( name : string , options ?: { required ?: boolean } ) => {
6+ const val =
7+ process . env [ `INPUT_${ name . replace ( / / g, "_" ) . toUpperCase ( ) } ` ] || "" ;
8+ if ( options && options . required && ! val ) {
9+ throw new Error ( `Input required and not supplied: ${ name } ` ) ;
10+ }
11+ return val . trim ( ) ;
12+ } ) ,
13+ setOutput : jest . fn ( ) ,
14+ setFailed : jest . fn ( ) ,
15+ info : jest . fn ( ) ,
16+ warning : jest . fn ( ) ,
17+ debug : jest . fn ( ) ,
18+ error : jest . fn ( ) ,
19+ saveState : jest . fn ( ) ,
20+ getState : jest . fn ( ( ) => "" ) ,
21+ isDebug : jest . fn ( ( ) => false ) ,
22+ exportVariable : jest . fn ( ) ,
23+ addPath : jest . fn ( ) ,
24+ group : jest . fn ( ( name : string , fn : ( ) => Promise < unknown > ) => fn ( ) ) ,
25+ startGroup : jest . fn ( ) ,
26+ endGroup : jest . fn ( )
27+ } ) ) ;
28+
29+ // Mock @actions/cache
30+ jest . unstable_mockModule ( "@actions/cache" , ( ) => ( {
31+ restoreCache : jest . fn ( ) ,
32+ saveCache : jest . fn ( ) ,
33+ isFeatureAvailable : jest . fn ( ( ) => true ) ,
34+ ReserveCacheError : class ReserveCacheError extends Error {
35+ constructor ( message : string ) {
36+ super ( message ) ;
37+ this . name = "ReserveCacheError" ;
38+ }
39+ }
40+ } ) ) ;
741
8- jest . mock ( "@actions/core" ) ;
9- jest . mock ( "@actions/cache" ) ;
42+ const core = await import ( "@actions/core" ) ;
43+ const cache = await import ( "@actions/cache" ) ;
44+ const { Events, RefKey } = await import ( "../src/constants" ) ;
45+ const actionUtils = await import ( "../src/utils/actionUtils" ) ;
46+ const testUtils = await import ( "../src/utils/testUtils" ) ;
1047
1148let pristineEnv : NodeJS . ProcessEnv ;
1249
13- beforeAll ( ( ) => {
14- pristineEnv = process . env ;
15- jest . spyOn ( core , "getInput" ) . mockImplementation ( ( name , options ) => {
16- return jest . requireActual ( "@actions/core" ) . getInput ( name , options ) ;
17- } ) ;
18- } ) ;
19-
2050beforeEach ( ( ) => {
21- jest . resetModules ( ) ;
22- process . env = pristineEnv ;
51+ pristineEnv = { ...process . env } ;
52+ jest . clearAllMocks ( ) ;
53+ ( core . getInput as jest . Mock ) . mockImplementation (
54+ ( name : string , options ?: { required ?: boolean } ) => {
55+ const val =
56+ process . env [
57+ `INPUT_${ name . replace ( / / g, "_" ) . toUpperCase ( ) } `
58+ ] || "" ;
59+ if ( options && options . required && ! val ) {
60+ throw new Error (
61+ `Input required and not supplied: ${ name } `
62+ ) ;
63+ }
64+ return val . trim ( ) ;
65+ }
66+ ) ;
2367 delete process . env [ Events . Key ] ;
2468 delete process . env [ RefKey ] ;
2569} ) ;
@@ -47,74 +91,44 @@ test("isGhes returns false when server url is github.com", () => {
4791} ) ;
4892
4993test ( "isExactKeyMatch with undefined cache key returns false" , ( ) => {
50- const key = "linux-rust" ;
51- const cacheKey = undefined ;
52-
53- expect ( actionUtils . isExactKeyMatch ( key , cacheKey ) ) . toBe ( false ) ;
94+ expect ( actionUtils . isExactKeyMatch ( "linux-rust" , undefined ) ) . toBe ( false ) ;
5495} ) ;
5596
5697test ( "isExactKeyMatch with empty cache key returns false" , ( ) => {
57- const key = "linux-rust" ;
58- const cacheKey = "" ;
59-
60- expect ( actionUtils . isExactKeyMatch ( key , cacheKey ) ) . toBe ( false ) ;
98+ expect ( actionUtils . isExactKeyMatch ( "linux-rust" , "" ) ) . toBe ( false ) ;
6199} ) ;
62100
63101test ( "isExactKeyMatch with different keys returns false" , ( ) => {
64- const key = "linux-rust" ;
65- const cacheKey = "linux-" ;
66-
67- expect ( actionUtils . isExactKeyMatch ( key , cacheKey ) ) . toBe ( false ) ;
102+ expect ( actionUtils . isExactKeyMatch ( "linux-rust" , "linux-" ) ) . toBe ( false ) ;
68103} ) ;
69104
70105test ( "isExactKeyMatch with different key accents returns false" , ( ) => {
71- const key = "linux-áccent" ;
72- const cacheKey = "linux-accent" ;
73-
74- expect ( actionUtils . isExactKeyMatch ( key , cacheKey ) ) . toBe ( false ) ;
106+ expect ( actionUtils . isExactKeyMatch ( "linux-áccent" , "linux-accent" ) ) . toBe ( false ) ;
75107} ) ;
76108
77109test ( "isExactKeyMatch with same key returns true" , ( ) => {
78- const key = "linux-rust" ;
79- const cacheKey = "linux-rust" ;
80-
81- expect ( actionUtils . isExactKeyMatch ( key , cacheKey ) ) . toBe ( true ) ;
110+ expect ( actionUtils . isExactKeyMatch ( "linux-rust" , "linux-rust" ) ) . toBe ( true ) ;
82111} ) ;
83112
84113test ( "isExactKeyMatch with same key and different casing returns true" , ( ) => {
85- const key = "linux-rust" ;
86- const cacheKey = "LINUX-RUST" ;
87-
88- expect ( actionUtils . isExactKeyMatch ( key , cacheKey ) ) . toBe ( true ) ;
114+ expect ( actionUtils . isExactKeyMatch ( "linux-rust" , "LINUX-RUST" ) ) . toBe ( true ) ;
89115} ) ;
90116
91117test ( "logWarning logs a message with a warning prefix" , ( ) => {
92118 const message = "A warning occurred." ;
93-
94- const infoMock = jest . spyOn ( core , "info" ) ;
95-
96119 actionUtils . logWarning ( message ) ;
97-
98- expect ( infoMock ) . toHaveBeenCalledWith ( `[warning]${ message } ` ) ;
120+ expect ( core . info ) . toHaveBeenCalledWith ( `[warning]${ message } ` ) ;
99121} ) ;
100122
101123test ( "isValidEvent returns false for event that does not have a branch or tag" , ( ) => {
102- const event = "foo" ;
103- process . env [ Events . Key ] = event ;
104-
105- const isValidEvent = actionUtils . isValidEvent ( ) ;
106-
107- expect ( isValidEvent ) . toBe ( false ) ;
124+ process . env [ Events . Key ] = "foo" ;
125+ expect ( actionUtils . isValidEvent ( ) ) . toBe ( false ) ;
108126} ) ;
109127
110128test ( "isValidEvent returns true for event that has a ref" , ( ) => {
111- const event = Events . Push ;
112- process . env [ Events . Key ] = event ;
129+ process . env [ Events . Key ] = Events . Push ;
113130 process . env [ RefKey ] = "ref/heads/feature" ;
114-
115- const isValidEvent = actionUtils . isValidEvent ( ) ;
116-
117- expect ( isValidEvent ) . toBe ( true ) ;
131+ expect ( actionUtils . isValidEvent ( ) ) . toBe ( true ) ;
118132} ) ;
119133
120134test ( "getInputAsArray returns empty array if not required and missing" , ( ) => {
@@ -124,7 +138,7 @@ test("getInputAsArray returns empty array if not required and missing", () => {
124138test ( "getInputAsArray throws error if required and missing" , ( ) => {
125139 expect ( ( ) =>
126140 actionUtils . getInputAsArray ( "foo" , { required : true } )
127- ) . toThrowError ( ) ;
141+ ) . toThrow ( ) ;
128142} ) ;
129143
130144test ( "getInputAsArray handles single line correctly" , ( ) => {
@@ -180,7 +194,7 @@ test("getInputAsInt returns undefined if input is invalid or NaN", () => {
180194test ( "getInputAsInt throws if required and value missing" , ( ) => {
181195 expect ( ( ) =>
182196 actionUtils . getInputAsInt ( "undefined" , { required : true } )
183- ) . toThrowError ( ) ;
197+ ) . toThrow ( ) ;
184198} ) ;
185199
186200test ( "getInputAsBool returns false if input not set" , ( ) => {
@@ -200,68 +214,65 @@ test("getInputAsBool returns false if input is invalid or NaN", () => {
200214test ( "getInputAsBool throws if required and value missing" , ( ) => {
201215 expect ( ( ) =>
202216 actionUtils . getInputAsBool ( "undefined2" , { required : true } )
203- ) . toThrowError ( ) ;
217+ ) . toThrow ( ) ;
204218} ) ;
205219
206220test ( "isCacheFeatureAvailable for ac enabled" , ( ) => {
207- jest . spyOn ( cache , "isFeatureAvailable" ) . mockImplementation ( ( ) => true ) ;
208-
221+ ( cache . isFeatureAvailable as jest . Mock ) . mockReturnValue ( true ) ;
209222 expect ( actionUtils . isCacheFeatureAvailable ( ) ) . toBe ( true ) ;
210223} ) ;
211224
212225test ( "isCacheFeatureAvailable for ac disabled on GHES" , ( ) => {
213- jest . spyOn ( cache , " isFeatureAvailable" ) . mockImplementation ( ( ) => false ) ;
226+ ( cache . isFeatureAvailable as jest . Mock ) . mockReturnValue ( false ) ;
214227
215228 const message = `Cache action is only supported on GHES version >= 3.5. If you are on version >=3.5 Please check with GHES admin if Actions cache service is enabled or not.
216229Otherwise please upgrade to GHES version >= 3.5 and If you are also using Github Connect, please unretire the actions/cache namespace before upgrade (see https://docs.github.com/en/[email protected] /admin/github-actions/managing-access-to-actions-from-githubcom/enabling-automatic-access-to-githubcom-actions-using-github-connect#automatic-retirement-of-namespaces-for-actions-accessed-on-githubcom)` ; 217- const infoMock = jest . spyOn ( core , "info" ) ;
218230
219231 try {
220232 process . env [ "GITHUB_SERVER_URL" ] = "http://example.com" ;
221233 expect ( actionUtils . isCacheFeatureAvailable ( ) ) . toBe ( false ) ;
222- expect ( infoMock ) . toHaveBeenCalledWith ( `[warning]${ message } ` ) ;
234+ expect ( core . info ) . toHaveBeenCalledWith ( `[warning]${ message } ` ) ;
223235 } finally {
224236 delete process . env [ "GITHUB_SERVER_URL" ] ;
225237 }
226238} ) ;
227239
228240test ( "isCacheFeatureAvailable for ac disabled on dotcom" , ( ) => {
229- jest . spyOn ( cache , " isFeatureAvailable" ) . mockImplementation ( ( ) => false ) ;
241+ ( cache . isFeatureAvailable as jest . Mock ) . mockReturnValue ( false ) ;
230242
231243 const message =
232244 "An internal error has occurred in cache backend. Please check https://www.githubstatus.com/ for any ongoing issue in actions." ;
233- const infoMock = jest . spyOn ( core , "info" ) ;
234245
235246 try {
236247 process . env [ "GITHUB_SERVER_URL" ] = "http://github.com" ;
237248 expect ( actionUtils . isCacheFeatureAvailable ( ) ) . toBe ( false ) ;
238- expect ( infoMock ) . toHaveBeenCalledWith ( `[warning]${ message } ` ) ;
249+ expect ( core . info ) . toHaveBeenCalledWith ( `[warning]${ message } ` ) ;
239250 } finally {
240251 delete process . env [ "GITHUB_SERVER_URL" ] ;
241252 }
242253} ) ;
243254
244- test ( "isGhes returns false when the GITHUB_SERVER_URL environment variable is not defined" , async ( ) => {
255+ test ( "isGhes returns false when the GITHUB_SERVER_URL environment variable is not defined" , ( ) => {
245256 delete process . env [ "GITHUB_SERVER_URL" ] ;
246257 expect ( actionUtils . isGhes ( ) ) . toBeFalsy ( ) ;
247258} ) ;
248259
249- test ( "isGhes returns false when the GITHUB_SERVER_URL environment variable is set to github.com" , async ( ) => {
260+ test ( "isGhes returns false when the GITHUB_SERVER_URL environment variable is set to github.com" , ( ) => {
250261 process . env [ "GITHUB_SERVER_URL" ] = "https://github.com" ;
251262 expect ( actionUtils . isGhes ( ) ) . toBeFalsy ( ) ;
252263} ) ;
253264
254- test ( "isGhes returns false when the GITHUB_SERVER_URL environment variable is set to a GitHub Enterprise Cloud-style URL" , async ( ) => {
265+ test ( "isGhes returns false when the GITHUB_SERVER_URL environment variable is set to a GitHub Enterprise Cloud-style URL" , ( ) => {
255266 process . env [ "GITHUB_SERVER_URL" ] = "https://contoso.ghe.com" ;
256267 expect ( actionUtils . isGhes ( ) ) . toBeFalsy ( ) ;
257268} ) ;
258269
259- test ( "isGhes returns false when the GITHUB_SERVER_URL environment variable has a .localhost suffix" , async ( ) => {
270+ test ( "isGhes returns false when the GITHUB_SERVER_URL environment variable has a .localhost suffix" , ( ) => {
260271 process . env [ "GITHUB_SERVER_URL" ] = "https://mock-github.localhost" ;
261272 expect ( actionUtils . isGhes ( ) ) . toBeFalsy ( ) ;
262273} ) ;
263274
264- test ( "isGhes returns true when the GITHUB_SERVER_URL environment variable is set to some other URL" , async ( ) => {
275+ test ( "isGhes returns true when the GITHUB_SERVER_URL environment variable is set to some other URL" , ( ) => {
265276 process . env [ "GITHUB_SERVER_URL" ] = "https://src.onpremise.fabrikam.com" ;
266277 expect ( actionUtils . isGhes ( ) ) . toBeTruthy ( ) ;
267278} ) ;
0 commit comments