mirror of https://github.com/actions/cache.git
Merge master into releases/v1
parent
86dff562ab
commit
6491e51b66
@ -0,0 +1,16 @@
|
|||||||
|
{
|
||||||
|
"env": { "node": true, "jest": true },
|
||||||
|
"parser": "@typescript-eslint/parser",
|
||||||
|
"parserOptions": { "ecmaVersion": 2020, "sourceType": "module" },
|
||||||
|
"extends": [
|
||||||
|
"eslint:recommended",
|
||||||
|
"plugin:@typescript-eslint/eslint-recommended",
|
||||||
|
"plugin:@typescript-eslint/recommended",
|
||||||
|
"plugin:import/errors",
|
||||||
|
"plugin:import/warnings",
|
||||||
|
"plugin:import/typescript",
|
||||||
|
"plugin:prettier/recommended",
|
||||||
|
"prettier/@typescript-eslint"
|
||||||
|
],
|
||||||
|
"plugins": ["@typescript-eslint", "jest"]
|
||||||
|
}
|
@ -0,0 +1 @@
|
|||||||
|
hello world
|
@ -0,0 +1,226 @@
|
|||||||
|
import * as core from "@actions/core";
|
||||||
|
import * as os from "os";
|
||||||
|
import * as path from "path";
|
||||||
|
|
||||||
|
import { Events, Outputs, State } from "../src/constants";
|
||||||
|
import { ArtifactCacheEntry } from "../src/contracts";
|
||||||
|
import * as actionUtils from "../src/utils/actionUtils";
|
||||||
|
|
||||||
|
jest.mock("@actions/core");
|
||||||
|
jest.mock("os");
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
delete process.env[Events.Key];
|
||||||
|
});
|
||||||
|
|
||||||
|
test("getArchiveFileSize returns file size", () => {
|
||||||
|
const filePath = path.join(__dirname, "__fixtures__", "helloWorld.txt");
|
||||||
|
|
||||||
|
const size = actionUtils.getArchiveFileSize(filePath);
|
||||||
|
|
||||||
|
expect(size).toBe(11);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("isExactKeyMatch with undefined cache entry returns false", () => {
|
||||||
|
const key = "linux-rust";
|
||||||
|
const cacheEntry = undefined;
|
||||||
|
|
||||||
|
expect(actionUtils.isExactKeyMatch(key, cacheEntry)).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("isExactKeyMatch with empty cache entry returns false", () => {
|
||||||
|
const key = "linux-rust";
|
||||||
|
const cacheEntry: ArtifactCacheEntry = {};
|
||||||
|
|
||||||
|
expect(actionUtils.isExactKeyMatch(key, cacheEntry)).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("isExactKeyMatch with different keys returns false", () => {
|
||||||
|
const key = "linux-rust";
|
||||||
|
const cacheEntry: ArtifactCacheEntry = {
|
||||||
|
cacheKey: "linux-"
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(actionUtils.isExactKeyMatch(key, cacheEntry)).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("isExactKeyMatch with different key accents returns false", () => {
|
||||||
|
const key = "linux-áccent";
|
||||||
|
const cacheEntry: ArtifactCacheEntry = {
|
||||||
|
cacheKey: "linux-accent"
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(actionUtils.isExactKeyMatch(key, cacheEntry)).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("isExactKeyMatch with same key returns true", () => {
|
||||||
|
const key = "linux-rust";
|
||||||
|
const cacheEntry: ArtifactCacheEntry = {
|
||||||
|
cacheKey: "linux-rust"
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(actionUtils.isExactKeyMatch(key, cacheEntry)).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("isExactKeyMatch with same key and different casing returns true", () => {
|
||||||
|
const key = "linux-rust";
|
||||||
|
const cacheEntry: ArtifactCacheEntry = {
|
||||||
|
cacheKey: "LINUX-RUST"
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(actionUtils.isExactKeyMatch(key, cacheEntry)).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("setOutputAndState with undefined entry to set cache-hit output", () => {
|
||||||
|
const key = "linux-rust";
|
||||||
|
const cacheEntry = undefined;
|
||||||
|
|
||||||
|
const setOutputMock = jest.spyOn(core, "setOutput");
|
||||||
|
const saveStateMock = jest.spyOn(core, "saveState");
|
||||||
|
|
||||||
|
actionUtils.setOutputAndState(key, cacheEntry);
|
||||||
|
|
||||||
|
expect(setOutputMock).toHaveBeenCalledWith(Outputs.CacheHit, "false");
|
||||||
|
expect(setOutputMock).toHaveBeenCalledTimes(1);
|
||||||
|
|
||||||
|
expect(saveStateMock).toHaveBeenCalledTimes(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("setOutputAndState with exact match to set cache-hit output and state", () => {
|
||||||
|
const key = "linux-rust";
|
||||||
|
const cacheEntry: ArtifactCacheEntry = {
|
||||||
|
cacheKey: "linux-rust"
|
||||||
|
};
|
||||||
|
|
||||||
|
const setOutputMock = jest.spyOn(core, "setOutput");
|
||||||
|
const saveStateMock = jest.spyOn(core, "saveState");
|
||||||
|
|
||||||
|
actionUtils.setOutputAndState(key, cacheEntry);
|
||||||
|
|
||||||
|
expect(setOutputMock).toHaveBeenCalledWith(Outputs.CacheHit, "true");
|
||||||
|
expect(setOutputMock).toHaveBeenCalledTimes(1);
|
||||||
|
|
||||||
|
expect(saveStateMock).toHaveBeenCalledWith(
|
||||||
|
State.CacheResult,
|
||||||
|
JSON.stringify(cacheEntry)
|
||||||
|
);
|
||||||
|
expect(saveStateMock).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("setOutputAndState with no exact match to set cache-hit output and state", () => {
|
||||||
|
const key = "linux-rust";
|
||||||
|
const cacheEntry: ArtifactCacheEntry = {
|
||||||
|
cacheKey: "linux-rust-bb828da54c148048dd17899ba9fda624811cfb43"
|
||||||
|
};
|
||||||
|
|
||||||
|
const setOutputMock = jest.spyOn(core, "setOutput");
|
||||||
|
const saveStateMock = jest.spyOn(core, "saveState");
|
||||||
|
|
||||||
|
actionUtils.setOutputAndState(key, cacheEntry);
|
||||||
|
|
||||||
|
expect(setOutputMock).toHaveBeenCalledWith(Outputs.CacheHit, "false");
|
||||||
|
expect(setOutputMock).toHaveBeenCalledTimes(1);
|
||||||
|
|
||||||
|
expect(saveStateMock).toHaveBeenCalledWith(
|
||||||
|
State.CacheResult,
|
||||||
|
JSON.stringify(cacheEntry)
|
||||||
|
);
|
||||||
|
expect(saveStateMock).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("getCacheState with no state returns undefined", () => {
|
||||||
|
const getStateMock = jest.spyOn(core, "getState");
|
||||||
|
getStateMock.mockImplementation(() => {
|
||||||
|
return "";
|
||||||
|
});
|
||||||
|
|
||||||
|
const state = actionUtils.getCacheState();
|
||||||
|
|
||||||
|
expect(state).toBe(undefined);
|
||||||
|
|
||||||
|
expect(getStateMock).toHaveBeenCalledWith(State.CacheResult);
|
||||||
|
expect(getStateMock).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("getCacheState with valid state", () => {
|
||||||
|
const cacheEntry: ArtifactCacheEntry = {
|
||||||
|
cacheKey: "Linux-node-bb828da54c148048dd17899ba9fda624811cfb43",
|
||||||
|
scope: "refs/heads/master",
|
||||||
|
creationTime: "2019-11-13T19:18:02+00:00",
|
||||||
|
archiveLocation: "www.actionscache.test/download"
|
||||||
|
};
|
||||||
|
const getStateMock = jest.spyOn(core, "getState");
|
||||||
|
getStateMock.mockImplementation(() => {
|
||||||
|
return JSON.stringify(cacheEntry);
|
||||||
|
});
|
||||||
|
|
||||||
|
const state = actionUtils.getCacheState();
|
||||||
|
|
||||||
|
expect(state).toEqual(cacheEntry);
|
||||||
|
|
||||||
|
expect(getStateMock).toHaveBeenCalledWith(State.CacheResult);
|
||||||
|
expect(getStateMock).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("isValidEvent returns false for unknown event", () => {
|
||||||
|
const event = "foo";
|
||||||
|
process.env[Events.Key] = event;
|
||||||
|
|
||||||
|
const isValidEvent = actionUtils.isValidEvent();
|
||||||
|
|
||||||
|
expect(isValidEvent).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("resolvePath with no ~ in path", () => {
|
||||||
|
const filePath = ".cache/yarn";
|
||||||
|
|
||||||
|
const resolvedPath = actionUtils.resolvePath(filePath);
|
||||||
|
|
||||||
|
const expectedPath = path.resolve(filePath);
|
||||||
|
expect(resolvedPath).toBe(expectedPath);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("resolvePath with ~ in path", () => {
|
||||||
|
const filePath = "~/.cache/yarn";
|
||||||
|
|
||||||
|
const homedir = jest.requireActual("os").homedir();
|
||||||
|
const homedirMock = jest.spyOn(os, "homedir");
|
||||||
|
homedirMock.mockImplementation(() => {
|
||||||
|
return homedir;
|
||||||
|
});
|
||||||
|
|
||||||
|
const resolvedPath = actionUtils.resolvePath(filePath);
|
||||||
|
|
||||||
|
const expectedPath = path.join(homedir, ".cache/yarn");
|
||||||
|
expect(resolvedPath).toBe(expectedPath);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("resolvePath with home not found", () => {
|
||||||
|
const filePath = "~/.cache/yarn";
|
||||||
|
const homedirMock = jest.spyOn(os, "homedir");
|
||||||
|
homedirMock.mockImplementation(() => {
|
||||||
|
return "";
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(() => actionUtils.resolvePath(filePath)).toThrow(
|
||||||
|
"Unable to resolve `~` to HOME"
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("isValidEvent returns true for push event", () => {
|
||||||
|
const event = Events.Push;
|
||||||
|
process.env[Events.Key] = event;
|
||||||
|
|
||||||
|
const isValidEvent = actionUtils.isValidEvent();
|
||||||
|
|
||||||
|
expect(isValidEvent).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("isValidEvent returns true for pull request event", () => {
|
||||||
|
const event = Events.PullRequest;
|
||||||
|
process.env[Events.Key] = event;
|
||||||
|
|
||||||
|
const isValidEvent = actionUtils.isValidEvent();
|
||||||
|
|
||||||
|
expect(isValidEvent).toBe(true);
|
||||||
|
});
|
@ -1,22 +0,0 @@
|
|||||||
import * as core from "@actions/core";
|
|
||||||
|
|
||||||
import { Inputs } from "../src/constants";
|
|
||||||
import run from "../src/restore";
|
|
||||||
import * as testUtils from "../src/utils/testUtils";
|
|
||||||
|
|
||||||
test("restore with no path", async () => {
|
|
||||||
const failedMock = jest.spyOn(core, "setFailed");
|
|
||||||
await run();
|
|
||||||
expect(failedMock).toHaveBeenCalledWith(
|
|
||||||
"Input required and not supplied: path"
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("restore with no key", async () => {
|
|
||||||
testUtils.setInput(Inputs.Path, "node_modules");
|
|
||||||
const failedMock = jest.spyOn(core, "setFailed");
|
|
||||||
await run();
|
|
||||||
expect(failedMock).toHaveBeenCalledWith(
|
|
||||||
"Input required and not supplied: key"
|
|
||||||
);
|
|
||||||
});
|
|
@ -0,0 +1,450 @@
|
|||||||
|
import * as core from "@actions/core";
|
||||||
|
import * as exec from "@actions/exec";
|
||||||
|
import * as io from "@actions/io";
|
||||||
|
import * as path from "path";
|
||||||
|
import * as cacheHttpClient from "../src/cacheHttpClient";
|
||||||
|
import { Events, Inputs } from "../src/constants";
|
||||||
|
import { ArtifactCacheEntry } from "../src/contracts";
|
||||||
|
import run from "../src/restore";
|
||||||
|
import * as actionUtils from "../src/utils/actionUtils";
|
||||||
|
import * as testUtils from "../src/utils/testUtils";
|
||||||
|
|
||||||
|
jest.mock("@actions/exec");
|
||||||
|
jest.mock("@actions/io");
|
||||||
|
jest.mock("../src/utils/actionUtils");
|
||||||
|
jest.mock("../src/cacheHttpClient");
|
||||||
|
|
||||||
|
beforeAll(() => {
|
||||||
|
jest.spyOn(actionUtils, "resolvePath").mockImplementation(filePath => {
|
||||||
|
return path.resolve(filePath);
|
||||||
|
});
|
||||||
|
|
||||||
|
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, "getSupportedEvents").mockImplementation(() => {
|
||||||
|
const actualUtils = jest.requireActual("../src/utils/actionUtils");
|
||||||
|
return actualUtils.getSupportedEvents();
|
||||||
|
});
|
||||||
|
|
||||||
|
jest.spyOn(io, "which").mockImplementation(tool => {
|
||||||
|
return Promise.resolve(tool);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
process.env[Events.Key] = Events.Push;
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
testUtils.clearInputs();
|
||||||
|
delete process.env[Events.Key];
|
||||||
|
});
|
||||||
|
|
||||||
|
test("restore with invalid event", async () => {
|
||||||
|
const failedMock = jest.spyOn(core, "setFailed");
|
||||||
|
const invalidEvent = "commit_comment";
|
||||||
|
process.env[Events.Key] = invalidEvent;
|
||||||
|
await run();
|
||||||
|
expect(failedMock).toHaveBeenCalledWith(
|
||||||
|
`Event Validation Error: The event type ${invalidEvent} is not supported. Only push, pull_request events are supported at this time.`
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("restore with no path should fail", async () => {
|
||||||
|
const failedMock = jest.spyOn(core, "setFailed");
|
||||||
|
await run();
|
||||||
|
expect(failedMock).toHaveBeenCalledWith(
|
||||||
|
"Input required and not supplied: path"
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("restore with no key", async () => {
|
||||||
|
testUtils.setInput(Inputs.Path, "node_modules");
|
||||||
|
const failedMock = jest.spyOn(core, "setFailed");
|
||||||
|
await run();
|
||||||
|
expect(failedMock).toHaveBeenCalledWith(
|
||||||
|
"Input required and not supplied: key"
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("restore with too many keys should fail", async () => {
|
||||||
|
const key = "node-test";
|
||||||
|
const restoreKeys = [...Array(20).keys()].map(x => x.toString());
|
||||||
|
testUtils.setInputs({
|
||||||
|
path: "node_modules",
|
||||||
|
key,
|
||||||
|
restoreKeys
|
||||||
|
});
|
||||||
|
const failedMock = jest.spyOn(core, "setFailed");
|
||||||
|
await run();
|
||||||
|
expect(failedMock).toHaveBeenCalledWith(
|
||||||
|
`Key Validation Error: Keys are limited to a maximum of 10.`
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("restore with large key should fail", async () => {
|
||||||
|
const key = "foo".repeat(512); // Over the 512 character limit
|
||||||
|
testUtils.setInputs({
|
||||||
|
path: "node_modules",
|
||||||
|
key
|
||||||
|
});
|
||||||
|
const failedMock = jest.spyOn(core, "setFailed");
|
||||||
|
await run();
|
||||||
|
expect(failedMock).toHaveBeenCalledWith(
|
||||||
|
`Key Validation Error: ${key} cannot be larger than 512 characters.`
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("restore with invalid key should fail", async () => {
|
||||||
|
const key = "comma,comma";
|
||||||
|
testUtils.setInputs({
|
||||||
|
path: "node_modules",
|
||||||
|
key
|
||||||
|
});
|
||||||
|
const failedMock = jest.spyOn(core, "setFailed");
|
||||||
|
await run();
|
||||||
|
expect(failedMock).toHaveBeenCalledWith(
|
||||||
|
`Key Validation Error: ${key} cannot contain commas.`
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("restore with no cache found", async () => {
|
||||||
|
const key = "node-test";
|
||||||
|
testUtils.setInputs({
|
||||||
|
path: "node_modules",
|
||||||
|
key
|
||||||
|
});
|
||||||
|
|
||||||
|
const infoMock = jest.spyOn(core, "info");
|
||||||
|
const warningMock = jest.spyOn(core, "warning");
|
||||||
|
const failedMock = jest.spyOn(core, "setFailed");
|
||||||
|
const stateMock = jest.spyOn(core, "saveState");
|
||||||
|
|
||||||
|
const clientMock = jest.spyOn(cacheHttpClient, "getCacheEntry");
|
||||||
|
clientMock.mockImplementation(() => {
|
||||||
|
return Promise.resolve(null);
|
||||||
|
});
|
||||||
|
|
||||||
|
await run();
|
||||||
|
|
||||||
|
expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key);
|
||||||
|
expect(warningMock).toHaveBeenCalledTimes(0);
|
||||||
|
expect(failedMock).toHaveBeenCalledTimes(0);
|
||||||
|
|
||||||
|
expect(infoMock).toHaveBeenCalledWith(
|
||||||
|
`Cache not found for input keys: ${key}.`
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("restore with server error should fail", async () => {
|
||||||
|
const key = "node-test";
|
||||||
|
testUtils.setInputs({
|
||||||
|
path: "node_modules",
|
||||||
|
key
|
||||||
|
});
|
||||||
|
|
||||||
|
const warningMock = jest.spyOn(core, "warning");
|
||||||
|
const failedMock = jest.spyOn(core, "setFailed");
|
||||||
|
const stateMock = jest.spyOn(core, "saveState");
|
||||||
|
|
||||||
|
const clientMock = jest.spyOn(cacheHttpClient, "getCacheEntry");
|
||||||
|
clientMock.mockImplementation(() => {
|
||||||
|
throw new Error("HTTP Error Occurred");
|
||||||
|
});
|
||||||
|
|
||||||
|
const setCacheHitOutputMock = jest.spyOn(actionUtils, "setCacheHitOutput");
|
||||||
|
|
||||||
|
await run();
|
||||||
|
|
||||||
|
expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key);
|
||||||
|
|
||||||
|
expect(warningMock).toHaveBeenCalledTimes(1);
|
||||||
|
expect(warningMock).toHaveBeenCalledWith("HTTP Error Occurred");
|
||||||
|
|
||||||
|
expect(setCacheHitOutputMock).toHaveBeenCalledTimes(1);
|
||||||
|
expect(setCacheHitOutputMock).toHaveBeenCalledWith(false);
|
||||||
|
|
||||||
|
expect(failedMock).toHaveBeenCalledTimes(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("restore with restore keys and no cache found", async () => {
|
||||||
|
const key = "node-test";
|
||||||
|
const restoreKey = "node-";
|
||||||
|
testUtils.setInputs({
|
||||||
|
path: "node_modules",
|
||||||
|
key,
|
||||||
|
restoreKeys: [restoreKey]
|
||||||
|
});
|
||||||
|
|
||||||
|
const infoMock = jest.spyOn(core, "info");
|
||||||
|
const warningMock = jest.spyOn(core, "warning");
|
||||||
|
const failedMock = jest.spyOn(core, "setFailed");
|
||||||
|
const stateMock = jest.spyOn(core, "saveState");
|
||||||
|
|
||||||
|
const clientMock = jest.spyOn(cacheHttpClient, "getCacheEntry");
|
||||||
|
clientMock.mockImplementation(() => {
|
||||||
|
return Promise.resolve(null);
|
||||||
|
});
|
||||||
|
|
||||||
|
await run();
|
||||||
|
|
||||||
|
expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key);
|
||||||
|
expect(warningMock).toHaveBeenCalledTimes(0);
|
||||||
|
expect(failedMock).toHaveBeenCalledTimes(0);
|
||||||
|
|
||||||
|
expect(infoMock).toHaveBeenCalledWith(
|
||||||
|
`Cache not found for input keys: ${key}, ${restoreKey}.`
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("restore with cache found", async () => {
|
||||||
|
const key = "node-test";
|
||||||
|
const cachePath = path.resolve("node_modules");
|
||||||
|
testUtils.setInputs({
|
||||||
|
path: "node_modules",
|
||||||
|
key
|
||||||
|
});
|
||||||
|
|
||||||
|
const infoMock = jest.spyOn(core, "info");
|
||||||
|
const warningMock = jest.spyOn(core, "warning");
|
||||||
|
const failedMock = jest.spyOn(core, "setFailed");
|
||||||
|
const stateMock = jest.spyOn(core, "saveState");
|
||||||
|
|
||||||
|
const cacheEntry: ArtifactCacheEntry = {
|
||||||
|
cacheKey: key,
|
||||||
|
scope: "refs/heads/master",
|
||||||
|
archiveLocation: "www.actionscache.test/download"
|
||||||
|
};
|
||||||
|
const getCacheMock = jest.spyOn(cacheHttpClient, "getCacheEntry");
|
||||||
|
getCacheMock.mockImplementation(() => {
|
||||||
|
return Promise.resolve(cacheEntry);
|
||||||
|
});
|
||||||
|
const tempPath = "/foo/bar";
|
||||||
|
|
||||||
|
const createTempDirectoryMock = jest.spyOn(
|
||||||
|
actionUtils,
|
||||||
|
"createTempDirectory"
|
||||||
|
);
|
||||||
|
createTempDirectoryMock.mockImplementation(() => {
|
||||||
|
return Promise.resolve(tempPath);
|
||||||
|
});
|
||||||
|
|
||||||
|
const archivePath = path.join(tempPath, "cache.tgz");
|
||||||
|
const setCacheStateMock = jest.spyOn(actionUtils, "setCacheState");
|
||||||
|
const downloadCacheMock = jest.spyOn(cacheHttpClient, "downloadCache");
|
||||||
|
|
||||||
|
const fileSize = 142;
|
||||||
|
const getArchiveFileSizeMock = jest
|
||||||
|
.spyOn(actionUtils, "getArchiveFileSize")
|
||||||
|
.mockReturnValue(fileSize);
|
||||||
|
|
||||||
|
const mkdirMock = jest.spyOn(io, "mkdirP");
|
||||||
|
const execMock = jest.spyOn(exec, "exec");
|
||||||
|
const setCacheHitOutputMock = jest.spyOn(actionUtils, "setCacheHitOutput");
|
||||||
|
|
||||||
|
await run();
|
||||||
|
|
||||||
|
expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key);
|
||||||
|
expect(getCacheMock).toHaveBeenCalledWith([key]);
|
||||||
|
expect(setCacheStateMock).toHaveBeenCalledWith(cacheEntry);
|
||||||
|
expect(createTempDirectoryMock).toHaveBeenCalledTimes(1);
|
||||||
|
expect(downloadCacheMock).toHaveBeenCalledWith(cacheEntry, archivePath);
|
||||||
|
expect(getArchiveFileSizeMock).toHaveBeenCalledWith(archivePath);
|
||||||
|
expect(mkdirMock).toHaveBeenCalledWith(cachePath);
|
||||||
|
|
||||||
|
const IS_WINDOWS = process.platform === "win32";
|
||||||
|
const args = IS_WINDOWS
|
||||||
|
? [
|
||||||
|
"-xz",
|
||||||
|
"--force-local",
|
||||||
|
"-f",
|
||||||
|
archivePath.replace(/\\/g, "/"),
|
||||||
|
"-C",
|
||||||
|
cachePath.replace(/\\/g, "/")
|
||||||
|
]
|
||||||
|
: ["-xz", "-f", archivePath, "-C", cachePath];
|
||||||
|
|
||||||
|
expect(execMock).toHaveBeenCalledTimes(1);
|
||||||
|
expect(execMock).toHaveBeenCalledWith(`"tar"`, args);
|
||||||
|
|
||||||
|
expect(setCacheHitOutputMock).toHaveBeenCalledTimes(1);
|
||||||
|
expect(setCacheHitOutputMock).toHaveBeenCalledWith(true);
|
||||||
|
|
||||||
|
expect(infoMock).toHaveBeenCalledWith(`Cache restored from key: ${key}`);
|
||||||
|
expect(warningMock).toHaveBeenCalledTimes(0);
|
||||||
|
expect(failedMock).toHaveBeenCalledTimes(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("restore with a pull request event and cache found", async () => {
|
||||||
|
const key = "node-test";
|
||||||
|
const cachePath = path.resolve("node_modules");
|
||||||
|
testUtils.setInputs({
|
||||||
|
path: "node_modules",
|
||||||
|
key
|
||||||
|
});
|
||||||
|
|
||||||
|
process.env[Events.Key] = Events.PullRequest;
|
||||||
|
|
||||||
|
const infoMock = jest.spyOn(core, "info");
|
||||||
|
const warningMock = jest.spyOn(core, "warning");
|
||||||
|
const failedMock = jest.spyOn(core, "setFailed");
|
||||||
|
const stateMock = jest.spyOn(core, "saveState");
|
||||||
|
|
||||||
|
const cacheEntry: ArtifactCacheEntry = {
|
||||||
|
cacheKey: key,
|
||||||
|
scope: "refs/heads/master",
|
||||||
|
archiveLocation: "www.actionscache.test/download"
|
||||||
|
};
|
||||||
|
const getCacheMock = jest.spyOn(cacheHttpClient, "getCacheEntry");
|
||||||
|
getCacheMock.mockImplementation(() => {
|
||||||
|
return Promise.resolve(cacheEntry);
|
||||||
|
});
|
||||||
|
const tempPath = "/foo/bar";
|
||||||
|
|
||||||
|
const createTempDirectoryMock = jest.spyOn(
|
||||||
|
actionUtils,
|
||||||
|
"createTempDirectory"
|
||||||
|
);
|
||||||
|
createTempDirectoryMock.mockImplementation(() => {
|
||||||
|
return Promise.resolve(tempPath);
|
||||||
|
});
|
||||||
|
|
||||||
|
const archivePath = path.join(tempPath, "cache.tgz");
|
||||||
|
const setCacheStateMock = jest.spyOn(actionUtils, "setCacheState");
|
||||||
|
const downloadCacheMock = jest.spyOn(cacheHttpClient, "downloadCache");
|
||||||
|
|
||||||
|
const fileSize = 62915000;
|
||||||
|
const getArchiveFileSizeMock = jest
|
||||||
|
.spyOn(actionUtils, "getArchiveFileSize")
|
||||||
|
.mockReturnValue(fileSize);
|
||||||
|
|
||||||
|
const mkdirMock = jest.spyOn(io, "mkdirP");
|
||||||
|
const execMock = jest.spyOn(exec, "exec");
|
||||||
|
const setCacheHitOutputMock = jest.spyOn(actionUtils, "setCacheHitOutput");
|
||||||
|
|
||||||
|
await run();
|
||||||
|
|
||||||
|
expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key);
|
||||||
|
expect(getCacheMock).toHaveBeenCalledWith([key]);
|
||||||
|
expect(setCacheStateMock).toHaveBeenCalledWith(cacheEntry);
|
||||||
|
expect(createTempDirectoryMock).toHaveBeenCalledTimes(1);
|
||||||
|
expect(downloadCacheMock).toHaveBeenCalledWith(cacheEntry, archivePath);
|
||||||
|
expect(getArchiveFileSizeMock).toHaveBeenCalledWith(archivePath);
|
||||||
|
expect(infoMock).toHaveBeenCalledWith(`Cache Size: ~60 MB (62915000 B)`);
|
||||||
|
expect(mkdirMock).toHaveBeenCalledWith(cachePath);
|
||||||
|
|
||||||
|
const IS_WINDOWS = process.platform === "win32";
|
||||||
|
const args = IS_WINDOWS
|
||||||
|
? [
|
||||||
|
"-xz",
|
||||||
|
"--force-local",
|
||||||
|
"-f",
|
||||||
|
archivePath.replace(/\\/g, "/"),
|
||||||
|
"-C",
|
||||||
|
cachePath.replace(/\\/g, "/")
|
||||||
|
]
|
||||||
|
: ["-xz", "-f", archivePath, "-C", cachePath];
|
||||||
|
|
||||||
|
expect(execMock).toHaveBeenCalledTimes(1);
|
||||||
|
expect(execMock).toHaveBeenCalledWith(`"tar"`, args);
|
||||||
|
|
||||||
|
expect(setCacheHitOutputMock).toHaveBeenCalledTimes(1);
|
||||||
|
expect(setCacheHitOutputMock).toHaveBeenCalledWith(true);
|
||||||
|
|
||||||
|
expect(infoMock).toHaveBeenCalledWith(`Cache restored from key: ${key}`);
|
||||||
|
expect(warningMock).toHaveBeenCalledTimes(0);
|
||||||
|
expect(failedMock).toHaveBeenCalledTimes(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("restore with cache found for restore key", async () => {
|
||||||
|
const key = "node-test";
|
||||||
|
const restoreKey = "node-";
|
||||||
|
const cachePath = path.resolve("node_modules");
|
||||||
|
testUtils.setInputs({
|
||||||
|
path: "node_modules",
|
||||||
|
key,
|
||||||
|
restoreKeys: [restoreKey]
|
||||||
|
});
|
||||||
|
|
||||||
|
const infoMock = jest.spyOn(core, "info");
|
||||||
|
const warningMock = jest.spyOn(core, "warning");
|
||||||
|
const failedMock = jest.spyOn(core, "setFailed");
|
||||||
|
const stateMock = jest.spyOn(core, "saveState");
|
||||||
|
|
||||||
|
const cacheEntry: ArtifactCacheEntry = {
|
||||||
|
cacheKey: restoreKey,
|
||||||
|
scope: "refs/heads/master",
|
||||||
|
archiveLocation: "www.actionscache.test/download"
|
||||||
|
};
|
||||||
|
const getCacheMock = jest.spyOn(cacheHttpClient, "getCacheEntry");
|
||||||
|
getCacheMock.mockImplementation(() => {
|
||||||
|
return Promise.resolve(cacheEntry);
|
||||||
|
});
|
||||||
|
const tempPath = "/foo/bar";
|
||||||
|
|
||||||
|
const createTempDirectoryMock = jest.spyOn(
|
||||||
|
actionUtils,
|
||||||
|
"createTempDirectory"
|
||||||
|
);
|
||||||
|
createTempDirectoryMock.mockImplementation(() => {
|
||||||
|
return Promise.resolve(tempPath);
|
||||||
|
});
|
||||||
|
|
||||||
|
const archivePath = path.join(tempPath, "cache.tgz");
|
||||||
|
const setCacheStateMock = jest.spyOn(actionUtils, "setCacheState");
|
||||||
|
const downloadCacheMock = jest.spyOn(cacheHttpClient, "downloadCache");
|
||||||
|
|
||||||
|
const fileSize = 142;
|
||||||
|
const getArchiveFileSizeMock = jest
|
||||||
|
.spyOn(actionUtils, "getArchiveFileSize")
|
||||||
|
.mockReturnValue(fileSize);
|
||||||
|
|
||||||
|
const mkdirMock = jest.spyOn(io, "mkdirP");
|
||||||
|
const execMock = jest.spyOn(exec, "exec");
|
||||||
|
const setCacheHitOutputMock = jest.spyOn(actionUtils, "setCacheHitOutput");
|
||||||
|
|
||||||
|
await run();
|
||||||
|
|
||||||
|
expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key);
|
||||||
|
expect(getCacheMock).toHaveBeenCalledWith([key, restoreKey]);
|
||||||
|
expect(setCacheStateMock).toHaveBeenCalledWith(cacheEntry);
|
||||||
|
expect(createTempDirectoryMock).toHaveBeenCalledTimes(1);
|
||||||
|
expect(downloadCacheMock).toHaveBeenCalledWith(cacheEntry, archivePath);
|
||||||
|
expect(getArchiveFileSizeMock).toHaveBeenCalledWith(archivePath);
|
||||||
|
expect(infoMock).toHaveBeenCalledWith(`Cache Size: ~0 MB (142 B)`);
|
||||||
|
expect(mkdirMock).toHaveBeenCalledWith(cachePath);
|
||||||
|
|
||||||
|
const IS_WINDOWS = process.platform === "win32";
|
||||||
|
const args = IS_WINDOWS
|
||||||
|
? [
|
||||||
|
"-xz",
|
||||||
|
"--force-local",
|
||||||
|
"-f",
|
||||||
|
archivePath.replace(/\\/g, "/"),
|
||||||
|
"-C",
|
||||||
|
cachePath.replace(/\\/g, "/")
|
||||||
|
]
|
||||||
|
: ["-xz", "-f", archivePath, "-C", cachePath];
|
||||||
|
|
||||||
|
expect(execMock).toHaveBeenCalledTimes(1);
|
||||||
|
expect(execMock).toHaveBeenCalledWith(`"tar"`, args);
|
||||||
|
|
||||||
|
expect(setCacheHitOutputMock).toHaveBeenCalledTimes(1);
|
||||||
|
expect(setCacheHitOutputMock).toHaveBeenCalledWith(false);
|
||||||
|
|
||||||
|
expect(infoMock).toHaveBeenCalledWith(
|
||||||
|
`Cache restored from key: ${restoreKey}`
|
||||||
|
);
|
||||||
|
expect(warningMock).toHaveBeenCalledTimes(0);
|
||||||
|
expect(failedMock).toHaveBeenCalledTimes(0);
|
||||||
|
});
|
@ -0,0 +1,329 @@
|
|||||||
|
import * as core from "@actions/core";
|
||||||
|
import * as exec from "@actions/exec";
|
||||||
|
import * as io from "@actions/io";
|
||||||
|
import * as path from "path";
|
||||||
|
import * as cacheHttpClient from "../src/cacheHttpClient";
|
||||||
|
import { Inputs } from "../src/constants";
|
||||||
|
import { ArtifactCacheEntry } from "../src/contracts";
|
||||||
|
import run from "../src/save";
|
||||||
|
import * as actionUtils from "../src/utils/actionUtils";
|
||||||
|
import * as testUtils from "../src/utils/testUtils";
|
||||||
|
|
||||||
|
jest.mock("@actions/core");
|
||||||
|
jest.mock("@actions/exec");
|
||||||
|
jest.mock("@actions/io");
|
||||||
|
jest.mock("../src/utils/actionUtils");
|
||||||
|
jest.mock("../src/cacheHttpClient");
|
||||||
|
|
||||||
|
beforeAll(() => {
|
||||||
|
jest.spyOn(core, "getInput").mockImplementation((name, options) => {
|
||||||
|
return jest.requireActual("@actions/core").getInput(name, options);
|
||||||
|
});
|
||||||
|
|
||||||
|
jest.spyOn(actionUtils, "getCacheState").mockImplementation(() => {
|
||||||
|
return jest.requireActual("../src/utils/actionUtils").getCacheState();
|
||||||
|
});
|
||||||
|
|
||||||
|
jest.spyOn(actionUtils, "isExactKeyMatch").mockImplementation(
|
||||||
|
(key, cacheResult) => {
|
||||||
|
return jest
|
||||||
|
.requireActual("../src/utils/actionUtils")
|
||||||
|
.isExactKeyMatch(key, cacheResult);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
jest.spyOn(actionUtils, "resolvePath").mockImplementation(filePath => {
|
||||||
|
return path.resolve(filePath);
|
||||||
|
});
|
||||||
|
|
||||||
|
jest.spyOn(actionUtils, "createTempDirectory").mockImplementation(() => {
|
||||||
|
return Promise.resolve("/foo/bar");
|
||||||
|
});
|
||||||
|
|
||||||
|
jest.spyOn(io, "which").mockImplementation(tool => {
|
||||||
|
return Promise.resolve(tool);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
testUtils.clearInputs();
|
||||||
|
});
|
||||||
|
|
||||||
|
test("save with no primary key in state outputs warning", async () => {
|
||||||
|
const warningMock = jest.spyOn(core, "warning");
|
||||||
|
const failedMock = jest.spyOn(core, "setFailed");
|
||||||
|
|
||||||
|
const cacheEntry: ArtifactCacheEntry = {
|
||||||
|
cacheKey: "Linux-node-bb828da54c148048dd17899ba9fda624811cfb43",
|
||||||
|
scope: "refs/heads/master",
|
||||||
|
creationTime: "2019-11-13T19:18:02+00:00",
|
||||||
|
archiveLocation: "www.actionscache.test/download"
|
||||||
|
};
|
||||||
|
|
||||||
|
jest.spyOn(core, "getState")
|
||||||
|
// Cache Entry State
|
||||||
|
.mockImplementationOnce(() => {
|
||||||
|
return JSON.stringify(cacheEntry);
|
||||||
|
})
|
||||||
|
// Cache Key State
|
||||||
|
.mockImplementationOnce(() => {
|
||||||
|
return "";
|
||||||
|
});
|
||||||
|
|
||||||
|
await run();
|
||||||
|
|
||||||
|
expect(warningMock).toHaveBeenCalledWith(
|
||||||
|
`Error retrieving key from state.`
|
||||||
|
);
|
||||||
|
expect(warningMock).toHaveBeenCalledTimes(1);
|
||||||
|
expect(failedMock).toHaveBeenCalledTimes(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("save with exact match returns early", async () => {
|
||||||
|
const infoMock = jest.spyOn(core, "info");
|
||||||
|
const warningMock = jest.spyOn(core, "warning");
|
||||||
|
const failedMock = jest.spyOn(core, "setFailed");
|
||||||
|
|
||||||
|
const primaryKey = "Linux-node-bb828da54c148048dd17899ba9fda624811cfb43";
|
||||||
|
const cacheEntry: ArtifactCacheEntry = {
|
||||||
|
cacheKey: primaryKey,
|
||||||
|
scope: "refs/heads/master",
|
||||||
|
creationTime: "2019-11-13T19:18:02+00:00",
|
||||||
|
archiveLocation: "www.actionscache.test/download"
|
||||||
|
};
|
||||||
|
|
||||||
|
jest.spyOn(core, "getState")
|
||||||
|
// Cache Entry State
|
||||||
|
.mockImplementationOnce(() => {
|
||||||
|
return JSON.stringify(cacheEntry);
|
||||||
|
})
|
||||||
|
// Cache Key State
|
||||||
|
.mockImplementationOnce(() => {
|
||||||
|
return primaryKey;
|
||||||
|
});
|
||||||
|
|
||||||
|
const execMock = jest.spyOn(exec, "exec");
|
||||||
|
|
||||||
|
await run();
|
||||||
|
|
||||||
|
expect(infoMock).toHaveBeenCalledWith(
|
||||||
|
`Cache hit occurred on the primary key ${primaryKey}, not saving cache.`
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(execMock).toHaveBeenCalledTimes(0);
|
||||||
|
|
||||||
|
expect(warningMock).toHaveBeenCalledTimes(0);
|
||||||
|
expect(failedMock).toHaveBeenCalledTimes(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("save with missing input outputs warning", async () => {
|
||||||
|
const warningMock = jest.spyOn(core, "warning");
|
||||||
|
const failedMock = jest.spyOn(core, "setFailed");
|
||||||
|
|
||||||
|
const primaryKey = "Linux-node-bb828da54c148048dd17899ba9fda624811cfb43";
|
||||||
|
const cacheEntry: ArtifactCacheEntry = {
|
||||||
|
cacheKey: "Linux-node-",
|
||||||
|
scope: "refs/heads/master",
|
||||||
|
creationTime: "2019-11-13T19:18:02+00:00",
|
||||||
|
archiveLocation: "www.actionscache.test/download"
|
||||||
|
};
|
||||||
|
|
||||||
|
jest.spyOn(core, "getState")
|
||||||
|
// Cache Entry State
|
||||||
|
.mockImplementationOnce(() => {
|
||||||
|
return JSON.stringify(cacheEntry);
|
||||||
|
})
|
||||||
|
// Cache Key State
|
||||||
|
.mockImplementationOnce(() => {
|
||||||
|
return primaryKey;
|
||||||
|
});
|
||||||
|
|
||||||
|
await run();
|
||||||
|
|
||||||
|
expect(warningMock).toHaveBeenCalledWith(
|
||||||
|
"Input required and not supplied: path"
|
||||||
|
);
|
||||||
|
expect(warningMock).toHaveBeenCalledTimes(1);
|
||||||
|
expect(failedMock).toHaveBeenCalledTimes(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("save with large cache outputs warning", async () => {
|
||||||
|
const warningMock = jest.spyOn(core, "warning");
|
||||||
|
const failedMock = jest.spyOn(core, "setFailed");
|
||||||
|
|
||||||
|
const primaryKey = "Linux-node-bb828da54c148048dd17899ba9fda624811cfb43";
|
||||||
|
const cacheEntry: ArtifactCacheEntry = {
|
||||||
|
cacheKey: "Linux-node-",
|
||||||
|
scope: "refs/heads/master",
|
||||||
|
creationTime: "2019-11-13T19:18:02+00:00",
|
||||||
|
archiveLocation: "www.actionscache.test/download"
|
||||||
|
};
|
||||||
|
|
||||||
|
jest.spyOn(core, "getState")
|
||||||
|
// Cache Entry State
|
||||||
|
.mockImplementationOnce(() => {
|
||||||
|
return JSON.stringify(cacheEntry);
|
||||||
|
})
|
||||||
|
// Cache Key State
|
||||||
|
.mockImplementationOnce(() => {
|
||||||
|
return primaryKey;
|
||||||
|
});
|
||||||
|
|
||||||
|
const inputPath = "node_modules";
|
||||||
|
const cachePath = path.resolve(inputPath);
|
||||||
|
testUtils.setInput(Inputs.Path, inputPath);
|
||||||
|
|
||||||
|
const execMock = jest.spyOn(exec, "exec");
|
||||||
|
|
||||||
|
const cacheSize = 1024 * 1024 * 1024; //~1GB, over the 400MB limit
|
||||||
|
jest.spyOn(actionUtils, "getArchiveFileSize").mockImplementationOnce(() => {
|
||||||
|
return cacheSize;
|
||||||
|
});
|
||||||
|
|
||||||
|
await run();
|
||||||
|
|
||||||
|
const archivePath = path.join("/foo/bar", "cache.tgz");
|
||||||
|
|
||||||
|
const IS_WINDOWS = process.platform === "win32";
|
||||||
|
const args = IS_WINDOWS
|
||||||
|
? [
|
||||||
|
"-cz",
|
||||||
|
"--force-local",
|
||||||
|
"-f",
|
||||||
|
archivePath.replace(/\\/g, "/"),
|
||||||
|
"-C",
|
||||||
|
cachePath.replace(/\\/g, "/"),
|
||||||
|
"."
|
||||||
|
]
|
||||||
|
: ["-cz", "-f", archivePath, "-C", cachePath, "."];
|
||||||
|
|
||||||
|
expect(execMock).toHaveBeenCalledTimes(1);
|
||||||
|
expect(execMock).toHaveBeenCalledWith(`"tar"`, args);
|
||||||
|
|
||||||
|
expect(warningMock).toHaveBeenCalledTimes(1);
|
||||||
|
expect(warningMock).toHaveBeenCalledWith(
|
||||||
|
"Cache size of ~1024 MB (1073741824 B) is over the 400MB limit, not saving cache."
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(failedMock).toHaveBeenCalledTimes(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("save with server error outputs warning", async () => {
|
||||||
|
const warningMock = jest.spyOn(core, "warning");
|
||||||
|
const failedMock = jest.spyOn(core, "setFailed");
|
||||||
|
|
||||||
|
const primaryKey = "Linux-node-bb828da54c148048dd17899ba9fda624811cfb43";
|
||||||
|
const cacheEntry: ArtifactCacheEntry = {
|
||||||
|
cacheKey: "Linux-node-",
|
||||||
|
scope: "refs/heads/master",
|
||||||
|
creationTime: "2019-11-13T19:18:02+00:00",
|
||||||
|
archiveLocation: "www.actionscache.test/download"
|
||||||
|
};
|
||||||
|
|
||||||
|
jest.spyOn(core, "getState")
|
||||||
|
// Cache Entry State
|
||||||
|
.mockImplementationOnce(() => {
|
||||||
|
return JSON.stringify(cacheEntry);
|
||||||
|
})
|
||||||
|
// Cache Key State
|
||||||
|
.mockImplementationOnce(() => {
|
||||||
|
return primaryKey;
|
||||||
|
});
|
||||||
|
|
||||||
|
const inputPath = "node_modules";
|
||||||
|
const cachePath = path.resolve(inputPath);
|
||||||
|
testUtils.setInput(Inputs.Path, inputPath);
|
||||||
|
|
||||||
|
const execMock = jest.spyOn(exec, "exec");
|
||||||
|
|
||||||
|
const saveCacheMock = jest
|
||||||
|
.spyOn(cacheHttpClient, "saveCache")
|
||||||
|
.mockImplementationOnce(() => {
|
||||||
|
throw new Error("HTTP Error Occurred");
|
||||||
|
});
|
||||||
|
|
||||||
|
await run();
|
||||||
|
|
||||||
|
const archivePath = path.join("/foo/bar", "cache.tgz");
|
||||||
|
|
||||||
|
const IS_WINDOWS = process.platform === "win32";
|
||||||
|
const args = IS_WINDOWS
|
||||||
|
? [
|
||||||
|
"-cz",
|
||||||
|
"--force-local",
|
||||||
|
"-f",
|
||||||
|
archivePath.replace(/\\/g, "/"),
|
||||||
|
"-C",
|
||||||
|
cachePath.replace(/\\/g, "/"),
|
||||||
|
"."
|
||||||
|
]
|
||||||
|
: ["-cz", "-f", archivePath, "-C", cachePath, "."];
|
||||||
|
|
||||||
|
expect(execMock).toHaveBeenCalledTimes(1);
|
||||||
|
expect(execMock).toHaveBeenCalledWith(`"tar"`, args);
|
||||||
|
|
||||||
|
expect(saveCacheMock).toHaveBeenCalledTimes(1);
|
||||||
|
expect(saveCacheMock).toHaveBeenCalledWith(primaryKey, archivePath);
|
||||||
|
|
||||||
|
expect(warningMock).toHaveBeenCalledTimes(1);
|
||||||
|
expect(warningMock).toHaveBeenCalledWith("HTTP Error Occurred");
|
||||||
|
|
||||||
|
expect(failedMock).toHaveBeenCalledTimes(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("save with valid inputs uploads a cache", async () => {
|
||||||
|
const warningMock = jest.spyOn(core, "warning");
|
||||||
|
const failedMock = jest.spyOn(core, "setFailed");
|
||||||
|
|
||||||
|
const primaryKey = "Linux-node-bb828da54c148048dd17899ba9fda624811cfb43";
|
||||||
|
const cacheEntry: ArtifactCacheEntry = {
|
||||||
|
cacheKey: "Linux-node-",
|
||||||
|
scope: "refs/heads/master",
|
||||||
|
creationTime: "2019-11-13T19:18:02+00:00",
|
||||||
|
archiveLocation: "www.actionscache.test/download"
|
||||||
|
};
|
||||||
|
|
||||||
|
jest.spyOn(core, "getState")
|
||||||
|
// Cache Entry State
|
||||||
|
.mockImplementationOnce(() => {
|
||||||
|
return JSON.stringify(cacheEntry);
|
||||||
|
})
|
||||||
|
// Cache Key State
|
||||||
|
.mockImplementationOnce(() => {
|
||||||
|
return primaryKey;
|
||||||
|
});
|
||||||
|
|
||||||
|
const inputPath = "node_modules";
|
||||||
|
const cachePath = path.resolve(inputPath);
|
||||||
|
testUtils.setInput(Inputs.Path, inputPath);
|
||||||
|
|
||||||
|
const execMock = jest.spyOn(exec, "exec");
|
||||||
|
|
||||||
|
const saveCacheMock = jest.spyOn(cacheHttpClient, "saveCache");
|
||||||
|
|
||||||
|
await run();
|
||||||
|
|
||||||
|
const archivePath = path.join("/foo/bar", "cache.tgz");
|
||||||
|
|
||||||
|
const IS_WINDOWS = process.platform === "win32";
|
||||||
|
const args = IS_WINDOWS
|
||||||
|
? [
|
||||||
|
"-cz",
|
||||||
|
"--force-local",
|
||||||
|
"-f",
|
||||||
|
archivePath.replace(/\\/g, "/"),
|
||||||
|
"-C",
|
||||||
|
cachePath.replace(/\\/g, "/"),
|
||||||
|
"."
|
||||||
|
]
|
||||||
|
: ["-cz", "-f", archivePath, "-C", cachePath, "."];
|
||||||
|
|
||||||
|
expect(execMock).toHaveBeenCalledTimes(1);
|
||||||
|
expect(execMock).toHaveBeenCalledWith(`"tar"`, args);
|
||||||
|
|
||||||
|
expect(saveCacheMock).toHaveBeenCalledTimes(1);
|
||||||
|
expect(saveCacheMock).toHaveBeenCalledWith(primaryKey, archivePath);
|
||||||
|
|
||||||
|
expect(warningMock).toHaveBeenCalledTimes(0);
|
||||||
|
expect(failedMock).toHaveBeenCalledTimes(0);
|
||||||
|
});
|
@ -1,20 +1,23 @@
|
|||||||
|
require("nock").disableNetConnect();
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
clearMocks: true,
|
clearMocks: true,
|
||||||
moduleFileExtensions: ['js', 'ts'],
|
moduleFileExtensions: ["js", "ts"],
|
||||||
testEnvironment: 'node',
|
testEnvironment: "node",
|
||||||
testMatch: ['**/*.test.ts'],
|
testMatch: ["**/*.test.ts"],
|
||||||
testRunner: 'jest-circus/runner',
|
testRunner: "jest-circus/runner",
|
||||||
transform: {
|
transform: {
|
||||||
'^.+\\.ts$': 'ts-jest'
|
"^.+\\.ts$": "ts-jest"
|
||||||
},
|
},
|
||||||
verbose: true
|
verbose: true
|
||||||
}
|
};
|
||||||
|
|
||||||
const processStdoutWrite = process.stdout.write.bind(process.stdout)
|
const processStdoutWrite = process.stdout.write.bind(process.stdout);
|
||||||
|
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
||||||
process.stdout.write = (str, encoding, cb) => {
|
process.stdout.write = (str, encoding, cb) => {
|
||||||
// Core library will directly call process.stdout.write for commands
|
// Core library will directly call process.stdout.write for commands
|
||||||
// We don't want :: commands to be executed by the runner during tests
|
// We don't want :: commands to be executed by the runner during tests
|
||||||
if (!str.match(/^::/)) {
|
if (!str.match(/^::/)) {
|
||||||
return processStdoutWrite(str, encoding, cb);
|
return processStdoutWrite(str, encoding, cb);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -1,14 +1,20 @@
|
|||||||
export namespace Inputs {
|
export enum Inputs {
|
||||||
export const Key = "key";
|
Key = "key",
|
||||||
export const Path = "path";
|
Path = "path",
|
||||||
export const RestoreKeys = "restore-keys";
|
RestoreKeys = "restore-keys"
|
||||||
}
|
}
|
||||||
|
|
||||||
export namespace Outputs {
|
export enum Outputs {
|
||||||
export const CacheHit = "cache-hit";
|
CacheHit = "cache-hit"
|
||||||
}
|
}
|
||||||
|
|
||||||
export namespace State {
|
export enum State {
|
||||||
export const CacheKey = "CACHE_KEY";
|
CacheKey = "CACHE_KEY",
|
||||||
export const CacheResult = "CACHE_RESULT";
|
CacheResult = "CACHE_RESULT"
|
||||||
|
}
|
||||||
|
|
||||||
|
export enum Events {
|
||||||
|
Key = "GITHUB_EVENT_NAME",
|
||||||
|
Push = "push",
|
||||||
|
PullRequest = "pull_request"
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,29 @@
|
|||||||
|
import { Inputs } from "../constants";
|
||||||
|
|
||||||
|
// See: https://github.com/actions/toolkit/blob/master/packages/core/src/core.ts#L67
|
||||||
function getInputName(name: string): string {
|
function getInputName(name: string): string {
|
||||||
return `INPUT_${name.replace(/ /g, "_").toUpperCase()}`;
|
return `INPUT_${name.replace(/ /g, "_").toUpperCase()}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function setInput(name: string, value: string) {
|
export function setInput(name: string, value: string): void {
|
||||||
process.env[getInputName(name)] = value;
|
process.env[getInputName(name)] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface CacheInput {
|
||||||
|
path: string;
|
||||||
|
key: string;
|
||||||
|
restoreKeys?: string[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export function setInputs(input: CacheInput): void {
|
||||||
|
setInput(Inputs.Path, input.path);
|
||||||
|
setInput(Inputs.Key, input.key);
|
||||||
|
input.restoreKeys &&
|
||||||
|
setInput(Inputs.RestoreKeys, input.restoreKeys.join("\n"));
|
||||||
|
}
|
||||||
|
|
||||||
|
export function clearInputs(): void {
|
||||||
|
delete process.env[getInputName(Inputs.Path)];
|
||||||
|
delete process.env[getInputName(Inputs.Key)];
|
||||||
|
delete process.env[getInputName(Inputs.RestoreKeys)];
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue