@ -324,3 +324,113 @@ test("restore with cache found for restore key", async () => {
) ;
) ;
expect ( failedMock ) . toHaveBeenCalledTimes ( 0 ) ;
expect ( failedMock ) . toHaveBeenCalledTimes ( 0 ) ;
} ) ;
} ) ;
test ( "restore with enabling save on any failure feature" , async ( ) = > {
const path = "node_modules" ;
const key = "node-test" ;
const restoreKey = "node-" ;
testUtils . setInputs ( {
path : path ,
key ,
restoreKeys : [ restoreKey ] ,
saveOnAnyFailure : "true"
} ) ;
const debugMock = jest . spyOn ( core , "debug" ) ;
const infoMock = jest . spyOn ( core , "info" ) ;
const failedMock = jest . spyOn ( core , "setFailed" ) ;
const stateMock = jest . spyOn ( core , "saveState" ) ;
const setCacheHitOutputMock = jest . spyOn ( actionUtils , "setCacheHitOutput" ) ;
const restoreCacheMock = jest
. spyOn ( cache , "restoreCache" )
. mockImplementationOnce ( ( ) = > {
return Promise . resolve ( restoreKey ) ;
} ) ;
await run ( ) ;
expect ( restoreCacheMock ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( restoreCacheMock ) . toHaveBeenCalledWith ( [ path ] , key , [ restoreKey ] ) ;
expect ( stateMock ) . toHaveBeenCalledWith ( "CACHE_KEY" , key ) ;
expect ( setCacheHitOutputMock ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( setCacheHitOutputMock ) . toHaveBeenCalledWith ( false ) ;
expect ( debugMock ) . toHaveBeenCalledWith (
` Exporting environment variable SAVE_CACHE_ON_ANY_FAILURE `
) ;
expect ( infoMock ) . toHaveBeenCalledWith (
` Input Variable SAVE_CACHE_ON_ANY_FAILURE is set to true, the cache will be saved despite of any failure in the build. `
) ;
expect ( failedMock ) . toHaveBeenCalledTimes ( 0 ) ;
} ) ;
test ( "Fail restore with strict restore enabled when primary key not found" , async ( ) = > {
const path = "node_modules" ;
const key = "node-test" ;
const restoreKey = "node-" ;
testUtils . setInputs ( {
path : path ,
key ,
restoreKeys : [ restoreKey ] ,
strictRestore : "true"
} ) ;
const failedMock = jest . spyOn ( core , "setFailed" ) ;
const stateMock = jest . spyOn ( core , "saveState" ) ;
const setCacheHitOutputMock = jest . spyOn ( actionUtils , "setCacheHitOutput" ) ;
const restoreCacheMock = jest
. spyOn ( cache , "restoreCache" )
. mockImplementationOnce ( ( ) = > {
return Promise . resolve ( undefined ) ;
} ) ;
await run ( ) ;
expect ( restoreCacheMock ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( restoreCacheMock ) . toHaveBeenCalledWith ( [ path ] , key , [ restoreKey ] ) ;
expect ( stateMock ) . toHaveBeenCalledWith ( "CACHE_KEY" , key ) ;
expect ( setCacheHitOutputMock ) . toHaveBeenCalledTimes ( 0 ) ;
expect ( failedMock ) . toHaveBeenCalledWith (
` Cache with the given input key ${ key } is not found, hence exiting the workflow as the strict-restore requirement is not met. `
) ;
expect ( failedMock ) . toHaveBeenCalledTimes ( 1 ) ;
} ) ;
test ( "Fail restore with strict restore enabled when primary key doesn't match restored key" , async ( ) = > {
const path = "node_modules" ;
const key = "node-test" ;
const restoreKey = "node-" ;
testUtils . setInputs ( {
path : path ,
key ,
restoreKeys : [ restoreKey ] ,
strictRestore : "true"
} ) ;
const failedMock = jest . spyOn ( core , "setFailed" ) ;
const stateMock = jest . spyOn ( core , "saveState" ) ;
const setCacheHitOutputMock = jest . spyOn ( actionUtils , "setCacheHitOutput" ) ;
const restoreCacheMock = jest
. spyOn ( cache , "restoreCache" )
. mockImplementationOnce ( ( ) = > {
return Promise . resolve ( restoreKey ) ;
} ) ;
await run ( ) ;
expect ( restoreCacheMock ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( restoreCacheMock ) . toHaveBeenCalledWith ( [ path ] , key , [ restoreKey ] ) ;
expect ( stateMock ) . toHaveBeenCalledWith ( "CACHE_KEY" , key ) ;
expect ( setCacheHitOutputMock ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( setCacheHitOutputMock ) . toHaveBeenCalledWith ( false ) ;
expect ( failedMock ) . toHaveBeenCalledWith (
` Restored cache key doesn't match the given input key ${ key } , hence exiting the workflow as the strict-restore requirement is not met. `
) ;
expect ( failedMock ) . toHaveBeenCalledTimes ( 1 ) ;
} ) ;