2424 * Forked from https://github.com/actions/setup-java/blob/5b36705a13905facb447b6812d613a06a07e371d/__tests__/cache.test.ts
2525 */
2626
27+ import { jest } from '@jest/globals'
2728import { mkdtempSync } from 'fs'
2829import { tmpdir } from 'os'
2930import { join } from 'path'
30- import { restore , save } from '../src/features/cache'
3131import * as fs from 'fs'
3232import * as os from 'os'
33- import * as core from '@actions/core'
34- import * as cache from '@actions/cache'
33+ import * as cache from '../__fixtures__/cache.js'
34+ import * as core from '../__fixtures__/core.js'
35+
36+ // Mocks should be declared before the module being tested is imported.
37+ jest . unstable_mockModule ( '@actions/core' , ( ) => core )
38+ jest . unstable_mockModule ( '@actions/cache' , ( ) => cache )
39+
40+ // The module being tested should be imported dynamically. This ensures that the
41+ // mocks are used in place of any actual dependencies.
42+ const { restore, save } = await import ( '../src/features/cache.js' )
3543
3644describe ( 'dependency cache' , ( ) => {
3745 const ORIGINAL_RUNNER_OS = process . env [ 'RUNNER_OS' ]
3846 const ORIGINAL_GITHUB_WORKSPACE = process . env [ 'GITHUB_WORKSPACE' ]
3947 const ORIGINAL_CWD = process . cwd ( )
4048 let workspace : string
41- let spyInfo : jest . SpyInstance < void , Parameters < typeof core . info > >
42- let spyWarning : jest . SpyInstance < void , Parameters < typeof core . warning > >
43- let spyDebug : jest . SpyInstance < void , Parameters < typeof core . debug > >
44- let spySaveState : jest . SpyInstance < void , Parameters < typeof core . saveState > >
4549
4650 beforeEach ( ( ) => {
4751 workspace = mkdtempSync ( join ( tmpdir ( ) , 'setup-graalvm-cache-' ) )
@@ -65,17 +69,9 @@ describe('dependency cache', () => {
6569 } )
6670
6771 beforeEach ( ( ) => {
68- spyInfo = jest . spyOn ( core , 'info' )
69- spyInfo . mockImplementation ( ( ) => null )
70-
71- spyWarning = jest . spyOn ( core , 'warning' )
72- spyWarning . mockImplementation ( ( ) => null )
73-
74- spyDebug = jest . spyOn ( core , 'debug' )
75- spyDebug . mockImplementation ( ( ) => null )
76-
77- spySaveState = jest . spyOn ( core , 'saveState' )
78- spySaveState . mockImplementation ( ( ) => null )
72+ core . info . mockImplementation ( ( ) => null )
73+ core . warning . mockImplementation ( ( ) => null )
74+ core . debug . mockImplementation ( ( ) => null )
7975 } )
8076
8177 afterEach ( ( ) => {
@@ -86,13 +82,8 @@ describe('dependency cache', () => {
8682 } )
8783
8884 describe ( 'restore' , ( ) => {
89- let spyCacheRestore : jest . SpyInstance < ReturnType < typeof cache . restoreCache > , Parameters < typeof cache . restoreCache > >
90-
9185 beforeEach ( ( ) => {
92- spyCacheRestore = jest
93- . spyOn ( cache , 'restoreCache' )
94- . mockImplementation ( ( _paths : string [ ] , _primaryKey : string ) => Promise . resolve ( undefined ) )
95- spyWarning . mockImplementation ( ( ) => null )
86+ cache . restoreCache . mockImplementation ( ( _paths : string [ ] , _primaryKey : string ) => Promise . resolve ( undefined ) )
9687 } )
9788
9889 it ( 'throws error if unsupported package manager specified' , ( ) => {
@@ -111,9 +102,9 @@ describe('dependency cache', () => {
111102 createFile ( join ( workspace , 'pom.xml' ) )
112103
113104 await restore ( 'maven' )
114- expect ( spyCacheRestore ) . toHaveBeenCalled ( )
115- expect ( spyWarning ) . not . toHaveBeenCalled ( )
116- expect ( spyInfo ) . toHaveBeenCalledWith ( 'maven cache is not found' )
105+ expect ( cache . restoreCache ) . toHaveBeenCalled ( )
106+ expect ( core . warning ) . not . toHaveBeenCalled ( )
107+ expect ( core . info ) . toHaveBeenCalledWith ( 'maven cache is not found' )
117108 } )
118109 } )
119110 describe ( 'for gradle' , ( ) => {
@@ -128,27 +119,27 @@ describe('dependency cache', () => {
128119 createFile ( join ( workspace , 'build.gradle' ) )
129120
130121 await restore ( 'gradle' )
131- expect ( spyCacheRestore ) . toHaveBeenCalled ( )
132- expect ( spyWarning ) . not . toHaveBeenCalled ( )
133- expect ( spyInfo ) . toHaveBeenCalledWith ( 'gradle cache is not found' )
122+ expect ( cache . restoreCache ) . toHaveBeenCalled ( )
123+ expect ( core . warning ) . not . toHaveBeenCalled ( )
124+ expect ( core . info ) . toHaveBeenCalledWith ( 'gradle cache is not found' )
134125 } )
135126 it ( 'downloads cache based on build.gradle.kts' , async ( ) => {
136127 createFile ( join ( workspace , 'build.gradle.kts' ) )
137128
138129 await restore ( 'gradle' )
139- expect ( spyCacheRestore ) . toHaveBeenCalled ( )
140- expect ( spyWarning ) . not . toHaveBeenCalled ( )
141- expect ( spyInfo ) . toHaveBeenCalledWith ( 'gradle cache is not found' )
130+ expect ( cache . restoreCache ) . toHaveBeenCalled ( )
131+ expect ( core . warning ) . not . toHaveBeenCalled ( )
132+ expect ( core . info ) . toHaveBeenCalledWith ( 'gradle cache is not found' )
142133 } )
143134 } )
144135 it ( 'downloads cache based on buildSrc/Versions.kt' , async ( ) => {
145136 createDirectory ( join ( workspace , 'buildSrc' ) )
146137 createFile ( join ( workspace , 'buildSrc' , 'Versions.kt' ) )
147138
148139 await restore ( 'gradle' )
149- expect ( spyCacheRestore ) . toHaveBeenCalled ( )
150- expect ( spyWarning ) . not . toHaveBeenCalled ( )
151- expect ( spyInfo ) . toHaveBeenCalledWith ( 'gradle cache is not found' )
140+ expect ( cache . restoreCache ) . toHaveBeenCalled ( )
141+ expect ( core . warning ) . not . toHaveBeenCalled ( )
142+ expect ( core . info ) . toHaveBeenCalledWith ( 'gradle cache is not found' )
152143 } )
153144 describe ( 'for sbt' , ( ) => {
154145 it ( 'throws error if no build.sbt found' , async ( ) => {
@@ -162,39 +153,35 @@ describe('dependency cache', () => {
162153 createFile ( join ( workspace , 'build.sbt' ) )
163154
164155 await restore ( 'sbt' )
165- expect ( spyCacheRestore ) . toHaveBeenCalled ( )
166- expect ( spyWarning ) . not . toHaveBeenCalled ( )
167- expect ( spyInfo ) . toHaveBeenCalledWith ( 'sbt cache is not found' )
156+ expect ( cache . restoreCache ) . toHaveBeenCalled ( )
157+ expect ( core . warning ) . not . toHaveBeenCalled ( )
158+ expect ( core . info ) . toHaveBeenCalledWith ( 'sbt cache is not found' )
168159 } )
169160 } )
170161 } )
171162 describe ( 'save' , ( ) => {
172- let spyCacheSave : jest . SpyInstance < ReturnType < typeof cache . saveCache > , Parameters < typeof cache . saveCache > >
173-
174163 beforeEach ( ( ) => {
175- spyCacheSave = jest
176- . spyOn ( cache , 'saveCache' )
177- . mockImplementation ( ( _paths : string [ ] , _key : string ) => Promise . resolve ( 0 ) )
178- spyWarning . mockImplementation ( ( ) => null )
164+ cache . saveCache . mockImplementation ( ( _paths : string [ ] , _key : string ) => Promise . resolve ( 0 ) )
165+ core . warning . mockImplementation ( ( ) => null )
179166 } )
180167
181168 it ( 'throws error if unsupported package manager specified' , ( ) => {
182169 return expect ( save ( 'ant' ) ) . rejects . toThrow ( 'unknown package manager specified: ant' )
183170 } )
184171
185172 it ( 'save with -1 cacheId , should not fail workflow' , async ( ) => {
186- spyCacheSave . mockImplementation ( ( ) => Promise . resolve ( - 1 ) )
173+ cache . saveCache . mockImplementation ( ( ) => Promise . resolve ( - 1 ) )
187174 createStateForMissingBuildFile ( )
188175
189176 await save ( 'maven' )
190- expect ( spyCacheSave ) . toHaveBeenCalled ( )
191- expect ( spyWarning ) . not . toHaveBeenCalled ( )
192- expect ( spyInfo ) . toHaveBeenCalled ( )
193- expect ( spyInfo ) . toHaveBeenCalledWith ( expect . stringMatching ( / ^ C a c h e s a v e d w i t h t h e k e y : .* / ) )
177+ expect ( cache . saveCache ) . toHaveBeenCalled ( )
178+ expect ( core . warning ) . not . toHaveBeenCalled ( )
179+ expect ( core . info ) . toHaveBeenCalled ( )
180+ expect ( core . info ) . toHaveBeenCalledWith ( expect . stringMatching ( / ^ C a c h e s a v e d w i t h t h e k e y : .* / ) )
194181 } )
195182
196183 it ( 'saves with error from toolkit, should fail workflow' , async ( ) => {
197- spyCacheSave . mockImplementation ( ( ) => Promise . reject ( new cache . ValidationError ( 'Validation failed' ) ) )
184+ cache . saveCache . mockImplementation ( ( ) => Promise . reject ( new cache . ValidationError ( 'Validation failed' ) ) )
198185 createStateForMissingBuildFile ( )
199186
200187 expect . assertions ( 1 )
@@ -205,106 +192,106 @@ describe('dependency cache', () => {
205192 it ( 'uploads cache even if no pom.xml found' , async ( ) => {
206193 createStateForMissingBuildFile ( )
207194 await save ( 'maven' )
208- expect ( spyCacheSave ) . toHaveBeenCalled ( )
209- expect ( spyWarning ) . not . toHaveBeenCalled ( )
195+ expect ( cache . saveCache ) . toHaveBeenCalled ( )
196+ expect ( core . warning ) . not . toHaveBeenCalled ( )
210197 } )
211198 it ( 'does not upload cache if no restore run before' , async ( ) => {
212199 createFile ( join ( workspace , 'pom.xml' ) )
213200
214201 await save ( 'maven' )
215- expect ( spyCacheSave ) . not . toHaveBeenCalled ( )
216- expect ( spyWarning ) . toHaveBeenCalledWith ( 'Error retrieving key from state.' )
202+ expect ( cache . saveCache ) . not . toHaveBeenCalled ( )
203+ expect ( core . warning ) . toHaveBeenCalledWith ( 'Error retrieving key from state.' )
217204 } )
218205 it ( 'uploads cache' , async ( ) => {
219206 createFile ( join ( workspace , 'pom.xml' ) )
220207 createStateForSuccessfulRestore ( )
221208
222209 await save ( 'maven' )
223- expect ( spyCacheSave ) . toHaveBeenCalled ( )
224- expect ( spyWarning ) . not . toHaveBeenCalled ( )
225- expect ( spyInfo ) . toHaveBeenCalledWith ( expect . stringMatching ( / ^ C a c h e s a v e d w i t h t h e k e y : .* / ) )
210+ expect ( cache . saveCache ) . toHaveBeenCalled ( )
211+ expect ( core . warning ) . not . toHaveBeenCalled ( )
212+ expect ( core . info ) . toHaveBeenCalledWith ( expect . stringMatching ( / ^ C a c h e s a v e d w i t h t h e k e y : .* / ) )
226213 } )
227214 } )
228215 describe ( 'for gradle' , ( ) => {
229216 it ( 'uploads cache even if no build.gradle found' , async ( ) => {
230217 createStateForMissingBuildFile ( )
231218
232219 await save ( 'gradle' )
233- expect ( spyCacheSave ) . toHaveBeenCalled ( )
234- expect ( spyWarning ) . not . toHaveBeenCalled ( )
220+ expect ( cache . saveCache ) . toHaveBeenCalled ( )
221+ expect ( core . warning ) . not . toHaveBeenCalled ( )
235222 } )
236223 it ( 'does not upload cache if no restore run before' , async ( ) => {
237224 createFile ( join ( workspace , 'build.gradle' ) )
238225
239226 await save ( 'gradle' )
240- expect ( spyCacheSave ) . not . toHaveBeenCalled ( )
241- expect ( spyWarning ) . toHaveBeenCalledWith ( 'Error retrieving key from state.' )
227+ expect ( cache . saveCache ) . not . toHaveBeenCalled ( )
228+ expect ( core . warning ) . toHaveBeenCalledWith ( 'Error retrieving key from state.' )
242229 } )
243230 it ( 'uploads cache based on build.gradle' , async ( ) => {
244231 createFile ( join ( workspace , 'build.gradle' ) )
245232 createStateForSuccessfulRestore ( )
246233
247234 await save ( 'gradle' )
248- expect ( spyCacheSave ) . toHaveBeenCalled ( )
249- expect ( spyWarning ) . not . toHaveBeenCalled ( )
250- expect ( spyInfo ) . toHaveBeenCalledWith ( expect . stringMatching ( / ^ C a c h e s a v e d w i t h t h e k e y : .* / ) )
235+ expect ( cache . saveCache ) . toHaveBeenCalled ( )
236+ expect ( core . warning ) . not . toHaveBeenCalled ( )
237+ expect ( core . info ) . toHaveBeenCalledWith ( expect . stringMatching ( / ^ C a c h e s a v e d w i t h t h e k e y : .* / ) )
251238 } )
252239 it ( 'uploads cache based on build.gradle.kts' , async ( ) => {
253240 createFile ( join ( workspace , 'build.gradle.kts' ) )
254241 createStateForSuccessfulRestore ( )
255242
256243 await save ( 'gradle' )
257- expect ( spyCacheSave ) . toHaveBeenCalled ( )
258- expect ( spyWarning ) . not . toHaveBeenCalled ( )
259- expect ( spyInfo ) . toHaveBeenCalledWith ( expect . stringMatching ( / ^ C a c h e s a v e d w i t h t h e k e y : .* / ) )
244+ expect ( cache . saveCache ) . toHaveBeenCalled ( )
245+ expect ( core . warning ) . not . toHaveBeenCalled ( )
246+ expect ( core . info ) . toHaveBeenCalledWith ( expect . stringMatching ( / ^ C a c h e s a v e d w i t h t h e k e y : .* / ) )
260247 } )
261248 it ( 'uploads cache based on buildSrc/Versions.kt' , async ( ) => {
262249 createDirectory ( join ( workspace , 'buildSrc' ) )
263250 createFile ( join ( workspace , 'buildSrc' , 'Versions.kt' ) )
264251 createStateForSuccessfulRestore ( )
265252
266253 await save ( 'gradle' )
267- expect ( spyCacheSave ) . toHaveBeenCalled ( )
268- expect ( spyWarning ) . not . toHaveBeenCalled ( )
269- expect ( spyInfo ) . toHaveBeenCalledWith ( expect . stringMatching ( / ^ C a c h e s a v e d w i t h t h e k e y : .* / ) )
254+ expect ( cache . saveCache ) . toHaveBeenCalled ( )
255+ expect ( core . warning ) . not . toHaveBeenCalled ( )
256+ expect ( core . info ) . toHaveBeenCalledWith ( expect . stringMatching ( / ^ C a c h e s a v e d w i t h t h e k e y : .* / ) )
270257 } )
271258 } )
272259 describe ( 'for sbt' , ( ) => {
273260 it ( 'uploads cache even if no build.sbt found' , async ( ) => {
274261 createStateForMissingBuildFile ( )
275262 await save ( 'sbt' )
276- expect ( spyCacheSave ) . toHaveBeenCalled ( )
277- expect ( spyWarning ) . not . toHaveBeenCalled ( )
263+ expect ( cache . saveCache ) . toHaveBeenCalled ( )
264+ expect ( core . warning ) . not . toHaveBeenCalled ( )
278265 } )
279266 it ( 'does not upload cache if no restore run before' , async ( ) => {
280267 createFile ( join ( workspace , 'build.sbt' ) )
281268
282269 await save ( 'sbt' )
283- expect ( spyCacheSave ) . not . toHaveBeenCalled ( )
284- expect ( spyWarning ) . toHaveBeenCalledWith ( 'Error retrieving key from state.' )
270+ expect ( cache . saveCache ) . not . toHaveBeenCalled ( )
271+ expect ( core . warning ) . toHaveBeenCalledWith ( 'Error retrieving key from state.' )
285272 } )
286273 it ( 'uploads cache' , async ( ) => {
287274 createFile ( join ( workspace , 'build.sbt' ) )
288275 createStateForSuccessfulRestore ( )
289276
290277 await save ( 'sbt' )
291- expect ( spyCacheSave ) . toHaveBeenCalled ( )
292- expect ( spyWarning ) . not . toHaveBeenCalled ( )
293- expect ( spyInfo ) . toHaveBeenCalledWith ( expect . stringMatching ( / ^ C a c h e s a v e d w i t h t h e k e y : .* / ) )
278+ expect ( cache . saveCache ) . toHaveBeenCalled ( )
279+ expect ( core . warning ) . not . toHaveBeenCalled ( )
280+ expect ( core . info ) . toHaveBeenCalledWith ( expect . stringMatching ( / ^ C a c h e s a v e d w i t h t h e k e y : .* / ) )
294281 } )
295282 } )
296283 } )
297284} )
298285
299286function resetState ( ) {
300- jest . spyOn ( core , ' getState' ) . mockReset ( )
287+ core . getState . mockReset ( )
301288}
302289
303290/**
304291 * Create states to emulate a restore process without build file.
305292 */
306293function createStateForMissingBuildFile ( ) {
307- jest . spyOn ( core , ' getState' ) . mockImplementation ( ( name ) => {
294+ core . getState . mockImplementation ( ( name ) => {
308295 switch ( name ) {
309296 case 'cache-primary-key' :
310297 return 'setup-graalvm-cache-'
@@ -318,7 +305,7 @@ function createStateForMissingBuildFile() {
318305 * Create states to emulate a successful restore process.
319306 */
320307function createStateForSuccessfulRestore ( ) {
321- jest . spyOn ( core , ' getState' ) . mockImplementation ( ( name ) => {
308+ core . getState . mockImplementation ( ( name ) => {
322309 switch ( name ) {
323310 case 'cache-primary-key' :
324311 return 'setup-graalvm-cache-primary-key'
0 commit comments