From d79a09bc0e4f1dde4d6ebd0ad455f01cad5f1321 Mon Sep 17 00:00:00 2001 From: Sampark Sharma Date: Wed, 7 Dec 2022 08:27:08 +0000 Subject: [PATCH] Address review comments --- packages/cache/__tests__/restoreCache.test.ts | 2 +- packages/cache/src/cache.ts | 31 ++++++------------- .../cache/src/internal/cacheHttpClient.ts | 26 ++++++++++++++++ 3 files changed, 37 insertions(+), 22 deletions(-) diff --git a/packages/cache/__tests__/restoreCache.test.ts b/packages/cache/__tests__/restoreCache.test.ts index 2c9b4525..82b259b9 100644 --- a/packages/cache/__tests__/restoreCache.test.ts +++ b/packages/cache/__tests__/restoreCache.test.ts @@ -174,7 +174,7 @@ test('restore with zstd as default but gzip compressed cache found on windows', const getCacheMock = jest.spyOn(cacheHttpClient, 'getCacheEntry') getCacheMock .mockImplementationOnce(async () => { - return Promise.resolve({}) + throw new Error('Cache not found.') }) .mockImplementationOnce(async () => { return Promise.resolve(cacheEntry) diff --git a/packages/cache/src/cache.ts b/packages/cache/src/cache.ts index c0d0f85a..b2185e5e 100644 --- a/packages/cache/src/cache.ts +++ b/packages/cache/src/cache.ts @@ -96,30 +96,19 @@ export async function restoreCache( cacheEntry = await cacheHttpClient.getCacheEntry(keys, paths, { compressionMethod }) - - if (!cacheEntry?.archiveLocation) { - // Cache not found - return undefined - } } catch (error) { - if ( - process.platform === 'win32' && - compressionMethod !== CompressionMethod.Gzip - ) { - // This is to support the old cache entry created - // by the old version of the cache action on windows. - compressionMethod = CompressionMethod.Gzip - cacheEntry = await cacheHttpClient.getCacheEntry(keys, paths, { - compressionMethod - }) - if (!cacheEntry?.archiveLocation) { - throw error - } - } else { - throw error - } + cacheEntry = await cacheHttpClient.getCacheEntryForGzipFallbackOnWindows( + keys, + paths, + compressionMethod, + error + ) } + if (!cacheEntry?.archiveLocation) { + // Cache not found + return undefined + } archivePath = path.join( await utils.createTempDirectory(), utils.getCacheFileName(compressionMethod) diff --git a/packages/cache/src/internal/cacheHttpClient.ts b/packages/cache/src/internal/cacheHttpClient.ts index c66d1a73..04882612 100644 --- a/packages/cache/src/internal/cacheHttpClient.ts +++ b/packages/cache/src/internal/cacheHttpClient.ts @@ -122,6 +122,32 @@ export async function getCacheEntry( return cacheResult } +// This is to support the old cache entry created +// by the old version of the cache action on windows. +export async function getCacheEntryForGzipFallbackOnWindows( + keys: string[], + paths: string[], + compressionMethod: CompressionMethod, + error: unknown +): Promise { + let cacheEntry: ArtifactCacheEntry | null + if ( + process.platform === 'win32' && + compressionMethod !== CompressionMethod.Gzip + ) { + compressionMethod = CompressionMethod.Gzip + cacheEntry = await getCacheEntry(keys, paths, { + compressionMethod + }) + if (!cacheEntry?.archiveLocation) { + throw error + } + } else { + throw error + } + return cacheEntry +} + export async function downloadCache( archiveLocation: string, archivePath: string,