1
0
Fork 0

Only mock the cacheUtil methods we need

pull/1879/head
John Sudol 2024-11-26 23:05:11 +00:00
parent 208dbe2131
commit 94f18eb26e
2 changed files with 20 additions and 15 deletions

View File

@ -8,10 +8,10 @@ import * as tar from '../src/internal/tar'
import {CacheServiceClientJSON} from '../src/generated/results/api/v1/cache.twirp' import {CacheServiceClientJSON} from '../src/generated/results/api/v1/cache.twirp'
import * as uploadCacheModule from '../src/internal/blob/upload-cache' import * as uploadCacheModule from '../src/internal/blob/upload-cache'
import {BlobUploadCommonResponse} from '@azure/storage-blob' import {BlobUploadCommonResponse} from '@azure/storage-blob'
import {InvalidResponseError} from '../src/internal/shared/errors'
let logDebugMock: jest.SpyInstance let logDebugMock: jest.SpyInstance
jest.mock('../src/internal/cacheUtils')
jest.mock('../src/internal/tar') jest.mock('../src/internal/tar')
beforeAll(() => { beforeAll(() => {
@ -21,14 +21,6 @@ beforeAll(() => {
jest.spyOn(core, 'info').mockImplementation(() => {}) jest.spyOn(core, 'info').mockImplementation(() => {})
jest.spyOn(core, 'warning').mockImplementation(() => {}) jest.spyOn(core, 'warning').mockImplementation(() => {})
jest.spyOn(core, 'error').mockImplementation(() => {}) jest.spyOn(core, 'error').mockImplementation(() => {})
jest.spyOn(cacheUtils, 'getCacheFileName').mockImplementation(cm => {
const actualUtils = jest.requireActual('../src/internal/cacheUtils')
return actualUtils.getCacheFileName(cm)
})
jest.spyOn(cacheUtils, 'getCacheVersion').mockImplementation((paths, cm) => {
const actualUtils = jest.requireActual('../src/internal/cacheUtils')
return actualUtils.getCacheVersion(paths, cm)
})
jest.spyOn(cacheUtils, 'resolvePaths').mockImplementation(async filePaths => { jest.spyOn(cacheUtils, 'resolvePaths').mockImplementation(async filePaths => {
return filePaths.map(x => path.resolve(x)) return filePaths.map(x => path.resolve(x))
}) })
@ -107,6 +99,10 @@ test('create cache entry failure', async () => {
const getCompressionMock = jest const getCompressionMock = jest
.spyOn(cacheUtils, 'getCompressionMethod') .spyOn(cacheUtils, 'getCompressionMethod')
.mockReturnValueOnce(Promise.resolve(compression)) .mockReturnValueOnce(Promise.resolve(compression))
const archiveFileSize = 1024
jest
.spyOn(cacheUtils, 'getArchiveFileSizeInBytes')
.mockReturnValueOnce(archiveFileSize)
const cacheVersion = cacheUtils.getCacheVersion(paths, compression) const cacheVersion = cacheUtils.getCacheVersion(paths, compression)
const uploadCacheFileMock = jest const uploadCacheFileMock = jest
.spyOn(uploadCacheModule, 'uploadCacheFile') .spyOn(uploadCacheModule, 'uploadCacheFile')
@ -214,15 +210,15 @@ test('save with uploadCache Server error will fail', async () => {
Promise.resolve({ok: true, signedUploadUrl: signedUploadURL}) Promise.resolve({ok: true, signedUploadUrl: signedUploadURL})
) )
const archiveFileSize = 1024
jest
.spyOn(cacheUtils, 'getArchiveFileSizeInBytes')
.mockReturnValueOnce(archiveFileSize)
jest jest
.spyOn(uploadCacheModule, 'uploadCacheFile') .spyOn(uploadCacheModule, 'uploadCacheFile')
.mockReturnValueOnce(Promise.reject(new Error('HTTP Error Occurred'))) .mockRejectedValueOnce(new InvalidResponseError('boom'))
const cacheId = await saveCache([paths], key) const cacheId = await saveCache([paths], key)
expect(logWarningMock).toHaveBeenCalledWith(
`Failed to save: HTTP Error Occurred`
)
expect(cacheId).toBe(-1) expect(cacheId).toBe(-1)
}) })

View File

@ -5,6 +5,7 @@ import {
BlockBlobClient, BlockBlobClient,
BlockBlobParallelUploadOptions BlockBlobParallelUploadOptions
} from '@azure/storage-blob' } from '@azure/storage-blob'
import {InvalidResponseError} from '../shared/errors'
export async function uploadCacheFile( export async function uploadCacheFile(
signedUploadURL: string, signedUploadURL: string,
@ -24,5 +25,13 @@ export async function uploadCacheFile(
`BlobClient: ${blobClient.name}:${blobClient.accountName}:${blobClient.containerName}` `BlobClient: ${blobClient.name}:${blobClient.accountName}:${blobClient.containerName}`
) )
return blockBlobClient.uploadFile(archivePath, uploadOptions) const resp = await blockBlobClient.uploadFile(archivePath, uploadOptions)
if (resp._response.status >= 400) {
throw new InvalidResponseError(
`Upload failed with status code: ${resp._response.status}`
)
}
return resp
} }