@ -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,16 +65,23 @@ 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 ) (
"restore with no path should fail" ,
async ( refKey , ref ) = > {
process . env [ refKey ] = ref ;
const failedMock = jest . spyOn ( core , "setFailed" ) ;
const failedMock = jest . spyOn ( core , "setFailed" ) ;
await run ( ) ;
await run ( ) ;
// this input isn't necessary for restore b/c tarball contains entries relative to workspace
// this input isn't necessary for restore b/c tarball contains entries relative to workspace
expect ( failedMock ) . not . toHaveBeenCalledWith (
expect ( failedMock ) . not . toHaveBeenCalledWith (
"Input required and not supplied: path"
"Input required and not supplied: path"
) ;
) ;
} ) ;
}
) ;
test . each ( refKeySet ) ( "restore with no key" , async ( refKey , ref ) = > {
process . env [ refKey ] = ref ;
test ( "restore with no key" , async ( ) = > {
testUtils . setInput ( Inputs . Path , "node_modules" ) ;
testUtils . setInput ( Inputs . Path , "node_modules" ) ;
const failedMock = jest . spyOn ( core , "setFailed" ) ;
const failedMock = jest . spyOn ( core , "setFailed" ) ;
await run ( ) ;
await run ( ) ;
@ -80,7 +90,11 @@ test("restore with no key", async () => {
) ;
) ;
} ) ;
} ) ;
test ( "restore with too many keys should fail" , async ( ) = > {
test . each ( refKeySet ) (
"restore with too many keys should fail" ,
async ( refKey , ref ) = > {
process . env [ refKey ] = ref ;
const key = "node-test" ;
const key = "node-test" ;
const restoreKeys = [ . . . Array ( 20 ) . keys ( ) ] . map ( x = > x . toString ( ) ) ;
const restoreKeys = [ . . . Array ( 20 ) . keys ( ) ] . map ( x = > x . toString ( ) ) ;
testUtils . setInputs ( {
testUtils . setInputs ( {
@ -93,9 +107,14 @@ test("restore with too many keys should fail", async () => {
expect ( failedMock ) . toHaveBeenCalledWith (
expect ( failedMock ) . toHaveBeenCalledWith (
` Key Validation Error: Keys are limited to a maximum of 10. `
` 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 ;
test ( "restore with large key should fail" , async ( ) = > {
const key = "foo" . repeat ( 512 ) ; // Over the 512 character limit
const key = "foo" . repeat ( 512 ) ; // Over the 512 character limit
testUtils . setInputs ( {
testUtils . setInputs ( {
path : "node_modules" ,
path : "node_modules" ,
@ -106,9 +125,14 @@ test("restore with large key should fail", async () => {
expect ( failedMock ) . toHaveBeenCalledWith (
expect ( failedMock ) . toHaveBeenCalledWith (
` Key Validation Error: ${ key } cannot be larger than 512 characters. `
` 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 ;
test ( "restore with invalid key should fail" , async ( ) = > {
const key = "comma,comma" ;
const key = "comma,comma" ;
testUtils . setInputs ( {
testUtils . setInputs ( {
path : "node_modules" ,
path : "node_modules" ,
@ -119,9 +143,12 @@ test("restore with invalid key should fail", async () => {
expect ( failedMock ) . toHaveBeenCalledWith (
expect ( failedMock ) . toHaveBeenCalledWith (
` Key Validation Error: ${ key } cannot contain commas. `
` 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,7 +174,11 @@ test("restore with no cache found", async () => {
) ;
) ;
} ) ;
} ) ;
test ( "restore with server error should fail" , async ( ) = > {
test . each ( refKeySet ) (
"restore with server error should fail" ,
async ( refKey , ref ) = > {
process . env [ refKey ] = ref ;
const key = "node-test" ;
const key = "node-test" ;
testUtils . setInputs ( {
testUtils . setInputs ( {
path : "node_modules" ,
path : "node_modules" ,
@ -163,7 +194,10 @@ test("restore with server error should fail", async () => {
throw new Error ( "HTTP Error Occurred" ) ;
throw new Error ( "HTTP Error Occurred" ) ;
} ) ;
} ) ;
const setCacheHitOutputMock = jest . spyOn ( actionUtils , "setCacheHitOutput" ) ;
const setCacheHitOutputMock = jest . spyOn (
actionUtils ,
"setCacheHitOutput"
) ;
await run ( ) ;
await run ( ) ;
@ -176,9 +210,14 @@ test("restore with server error should fail", async () => {
expect ( setCacheHitOutputMock ) . toHaveBeenCalledWith ( false ) ;
expect ( setCacheHitOutputMock ) . toHaveBeenCalledWith ( false ) ;
expect ( failedMock ) . toHaveBeenCalledTimes ( 0 ) ;
expect ( failedMock ) . toHaveBeenCalledTimes ( 0 ) ;
} ) ;
}
) ;
test . each ( refKeySet ) (
"restore with restore keys and no cache found" ,
async ( refKey , ref ) = > {
process . env [ refKey ] = ref ;
test ( "restore with restore keys and no cache found" , async ( ) = > {
const key = "node-test" ;
const key = "node-test" ;
const restoreKey = "node-" ;
const restoreKey = "node-" ;
testUtils . setInputs ( {
testUtils . setInputs ( {
@ -204,9 +243,14 @@ test("restore with restore keys and no cache found", async () => {
expect ( infoMock ) . toHaveBeenCalledWith (
expect ( infoMock ) . toHaveBeenCalledWith (
` Cache not found for input keys: ${ key } , ${ restoreKey } `
` Cache not found for input keys: ${ key } , ${ restoreKey } `
) ;
) ;
} ) ;
}
) ;
test . each ( refKeySet ) (
"restore with gzip compressed cache found" ,
async ( refKey , ref ) = > {
process . env [ refKey ] = ref ;
test ( "restore with gzip compressed cache found" , async ( ) = > {
const key = "node-test" ;
const key = "node-test" ;
testUtils . setInputs ( {
testUtils . setInputs ( {
path : "node_modules" ,
path : "node_modules" ,
@ -247,7 +291,10 @@ test("restore with gzip compressed cache found", async () => {
const extractTarMock = jest . spyOn ( tar , "extractTar" ) ;
const extractTarMock = jest . spyOn ( tar , "extractTar" ) ;
const unlinkFileMock = jest . spyOn ( actionUtils , "unlinkFile" ) ;
const unlinkFileMock = jest . spyOn ( actionUtils , "unlinkFile" ) ;
const setCacheHitOutputMock = jest . spyOn ( actionUtils , "setCacheHitOutput" ) ;
const setCacheHitOutputMock = jest . spyOn (
actionUtils ,
"setCacheHitOutput"
) ;
const compression = CompressionMethod . Gzip ;
const compression = CompressionMethod . Gzip ;
const getCompressionMock = jest
const getCompressionMock = jest
@ -277,12 +324,19 @@ test("restore with gzip compressed cache found", async () => {
expect ( setCacheHitOutputMock ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( setCacheHitOutputMock ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( setCacheHitOutputMock ) . toHaveBeenCalledWith ( true ) ;
expect ( setCacheHitOutputMock ) . toHaveBeenCalledWith ( true ) ;
expect ( infoMock ) . toHaveBeenCalledWith ( ` Cache restored from key: ${ key } ` ) ;
expect ( infoMock ) . toHaveBeenCalledWith (
` Cache restored from key: ${ key } `
) ;
expect ( failedMock ) . toHaveBeenCalledTimes ( 0 ) ;
expect ( failedMock ) . toHaveBeenCalledTimes ( 0 ) ;
expect ( getCompressionMock ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( getCompressionMock ) . toHaveBeenCalledTimes ( 1 ) ;
} ) ;
}
) ;
test . each ( refKeySet ) (
"restore with a pull request event and zstd compressed cache found" ,
async ( refKey , ref ) = > {
process . env [ refKey ] = ref ;
test ( "restore with a pull request event and zstd compressed cache found" , async ( ) = > {
const key = "node-test" ;
const key = "node-test" ;
testUtils . setInputs ( {
testUtils . setInputs ( {
path : "node_modules" ,
path : "node_modules" ,
@ -324,7 +378,10 @@ test("restore with a pull request event and zstd compressed cache found", async
. mockReturnValue ( fileSize ) ;
. mockReturnValue ( fileSize ) ;
const extractTarMock = jest . spyOn ( tar , "extractTar" ) ;
const extractTarMock = jest . spyOn ( tar , "extractTar" ) ;
const setCacheHitOutputMock = jest . spyOn ( actionUtils , "setCacheHitOutput" ) ;
const setCacheHitOutputMock = jest . spyOn (
actionUtils ,
"setCacheHitOutput"
) ;
const compression = CompressionMethod . Zstd ;
const compression = CompressionMethod . Zstd ;
const getCompressionMock = jest
const getCompressionMock = jest
. spyOn ( actionUtils , "getCompressionMethod" )
. spyOn ( actionUtils , "getCompressionMethod" )
@ -343,7 +400,9 @@ test("restore with a pull request event and zstd compressed cache found", async
archivePath
archivePath
) ;
) ;
expect ( getArchiveFileSizeMock ) . toHaveBeenCalledWith ( archivePath ) ;
expect ( getArchiveFileSizeMock ) . toHaveBeenCalledWith ( archivePath ) ;
expect ( infoMock ) . toHaveBeenCalledWith ( ` Cache Size: ~60 MB (62915000 B) ` ) ;
expect ( infoMock ) . toHaveBeenCalledWith (
` Cache Size: ~60 MB (62915000 B) `
) ;
expect ( extractTarMock ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( extractTarMock ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( extractTarMock ) . toHaveBeenCalledWith ( archivePath , compression ) ;
expect ( extractTarMock ) . toHaveBeenCalledWith ( archivePath , compression ) ;
@ -351,12 +410,19 @@ test("restore with a pull request event and zstd compressed cache found", async
expect ( setCacheHitOutputMock ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( setCacheHitOutputMock ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( setCacheHitOutputMock ) . toHaveBeenCalledWith ( true ) ;
expect ( setCacheHitOutputMock ) . toHaveBeenCalledWith ( true ) ;
expect ( infoMock ) . toHaveBeenCalledWith ( ` Cache restored from key: ${ key } ` ) ;
expect ( infoMock ) . toHaveBeenCalledWith (
` Cache restored from key: ${ key } `
) ;
expect ( failedMock ) . toHaveBeenCalledTimes ( 0 ) ;
expect ( failedMock ) . toHaveBeenCalledTimes ( 0 ) ;
expect ( getCompressionMock ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( getCompressionMock ) . toHaveBeenCalledTimes ( 1 ) ;
} ) ;
}
) ;
test . each ( refKeySet ) (
"restore with cache found for restore key" ,
async ( refKey , ref ) = > {
process . env [ refKey ] = ref ;
test ( "restore with cache found for restore key" , async ( ) = > {
const key = "node-test" ;
const key = "node-test" ;
const restoreKey = "node-" ;
const restoreKey = "node-" ;
testUtils . setInputs ( {
testUtils . setInputs ( {
@ -398,7 +464,10 @@ test("restore with cache found for restore key", async () => {
. mockReturnValue ( fileSize ) ;
. mockReturnValue ( fileSize ) ;
const extractTarMock = jest . spyOn ( tar , "extractTar" ) ;
const extractTarMock = jest . spyOn ( tar , "extractTar" ) ;
const setCacheHitOutputMock = jest . spyOn ( actionUtils , "setCacheHitOutput" ) ;
const setCacheHitOutputMock = jest . spyOn (
actionUtils ,
"setCacheHitOutput"
) ;
const compression = CompressionMethod . Zstd ;
const compression = CompressionMethod . Zstd ;
const getCompressionMock = jest
const getCompressionMock = jest
. spyOn ( actionUtils , "getCompressionMethod" )
. spyOn ( actionUtils , "getCompressionMethod" )
@ -430,4 +499,5 @@ test("restore with cache found for restore key", async () => {
) ;
) ;
expect ( failedMock ) . toHaveBeenCalledTimes ( 0 ) ;
expect ( failedMock ) . toHaveBeenCalledTimes ( 0 ) ;
expect ( getCompressionMock ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( getCompressionMock ) . toHaveBeenCalledTimes ( 1 ) ;
} ) ;
}
) ;