From 667e98af5a2108c90438fbbac17d0a57f6997784 Mon Sep 17 00:00:00 2001 From: Sankalp Kotewar <98868223+kotewar@users.noreply.github.com> Date: Tue, 3 Jan 2023 10:22:38 +0000 Subject: [PATCH] Fixed test cases --- __tests__/saveOnly.test.ts | 4 ++++ __tests__/stateProvider.test.ts | 21 +++++++++++++-------- dist/restore-only/index.js | 7 ++++++- dist/restore/index.js | 7 ++++++- dist/save-only/index.js | 7 ++++++- dist/save/index.js | 7 ++++++- src/stateProvider.ts | 7 ++++++- 7 files changed, 47 insertions(+), 13 deletions(-) diff --git a/__tests__/saveOnly.test.ts b/__tests__/saveOnly.test.ts index b9bf246..a431e73 100644 --- a/__tests__/saveOnly.test.ts +++ b/__tests__/saveOnly.test.ts @@ -15,6 +15,10 @@ beforeAll(() => { return testUtils.getInput(name); }); + jest.spyOn(core, "getState").mockImplementation(name => { + return jest.requireActual("@actions/core").getState(name); + }); + jest.spyOn(actionUtils, "getInputAsArray").mockImplementation( (name, options) => { return jest diff --git a/__tests__/stateProvider.test.ts b/__tests__/stateProvider.test.ts index 39ca350..540e065 100644 --- a/__tests__/stateProvider.test.ts +++ b/__tests__/stateProvider.test.ts @@ -1,11 +1,12 @@ import * as core from "@actions/core"; -import { Events, RefKey, State } from "../src/constants"; +import { Events, Inputs, RefKey, State } from "../src/constants"; import { IStateProvider, NullStateProvider, StateProvider } from "../src/stateProvider"; +import * as testUtils from "../src/utils/testUtils"; jest.mock("@actions/core"); @@ -62,7 +63,7 @@ test("NullStateProvider saves outputs", async () => { const getInputMock = jest .spyOn(core, "getInput") - .mockImplementation(key => states.get(key) || ""); + .mockImplementation(key => testUtils.getInput(key)); const getStateMock = jest .spyOn(core, "getState") @@ -73,7 +74,7 @@ test("NullStateProvider saves outputs", async () => { const setOutputMock = jest .spyOn(core, "setOutput") .mockImplementation((key, value) => { - return jest.requireActual("@actions/core").setOutput(key, value); + states.set(key, value); }); const saveStateMock = jest @@ -83,14 +84,18 @@ test("NullStateProvider saves outputs", async () => { }); const cacheMatchedKey = "node-cache"; + const cachePrimaryKey = "primary-key"; const nullStateProvider: IStateProvider = new NullStateProvider(); - nullStateProvider.setState(State.CacheMatchedKey, "outputValue"); - nullStateProvider.setState(State.CachePrimaryKey, cacheMatchedKey); - nullStateProvider.getState("outputKey"); - nullStateProvider.getCacheState(); + testUtils.setInput(Inputs.Key, cachePrimaryKey); + nullStateProvider.setState(State.CachePrimaryKey, cachePrimaryKey); + nullStateProvider.setState(State.CacheMatchedKey, cacheMatchedKey); + const output1 = nullStateProvider.getState(State.CachePrimaryKey); + const output2 = nullStateProvider.getCacheState(); expect(getStateMock).toHaveBeenCalledTimes(0); - expect(getInputMock).toHaveBeenCalledTimes(2); + expect(getInputMock).toHaveBeenCalledTimes(1); + expect(output1).toBe("primary-key"); + expect(output2).toBe(undefined); expect(setOutputMock).toHaveBeenCalledTimes(2); expect(saveStateMock).toHaveBeenCalledTimes(0); }); diff --git a/dist/restore-only/index.js b/dist/restore-only/index.js index bb5d187..f47996e 100644 --- a/dist/restore-only/index.js +++ b/dist/restore-only/index.js @@ -9413,9 +9413,14 @@ class NullStateProvider extends StateProviderBase { [constants_1.State.CachePrimaryKey, constants_1.Outputs.CachePrimaryKey] ]); this.setState = (key, value) => { - core.setOutput(this.stateToOutputMap.get(key), value); + if (this.stateToOutputMap.has(key)) { + core.setOutput(this.stateToOutputMap.get(key), value); + } }; this.getState = (key) => { + if (!this.stateToInputMap.has(key)) { + return ""; + } return core.getInput(this.stateToInputMap.get(key)); }; } diff --git a/dist/restore/index.js b/dist/restore/index.js index c15e0f7..829985a 100644 --- a/dist/restore/index.js +++ b/dist/restore/index.js @@ -9413,9 +9413,14 @@ class NullStateProvider extends StateProviderBase { [constants_1.State.CachePrimaryKey, constants_1.Outputs.CachePrimaryKey] ]); this.setState = (key, value) => { - core.setOutput(this.stateToOutputMap.get(key), value); + if (this.stateToOutputMap.has(key)) { + core.setOutput(this.stateToOutputMap.get(key), value); + } }; this.getState = (key) => { + if (!this.stateToInputMap.has(key)) { + return ""; + } return core.getInput(this.stateToInputMap.get(key)); }; } diff --git a/dist/save-only/index.js b/dist/save-only/index.js index f9e20ed..a2be0c1 100644 --- a/dist/save-only/index.js +++ b/dist/save-only/index.js @@ -9469,9 +9469,14 @@ class NullStateProvider extends StateProviderBase { [constants_1.State.CachePrimaryKey, constants_1.Outputs.CachePrimaryKey] ]); this.setState = (key, value) => { - core.setOutput(this.stateToOutputMap.get(key), value); + if (this.stateToOutputMap.has(key)) { + core.setOutput(this.stateToOutputMap.get(key), value); + } }; this.getState = (key) => { + if (!this.stateToInputMap.has(key)) { + return ""; + } return core.getInput(this.stateToInputMap.get(key)); }; } diff --git a/dist/save/index.js b/dist/save/index.js index 065b00f..41bf2d6 100644 --- a/dist/save/index.js +++ b/dist/save/index.js @@ -9413,9 +9413,14 @@ class NullStateProvider extends StateProviderBase { [constants_1.State.CachePrimaryKey, constants_1.Outputs.CachePrimaryKey] ]); this.setState = (key, value) => { - core.setOutput(this.stateToOutputMap.get(key), value); + if (this.stateToOutputMap.has(key)) { + core.setOutput(this.stateToOutputMap.get(key), value); + } }; this.getState = (key) => { + if (!this.stateToInputMap.has(key)) { + return ""; + } return core.getInput(this.stateToInputMap.get(key)); }; } diff --git a/src/stateProvider.ts b/src/stateProvider.ts index a2de728..131ec69 100644 --- a/src/stateProvider.ts +++ b/src/stateProvider.ts @@ -43,10 +43,15 @@ export class NullStateProvider extends StateProviderBase { ]); setState = (key: string, value: string) => { - core.setOutput(this.stateToOutputMap.get(key) as string, value); + if (this.stateToOutputMap.has(key)) { + core.setOutput(this.stateToOutputMap.get(key) as string, value); + } }; getState = (key: string) => { + if (!this.stateToInputMap.has(key)) { + return ""; + } return core.getInput(this.stateToInputMap.get(key) as string); }; }