From 5b7eeecaebfc25e4ec267629647daa09be203c48 Mon Sep 17 00:00:00 2001 From: Sankalp Kotewar <98868223+kotewar@users.noreply.github.com> Date: Mon, 2 Jan 2023 07:29:20 +0000 Subject: [PATCH] stateinputprovider with pending test cases fix --- __tests__/saveOnly.test.ts | 8 ++------ __tests__/stateProvider.test.ts | 15 +++++++++++---- dist/restore-only/index.js | 8 ++++++-- dist/restore/index.js | 8 ++++++-- dist/save-only/index.js | 11 +++++++---- dist/save/index.js | 11 +++++++---- src/saveImpl.ts | 4 +--- src/stateProvider.ts | 12 +++++++++--- src/utils/testUtils.ts | 4 ++++ 9 files changed, 53 insertions(+), 28 deletions(-) diff --git a/__tests__/saveOnly.test.ts b/__tests__/saveOnly.test.ts index 44bd732..b9bf246 100644 --- a/__tests__/saveOnly.test.ts +++ b/__tests__/saveOnly.test.ts @@ -11,12 +11,8 @@ jest.mock("@actions/cache"); jest.mock("../src/utils/actionUtils"); beforeAll(() => { - jest.spyOn(core, "getInput").mockImplementation((name, options) => { - return jest.requireActual("@actions/core").getInput(name, options); - }); - - jest.spyOn(core, "setOutput").mockImplementation((key, value) => { - return jest.requireActual("@actions/core").getInput(key, value); + jest.spyOn(core, "getInput").mockImplementation(name => { + return testUtils.getInput(name); }); jest.spyOn(actionUtils, "getInputAsArray").mockImplementation( diff --git a/__tests__/stateProvider.test.ts b/__tests__/stateProvider.test.ts index 255b5fb..39ca350 100644 --- a/__tests__/stateProvider.test.ts +++ b/__tests__/stateProvider.test.ts @@ -58,11 +58,17 @@ test("StateProvider saves states", async () => { }); test("NullStateProvider saves outputs", async () => { + const states = new Map(); + + const getInputMock = jest + .spyOn(core, "getInput") + .mockImplementation(key => states.get(key) || ""); + const getStateMock = jest .spyOn(core, "getState") - .mockImplementation(name => - jest.requireActual("@actions/core").getState(name) - ); + .mockImplementation(key => { + return jest.requireActual("@actions/core").getState(key); + }); const setOutputMock = jest .spyOn(core, "setOutput") @@ -73,7 +79,7 @@ test("NullStateProvider saves outputs", async () => { const saveStateMock = jest .spyOn(core, "saveState") .mockImplementation((key, value) => { - return jest.requireActual("@actions/core").saveState(key, value); + states.set(key, value); }); const cacheMatchedKey = "node-cache"; @@ -84,6 +90,7 @@ test("NullStateProvider saves outputs", async () => { nullStateProvider.getCacheState(); expect(getStateMock).toHaveBeenCalledTimes(0); + expect(getInputMock).toHaveBeenCalledTimes(2); expect(setOutputMock).toHaveBeenCalledTimes(2); expect(saveStateMock).toHaveBeenCalledTimes(0); }); diff --git a/dist/restore-only/index.js b/dist/restore-only/index.js index 4f19308..bb5d187 100644 --- a/dist/restore-only/index.js +++ b/dist/restore-only/index.js @@ -9405,6 +9405,9 @@ exports.StateProvider = StateProvider; class NullStateProvider extends StateProviderBase { constructor() { super(...arguments); + this.stateToInputMap = new Map([ + [constants_1.State.CachePrimaryKey, constants_1.Inputs.Key] + ]); this.stateToOutputMap = new Map([ [constants_1.State.CacheMatchedKey, constants_1.Outputs.CacheMatchedKey], [constants_1.State.CachePrimaryKey, constants_1.Outputs.CachePrimaryKey] @@ -9412,8 +9415,9 @@ class NullStateProvider extends StateProviderBase { this.setState = (key, value) => { core.setOutput(this.stateToOutputMap.get(key), value); }; - // eslint-disable-next-line @typescript-eslint/no-unused-vars - this.getState = (key) => ""; + this.getState = (key) => { + return core.getInput(this.stateToInputMap.get(key)); + }; } } exports.NullStateProvider = NullStateProvider; diff --git a/dist/restore/index.js b/dist/restore/index.js index 81e33e9..c15e0f7 100644 --- a/dist/restore/index.js +++ b/dist/restore/index.js @@ -9405,6 +9405,9 @@ exports.StateProvider = StateProvider; class NullStateProvider extends StateProviderBase { constructor() { super(...arguments); + this.stateToInputMap = new Map([ + [constants_1.State.CachePrimaryKey, constants_1.Inputs.Key] + ]); this.stateToOutputMap = new Map([ [constants_1.State.CacheMatchedKey, constants_1.Outputs.CacheMatchedKey], [constants_1.State.CachePrimaryKey, constants_1.Outputs.CachePrimaryKey] @@ -9412,8 +9415,9 @@ class NullStateProvider extends StateProviderBase { this.setState = (key, value) => { core.setOutput(this.stateToOutputMap.get(key), value); }; - // eslint-disable-next-line @typescript-eslint/no-unused-vars - this.getState = (key) => ""; + this.getState = (key) => { + return core.getInput(this.stateToInputMap.get(key)); + }; } } exports.NullStateProvider = NullStateProvider; diff --git a/dist/save-only/index.js b/dist/save-only/index.js index 8573690..f9e20ed 100644 --- a/dist/save-only/index.js +++ b/dist/save-only/index.js @@ -9461,6 +9461,9 @@ exports.StateProvider = StateProvider; class NullStateProvider extends StateProviderBase { constructor() { super(...arguments); + this.stateToInputMap = new Map([ + [constants_1.State.CachePrimaryKey, constants_1.Inputs.Key] + ]); this.stateToOutputMap = new Map([ [constants_1.State.CacheMatchedKey, constants_1.Outputs.CacheMatchedKey], [constants_1.State.CachePrimaryKey, constants_1.Outputs.CachePrimaryKey] @@ -9468,8 +9471,9 @@ class NullStateProvider extends StateProviderBase { this.setState = (key, value) => { core.setOutput(this.stateToOutputMap.get(key), value); }; - // eslint-disable-next-line @typescript-eslint/no-unused-vars - this.getState = (key) => ""; + this.getState = (key) => { + return core.getInput(this.stateToInputMap.get(key)); + }; } } exports.NullStateProvider = NullStateProvider; @@ -41059,8 +41063,7 @@ function saveImpl(stateProvider) { } // If restore has stored a primary key in state, reuse that // Else re-evaluate from inputs - const primaryKey = stateProvider.getState(constants_1.State.CachePrimaryKey) || - core.getInput(constants_1.Inputs.Key); + const primaryKey = stateProvider.getState(constants_1.State.CachePrimaryKey); if (!primaryKey) { utils.logWarning(`Key is not specified.`); return; diff --git a/dist/save/index.js b/dist/save/index.js index 9657785..065b00f 100644 --- a/dist/save/index.js +++ b/dist/save/index.js @@ -9405,6 +9405,9 @@ exports.StateProvider = StateProvider; class NullStateProvider extends StateProviderBase { constructor() { super(...arguments); + this.stateToInputMap = new Map([ + [constants_1.State.CachePrimaryKey, constants_1.Inputs.Key] + ]); this.stateToOutputMap = new Map([ [constants_1.State.CacheMatchedKey, constants_1.Outputs.CacheMatchedKey], [constants_1.State.CachePrimaryKey, constants_1.Outputs.CachePrimaryKey] @@ -9412,8 +9415,9 @@ class NullStateProvider extends StateProviderBase { this.setState = (key, value) => { core.setOutput(this.stateToOutputMap.get(key), value); }; - // eslint-disable-next-line @typescript-eslint/no-unused-vars - this.getState = (key) => ""; + this.getState = (key) => { + return core.getInput(this.stateToInputMap.get(key)); + }; } } exports.NullStateProvider = NullStateProvider; @@ -41003,8 +41007,7 @@ function saveImpl(stateProvider) { } // If restore has stored a primary key in state, reuse that // Else re-evaluate from inputs - const primaryKey = stateProvider.getState(constants_1.State.CachePrimaryKey) || - core.getInput(constants_1.Inputs.Key); + const primaryKey = stateProvider.getState(constants_1.State.CachePrimaryKey); if (!primaryKey) { utils.logWarning(`Key is not specified.`); return; diff --git a/src/saveImpl.ts b/src/saveImpl.ts index d8d7674..e49a8ba 100644 --- a/src/saveImpl.ts +++ b/src/saveImpl.ts @@ -28,9 +28,7 @@ async function saveImpl(stateProvider: IStateProvider): Promise { // If restore has stored a primary key in state, reuse that // Else re-evaluate from inputs - const primaryKey = - stateProvider.getState(State.CachePrimaryKey) || - core.getInput(Inputs.Key); + const primaryKey = stateProvider.getState(State.CachePrimaryKey); if (!primaryKey) { utils.logWarning(`Key is not specified.`); diff --git a/src/stateProvider.ts b/src/stateProvider.ts index beb41e5..a2de728 100644 --- a/src/stateProvider.ts +++ b/src/stateProvider.ts @@ -1,6 +1,6 @@ import * as core from "@actions/core"; -import { Outputs, State } from "./constants"; +import { Inputs, Outputs, State } from "./constants"; export interface IStateProvider { setState(key: string, value: string): void; @@ -33,6 +33,10 @@ export class StateProvider extends StateProviderBase { } export class NullStateProvider extends StateProviderBase { + stateToInputMap = new Map([ + [State.CachePrimaryKey, Inputs.Key] + ]); + stateToOutputMap = new Map([ [State.CacheMatchedKey, Outputs.CacheMatchedKey], [State.CachePrimaryKey, Outputs.CachePrimaryKey] @@ -41,6 +45,8 @@ export class NullStateProvider extends StateProviderBase { setState = (key: string, value: string) => { core.setOutput(this.stateToOutputMap.get(key) as string, value); }; - // eslint-disable-next-line @typescript-eslint/no-unused-vars - getState = (key: string) => ""; + + getState = (key: string) => { + return core.getInput(this.stateToInputMap.get(key) as string); + }; } diff --git a/src/utils/testUtils.ts b/src/utils/testUtils.ts index 9e2134f..8edef15 100644 --- a/src/utils/testUtils.ts +++ b/src/utils/testUtils.ts @@ -9,6 +9,10 @@ export function setInput(name: string, value: string): void { process.env[getInputName(name)] = value; } +export function getInput(name: string): string { + return process.env[getInputName(name)] as string; +} + interface CacheInput { path: string; key: string;