2022-12-12 09:53:50 +00:00
|
|
|
import * as core from "@actions/core";
|
|
|
|
|
|
|
|
import { Events, RefKey, State } from "../src/constants";
|
|
|
|
import {
|
|
|
|
IStateProvider,
|
|
|
|
NullStateProvider,
|
|
|
|
StateProvider
|
|
|
|
} from "../src/stateProvider";
|
|
|
|
|
|
|
|
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 () => {
|
2022-12-19 04:19:49 +00:00
|
|
|
let states = new Map<string, string>();
|
2022-12-12 09:53:50 +00:00
|
|
|
const getStateMock = jest
|
|
|
|
.spyOn(core, "getState")
|
2022-12-19 04:19:49 +00:00
|
|
|
.mockImplementation(key => states.get(key) || "");
|
2022-12-12 13:50:54 +00:00
|
|
|
|
2022-12-12 09:53:50 +00:00
|
|
|
const saveStateMock = jest
|
|
|
|
.spyOn(core, "saveState")
|
|
|
|
.mockImplementation((key, value) => {
|
2022-12-19 04:19:49 +00:00
|
|
|
states.set(key, value);
|
2022-12-12 09:53:50 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 13:50:54 +00:00
|
|
|
const setOutputMock = jest
|
|
|
|
.spyOn(core, "setOutput")
|
|
|
|
.mockImplementation((key, value) => {
|
|
|
|
return jest.requireActual("@actions/core").setOutput(key, value);
|
|
|
|
});
|
|
|
|
|
2022-12-12 09:53:50 +00:00
|
|
|
const cacheMatchedKey = "node-cache";
|
|
|
|
|
|
|
|
const stateProvider: IStateProvider = new StateProvider();
|
|
|
|
stateProvider.setState("stateKey", "stateValue");
|
|
|
|
stateProvider.setState(State.CacheMatchedKey, cacheMatchedKey);
|
2022-12-19 04:19:49 +00:00
|
|
|
let stateValue = stateProvider.getState("stateKey");
|
|
|
|
let cacheStateValue = stateProvider.getCacheState();
|
2022-12-12 09:53:50 +00:00
|
|
|
|
2022-12-19 04:19:49 +00:00
|
|
|
expect(stateValue).toBe("stateValue");
|
|
|
|
expect(cacheStateValue).toBe(cacheMatchedKey);
|
2022-12-12 09:53:50 +00:00
|
|
|
expect(getStateMock).toHaveBeenCalledTimes(2);
|
|
|
|
expect(saveStateMock).toHaveBeenCalledTimes(2);
|
2022-12-12 13:50:54 +00:00
|
|
|
expect(setOutputMock).toHaveBeenCalledTimes(0);
|
2022-12-12 09:53:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test("NullStateProvider saves outputs", async () => {
|
|
|
|
const getStateMock = jest
|
|
|
|
.spyOn(core, "getState")
|
|
|
|
.mockImplementation(name =>
|
|
|
|
jest.requireActual("@actions/core").getState(name)
|
|
|
|
);
|
2022-12-12 13:50:54 +00:00
|
|
|
|
2022-12-12 09:53:50 +00:00
|
|
|
const setOutputMock = jest
|
|
|
|
.spyOn(core, "setOutput")
|
|
|
|
.mockImplementation((key, value) => {
|
|
|
|
return jest.requireActual("@actions/core").setOutput(key, value);
|
|
|
|
});
|
2022-12-12 13:50:54 +00:00
|
|
|
|
|
|
|
const saveStateMock = jest
|
|
|
|
.spyOn(core, "saveState")
|
|
|
|
.mockImplementation((key, value) => {
|
|
|
|
return jest.requireActual("@actions/core").saveState(key, value);
|
|
|
|
});
|
|
|
|
|
2022-12-12 09:53:50 +00:00
|
|
|
const cacheMatchedKey = "node-cache";
|
|
|
|
const nullStateProvider: IStateProvider = new NullStateProvider();
|
2022-12-12 10:01:21 +00:00
|
|
|
nullStateProvider.setState(State.CacheMatchedKey, "outputValue");
|
|
|
|
nullStateProvider.setState(State.CachePrimaryKey, cacheMatchedKey);
|
2022-12-12 09:53:50 +00:00
|
|
|
nullStateProvider.getState("outputKey");
|
|
|
|
nullStateProvider.getCacheState();
|
|
|
|
|
|
|
|
expect(getStateMock).toHaveBeenCalledTimes(0);
|
|
|
|
expect(setOutputMock).toHaveBeenCalledTimes(2);
|
2022-12-12 13:50:54 +00:00
|
|
|
expect(saveStateMock).toHaveBeenCalledTimes(0);
|
2022-12-12 09:53:50 +00:00
|
|
|
});
|