From 60b802d0e873ec7b062472ac8ebec7d98e48da98 Mon Sep 17 00:00:00 2001 From: Shubham Tiwari <64764738+tiwarishub@users.noreply.github.com> Date: Sun, 26 Jun 2022 07:59:55 +0000 Subject: [PATCH] adding test case --- __tests__/cache-save.test.ts | 57 ++++++++++++++++++++++++++++++++++++ dist/cache-save/index.js | 13 +++----- src/cache-save.ts | 14 ++++----- 3 files changed, 66 insertions(+), 18 deletions(-) diff --git a/__tests__/cache-save.test.ts b/__tests__/cache-save.test.ts index 82db2195..75dd189d 100644 --- a/__tests__/cache-save.test.ts +++ b/__tests__/cache-save.test.ts @@ -294,6 +294,63 @@ describe('run', () => { ); expect(setFailedSpy).not.toHaveBeenCalled(); }); + + it('save with -1 cacheId , should not fail workflow', async () => { + inputs['cache'] = 'npm'; + getStateSpy.mockImplementation((name: string) => { + if (name === State.CacheMatchedKey) { + return npmFileHash; + } else { + return yarnFileHash; + } + }); + getCommandOutputSpy.mockImplementationOnce(() => `${commonPath}/npm`); + saveCacheSpy.mockImplementation(() => { + return -1; + }); + + await run(); + + expect(getInputSpy).toHaveBeenCalled(); + expect(getStateSpy).toHaveBeenCalledTimes(2); + expect(getCommandOutputSpy).toHaveBeenCalledTimes(1); + expect(debugSpy).toHaveBeenCalledWith(`npm path is ${commonPath}/npm`); + expect(infoSpy).not.toHaveBeenCalledWith( + `Cache hit occurred on the primary key ${npmFileHash}, not saving cache.` + ); + expect(saveCacheSpy).toHaveBeenCalled(); + expect(infoSpy).not.toHaveBeenLastCalledWith( + `Cache saved with the key: ${yarnFileHash}` + ); + expect(setFailedSpy).not.toHaveBeenCalled(); + }); + + it('saves with error from toolkit, should fail workflow', async () => { + inputs['cache'] = 'npm'; + getStateSpy.mockImplementation((name: string) => { + if (name === State.CacheMatchedKey) { + return npmFileHash; + } else { + return yarnFileHash; + } + }); + getCommandOutputSpy.mockImplementationOnce(() => `${commonPath}/npm`); + saveCacheSpy.mockImplementation(() => { + throw new cache.ValidationError('Validation failed'); + }); + + await run(); + + expect(getInputSpy).toHaveBeenCalled(); + expect(getStateSpy).toHaveBeenCalledTimes(2); + expect(getCommandOutputSpy).toHaveBeenCalledTimes(1); + expect(debugSpy).toHaveBeenCalledWith(`npm path is ${commonPath}/npm`); + expect(infoSpy).not.toHaveBeenCalledWith( + `Cache hit occurred on the primary key ${npmFileHash}, not saving cache.` + ); + expect(saveCacheSpy).toHaveBeenCalled(); + expect(setFailedSpy).toHaveBeenCalled(); + }); }); afterEach(() => { diff --git a/dist/cache-save/index.js b/dist/cache-save/index.js index a6af69fd..a55d2327 100644 --- a/dist/cache-save/index.js +++ b/dist/cache-save/index.js @@ -59888,16 +59888,11 @@ const cachePackages = (packageManager) => __awaiter(void 0, void 0, void 0, func core.info(`Cache hit occurred on the primary key ${primaryKey}, not saving cache.`); return; } - try { - const cacheId = yield cache.saveCache([cachePath], primaryKey); - if (cacheId == -1) { - return; - } - core.info(`Cache saved with the key: ${primaryKey}`); - } - catch (error) { - core.warning(`${error.message}`); + const cacheId = yield cache.saveCache([cachePath], primaryKey); + if (cacheId == -1) { + return; } + core.info(`Cache saved with the key: ${primaryKey}`); }); run(); diff --git a/src/cache-save.ts b/src/cache-save.ts index 33209f3c..24565a8e 100644 --- a/src/cache-save.ts +++ b/src/cache-save.ts @@ -49,16 +49,12 @@ const cachePackages = async (packageManager: string) => { return; } - try { - const cacheId = await cache.saveCache([cachePath], primaryKey); - if (cacheId == -1) { - return; - } - - core.info(`Cache saved with the key: ${primaryKey}`); - } catch (error) { - core.warning(`${error.message}`); + const cacheId = await cache.saveCache([cachePath], primaryKey); + if (cacheId == -1) { + return; } + + core.info(`Cache saved with the key: ${primaryKey}`); }; run();