2022-12-21 15:08:44 +01:00
|
|
|
import * as cache from "@actions/cache";
|
|
|
|
import * as core from "@actions/core";
|
|
|
|
|
|
|
|
import { Events, RefKey } from "../src/constants";
|
|
|
|
import run from "../src/restoreOnly";
|
|
|
|
import * as actionUtils from "../src/utils/actionUtils";
|
|
|
|
import * as testUtils from "../src/utils/testUtils";
|
|
|
|
|
|
|
|
jest.mock("../src/utils/actionUtils");
|
|
|
|
|
|
|
|
beforeAll(() => {
|
|
|
|
jest.spyOn(actionUtils, "isExactKeyMatch").mockImplementation(
|
|
|
|
(key, cacheResult) => {
|
|
|
|
const actualUtils = jest.requireActual("../src/utils/actionUtils");
|
|
|
|
return actualUtils.isExactKeyMatch(key, cacheResult);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
jest.spyOn(actionUtils, "isValidEvent").mockImplementation(() => {
|
|
|
|
const actualUtils = jest.requireActual("../src/utils/actionUtils");
|
|
|
|
return actualUtils.isValidEvent();
|
|
|
|
});
|
|
|
|
|
|
|
|
jest.spyOn(actionUtils, "getInputAsArray").mockImplementation(
|
|
|
|
(name, options) => {
|
|
|
|
const actualUtils = jest.requireActual("../src/utils/actionUtils");
|
|
|
|
return actualUtils.getInputAsArray(name, options);
|
|
|
|
}
|
|
|
|
);
|
2023-01-05 12:19:13 +01:00
|
|
|
|
|
|
|
jest.spyOn(actionUtils, "getInputAsBool").mockImplementation(
|
|
|
|
(name, options) => {
|
|
|
|
return jest
|
|
|
|
.requireActual("../src/utils/actionUtils")
|
|
|
|
.getInputAsBool(name, options);
|
|
|
|
}
|
|
|
|
);
|
2022-12-21 15:08:44 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2023-01-05 12:19:13 +01:00
|
|
|
jest.restoreAllMocks();
|
2022-12-21 15:08:44 +01:00
|
|
|
process.env[Events.Key] = Events.Push;
|
|
|
|
process.env[RefKey] = "refs/heads/feature-branch";
|
|
|
|
|
|
|
|
jest.spyOn(actionUtils, "isGhes").mockImplementation(() => false);
|
|
|
|
jest.spyOn(actionUtils, "isCacheFeatureAvailable").mockImplementation(
|
|
|
|
() => true
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
testUtils.clearInputs();
|
|
|
|
delete process.env[Events.Key];
|
|
|
|
delete process.env[RefKey];
|
|
|
|
});
|
|
|
|
|
|
|
|
test("restore with no cache found", async () => {
|
|
|
|
const path = "node_modules";
|
|
|
|
const key = "node-test";
|
|
|
|
testUtils.setInputs({
|
|
|
|
path: path,
|
2023-01-05 12:19:13 +01:00
|
|
|
key,
|
|
|
|
enableCrossOsArchive: false
|
2022-12-21 15:08:44 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
const infoMock = jest.spyOn(core, "info");
|
|
|
|
const failedMock = jest.spyOn(core, "setFailed");
|
|
|
|
const outputMock = jest.spyOn(core, "setOutput");
|
|
|
|
const restoreCacheMock = jest
|
|
|
|
.spyOn(cache, "restoreCache")
|
|
|
|
.mockImplementationOnce(() => {
|
|
|
|
return Promise.resolve(undefined);
|
|
|
|
});
|
|
|
|
|
|
|
|
await run();
|
|
|
|
|
|
|
|
expect(restoreCacheMock).toHaveBeenCalledTimes(1);
|
2023-03-09 13:30:28 +01:00
|
|
|
expect(restoreCacheMock).toHaveBeenCalledWith(
|
|
|
|
[path],
|
|
|
|
key,
|
|
|
|
[],
|
|
|
|
{
|
|
|
|
lookupOnly: false
|
|
|
|
},
|
|
|
|
false
|
|
|
|
);
|
2022-12-21 15:08:44 +01:00
|
|
|
|
|
|
|
expect(outputMock).toHaveBeenCalledWith("cache-primary-key", key);
|
|
|
|
expect(outputMock).toHaveBeenCalledTimes(1);
|
|
|
|
expect(failedMock).toHaveBeenCalledTimes(0);
|
|
|
|
|
|
|
|
expect(infoMock).toHaveBeenCalledWith(
|
|
|
|
`Cache not found for input keys: ${key}`
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("restore with restore keys and no cache found", async () => {
|
|
|
|
const path = "node_modules";
|
|
|
|
const key = "node-test";
|
|
|
|
const restoreKey = "node-";
|
|
|
|
testUtils.setInputs({
|
|
|
|
path: path,
|
|
|
|
key,
|
2023-01-05 12:19:13 +01:00
|
|
|
restoreKeys: [restoreKey],
|
|
|
|
enableCrossOsArchive: false
|
2022-12-21 15:08:44 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
const infoMock = jest.spyOn(core, "info");
|
|
|
|
const failedMock = jest.spyOn(core, "setFailed");
|
|
|
|
const outputMock = jest.spyOn(core, "setOutput");
|
|
|
|
const restoreCacheMock = jest
|
|
|
|
.spyOn(cache, "restoreCache")
|
|
|
|
.mockImplementationOnce(() => {
|
|
|
|
return Promise.resolve(undefined);
|
|
|
|
});
|
|
|
|
|
|
|
|
await run();
|
|
|
|
|
|
|
|
expect(restoreCacheMock).toHaveBeenCalledTimes(1);
|
2023-01-05 12:19:13 +01:00
|
|
|
expect(restoreCacheMock).toHaveBeenCalledWith(
|
|
|
|
[path],
|
|
|
|
key,
|
|
|
|
[restoreKey],
|
2023-03-09 13:30:28 +01:00
|
|
|
{
|
|
|
|
lookupOnly: false
|
|
|
|
},
|
2023-01-05 12:19:13 +01:00
|
|
|
false
|
|
|
|
);
|
2022-12-21 15:08:44 +01:00
|
|
|
|
|
|
|
expect(outputMock).toHaveBeenCalledWith("cache-primary-key", key);
|
|
|
|
expect(failedMock).toHaveBeenCalledTimes(0);
|
|
|
|
|
|
|
|
expect(infoMock).toHaveBeenCalledWith(
|
|
|
|
`Cache not found for input keys: ${key}, ${restoreKey}`
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("restore with cache found for key", async () => {
|
|
|
|
const path = "node_modules";
|
|
|
|
const key = "node-test";
|
|
|
|
testUtils.setInputs({
|
|
|
|
path: path,
|
2023-01-05 12:19:13 +01:00
|
|
|
key,
|
|
|
|
enableCrossOsArchive: false
|
2022-12-21 15:08:44 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
const infoMock = jest.spyOn(core, "info");
|
|
|
|
const failedMock = jest.spyOn(core, "setFailed");
|
|
|
|
const outputMock = jest.spyOn(core, "setOutput");
|
|
|
|
const restoreCacheMock = jest
|
|
|
|
.spyOn(cache, "restoreCache")
|
|
|
|
.mockImplementationOnce(() => {
|
|
|
|
return Promise.resolve(key);
|
|
|
|
});
|
|
|
|
|
|
|
|
await run();
|
|
|
|
|
|
|
|
expect(restoreCacheMock).toHaveBeenCalledTimes(1);
|
2023-03-09 13:30:28 +01:00
|
|
|
expect(restoreCacheMock).toHaveBeenCalledWith(
|
|
|
|
[path],
|
|
|
|
key,
|
|
|
|
[],
|
|
|
|
{
|
|
|
|
lookupOnly: false
|
|
|
|
},
|
|
|
|
false
|
|
|
|
);
|
2022-12-21 15:08:44 +01:00
|
|
|
|
|
|
|
expect(outputMock).toHaveBeenCalledWith("cache-primary-key", key);
|
|
|
|
expect(outputMock).toHaveBeenCalledWith("cache-hit", "true");
|
|
|
|
expect(outputMock).toHaveBeenCalledWith("cache-matched-key", key);
|
|
|
|
|
|
|
|
expect(outputMock).toHaveBeenCalledTimes(3);
|
|
|
|
|
|
|
|
expect(infoMock).toHaveBeenCalledWith(`Cache restored from key: ${key}`);
|
|
|
|
expect(failedMock).toHaveBeenCalledTimes(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("restore with cache found for restore key", async () => {
|
|
|
|
const path = "node_modules";
|
|
|
|
const key = "node-test";
|
|
|
|
const restoreKey = "node-";
|
|
|
|
testUtils.setInputs({
|
|
|
|
path: path,
|
|
|
|
key,
|
2023-01-05 12:19:13 +01:00
|
|
|
restoreKeys: [restoreKey],
|
|
|
|
enableCrossOsArchive: false
|
2022-12-21 15:08:44 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
const infoMock = jest.spyOn(core, "info");
|
|
|
|
const failedMock = jest.spyOn(core, "setFailed");
|
|
|
|
const outputMock = jest.spyOn(core, "setOutput");
|
|
|
|
const restoreCacheMock = jest
|
|
|
|
.spyOn(cache, "restoreCache")
|
|
|
|
.mockImplementationOnce(() => {
|
|
|
|
return Promise.resolve(restoreKey);
|
|
|
|
});
|
|
|
|
|
|
|
|
await run();
|
|
|
|
|
|
|
|
expect(restoreCacheMock).toHaveBeenCalledTimes(1);
|
2023-01-05 12:19:13 +01:00
|
|
|
expect(restoreCacheMock).toHaveBeenCalledWith(
|
|
|
|
[path],
|
|
|
|
key,
|
|
|
|
[restoreKey],
|
2023-03-09 13:30:28 +01:00
|
|
|
{
|
|
|
|
lookupOnly: false
|
|
|
|
},
|
2023-01-05 12:19:13 +01:00
|
|
|
false
|
|
|
|
);
|
2022-12-21 15:08:44 +01:00
|
|
|
|
|
|
|
expect(outputMock).toHaveBeenCalledWith("cache-primary-key", key);
|
|
|
|
expect(outputMock).toHaveBeenCalledWith("cache-hit", "false");
|
|
|
|
expect(outputMock).toHaveBeenCalledWith("cache-matched-key", restoreKey);
|
|
|
|
|
|
|
|
expect(outputMock).toHaveBeenCalledTimes(3);
|
|
|
|
|
|
|
|
expect(infoMock).toHaveBeenCalledWith(
|
|
|
|
`Cache restored from key: ${restoreKey}`
|
|
|
|
);
|
|
|
|
expect(failedMock).toHaveBeenCalledTimes(0);
|
|
|
|
});
|