@@ -21,25 +21,15 @@ jest.unstable_mockModule('@actions/core', () => ({
2121} ) )
2222
2323// Mock @actions/glob
24- const mockGlob = jest . fn < ( ) => Promise < string [ ] > > ( )
24+ const mockHashFiles = jest . fn < ( pattern : string ) => Promise < string > > ( )
2525jest . unstable_mockModule ( '@actions/glob' , ( ) => ( {
26- create : jest . fn < ( ) => Promise < { glob : typeof mockGlob } > > ( ) . mockImplementation ( async ( ) => ( {
27- glob : mockGlob
28- } ) )
29- } ) )
30-
31- // Mock fs
32- const mockReadFileSync = jest . fn ( )
33- const mockStatSync = jest . fn ( )
34- jest . unstable_mockModule ( 'fs' , ( ) => ( {
35- readFileSync : mockReadFileSync ,
36- statSync : mockStatSync ,
37- default : { readFileSync : mockReadFileSync , statSync : mockStatSync }
26+ hashFiles : mockHashFiles
3827} ) )
3928
4029const { BasicCacheService} = await import ( '../../src/cache-service-basic' )
4130
42- const KEY_PREFIX = `gradle-actions-basic-Linux-${ process . arch } -gradle-`
31+ const HASH = 'abc123def456'
32+ const PRIMARY_KEY = `setup-java-Linux-${ process . arch } -gradle-${ HASH } `
4333
4434describe ( 'BasicCacheService' , ( ) => {
4535 let service : InstanceType < typeof BasicCacheService >
@@ -48,7 +38,7 @@ describe('BasicCacheService', () => {
4838 jest . clearAllMocks ( )
4939 service = new BasicCacheService ( )
5040 process . env [ 'RUNNER_OS' ] = 'Linux'
51- mockGlob . mockResolvedValue ( [ ] )
41+ mockHashFiles . mockResolvedValue ( HASH )
5242 } )
5343
5444 describe ( 'restore' , ( ) => {
@@ -82,8 +72,8 @@ describe('BasicCacheService', () => {
8272 expect ( mockRestoreCache ) . not . toHaveBeenCalled ( )
8373 } )
8474
85- it ( 'restores cache and saves state on hit ' , async ( ) => {
86- mockRestoreCache . mockResolvedValue ( ` ${ KEY_PREFIX } abc123` )
75+ it ( 'restores cache without restoreKeys and saves both keys to state ' , async ( ) => {
76+ mockRestoreCache . mockResolvedValue ( PRIMARY_KEY )
8777
8878 await service . restore ( '/home/.gradle' , {
8979 disabled : false ,
@@ -96,15 +86,13 @@ describe('BasicCacheService', () => {
9686 excludes : [ ]
9787 } )
9888
89+ // No restoreKeys parameter — exact match only (setup-java#269)
9990 expect ( mockRestoreCache ) . toHaveBeenCalledWith (
10091 [ '/home/.gradle/caches' , '/home/.gradle/wrapper' ] ,
101- expect . stringContaining ( KEY_PREFIX ) ,
102- [ KEY_PREFIX ]
103- )
104- expect ( mockSaveState ) . toHaveBeenCalledWith (
105- 'BASIC_CACHE_RESTORED_KEY' ,
106- `${ KEY_PREFIX } abc123`
92+ PRIMARY_KEY
10793 )
94+ expect ( mockSaveState ) . toHaveBeenCalledWith ( 'BASIC_CACHE_PRIMARY_KEY' , PRIMARY_KEY )
95+ expect ( mockSaveState ) . toHaveBeenCalledWith ( 'BASIC_CACHE_RESTORED_KEY' , PRIMARY_KEY )
10896 } )
10997
11098 it ( 'handles cache miss gracefully' , async ( ) => {
@@ -121,7 +109,10 @@ describe('BasicCacheService', () => {
121109 excludes : [ ]
122110 } )
123111
124- expect ( mockSaveState ) . not . toHaveBeenCalled ( )
112+ // Primary key should still be saved to state even on miss
113+ expect ( mockSaveState ) . toHaveBeenCalledWith ( 'BASIC_CACHE_PRIMARY_KEY' , PRIMARY_KEY )
114+ // Restored key should NOT be saved
115+ expect ( mockSaveState ) . not . toHaveBeenCalledWith ( 'BASIC_CACHE_RESTORED_KEY' , expect . anything ( ) )
125116 expect ( mockInfo ) . toHaveBeenCalledWith (
126117 expect . stringContaining ( 'not found' )
127118 )
@@ -145,6 +136,23 @@ describe('BasicCacheService', () => {
145136 expect . stringContaining ( 'Failed to restore' )
146137 )
147138 } )
139+
140+ it ( 'throws when no build files are found' , async ( ) => {
141+ mockHashFiles . mockResolvedValue ( '' )
142+
143+ await expect (
144+ service . restore ( '/home/.gradle' , {
145+ disabled : false ,
146+ readOnly : false ,
147+ writeOnly : false ,
148+ overwriteExisting : false ,
149+ strictMatch : false ,
150+ cleanup : 'never' ,
151+ includes : [ ] ,
152+ excludes : [ ]
153+ } )
154+ ) . rejects . toThrow ( 'No file in' )
155+ } )
148156 } )
149157
150158 describe ( 'save' , ( ) => {
@@ -166,7 +174,10 @@ describe('BasicCacheService', () => {
166174 } )
167175
168176 it ( 'skips save when readOnly and reports restored key' , async ( ) => {
169- mockGetState . mockReturnValue ( `${ KEY_PREFIX } abc123` )
177+ mockGetState . mockImplementation ( ( name : string ) => {
178+ if ( name === 'BASIC_CACHE_RESTORED_KEY' ) return PRIMARY_KEY
179+ return ''
180+ } )
170181 const report = await service . save ( '/home/.gradle' , [ ] , {
171182 disabled : false ,
172183 readOnly : true ,
@@ -180,7 +191,7 @@ describe('BasicCacheService', () => {
180191
181192 expect ( mockSaveCache ) . not . toHaveBeenCalled ( )
182193 expect ( report ) . toContain ( 'restored from cache key' )
183- expect ( report ) . toContain ( ` ${ KEY_PREFIX } abc123` )
194+ expect ( report ) . toContain ( PRIMARY_KEY )
184195 expect ( report ) . toContain ( 'read-only' )
185196 } )
186197
@@ -201,7 +212,55 @@ describe('BasicCacheService', () => {
201212 expect ( report ) . toContain ( 'not restored' )
202213 } )
203214
204- it ( 'saves cache and returns report on success' , async ( ) => {
215+ it ( 'skips save when matched key equals primary key' , async ( ) => {
216+ mockGetState . mockImplementation ( ( name : string ) => {
217+ if ( name === 'BASIC_CACHE_RESTORED_KEY' ) return PRIMARY_KEY
218+ if ( name === 'BASIC_CACHE_PRIMARY_KEY' ) return PRIMARY_KEY
219+ return ''
220+ } )
221+
222+ const report = await service . save ( '/home/.gradle' , [ ] , {
223+ disabled : false ,
224+ readOnly : false ,
225+ writeOnly : false ,
226+ overwriteExisting : false ,
227+ strictMatch : false ,
228+ cleanup : 'never' ,
229+ includes : [ ] ,
230+ excludes : [ ]
231+ } )
232+
233+ expect ( mockSaveCache ) . not . toHaveBeenCalled ( )
234+ expect ( report ) . toContain ( 'Save was skipped' )
235+ } )
236+
237+ it ( 'uses primary key from state, not recomputed' , async ( ) => {
238+ const stateKey = `setup-java-Linux-${ process . arch } -gradle-statedhash`
239+ mockGetState . mockImplementation ( ( name : string ) => {
240+ if ( name === 'BASIC_CACHE_PRIMARY_KEY' ) return stateKey
241+ return ''
242+ } )
243+ mockSaveCache . mockResolvedValue ( 0 )
244+
245+ const report = await service . save ( '/home/.gradle' , [ ] , {
246+ disabled : false ,
247+ readOnly : false ,
248+ writeOnly : false ,
249+ overwriteExisting : false ,
250+ strictMatch : false ,
251+ cleanup : 'never' ,
252+ includes : [ ] ,
253+ excludes : [ ]
254+ } )
255+
256+ expect ( mockSaveCache ) . toHaveBeenCalledWith (
257+ [ '/home/.gradle/caches' , '/home/.gradle/wrapper' ] ,
258+ stateKey
259+ )
260+ expect ( report ) . toContain ( stateKey )
261+ } )
262+
263+ it ( 'computes key as fallback when primary key not in state' , async ( ) => {
205264 mockGetState . mockReturnValue ( '' )
206265 mockSaveCache . mockResolvedValue ( 0 )
207266
@@ -218,7 +277,7 @@ describe('BasicCacheService', () => {
218277
219278 expect ( mockSaveCache ) . toHaveBeenCalledWith (
220279 [ '/home/.gradle/caches' , '/home/.gradle/wrapper' ] ,
221- expect . stringContaining ( KEY_PREFIX )
280+ PRIMARY_KEY
222281 )
223282 expect ( report ) . toContain ( 'saved to cache' )
224283 } )
0 commit comments