2022-12-21 21:08:44 +07:00
|
|
|
import * as core from "@actions/core";
|
|
|
|
|
2023-01-03 17:22:38 +07:00
|
|
|
import { Events, Inputs, RefKey, State } from "../src/constants";
|
2022-12-21 21:08:44 +07:00
|
|
|
import {
|
|
|
|
IStateProvider,
|
|
|
|
NullStateProvider,
|
|
|
|
StateProvider
|
|
|
|
} from "../src/stateProvider";
|
2023-01-03 17:22:38 +07:00
|
|
|
import * as testUtils from "../src/utils/testUtils";
|
2022-12-21 21:08:44 +07:00
|
|
|
|
|
|
|
jest.mock("@actions/core");
|
|
|
|
|
|
|
|
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").setOutput(key, value);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
delete process.env[Events.Key];
|
|
|
|
delete process.env[RefKey];
|
|
|
|
});
|
|
|
|
|
|
|
|
test("StateProvider saves states", async () => {
|
|
|
|
const states = new Map<string, string>();
|
|
|
|
const getStateMock = jest
|
|
|
|
.spyOn(core, "getState")
|
|
|
|
.mockImplementation(key => states.get(key) || "");
|
|
|
|
|
|
|
|
const saveStateMock = jest
|
|
|
|
.spyOn(core, "saveState")
|
|
|
|
.mockImplementation((key, value) => {
|
|
|
|
states.set(key, value);
|
|
|
|
});
|
|
|
|
|
|
|
|
const setOutputMock = jest
|
|
|
|
.spyOn(core, "setOutput")
|
|
|
|
.mockImplementation((key, value) => {
|
|
|
|
return jest.requireActual("@actions/core").setOutput(key, value);
|
|
|
|
});
|
|
|
|
|
|
|
|
const cacheMatchedKey = "node-cache";
|
|
|
|
|
|
|
|
const stateProvider: IStateProvider = new StateProvider();
|
|
|
|
stateProvider.setState("stateKey", "stateValue");
|
|
|
|
stateProvider.setState(State.CacheMatchedKey, cacheMatchedKey);
|
|
|
|
const stateValue = stateProvider.getState("stateKey");
|
|
|
|
const cacheStateValue = stateProvider.getCacheState();
|
|
|
|
|
|
|
|
expect(stateValue).toBe("stateValue");
|
|
|
|
expect(cacheStateValue).toBe(cacheMatchedKey);
|
|
|
|
expect(getStateMock).toHaveBeenCalledTimes(2);
|
|
|
|
expect(saveStateMock).toHaveBeenCalledTimes(2);
|
|
|
|
expect(setOutputMock).toHaveBeenCalledTimes(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("NullStateProvider saves outputs", async () => {
|
2023-01-02 14:29:20 +07:00
|
|
|
const states = new Map<string, string>();
|
|
|
|
|
|
|
|
const getInputMock = jest
|
|
|
|
.spyOn(core, "getInput")
|
2023-01-03 17:22:38 +07:00
|
|
|
.mockImplementation(key => testUtils.getInput(key));
|
2023-01-02 14:29:20 +07:00
|
|
|
|
2022-12-21 21:08:44 +07:00
|
|
|
const getStateMock = jest
|
|
|
|
.spyOn(core, "getState")
|
2023-01-02 14:29:20 +07:00
|
|
|
.mockImplementation(key => {
|
|
|
|
return jest.requireActual("@actions/core").getState(key);
|
|
|
|
});
|
2022-12-21 21:08:44 +07:00
|
|
|
|
|
|
|
const setOutputMock = jest
|
|
|
|
.spyOn(core, "setOutput")
|
|
|
|
.mockImplementation((key, value) => {
|
2023-01-03 17:22:38 +07:00
|
|
|
states.set(key, value);
|
2022-12-21 21:08:44 +07:00
|
|
|
});
|
|
|
|
|
|
|
|
const saveStateMock = jest
|
|
|
|
.spyOn(core, "saveState")
|
|
|
|
.mockImplementation((key, value) => {
|
2023-01-02 14:29:20 +07:00
|
|
|
states.set(key, value);
|
2022-12-21 21:08:44 +07:00
|
|
|
});
|
|
|
|
|
|
|
|
const cacheMatchedKey = "node-cache";
|
2023-01-03 17:22:38 +07:00
|
|
|
const cachePrimaryKey = "primary-key";
|
2022-12-21 21:08:44 +07:00
|
|
|
const nullStateProvider: IStateProvider = new NullStateProvider();
|
2023-01-03 17:22:38 +07:00
|
|
|
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();
|
2022-12-21 21:08:44 +07:00
|
|
|
|
|
|
|
expect(getStateMock).toHaveBeenCalledTimes(0);
|
2023-01-03 17:22:38 +07:00
|
|
|
expect(getInputMock).toHaveBeenCalledTimes(1);
|
|
|
|
expect(output1).toBe("primary-key");
|
|
|
|
expect(output2).toBe(undefined);
|
2022-12-21 21:08:44 +07:00
|
|
|
expect(setOutputMock).toHaveBeenCalledTimes(2);
|
|
|
|
expect(saveStateMock).toHaveBeenCalledTimes(0);
|
|
|
|
});
|