From 94f18eb26eb16f9d1d79470a9146d4a3cd0cff08 Mon Sep 17 00:00:00 2001 From: John Sudol <24583161+johnsudol@users.noreply.github.com> Date: Tue, 26 Nov 2024 23:05:11 +0000 Subject: [PATCH] Only mock the cacheUtil methods we need --- packages/cache/__tests__/saveCacheV2.test.ts | 24 ++++++++----------- .../cache/src/internal/blob/upload-cache.ts | 11 ++++++++- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/packages/cache/__tests__/saveCacheV2.test.ts b/packages/cache/__tests__/saveCacheV2.test.ts index 28e82ae0..23869a1f 100644 --- a/packages/cache/__tests__/saveCacheV2.test.ts +++ b/packages/cache/__tests__/saveCacheV2.test.ts @@ -8,10 +8,10 @@ import * as tar from '../src/internal/tar' import {CacheServiceClientJSON} from '../src/generated/results/api/v1/cache.twirp' import * as uploadCacheModule from '../src/internal/blob/upload-cache' import {BlobUploadCommonResponse} from '@azure/storage-blob' +import {InvalidResponseError} from '../src/internal/shared/errors' let logDebugMock: jest.SpyInstance -jest.mock('../src/internal/cacheUtils') jest.mock('../src/internal/tar') beforeAll(() => { @@ -21,14 +21,6 @@ beforeAll(() => { jest.spyOn(core, 'info').mockImplementation(() => {}) jest.spyOn(core, 'warning').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 => { return filePaths.map(x => path.resolve(x)) }) @@ -107,6 +99,10 @@ test('create cache entry failure', async () => { const getCompressionMock = jest .spyOn(cacheUtils, 'getCompressionMethod') .mockReturnValueOnce(Promise.resolve(compression)) + const archiveFileSize = 1024 + jest + .spyOn(cacheUtils, 'getArchiveFileSizeInBytes') + .mockReturnValueOnce(archiveFileSize) const cacheVersion = cacheUtils.getCacheVersion(paths, compression) const uploadCacheFileMock = jest .spyOn(uploadCacheModule, 'uploadCacheFile') @@ -214,15 +210,15 @@ test('save with uploadCache Server error will fail', async () => { Promise.resolve({ok: true, signedUploadUrl: signedUploadURL}) ) + const archiveFileSize = 1024 + jest + .spyOn(cacheUtils, 'getArchiveFileSizeInBytes') + .mockReturnValueOnce(archiveFileSize) jest .spyOn(uploadCacheModule, 'uploadCacheFile') - .mockReturnValueOnce(Promise.reject(new Error('HTTP Error Occurred'))) + .mockRejectedValueOnce(new InvalidResponseError('boom')) const cacheId = await saveCache([paths], key) - - expect(logWarningMock).toHaveBeenCalledWith( - `Failed to save: HTTP Error Occurred` - ) expect(cacheId).toBe(-1) }) diff --git a/packages/cache/src/internal/blob/upload-cache.ts b/packages/cache/src/internal/blob/upload-cache.ts index a171c9da..934ecb6f 100644 --- a/packages/cache/src/internal/blob/upload-cache.ts +++ b/packages/cache/src/internal/blob/upload-cache.ts @@ -5,6 +5,7 @@ import { BlockBlobClient, BlockBlobParallelUploadOptions } from '@azure/storage-blob' +import {InvalidResponseError} from '../shared/errors' export async function uploadCacheFile( signedUploadURL: string, @@ -24,5 +25,13 @@ export async function uploadCacheFile( `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 }