@ -7,7 +7,7 @@ import {
CompressionMethod ,
CompressionMethod ,
Events ,
Events ,
Inputs ,
Inputs ,
RefKey
RefKey s
} from "../src/constants" ;
} from "../src/constants" ;
import { ArtifactCacheEntry } from "../src/contracts" ;
import { ArtifactCacheEntry } from "../src/contracts" ;
import run from "../src/restore" ;
import run from "../src/restore" ;
@ -40,13 +40,17 @@ beforeAll(() => {
beforeEach ( ( ) = > {
beforeEach ( ( ) = > {
process . env [ Events . Key ] = Events . Push ;
process . env [ Events . Key ] = Events . Push ;
process . env [ RefKey ] = "refs/heads/feature-branch" ;
} ) ;
} ) ;
afterEach ( ( ) = > {
afterEach ( ( ) = > {
testUtils . clearInputs ( ) ;
testUtils . clearInputs ( ) ;
delete process . env [ Events . Key ] ;
delete process . env [ Events . Key ] ;
delete process . env [ RefKey ] ;
RefKeys . forEach ( refKey = > delete process . env [ refKey ] ) ;
} ) ;
const refKeySet = RefKeys . map ( refKey = > {
return [ refKey , ` refs/heads/feature/ ${ refKey . toLowerCase ( ) } ` ] ;
} ) ;
} ) ;
test ( "restore with invalid event outputs warning" , async ( ) = > {
test ( "restore with invalid event outputs warning" , async ( ) = > {
@ -54,7 +58,6 @@ test("restore with invalid event outputs warning", async () => {
const failedMock = jest . spyOn ( core , "setFailed" ) ;
const failedMock = jest . spyOn ( core , "setFailed" ) ;
const invalidEvent = "commit_comment" ;
const invalidEvent = "commit_comment" ;
process . env [ Events . Key ] = invalidEvent ;
process . env [ Events . Key ] = invalidEvent ;
delete process . env [ RefKey ] ;
await run ( ) ;
await run ( ) ;
expect ( logWarningMock ) . toHaveBeenCalledWith (
expect ( logWarningMock ) . toHaveBeenCalledWith (
` Event Validation Error: The event type ${ invalidEvent } is not supported because it's not tied to a branch or tag ref. `
` Event Validation Error: The event type ${ invalidEvent } is not supported because it's not tied to a branch or tag ref. `
@ -62,66 +65,90 @@ test("restore with invalid event outputs warning", async () => {
expect ( failedMock ) . toHaveBeenCalledTimes ( 0 ) ;
expect ( failedMock ) . toHaveBeenCalledTimes ( 0 ) ;
} ) ;
} ) ;
test ( "restore with no path should fail" , async ( ) = > {
test . each ( refKeySet ) (
const failedMock = jest . spyOn ( core , "setFailed" ) ;
"restore with no path should fail" ,
await run ( ) ;
async ( refKey , ref ) = > {
// this input isn't necessary for restore b/c tarball contains entries relative to workspace
process . env [ refKey ] = ref ;
expect ( failedMock ) . not . toHaveBeenCalledWith (
"Input required and not supplied: path"
) ;
} ) ;
test ( "restore with no key" , async ( ) = > {
const failedMock = jest . spyOn ( core , "setFailed" ) ;
testUtils . setInput ( Inputs . Path , "node_modules" ) ;
await run ( ) ;
const failedMock = jest . spyOn ( core , "setFailed" ) ;
// this input isn't necessary for restore b/c tarball contains entries relative to workspace
await run ( ) ;
expect ( failedMock ) . not . toHaveBeenCalledWith (
expect ( failedMock ) . toHaveBeenCalledWith (
"Input required and not supplied: path"
"Input required and not supplied: key"
) ;
);
}
} ) ;
) ;
test ( "restore with too many keys should fail" , async ( ) = > {
test . each ( refKeySet ) ( "restore with no key" , async ( refKey , ref ) = > {
const key = "node-test" ;
process . env [ refKey ] = ref ;
const restoreKeys = [ . . . Array ( 20 ) . keys ( ) ] . map ( x = > x . toString ( ) ) ;
testUtils . setInputs ( {
path : "node_modules" ,
key ,
restoreKeys
} ) ;
const failedMock = jest . spyOn ( core , "setFailed" ) ;
await run ( ) ;
expect ( failedMock ) . toHaveBeenCalledWith (
` Key Validation Error: Keys are limited to a maximum of 10. `
) ;
} ) ;
test ( "restore with large key should fail" , async ( ) = > {
testUtils . setInput ( Inputs . Path , "node_modules" ) ;
const key = "foo" . repeat ( 512 ) ; // Over the 512 character limit
testUtils . setInputs ( {
path : "node_modules" ,
key
} ) ;
const failedMock = jest . spyOn ( core , "setFailed" ) ;
const failedMock = jest . spyOn ( core , "setFailed" ) ;
await run ( ) ;
await run ( ) ;
expect ( failedMock ) . toHaveBeenCalledWith (
expect ( failedMock ) . toHaveBeenCalledWith (
` Key Validation Error: ${ key } cannot be larger than 512 characters. `
"Input required and not supplied: key"
) ;
) ;
} ) ;
} ) ;
test ( "restore with invalid key should fail" , async ( ) = > {
test . each ( refKeySet ) (
const key = "comma,comma" ;
"restore with too many keys should fail" ,
testUtils . setInputs ( {
async ( refKey , ref ) = > {
path : "node_modules" ,
process . env [ refKey ] = ref ;
key
} ) ;
const key = "node-test" ;
const failedMock = jest . spyOn ( core , "setFailed" ) ;
const restoreKeys = [ . . . Array ( 20 ) . keys ( ) ] . map ( x = > x . toString ( ) ) ;
await run ( ) ;
testUtils . setInputs ( {
expect ( failedMock ) . toHaveBeenCalledWith (
path : "node_modules" ,
` Key Validation Error: ${ key } cannot contain commas. `
key ,
) ;
restoreKeys
} ) ;
} ) ;
const failedMock = jest . spyOn ( core , "setFailed" ) ;
await run ( ) ;
expect ( failedMock ) . toHaveBeenCalledWith (
` Key Validation Error: Keys are limited to a maximum of 10. `
) ;
}
) ;
test . each ( refKeySet ) (
"restore with large key should fail" ,
async ( refKey , ref ) = > {
process . env [ refKey ] = ref ;
const key = "foo" . repeat ( 512 ) ; // Over the 512 character limit
testUtils . setInputs ( {
path : "node_modules" ,
key
} ) ;
const failedMock = jest . spyOn ( core , "setFailed" ) ;
await run ( ) ;
expect ( failedMock ) . toHaveBeenCalledWith (
` Key Validation Error: ${ key } cannot be larger than 512 characters. `
) ;
}
) ;
test . each ( refKeySet ) (
"restore with invalid key should fail" ,
async ( refKey , ref ) = > {
process . env [ refKey ] = ref ;
const key = "comma,comma" ;
testUtils . setInputs ( {
path : "node_modules" ,
key
} ) ;
const failedMock = jest . spyOn ( core , "setFailed" ) ;
await run ( ) ;
expect ( failedMock ) . toHaveBeenCalledWith (
` Key Validation Error: ${ key } cannot contain commas. `
) ;
}
) ;
test . each ( refKeySet ) ( "restore with no cache found" , async ( refKey , ref ) = > {
process . env [ refKey ] = ref ;
test ( "restore with no cache found" , async ( ) = > {
const key = "node-test" ;
const key = "node-test" ;
testUtils . setInputs ( {
testUtils . setInputs ( {
path : "node_modules" ,
path : "node_modules" ,
@ -147,287 +174,330 @@ test("restore with no cache found", async () => {
) ;
) ;
} ) ;
} ) ;
test ( "restore with server error should fail" , async ( ) = > {
test . each ( refKeySet ) (
const key = "node-test" ;
"restore with server error should fail" ,
testUtils . setInputs ( {
async ( refKey , ref ) = > {
path : "node_modules" ,
process . env [ refKey ] = ref ;
key
} ) ;
const key = "node-test" ;
testUtils . setInputs ( {
const logWarningMock = jest . spyOn ( actionUtils , "logWarning" ) ;
path : "node_modules" ,
const failedMock = jest . spyOn ( core , "setFailed" ) ;
key
const stateMock = jest . spyOn ( core , "saveState" ) ;
} ) ;
const clientMock = jest . spyOn ( cacheHttpClient , "getCacheEntry" ) ;
const logWarningMock = jest . spyOn ( actionUtils , "logWarning" ) ;
clientMock . mockImplementation ( ( ) = > {
const failedMock = jest . spyOn ( core , "setFailed" ) ;
throw new Error ( "HTTP Error Occurred" ) ;
const stateMock = jest . spyOn ( core , "saveState" ) ;
} ) ;
const clientMock = jest . spyOn ( cacheHttpClient , "getCacheEntry" ) ;
const setCacheHitOutputMock = jest . spyOn ( actionUtils , "setCacheHitOutput" ) ;
clientMock . mockImplementation ( ( ) = > {
throw new Error ( "HTTP Error Occurred" ) ;
await run ( ) ;
} ) ;
expect ( stateMock ) . toHaveBeenCalledWith ( "CACHE_KEY" , key ) ;
const setCacheHitOutputMock = jest . spyOn (
actionUtils ,
expect ( logWarningMock ) . toHaveBeenCalledTimes ( 1 ) ;
"setCacheHitOutput"
expect ( logWarningMock ) . toHaveBeenCalledWith ( "HTTP Error Occurred" ) ;
) ;
expect ( setCacheHitOutputMock ) . toHaveBeenCalledTimes ( 1 ) ;
await run ( ) ;
expect ( setCacheHitOutputMock ) . toHaveBeenCalledWith ( false ) ;
expect ( stateMock ) . toHaveBeenCalledWith ( "CACHE_KEY" , key ) ;
expect ( failedMock ) . toHaveBeenCalledTimes ( 0 ) ;
} ) ;
expect ( logWarningMock ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( logWarningMock ) . toHaveBeenCalledWith ( "HTTP Error Occurred" ) ;
test ( "restore with restore keys and no cache found" , async ( ) = > {
const key = "node-test" ;
expect ( setCacheHitOutputMock ) . toHaveBeenCalledTimes ( 1 ) ;
const restoreKey = "node-" ;
expect ( setCacheHitOutputMock ) . toHaveBeenCalledWith ( false ) ;
testUtils . setInputs ( {
path : "node_modules" ,
expect ( failedMock ) . toHaveBeenCalledTimes ( 0 ) ;
key ,
}
restoreKeys : [ restoreKey ]
) ;
} ) ;
test . each ( refKeySet ) (
const infoMock = jest . spyOn ( core , "info" ) ;
"restore with restore keys and no cache found" ,
const failedMock = jest . spyOn ( core , "setFailed" ) ;
async ( refKey , ref ) = > {
const stateMock = jest . spyOn ( core , "saveState" ) ;
process . env [ refKey ] = ref ;
const clientMock = jest . spyOn ( cacheHttpClient , "getCacheEntry" ) ;
const key = "node-test" ;
clientMock . mockImplementation ( ( ) = > {
const restoreKey = "node-" ;
return Promise . resolve ( null ) ;
testUtils . setInputs ( {
} ) ;
path : "node_modules" ,
key ,
await run ( ) ;
restoreKeys : [ restoreKey ]
} ) ;
expect ( stateMock ) . toHaveBeenCalledWith ( "CACHE_KEY" , key ) ;
expect ( failedMock ) . toHaveBeenCalledTimes ( 0 ) ;
const infoMock = jest . spyOn ( core , "info" ) ;
const failedMock = jest . spyOn ( core , "setFailed" ) ;
expect ( infoMock ) . toHaveBeenCalledWith (
const stateMock = jest . spyOn ( core , "saveState" ) ;
` Cache not found for input keys: ${ key } , ${ restoreKey } `
) ;
const clientMock = jest . spyOn ( cacheHttpClient , "getCacheEntry" ) ;
} ) ;
clientMock . mockImplementation ( ( ) = > {
return Promise . resolve ( null ) ;
test ( "restore with gzip compressed cache found" , async ( ) = > {
} ) ;
const key = "node-test" ;
testUtils . setInputs ( {
await run ( ) ;
path : "node_modules" ,
key
expect ( stateMock ) . toHaveBeenCalledWith ( "CACHE_KEY" , key ) ;
} ) ;
expect ( failedMock ) . toHaveBeenCalledTimes ( 0 ) ;
const infoMock = jest . spyOn ( core , "info" ) ;
expect ( infoMock ) . toHaveBeenCalledWith (
const failedMock = jest . spyOn ( core , "setFailed" ) ;
` Cache not found for input keys: ${ key } , ${ restoreKey } `
const stateMock = jest . spyOn ( core , "saveState" ) ;
) ;
}
const cacheEntry : ArtifactCacheEntry = {
) ;
cacheKey : key ,
scope : "refs/heads/master" ,
test . each ( refKeySet ) (
archiveLocation : "www.actionscache.test/download"
"restore with gzip compressed cache found" ,
} ;
async ( refKey , ref ) = > {
const getCacheMock = jest . spyOn ( cacheHttpClient , "getCacheEntry" ) ;
process . env [ refKey ] = ref ;
getCacheMock . mockImplementation ( ( ) = > {
return Promise . resolve ( cacheEntry ) ;
const key = "node-test" ;
} ) ;
testUtils . setInputs ( {
const tempPath = "/foo/bar" ;
path : "node_modules" ,
key
const createTempDirectoryMock = jest . spyOn (
} ) ;
actionUtils ,
"createTempDirectory"
const infoMock = jest . spyOn ( core , "info" ) ;
) ;
const failedMock = jest . spyOn ( core , "setFailed" ) ;
createTempDirectoryMock . mockImplementation ( ( ) = > {
const stateMock = jest . spyOn ( core , "saveState" ) ;
return Promise . resolve ( tempPath ) ;
} ) ;
const cacheEntry : ArtifactCacheEntry = {
cacheKey : key ,
const archivePath = path . join ( tempPath , CacheFilename . Gzip ) ;
scope : "refs/heads/master" ,
const setCacheStateMock = jest . spyOn ( actionUtils , "setCacheState" ) ;
archiveLocation : "www.actionscache.test/download"
const downloadCacheMock = jest . spyOn ( cacheHttpClient , "downloadCache" ) ;
} ;
const getCacheMock = jest . spyOn ( cacheHttpClient , "getCacheEntry" ) ;
const fileSize = 142 ;
getCacheMock . mockImplementation ( ( ) = > {
const getArchiveFileSizeMock = jest
return Promise . resolve ( cacheEntry ) ;
. spyOn ( actionUtils , "getArchiveFileSize" )
} ) ;
. mockReturnValue ( fileSize ) ;
const tempPath = "/foo/bar" ;
const extractTarMock = jest . spyOn ( tar , "extractTar" ) ;
const createTempDirectoryMock = jest . spyOn (
const unlinkFileMock = jest . spyOn ( actionUtils , "unlinkFile" ) ;
actionUtils ,
const setCacheHitOutputMock = jest . spyOn ( actionUtils , "setCacheHitOutput" ) ;
"createTempDirectory"
) ;
const compression = CompressionMethod . Gzip ;
createTempDirectoryMock . mockImplementation ( ( ) = > {
const getCompressionMock = jest
return Promise . resolve ( tempPath ) ;
. spyOn ( actionUtils , "getCompressionMethod" )
} ) ;
. mockReturnValue ( Promise . resolve ( compression ) ) ;
const archivePath = path . join ( tempPath , CacheFilename . Gzip ) ;
await run ( ) ;
const setCacheStateMock = jest . spyOn ( actionUtils , "setCacheState" ) ;
const downloadCacheMock = jest . spyOn ( cacheHttpClient , "downloadCache" ) ;
expect ( stateMock ) . toHaveBeenCalledWith ( "CACHE_KEY" , key ) ;
expect ( getCacheMock ) . toHaveBeenCalledWith ( [ key ] , {
const fileSize = 142 ;
compressionMethod : compression
const getArchiveFileSizeMock = jest
} ) ;
. spyOn ( actionUtils , "getArchiveFileSize" )
expect ( setCacheStateMock ) . toHaveBeenCalledWith ( cacheEntry ) ;
. mockReturnValue ( fileSize ) ;
expect ( createTempDirectoryMock ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( downloadCacheMock ) . toHaveBeenCalledWith (
const extractTarMock = jest . spyOn ( tar , "extractTar" ) ;
cacheEntry . archiveLocation ,
const unlinkFileMock = jest . spyOn ( actionUtils , "unlinkFile" ) ;
archivePath
const setCacheHitOutputMock = jest . spyOn (
) ;
actionUtils ,
expect ( getArchiveFileSizeMock ) . toHaveBeenCalledWith ( archivePath ) ;
"setCacheHitOutput"
) ;
expect ( extractTarMock ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( extractTarMock ) . toHaveBeenCalledWith ( archivePath , compression ) ;
const compression = CompressionMethod . Gzip ;
const getCompressionMock = jest
expect ( unlinkFileMock ) . toHaveBeenCalledTimes ( 1 ) ;
. spyOn ( actionUtils , "getCompressionMethod" )
expect ( unlinkFileMock ) . toHaveBeenCalledWith ( archivePath ) ;
. mockReturnValue ( Promise . resolve ( compression ) ) ;
expect ( setCacheHitOutputMock ) . toHaveBeenCalledTimes ( 1 ) ;
await run ( ) ;
expect ( setCacheHitOutputMock ) . toHaveBeenCalledWith ( true ) ;
expect ( stateMock ) . toHaveBeenCalledWith ( "CACHE_KEY" , key ) ;
expect ( infoMock ) . toHaveBeenCalledWith ( ` Cache restored from key: ${ key } ` ) ;
expect ( getCacheMock ) . toHaveBeenCalledWith ( [ key ] , {
expect ( failedMock ) . toHaveBeenCalledTimes ( 0 ) ;
compressionMethod : compression
expect ( getCompressionMock ) . toHaveBeenCalledTimes ( 1 ) ;
} ) ;
} ) ;
expect ( setCacheStateMock ) . toHaveBeenCalledWith ( cacheEntry ) ;
expect ( createTempDirectoryMock ) . toHaveBeenCalledTimes ( 1 ) ;
test ( "restore with a pull request event and zstd compressed cache found" , async ( ) = > {
expect ( downloadCacheMock ) . toHaveBeenCalledWith (
const key = "node-test" ;
cacheEntry . archiveLocation ,
testUtils . setInputs ( {
archivePath
path : "node_modules" ,
) ;
key
expect ( getArchiveFileSizeMock ) . toHaveBeenCalledWith ( archivePath ) ;
} ) ;
expect ( extractTarMock ) . toHaveBeenCalledTimes ( 1 ) ;
process . env [ Events . Key ] = Events . PullRequest ;
expect ( extractTarMock ) . toHaveBeenCalledWith ( archivePath , compression ) ;
const infoMock = jest . spyOn ( core , "info" ) ;
expect ( unlinkFileMock ) . toHaveBeenCalledTimes ( 1 ) ;
const failedMock = jest . spyOn ( core , "setFailed" ) ;
expect ( unlinkFileMock ) . toHaveBeenCalledWith ( archivePath ) ;
const stateMock = jest . spyOn ( core , "saveState" ) ;
expect ( setCacheHitOutputMock ) . toHaveBeenCalledTimes ( 1 ) ;
const cacheEntry : ArtifactCacheEntry = {
expect ( setCacheHitOutputMock ) . toHaveBeenCalledWith ( true ) ;
cacheKey : key ,
scope : "refs/heads/master" ,
expect ( infoMock ) . toHaveBeenCalledWith (
archiveLocation : "www.actionscache.test/download"
` Cache restored from key: ${ key } `
} ;
) ;
const getCacheMock = jest . spyOn ( cacheHttpClient , "getCacheEntry" ) ;
expect ( failedMock ) . toHaveBeenCalledTimes ( 0 ) ;
getCacheMock . mockImplementation ( ( ) = > {
expect ( getCompressionMock ) . toHaveBeenCalledTimes ( 1 ) ;
return Promise . resolve ( cacheEntry ) ;
}
} ) ;
) ;
const tempPath = "/foo/bar" ;
test . each ( refKeySet ) (
const createTempDirectoryMock = jest . spyOn (
"restore with a pull request event and zstd compressed cache found" ,
actionUtils ,
async ( refKey , ref ) = > {
"createTempDirectory"
process . env [ refKey ] = ref ;
) ;
createTempDirectoryMock . mockImplementation ( ( ) = > {
const key = "node-test" ;
return Promise . resolve ( tempPath ) ;
testUtils . setInputs ( {
} ) ;
path : "node_modules" ,
key
const archivePath = path . join ( tempPath , CacheFilename . Zstd ) ;
} ) ;
const setCacheStateMock = jest . spyOn ( actionUtils , "setCacheState" ) ;
const downloadCacheMock = jest . spyOn ( cacheHttpClient , "downloadCache" ) ;
process . env [ Events . Key ] = Events . PullRequest ;
const fileSize = 62915000 ;
const infoMock = jest . spyOn ( core , "info" ) ;
const getArchiveFileSizeMock = jest
const failedMock = jest . spyOn ( core , "setFailed" ) ;
. spyOn ( actionUtils , "getArchiveFileSize" )
const stateMock = jest . spyOn ( core , "saveState" ) ;
. mockReturnValue ( fileSize ) ;
const cacheEntry : ArtifactCacheEntry = {
const extractTarMock = jest . spyOn ( tar , "extractTar" ) ;
cacheKey : key ,
const setCacheHitOutputMock = jest . spyOn ( actionUtils , "setCacheHitOutput" ) ;
scope : "refs/heads/master" ,
const compression = CompressionMethod . Zstd ;
archiveLocation : "www.actionscache.test/download"
const getCompressionMock = jest
} ;
. spyOn ( actionUtils , "getCompressionMethod" )
const getCacheMock = jest . spyOn ( cacheHttpClient , "getCacheEntry" ) ;
. mockReturnValue ( Promise . resolve ( compression ) ) ;
getCacheMock . mockImplementation ( ( ) = > {
return Promise . resolve ( cacheEntry ) ;
await run ( ) ;
} ) ;
const tempPath = "/foo/bar" ;
expect ( stateMock ) . toHaveBeenCalledWith ( "CACHE_KEY" , key ) ;
expect ( getCacheMock ) . toHaveBeenCalledWith ( [ key ] , {
const createTempDirectoryMock = jest . spyOn (
compressionMethod : compression
actionUtils ,
} ) ;
"createTempDirectory"
expect ( setCacheStateMock ) . toHaveBeenCalledWith ( cacheEntry ) ;
) ;
expect ( createTempDirectoryMock ) . toHaveBeenCalledTimes ( 1 ) ;
createTempDirectoryMock . mockImplementation ( ( ) = > {
expect ( downloadCacheMock ) . toHaveBeenCalledWith (
return Promise . resolve ( tempPath ) ;
cacheEntry . archiveLocation ,
} ) ;
archivePath
) ;
const archivePath = path . join ( tempPath , CacheFilename . Zstd ) ;
expect ( getArchiveFileSizeMock ) . toHaveBeenCalledWith ( archivePath ) ;
const setCacheStateMock = jest . spyOn ( actionUtils , "setCacheState" ) ;
expect ( infoMock ) . toHaveBeenCalledWith ( ` Cache Size: ~60 MB (62915000 B) ` ) ;
const downloadCacheMock = jest . spyOn ( cacheHttpClient , "downloadCache" ) ;
expect ( extractTarMock ) . toHaveBeenCalledTimes ( 1 ) ;
const fileSize = 62915000 ;
expect ( extractTarMock ) . toHaveBeenCalledWith ( archivePath , compression ) ;
const getArchiveFileSizeMock = jest
. spyOn ( actionUtils , "getArchiveFileSize" )
expect ( setCacheHitOutputMock ) . toHaveBeenCalledTimes ( 1 ) ;
. mockReturnValue ( fileSize ) ;
expect ( setCacheHitOutputMock ) . toHaveBeenCalledWith ( true ) ;
const extractTarMock = jest . spyOn ( tar , "extractTar" ) ;
expect ( infoMock ) . toHaveBeenCalledWith ( ` Cache restored from key: ${ key } ` ) ;
const setCacheHitOutputMock = jest . spyOn (
expect ( failedMock ) . toHaveBeenCalledTimes ( 0 ) ;
actionUtils ,
expect ( getCompressionMock ) . toHaveBeenCalledTimes ( 1 ) ;
"setCacheHitOutput"
} ) ;
) ;
const compression = CompressionMethod . Zstd ;
test ( "restore with cache found for restore key" , async ( ) = > {
const getCompressionMock = jest
const key = "node-test" ;
. spyOn ( actionUtils , "getCompressionMethod" )
const restoreKey = "node-" ;
. mockReturnValue ( Promise . resolve ( compression ) ) ;
testUtils . setInputs ( {
path : "node_modules" ,
await run ( ) ;
key ,
restoreKeys : [ restoreKey ]
expect ( stateMock ) . toHaveBeenCalledWith ( "CACHE_KEY" , key ) ;
} ) ;
expect ( getCacheMock ) . toHaveBeenCalledWith ( [ key ] , {
compressionMethod : compression
const infoMock = jest . spyOn ( core , "info" ) ;
} ) ;
const failedMock = jest . spyOn ( core , "setFailed" ) ;
expect ( setCacheStateMock ) . toHaveBeenCalledWith ( cacheEntry ) ;
const stateMock = jest . spyOn ( core , "saveState" ) ;
expect ( createTempDirectoryMock ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( downloadCacheMock ) . toHaveBeenCalledWith (
const cacheEntry : ArtifactCacheEntry = {
cacheEntry . archiveLocation ,
cacheKey : restoreKey ,
archivePath
scope : "refs/heads/master" ,
) ;
archiveLocation : "www.actionscache.test/download"
expect ( getArchiveFileSizeMock ) . toHaveBeenCalledWith ( archivePath ) ;
} ;
expect ( infoMock ) . toHaveBeenCalledWith (
const getCacheMock = jest . spyOn ( cacheHttpClient , "getCacheEntry" ) ;
` Cache Size: ~60 MB (62915000 B) `
getCacheMock . mockImplementation ( ( ) = > {
) ;
return Promise . resolve ( cacheEntry ) ;
} ) ;
expect ( extractTarMock ) . toHaveBeenCalledTimes ( 1 ) ;
const tempPath = "/foo/bar" ;
expect ( extractTarMock ) . toHaveBeenCalledWith ( archivePath , compression ) ;
const createTempDirectoryMock = jest . spyOn (
expect ( setCacheHitOutputMock ) . toHaveBeenCalledTimes ( 1 ) ;
actionUtils ,
expect ( setCacheHitOutputMock ) . toHaveBeenCalledWith ( true ) ;
"createTempDirectory"
) ;
expect ( infoMock ) . toHaveBeenCalledWith (
createTempDirectoryMock . mockImplementation ( ( ) = > {
` Cache restored from key: ${ key } `
return Promise . resolve ( tempPath ) ;
) ;
} ) ;
expect ( failedMock ) . toHaveBeenCalledTimes ( 0 ) ;
expect ( getCompressionMock ) . toHaveBeenCalledTimes ( 1 ) ;
const archivePath = path . join ( tempPath , CacheFilename . Zstd ) ;
}
const setCacheStateMock = jest . spyOn ( actionUtils , "setCacheState" ) ;
) ;
const downloadCacheMock = jest . spyOn ( cacheHttpClient , "downloadCache" ) ;
test . each ( refKeySet ) (
const fileSize = 142 ;
"restore with cache found for restore key" ,
const getArchiveFileSizeMock = jest
async ( refKey , ref ) = > {
. spyOn ( actionUtils , "getArchiveFileSize" )
process . env [ refKey ] = ref ;
. mockReturnValue ( fileSize ) ;
const key = "node-test" ;
const extractTarMock = jest . spyOn ( tar , "extractTar" ) ;
const restoreKey = "node-" ;
const setCacheHitOutputMock = jest . spyOn ( actionUtils , "setCacheHitOutput" ) ;
testUtils . setInputs ( {
const compression = CompressionMethod . Zstd ;
path : "node_modules" ,
const getCompressionMock = jest
key ,
. spyOn ( actionUtils , "getCompressionMethod" )
restoreKeys : [ restoreKey ]
. mockReturnValue ( Promise . resolve ( compression ) ) ;
} ) ;
await run ( ) ;
const infoMock = jest . spyOn ( core , "info" ) ;
const failedMock = jest . spyOn ( core , "setFailed" ) ;
expect ( stateMock ) . toHaveBeenCalledWith ( "CACHE_KEY" , key ) ;
const stateMock = jest . spyOn ( core , "saveState" ) ;
expect ( getCacheMock ) . toHaveBeenCalledWith ( [ key , restoreKey ] , {
compressionMethod : compression
const cacheEntry : ArtifactCacheEntry = {
} ) ;
cacheKey : restoreKey ,
expect ( setCacheStateMock ) . toHaveBeenCalledWith ( cacheEntry ) ;
scope : "refs/heads/master" ,
expect ( createTempDirectoryMock ) . toHaveBeenCalledTimes ( 1 ) ;
archiveLocation : "www.actionscache.test/download"
expect ( downloadCacheMock ) . toHaveBeenCalledWith (
} ;
cacheEntry . archiveLocation ,
const getCacheMock = jest . spyOn ( cacheHttpClient , "getCacheEntry" ) ;
archivePath
getCacheMock . mockImplementation ( ( ) = > {
) ;
return Promise . resolve ( cacheEntry ) ;
expect ( getArchiveFileSizeMock ) . toHaveBeenCalledWith ( archivePath ) ;
} ) ;
expect ( infoMock ) . toHaveBeenCalledWith ( ` Cache Size: ~0 MB (142 B) ` ) ;
const tempPath = "/foo/bar" ;
expect ( extractTarMock ) . toHaveBeenCalledTimes ( 1 ) ;
const createTempDirectoryMock = jest . spyOn (
expect ( extractTarMock ) . toHaveBeenCalledWith ( archivePath , compression ) ;
actionUtils ,
"createTempDirectory"
expect ( setCacheHitOutputMock ) . toHaveBeenCalledTimes ( 1 ) ;
) ;
expect ( setCacheHitOutputMock ) . toHaveBeenCalledWith ( false ) ;
createTempDirectoryMock . mockImplementation ( ( ) = > {
return Promise . resolve ( tempPath ) ;
expect ( infoMock ) . toHaveBeenCalledWith (
} ) ;
` Cache restored from key: ${ restoreKey } `
) ;
const archivePath = path . join ( tempPath , CacheFilename . Zstd ) ;
expect ( failedMock ) . toHaveBeenCalledTimes ( 0 ) ;
const setCacheStateMock = jest . spyOn ( actionUtils , "setCacheState" ) ;
expect ( getCompressionMock ) . toHaveBeenCalledTimes ( 1 ) ;
const downloadCacheMock = jest . spyOn ( cacheHttpClient , "downloadCache" ) ;
} ) ;
const fileSize = 142 ;
const getArchiveFileSizeMock = jest
. spyOn ( actionUtils , "getArchiveFileSize" )
. mockReturnValue ( fileSize ) ;
const extractTarMock = jest . spyOn ( tar , "extractTar" ) ;
const setCacheHitOutputMock = jest . spyOn (
actionUtils ,
"setCacheHitOutput"
) ;
const compression = CompressionMethod . Zstd ;
const getCompressionMock = jest
. spyOn ( actionUtils , "getCompressionMethod" )
. mockReturnValue ( Promise . resolve ( compression ) ) ;
await run ( ) ;
expect ( stateMock ) . toHaveBeenCalledWith ( "CACHE_KEY" , key ) ;
expect ( getCacheMock ) . toHaveBeenCalledWith ( [ key , restoreKey ] , {
compressionMethod : compression
} ) ;
expect ( setCacheStateMock ) . toHaveBeenCalledWith ( cacheEntry ) ;
expect ( createTempDirectoryMock ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( downloadCacheMock ) . toHaveBeenCalledWith (
cacheEntry . archiveLocation ,
archivePath
) ;
expect ( getArchiveFileSizeMock ) . toHaveBeenCalledWith ( archivePath ) ;
expect ( infoMock ) . toHaveBeenCalledWith ( ` Cache Size: ~0 MB (142 B) ` ) ;
expect ( extractTarMock ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( extractTarMock ) . toHaveBeenCalledWith ( archivePath , compression ) ;
expect ( setCacheHitOutputMock ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( setCacheHitOutputMock ) . toHaveBeenCalledWith ( false ) ;
expect ( infoMock ) . toHaveBeenCalledWith (
` Cache restored from key: ${ restoreKey } `
) ;
expect ( failedMock ) . toHaveBeenCalledTimes ( 0 ) ;
expect ( getCompressionMock ) . toHaveBeenCalledTimes ( 1 ) ;
}
) ;