2020-05-14 23:27:38 +02:00
|
|
|
import * as cache from "@actions/cache";
|
2019-11-06 19:41:45 +01:00
|
|
|
import * as core from "@actions/core";
|
2020-05-14 23:27:38 +02:00
|
|
|
|
2022-12-21 15:08:44 +01:00
|
|
|
import { Events, RefKey } from "../src/constants";
|
2023-08-09 16:36:51 +02:00
|
|
|
import { restoreRun } from "../src/restoreImpl";
|
2019-11-06 19:41:45 +01:00
|
|
|
import * as actionUtils from "../src/utils/actionUtils";
|
|
|
|
import * as testUtils from "../src/utils/testUtils";
|
|
|
|
|
2019-12-13 23:24:37 +01:00
|
|
|
jest.mock("../src/utils/actionUtils");
|
2019-11-06 19:41:45 +01:00
|
|
|
|
|
|
|
beforeAll(() => {
|
|
|
|
jest.spyOn(actionUtils, "isExactKeyMatch").mockImplementation(
|
|
|
|
(key, cacheResult) => {
|
|
|
|
const actualUtils = jest.requireActual("../src/utils/actionUtils");
|
|
|
|
return actualUtils.isExactKeyMatch(key, cacheResult);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-11-13 16:54:39 +01:00
|
|
|
jest.spyOn(actionUtils, "isValidEvent").mockImplementation(() => {
|
|
|
|
const actualUtils = jest.requireActual("../src/utils/actionUtils");
|
|
|
|
return actualUtils.isValidEvent();
|
|
|
|
});
|
2020-06-02 17:21:03 +02:00
|
|
|
|
|
|
|
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) => {
|
|
|
|
const actualUtils = jest.requireActual("../src/utils/actionUtils");
|
|
|
|
return actualUtils.getInputAsBool(name, options);
|
|
|
|
}
|
|
|
|
);
|
2019-11-06 19:41:45 +01:00
|
|
|
});
|
2019-11-13 16:54:39 +01:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2023-01-05 12:19:13 +01:00
|
|
|
jest.restoreAllMocks();
|
2019-11-13 16:54:39 +01:00
|
|
|
process.env[Events.Key] = Events.Push;
|
2020-04-17 21:46:46 +02:00
|
|
|
process.env[RefKey] = "refs/heads/feature-branch";
|
2020-09-29 17:23:21 +02:00
|
|
|
|
|
|
|
jest.spyOn(actionUtils, "isGhes").mockImplementation(() => false);
|
2022-03-30 12:16:49 +02:00
|
|
|
jest.spyOn(actionUtils, "isCacheFeatureAvailable").mockImplementation(
|
|
|
|
() => true
|
|
|
|
);
|
2019-11-13 16:54:39 +01:00
|
|
|
});
|
|
|
|
|
2019-11-06 19:41:45 +01:00
|
|
|
afterEach(() => {
|
|
|
|
testUtils.clearInputs();
|
2019-11-13 16:54:39 +01:00
|
|
|
delete process.env[Events.Key];
|
2020-04-17 21:46:46 +02:00
|
|
|
delete process.env[RefKey];
|
2019-11-13 16:54:39 +01:00
|
|
|
});
|
|
|
|
|
2019-11-06 19:41:45 +01:00
|
|
|
test("restore with no cache found", async () => {
|
2020-05-14 23:27:38 +02:00
|
|
|
const path = "node_modules";
|
2019-11-06 19:41:45 +01:00
|
|
|
const key = "node-test";
|
|
|
|
testUtils.setInputs({
|
2020-05-14 23:27:38 +02:00
|
|
|
path: path,
|
2023-01-05 12:19:13 +01:00
|
|
|
key,
|
|
|
|
enableCrossOsArchive: false
|
2019-11-06 19:41:45 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
const infoMock = jest.spyOn(core, "info");
|
|
|
|
const failedMock = jest.spyOn(core, "setFailed");
|
|
|
|
const stateMock = jest.spyOn(core, "saveState");
|
2020-05-14 23:27:38 +02:00
|
|
|
const restoreCacheMock = jest
|
|
|
|
.spyOn(cache, "restoreCache")
|
|
|
|
.mockImplementationOnce(() => {
|
|
|
|
return Promise.resolve(undefined);
|
|
|
|
});
|
2019-11-06 19:41:45 +01:00
|
|
|
|
2023-08-09 16:36:51 +02:00
|
|
|
await restoreRun();
|
2019-11-06 19:41:45 +01:00
|
|
|
|
2020-05-14 23:27:38 +02:00
|
|
|
expect(restoreCacheMock).toHaveBeenCalledTimes(1);
|
2023-03-09 13:30:28 +01:00
|
|
|
expect(restoreCacheMock).toHaveBeenCalledWith(
|
|
|
|
[path],
|
|
|
|
key,
|
|
|
|
[],
|
|
|
|
{
|
|
|
|
lookupOnly: false
|
|
|
|
},
|
|
|
|
false
|
|
|
|
);
|
2020-05-14 23:27:38 +02:00
|
|
|
|
2019-11-06 19:41:45 +01:00
|
|
|
expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key);
|
2022-12-21 15:08:44 +01:00
|
|
|
expect(stateMock).toHaveBeenCalledTimes(1);
|
|
|
|
|
2019-11-06 19:41:45 +01:00
|
|
|
expect(failedMock).toHaveBeenCalledTimes(0);
|
|
|
|
|
|
|
|
expect(infoMock).toHaveBeenCalledWith(
|
2020-02-25 20:16:36 +01:00
|
|
|
`Cache not found for input keys: ${key}`
|
2019-11-06 19:41:45 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("restore with restore keys and no cache found", async () => {
|
2020-05-14 23:27:38 +02:00
|
|
|
const path = "node_modules";
|
2019-11-06 19:41:45 +01:00
|
|
|
const key = "node-test";
|
|
|
|
const restoreKey = "node-";
|
|
|
|
testUtils.setInputs({
|
2020-05-14 23:27:38 +02:00
|
|
|
path: path,
|
2019-11-06 19:41:45 +01:00
|
|
|
key,
|
2023-01-05 12:19:13 +01:00
|
|
|
restoreKeys: [restoreKey],
|
|
|
|
enableCrossOsArchive: false
|
2019-11-06 19:41:45 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
const infoMock = jest.spyOn(core, "info");
|
|
|
|
const failedMock = jest.spyOn(core, "setFailed");
|
|
|
|
const stateMock = jest.spyOn(core, "saveState");
|
2020-05-14 23:27:38 +02:00
|
|
|
const restoreCacheMock = jest
|
|
|
|
.spyOn(cache, "restoreCache")
|
|
|
|
.mockImplementationOnce(() => {
|
|
|
|
return Promise.resolve(undefined);
|
|
|
|
});
|
2019-11-06 19:41:45 +01:00
|
|
|
|
2023-08-09 16:36:51 +02:00
|
|
|
await restoreRun();
|
2019-11-06 19:41:45 +01:00
|
|
|
|
2020-05-14 23:27:38 +02:00
|
|
|
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
|
|
|
|
);
|
2020-05-14 23:27:38 +02:00
|
|
|
|
2019-11-06 19:41:45 +01:00
|
|
|
expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key);
|
2022-12-21 15:08:44 +01:00
|
|
|
expect(stateMock).toHaveBeenCalledTimes(1);
|
|
|
|
|
2019-11-06 19:41:45 +01:00
|
|
|
expect(failedMock).toHaveBeenCalledTimes(0);
|
|
|
|
|
|
|
|
expect(infoMock).toHaveBeenCalledWith(
|
2020-02-25 20:16:36 +01:00
|
|
|
`Cache not found for input keys: ${key}, ${restoreKey}`
|
2019-11-06 19:41:45 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-05-14 23:27:38 +02:00
|
|
|
test("restore with cache found for key", async () => {
|
|
|
|
const path = "node_modules";
|
2019-11-06 19:41:45 +01:00
|
|
|
const key = "node-test";
|
|
|
|
testUtils.setInputs({
|
2020-05-14 23:27:38 +02:00
|
|
|
path: path,
|
2023-01-05 12:19:13 +01:00
|
|
|
key,
|
|
|
|
enableCrossOsArchive: false
|
2019-11-06 19:41:45 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
const infoMock = jest.spyOn(core, "info");
|
|
|
|
const failedMock = jest.spyOn(core, "setFailed");
|
|
|
|
const stateMock = jest.spyOn(core, "saveState");
|
2022-12-21 15:08:44 +01:00
|
|
|
const setCacheHitOutputMock = jest.spyOn(core, "setOutput");
|
2020-05-14 23:27:38 +02:00
|
|
|
const restoreCacheMock = jest
|
|
|
|
.spyOn(cache, "restoreCache")
|
|
|
|
.mockImplementationOnce(() => {
|
|
|
|
return Promise.resolve(key);
|
|
|
|
});
|
2020-04-22 22:36:34 +02:00
|
|
|
|
2023-08-09 16:36:51 +02:00
|
|
|
await restoreRun();
|
2019-11-06 19:41:45 +01:00
|
|
|
|
2020-05-14 23:27:38 +02:00
|
|
|
expect(restoreCacheMock).toHaveBeenCalledTimes(1);
|
2023-03-09 13:30:28 +01:00
|
|
|
expect(restoreCacheMock).toHaveBeenCalledWith(
|
|
|
|
[path],
|
|
|
|
key,
|
|
|
|
[],
|
|
|
|
{
|
|
|
|
lookupOnly: false
|
|
|
|
},
|
|
|
|
false
|
|
|
|
);
|
2019-11-13 16:54:39 +01:00
|
|
|
|
|
|
|
expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key);
|
2022-12-21 15:08:44 +01:00
|
|
|
expect(stateMock).toHaveBeenCalledWith("CACHE_RESULT", key);
|
|
|
|
expect(stateMock).toHaveBeenCalledTimes(2);
|
|
|
|
|
2019-11-06 19:41:45 +01:00
|
|
|
expect(setCacheHitOutputMock).toHaveBeenCalledTimes(1);
|
2022-12-21 15:08:44 +01:00
|
|
|
expect(setCacheHitOutputMock).toHaveBeenCalledWith("cache-hit", "true");
|
2019-11-06 19:41:45 +01:00
|
|
|
|
|
|
|
expect(infoMock).toHaveBeenCalledWith(`Cache restored from key: ${key}`);
|
|
|
|
expect(failedMock).toHaveBeenCalledTimes(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("restore with cache found for restore key", async () => {
|
2020-05-14 23:27:38 +02:00
|
|
|
const path = "node_modules";
|
2019-11-06 19:41:45 +01:00
|
|
|
const key = "node-test";
|
|
|
|
const restoreKey = "node-";
|
|
|
|
testUtils.setInputs({
|
2020-05-14 23:27:38 +02:00
|
|
|
path: path,
|
2019-11-06 19:41:45 +01:00
|
|
|
key,
|
2023-01-05 12:19:13 +01:00
|
|
|
restoreKeys: [restoreKey],
|
|
|
|
enableCrossOsArchive: false
|
2019-11-06 19:41:45 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
const infoMock = jest.spyOn(core, "info");
|
|
|
|
const failedMock = jest.spyOn(core, "setFailed");
|
|
|
|
const stateMock = jest.spyOn(core, "saveState");
|
2022-12-21 15:08:44 +01:00
|
|
|
const setCacheHitOutputMock = jest.spyOn(core, "setOutput");
|
2020-05-14 23:27:38 +02:00
|
|
|
const restoreCacheMock = jest
|
|
|
|
.spyOn(cache, "restoreCache")
|
|
|
|
.mockImplementationOnce(() => {
|
|
|
|
return Promise.resolve(restoreKey);
|
|
|
|
});
|
2019-11-06 19:41:45 +01:00
|
|
|
|
2023-08-09 16:36:51 +02:00
|
|
|
await restoreRun();
|
2019-11-06 19:41:45 +01:00
|
|
|
|
2020-05-14 23:27:38 +02:00
|
|
|
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
|
|
|
|
);
|
2019-11-06 19:41:45 +01:00
|
|
|
|
2020-05-14 23:27:38 +02:00
|
|
|
expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key);
|
2022-12-21 15:08:44 +01:00
|
|
|
expect(stateMock).toHaveBeenCalledWith("CACHE_RESULT", restoreKey);
|
|
|
|
expect(stateMock).toHaveBeenCalledTimes(2);
|
2019-11-06 19:41:45 +01:00
|
|
|
|
2022-12-21 15:08:44 +01:00
|
|
|
expect(setCacheHitOutputMock).toHaveBeenCalledTimes(1);
|
|
|
|
expect(setCacheHitOutputMock).toHaveBeenCalledWith("cache-hit", "false");
|
2019-11-06 19:41:45 +01:00
|
|
|
expect(infoMock).toHaveBeenCalledWith(
|
|
|
|
`Cache restored from key: ${restoreKey}`
|
|
|
|
);
|
|
|
|
expect(failedMock).toHaveBeenCalledTimes(0);
|
|
|
|
});
|
2023-01-30 12:10:58 +01:00
|
|
|
|
|
|
|
test("Fail restore when fail on cache miss is enabled and primary + restore keys not found", async () => {
|
|
|
|
const path = "node_modules";
|
|
|
|
const key = "node-test";
|
|
|
|
const restoreKey = "node-";
|
|
|
|
testUtils.setInputs({
|
|
|
|
path: path,
|
|
|
|
key,
|
|
|
|
restoreKeys: [restoreKey],
|
|
|
|
failOnCacheMiss: true
|
|
|
|
});
|
|
|
|
|
|
|
|
const failedMock = jest.spyOn(core, "setFailed");
|
|
|
|
const stateMock = jest.spyOn(core, "saveState");
|
|
|
|
const setCacheHitOutputMock = jest.spyOn(core, "setOutput");
|
|
|
|
const restoreCacheMock = jest
|
|
|
|
.spyOn(cache, "restoreCache")
|
|
|
|
.mockImplementationOnce(() => {
|
|
|
|
return Promise.resolve(undefined);
|
|
|
|
});
|
|
|
|
|
2023-08-09 16:36:51 +02:00
|
|
|
await restoreRun();
|
2023-01-30 12:10:58 +01:00
|
|
|
|
|
|
|
expect(restoreCacheMock).toHaveBeenCalledTimes(1);
|
|
|
|
expect(restoreCacheMock).toHaveBeenCalledWith(
|
|
|
|
[path],
|
|
|
|
key,
|
|
|
|
[restoreKey],
|
2023-03-09 13:30:28 +01:00
|
|
|
{
|
|
|
|
lookupOnly: false
|
|
|
|
},
|
2023-01-30 12:10:58 +01:00
|
|
|
false
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key);
|
|
|
|
expect(setCacheHitOutputMock).toHaveBeenCalledTimes(0);
|
|
|
|
|
|
|
|
expect(failedMock).toHaveBeenCalledWith(
|
|
|
|
`Failed to restore cache entry. Exiting as fail-on-cache-miss is set. Input key: ${key}`
|
|
|
|
);
|
|
|
|
expect(failedMock).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("restore when fail on cache miss is enabled and primary key doesn't match restored key", async () => {
|
|
|
|
const path = "node_modules";
|
|
|
|
const key = "node-test";
|
|
|
|
const restoreKey = "node-";
|
|
|
|
testUtils.setInputs({
|
|
|
|
path: path,
|
|
|
|
key,
|
|
|
|
restoreKeys: [restoreKey],
|
|
|
|
failOnCacheMiss: true
|
|
|
|
});
|
|
|
|
|
|
|
|
const infoMock = jest.spyOn(core, "info");
|
|
|
|
const failedMock = jest.spyOn(core, "setFailed");
|
|
|
|
const stateMock = jest.spyOn(core, "saveState");
|
|
|
|
const setCacheHitOutputMock = jest.spyOn(core, "setOutput");
|
|
|
|
const restoreCacheMock = jest
|
|
|
|
.spyOn(cache, "restoreCache")
|
|
|
|
.mockImplementationOnce(() => {
|
|
|
|
return Promise.resolve(restoreKey);
|
|
|
|
});
|
|
|
|
|
2023-08-09 16:36:51 +02:00
|
|
|
await restoreRun();
|
2023-01-30 12:10:58 +01:00
|
|
|
|
|
|
|
expect(restoreCacheMock).toHaveBeenCalledTimes(1);
|
|
|
|
expect(restoreCacheMock).toHaveBeenCalledWith(
|
|
|
|
[path],
|
|
|
|
key,
|
|
|
|
[restoreKey],
|
2023-03-09 13:30:28 +01:00
|
|
|
{
|
|
|
|
lookupOnly: false
|
|
|
|
},
|
2023-01-30 12:10:58 +01:00
|
|
|
false
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key);
|
|
|
|
expect(stateMock).toHaveBeenCalledWith("CACHE_RESULT", restoreKey);
|
|
|
|
expect(stateMock).toHaveBeenCalledTimes(2);
|
|
|
|
|
|
|
|
expect(setCacheHitOutputMock).toHaveBeenCalledTimes(1);
|
|
|
|
expect(setCacheHitOutputMock).toHaveBeenCalledWith("cache-hit", "false");
|
|
|
|
|
|
|
|
expect(infoMock).toHaveBeenCalledWith(
|
|
|
|
`Cache restored from key: ${restoreKey}`
|
|
|
|
);
|
|
|
|
expect(failedMock).toHaveBeenCalledTimes(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("restore with fail on cache miss disabled and no cache found", async () => {
|
|
|
|
const path = "node_modules";
|
|
|
|
const key = "node-test";
|
|
|
|
const restoreKey = "node-";
|
|
|
|
testUtils.setInputs({
|
|
|
|
path: path,
|
|
|
|
key,
|
|
|
|
restoreKeys: [restoreKey],
|
|
|
|
failOnCacheMiss: false
|
|
|
|
});
|
|
|
|
|
|
|
|
const infoMock = jest.spyOn(core, "info");
|
|
|
|
const failedMock = jest.spyOn(core, "setFailed");
|
|
|
|
const stateMock = jest.spyOn(core, "saveState");
|
|
|
|
const restoreCacheMock = jest
|
|
|
|
.spyOn(cache, "restoreCache")
|
|
|
|
.mockImplementationOnce(() => {
|
|
|
|
return Promise.resolve(undefined);
|
|
|
|
});
|
|
|
|
|
2023-08-09 16:36:51 +02:00
|
|
|
await restoreRun();
|
2023-01-30 12:10:58 +01:00
|
|
|
|
|
|
|
expect(restoreCacheMock).toHaveBeenCalledTimes(1);
|
|
|
|
expect(restoreCacheMock).toHaveBeenCalledWith(
|
|
|
|
[path],
|
|
|
|
key,
|
|
|
|
[restoreKey],
|
2023-03-09 13:30:28 +01:00
|
|
|
{
|
|
|
|
lookupOnly: false
|
|
|
|
},
|
2023-01-30 12:10:58 +01:00
|
|
|
false
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key);
|
|
|
|
expect(stateMock).toHaveBeenCalledTimes(1);
|
|
|
|
|
|
|
|
expect(infoMock).toHaveBeenCalledWith(
|
|
|
|
`Cache not found for input keys: ${key}, ${restoreKey}`
|
|
|
|
);
|
|
|
|
expect(failedMock).toHaveBeenCalledTimes(0);
|
|
|
|
});
|