11import * as core from '@actions/core'
22import * as path from 'path'
3- import { saveCache } from '../src/cache'
3+ import { saveCache , setFileSizeLimit } from '../src/cache'
44import * as cacheHttpClient from '../src/internal/cacheHttpClient'
55import * as cacheUtils from '../src/internal/cacheUtils'
66import * as config from '../src/internal/config'
7- import { CacheFilename , CompressionMethod } from '../src/internal/constants'
7+ import { CacheFilename , CacheFileSizeLimit , CompressionMethod } from '../src/internal/constants'
88import * as tar from '../src/internal/tar'
99import { TypedResponse } from '@actions/http-client/lib/interfaces'
1010import {
@@ -19,6 +19,7 @@ jest.mock('../src/internal/config')
1919jest . mock ( '../src/internal/tar' )
2020
2121beforeAll ( ( ) => {
22+ setFileSizeLimit ( CacheFileSizeLimit )
2223 jest . spyOn ( console , 'log' ) . mockImplementation ( ( ) => { } )
2324 jest . spyOn ( core , 'debug' ) . mockImplementation ( ( ) => { } )
2425 jest . spyOn ( core , 'info' ) . mockImplementation ( ( ) => { } )
@@ -79,6 +80,42 @@ test('save with large cache outputs should fail', async () => {
7980 expect ( getCompressionMock ) . toHaveBeenCalledTimes ( 1 )
8081} )
8182
83+ test ( 'save with small cache outputs should fail on changed limit' , async ( ) => {
84+ setFileSizeLimit ( 100 * 1024 * 1024 ) // set default limit to 100 MB
85+ const filePath = 'node_modules'
86+ const primaryKey = 'Linux-node-bb828da54c148048dd17899ba9fda624811cfb43'
87+ const cachePaths = [ path . resolve ( filePath ) ]
88+
89+ const createTarMock = jest . spyOn ( tar , 'createTar' )
90+ const logWarningMock = jest . spyOn ( core , 'warning' )
91+
92+ const cacheSize = 1024 * 1024 * 1024 //1GB, over the 100MB limit
93+ jest
94+ . spyOn ( cacheUtils , 'getArchiveFileSizeInBytes' )
95+ . mockReturnValueOnce ( cacheSize )
96+ const compression = CompressionMethod . Gzip
97+ const getCompressionMock = jest
98+ . spyOn ( cacheUtils , 'getCompressionMethod' )
99+ . mockReturnValueOnce ( Promise . resolve ( compression ) )
100+
101+ const cacheId = await saveCache ( [ filePath ] , primaryKey )
102+ expect ( cacheId ) . toBe ( - 1 )
103+ expect ( logWarningMock ) . toHaveBeenCalledTimes ( 1 )
104+ expect ( logWarningMock ) . toHaveBeenCalledWith (
105+ 'Failed to save: Cache size of ~1024 MB (1048576 B) is over the 100MB limit, not saving cache.'
106+ )
107+
108+ const archiveFolder = '/foo/bar'
109+
110+ expect ( createTarMock ) . toHaveBeenCalledTimes ( 1 )
111+ expect ( createTarMock ) . toHaveBeenCalledWith (
112+ archiveFolder ,
113+ cachePaths ,
114+ compression
115+ )
116+ expect ( getCompressionMock ) . toHaveBeenCalledTimes ( 1 )
117+ } )
118+
82119test ( 'save with large cache outputs should fail in GHES with error message' , async ( ) => {
83120 const filePath = 'node_modules'
84121 const primaryKey = 'Linux-node-bb828da54c148048dd17899ba9fda624811cfb43'
0 commit comments