From 5b41e77e7a897710a0c184a0f4ba8f49943da839 Mon Sep 17 00:00:00 2001 From: Eliza Brock Marcum Date: Wed, 24 Jun 2020 13:50:45 -0500 Subject: [PATCH] Fix caching using update key --- __tests__/actionUtils.test.ts | 10 ---------- __tests__/save.test.ts | 7 +++++-- src/save.ts | 22 +++++++++++++--------- src/utils/actionUtils.ts | 7 ------- src/utils/testUtils.ts | 3 +++ 5 files changed, 21 insertions(+), 28 deletions(-) diff --git a/__tests__/actionUtils.test.ts b/__tests__/actionUtils.test.ts index 0a11d1f..a6dc291 100644 --- a/__tests__/actionUtils.test.ts +++ b/__tests__/actionUtils.test.ts @@ -194,13 +194,3 @@ test("getInputAsArray handles empty lines correctly", () => { testUtils.setInput("foo", "\n\nbar\n\nbaz\n\n"); expect(actionUtils.getInputAsArray("foo")).toEqual(["bar", "baz"]); }); - -test("getInputAsBoolean returns true if the value is set to 'true'", () => { - testUtils.setInput("foo", "true"); - expect(actionUtils.getInputAsBoolean("foo")).toEqual(true); -}); - -test("getInputAsBoolean returns false if the value is set to anything else", () => { - testUtils.setInput("foo", "false"); - expect(actionUtils.getInputAsBoolean("foo")).toEqual(false); -}); diff --git a/__tests__/save.test.ts b/__tests__/save.test.ts index 3c55645..ab78d77 100644 --- a/__tests__/save.test.ts +++ b/__tests__/save.test.ts @@ -118,7 +118,8 @@ test("save with exact match returns early", async () => { expect(failedMock).toHaveBeenCalledTimes(0); }); -test("save with exact match updates when configured", async () => { +test("save with exact match and updates enabled updates the cache", async () => { + const infoMock = jest.spyOn(core, "info"); const failedMock = jest.spyOn(core, "setFailed"); const primaryKey = "Linux-node-bb828da54c148048dd17899ba9fda624811cfb43"; @@ -147,9 +148,11 @@ test("save with exact match updates when configured", async () => { await run(); + expect(infoMock).toHaveBeenCalledWith( + `Cache hit occurred on the primary key ${primaryKey}, but updates were enabled, so updating cache.` + ); expect(saveCacheMock).toHaveBeenCalledTimes(1); expect(saveCacheMock).toHaveBeenCalledWith([inputPath], primaryKey); - expect(failedMock).toHaveBeenCalledTimes(0); }); diff --git a/src/save.ts b/src/save.ts index 6897a57..24fa720 100644 --- a/src/save.ts +++ b/src/save.ts @@ -16,21 +16,25 @@ async function run(): Promise { } const state = utils.getCacheState(); - // Inputs are re-evaluated before the post action, so we want the original key used for restore + + // Inputs are re-evaluted before the post action, so we want the original key used for restore const primaryKey = core.getState(State.CachePrimaryKey); if (!primaryKey) { utils.logWarning(`Error retrieving key from state.`); return; } - if ( - utils.isExactKeyMatch(primaryKey, state) && - !utils.getInputAsBoolean(Inputs.Update) - ) { - core.info( - `Cache hit occurred on the primary key ${primaryKey}, not saving cache.` - ); - return; + if (utils.isExactKeyMatch(primaryKey, state)) { + if (core.getInput(Inputs.Update) === "true") { + core.info( + `Cache hit occurred on the primary key ${primaryKey}, but updates were enabled, so updating cache.` + ); + } else { + core.info( + `Cache hit occurred on the primary key ${primaryKey}, not saving cache.` + ); + return; + } } const cachePaths = utils.getInputAsArray(Inputs.Path, { diff --git a/src/utils/actionUtils.ts b/src/utils/actionUtils.ts index 256b8e6..26bb8c1 100644 --- a/src/utils/actionUtils.ts +++ b/src/utils/actionUtils.ts @@ -56,10 +56,3 @@ export function getInputAsArray( .map(s => s.trim()) .filter(x => x !== ""); } - -export function getInputAsBoolean( - name: string, - options?: core.InputOptions -): boolean { - return core.getInput(name, options) === "true"; -} diff --git a/src/utils/testUtils.ts b/src/utils/testUtils.ts index b1d20d0..f6b0261 100644 --- a/src/utils/testUtils.ts +++ b/src/utils/testUtils.ts @@ -13,11 +13,13 @@ interface CacheInput { path: string; key: string; restoreKeys?: string[]; + update: string; } export function setInputs(input: CacheInput): void { setInput(Inputs.Path, input.path); setInput(Inputs.Key, input.key); + setInput(Inputs.Update, input.update); input.restoreKeys && setInput(Inputs.RestoreKeys, input.restoreKeys.join("\n")); } @@ -25,5 +27,6 @@ export function setInputs(input: CacheInput): void { export function clearInputs(): void { delete process.env[getInputName(Inputs.Path)]; delete process.env[getInputName(Inputs.Key)]; + delete process.env[getInputName(Inputs.Update)]; delete process.env[getInputName(Inputs.RestoreKeys)]; }