1
1
import * as core from '@actions/core'
2
2
import * as path from 'path'
3
- import { saveCache } from '../src/cache'
3
+ import { saveCache , setFileSizeLimit } from '../src/cache'
4
4
import * as cacheHttpClient from '../src/internal/cacheHttpClient'
5
5
import * as cacheUtils from '../src/internal/cacheUtils'
6
6
import * as config from '../src/internal/config'
7
- import { CacheFilename , CompressionMethod } from '../src/internal/constants'
7
+ import { CacheFilename , CacheFileSizeLimit , CompressionMethod } from '../src/internal/constants'
8
8
import * as tar from '../src/internal/tar'
9
9
import { TypedResponse } from '@actions/http-client/lib/interfaces'
10
10
import {
@@ -19,6 +19,7 @@ jest.mock('../src/internal/config')
19
19
jest . mock ( '../src/internal/tar' )
20
20
21
21
beforeAll ( ( ) => {
22
+ setFileSizeLimit ( CacheFileSizeLimit )
22
23
jest . spyOn ( console , 'log' ) . mockImplementation ( ( ) => { } )
23
24
jest . spyOn ( core , 'debug' ) . mockImplementation ( ( ) => { } )
24
25
jest . spyOn ( core , 'info' ) . mockImplementation ( ( ) => { } )
@@ -79,6 +80,42 @@ test('save with large cache outputs should fail', async () => {
79
80
expect ( getCompressionMock ) . toHaveBeenCalledTimes ( 1 )
80
81
} )
81
82
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
+
82
119
test ( 'save with large cache outputs should fail in GHES with error message' , async ( ) => {
83
120
const filePath = 'node_modules'
84
121
const primaryKey = 'Linux-node-bb828da54c148048dd17899ba9fda624811cfb43'
0 commit comments