Skip to content

Commit

Permalink
add explicit compression support to cache save
Browse files Browse the repository at this point in the history
  • Loading branch information
planetmarshall committed Nov 25, 2024
1 parent 7ae9753 commit abfe1d3
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 10 deletions.
52 changes: 50 additions & 2 deletions packages/cache/__tests__/saveCache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {CacheFilename, CompressionMethod} from '../src/internal/constants'
import * as tar from '../src/internal/tar'
import {TypedResponse} from '@actions/http-client/lib/interfaces'
import {
ReserveCacheResponse,
ITypedResponseWithError
ITypedResponseWithError,
ReserveCacheResponse
} from '../src/internal/contracts'
import {HttpClientError} from '@actions/http-client'

Expand Down Expand Up @@ -319,6 +319,54 @@ test('save with valid inputs uploads a cache', async () => {
expect(getCompressionMock).toHaveBeenCalledTimes(1)
})

test('upload a cache without compression', async () => {
const filePath = 'node_modules'
const primaryKey = 'Linux-node-bb828da54c148048dd17899ba9fda624811cfb43'
const cachePaths = [path.resolve(filePath)]

const cacheId = 4
const reserveCacheMock = jest
.spyOn(cacheHttpClient, 'reserveCache')
.mockImplementation(async () => {
const response: TypedResponse<ReserveCacheResponse> = {
statusCode: 500,
result: {cacheId},
headers: {}
}
return response
})
const createTarMock = jest.spyOn(tar, 'createTar')

const saveCacheMock = jest.spyOn(cacheHttpClient, 'saveCache')
const getCompressionMock = jest.spyOn(cacheUtils, 'getCompressionMethod')

await saveCache(
[filePath],
primaryKey,
undefined,
false,
CompressionMethod.None
)

expect(reserveCacheMock).toHaveBeenCalledTimes(1)
expect(reserveCacheMock).toHaveBeenCalledWith(primaryKey, [filePath], {
cacheSize: undefined,
compressionMethod: CompressionMethod.None,
enableCrossOsArchive: false
})
const archiveFolder = '/foo/bar'
const archiveFile = path.join(archiveFolder, CacheFilename.None)
expect(createTarMock).toHaveBeenCalledTimes(1)
expect(createTarMock).toHaveBeenCalledWith(
archiveFolder,
cachePaths,
CompressionMethod.None
)
expect(saveCacheMock).toHaveBeenCalledTimes(1)
expect(saveCacheMock).toHaveBeenCalledWith(cacheId, archiveFile, undefined)
expect(getCompressionMock).toHaveBeenCalledTimes(0)
})

test('save with non existing path should not save cache', async () => {
const path = 'node_modules'
const primaryKey = 'Linux-node-bb828da54c148048dd17899ba9fda624811cfb43'
Expand Down
38 changes: 30 additions & 8 deletions packages/cache/src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,26 +318,41 @@ async function restoreCacheV2(
*
* @param paths a list of file paths to be cached
* @param key an explicit key for restoring the cache
* @param enableCrossOsArchive an optional boolean enabled to save cache on windows which could be restored on any platform
* @param options cache upload options
* @param enableCrossOsArchive an optional boolean enabled to save cache on windows which could be restored on any platform
* @param compressionMethod optionally explicitly set the compression method. The default behaviour is 'Auto' which will
* use Zstd if it is available, otherwise Gzip
* @returns number returns cacheId if the cache was saved successfully and throws an error if save fails
*/
export async function saveCache(
paths: string[],
key: string,
options?: UploadOptions,
enableCrossOsArchive = false
enableCrossOsArchive = false,
compressionMethod = CompressionMethod.Auto
): Promise<number> {
checkPaths(paths)
checkKey(key)

const cacheServiceVersion: string = getCacheServiceVersion()
switch (cacheServiceVersion) {
case 'v2':
return await saveCacheV2(paths, key, options, enableCrossOsArchive)
return await saveCacheV2(
paths,
key,
options,
enableCrossOsArchive,
compressionMethod
)
case 'v1':
default:
return await saveCacheV1(paths, key, options, enableCrossOsArchive)
return await saveCacheV1(
paths,
key,
options,
enableCrossOsArchive,
compressionMethod
)
}
}

Expand All @@ -348,15 +363,19 @@ export async function saveCache(
* @param key
* @param options
* @param enableCrossOsArchive
* @param compressionMethod
* @returns
*/
async function saveCacheV1(
paths: string[],
key: string,
options?: UploadOptions,
enableCrossOsArchive = false
enableCrossOsArchive = false,
compressionMethod = CompressionMethod.Auto
): Promise<number> {
const compressionMethod = await utils.getCompressionMethod()
if (compressionMethod === CompressionMethod.Auto) {
compressionMethod = await utils.getCompressionMethod()
}
let cacheId = -1

const cachePaths = await utils.resolvePaths(paths)
Expand Down Expand Up @@ -457,9 +476,12 @@ async function saveCacheV2(
paths: string[],
key: string,
options?: UploadOptions,
enableCrossOsArchive = false
enableCrossOsArchive = false,
compressionMethod = CompressionMethod.Auto
): Promise<number> {
const compressionMethod = await utils.getCompressionMethod()
if (compressionMethod === CompressionMethod.Auto) {
compressionMethod = await utils.getCompressionMethod()
}
const twirpClient = cacheTwirpClient.internalCacheTwirpClient()
let cacheId = -1

Expand Down

0 comments on commit abfe1d3

Please sign in to comment.