1
0
Fork 0
mirror of https://code.forgejo.org/actions/cache.git synced 2024-11-30 15:59:14 +01:00
cache/__tests__/actionUtils.test.ts

235 lines
7.2 KiB
TypeScript
Raw Normal View History

import * as core from "@actions/core";
2020-04-17 21:46:46 +02:00
import { Events, Outputs, RefKey, State } from "../src/constants";
import * as actionUtils from "../src/utils/actionUtils";
2020-06-02 17:21:03 +02:00
import * as testUtils from "../src/utils/testUtils";
jest.mock("@actions/core");
2020-06-02 17:21:03 +02:00
beforeAll(() => {
jest.spyOn(core, "getInput").mockImplementation((name, options) => {
return jest.requireActual("@actions/core").getInput(name, options);
});
});
afterEach(() => {
delete process.env[Events.Key];
2020-04-17 21:46:46 +02:00
delete process.env[RefKey];
});
2020-09-29 19:36:19 +02:00
test("isGhes returns true if server url is not github.com", () => {
try {
process.env["GITHUB_SERVER_URL"] = "http://example.com";
expect(actionUtils.isGhes()).toBe(true);
} finally {
process.env["GITHUB_SERVER_URL"] = undefined;
}
});
test("isGhes returns true when server url is github.com", () => {
try {
process.env["GITHUB_SERVER_URL"] = "http://github.com";
expect(actionUtils.isGhes()).toBe(false);
} finally {
process.env["GITHUB_SERVER_URL"] = undefined;
}
});
test("isExactKeyMatch with undefined cache key returns false", () => {
const key = "linux-rust";
const cacheKey = undefined;
expect(actionUtils.isExactKeyMatch(key, cacheKey)).toBe(false);
});
test("isExactKeyMatch with empty cache key returns false", () => {
const key = "linux-rust";
const cacheKey = "";
expect(actionUtils.isExactKeyMatch(key, cacheKey)).toBe(false);
});
test("isExactKeyMatch with different keys returns false", () => {
const key = "linux-rust";
const cacheKey = "linux-";
expect(actionUtils.isExactKeyMatch(key, cacheKey)).toBe(false);
});
test("isExactKeyMatch with different key accents returns false", () => {
const key = "linux-áccent";
const cacheKey = "linux-accent";
expect(actionUtils.isExactKeyMatch(key, cacheKey)).toBe(false);
});
test("isExactKeyMatch with same key returns true", () => {
const key = "linux-rust";
const cacheKey = "linux-rust";
expect(actionUtils.isExactKeyMatch(key, cacheKey)).toBe(true);
});
test("isExactKeyMatch with same key and different casing returns true", () => {
const key = "linux-rust";
const cacheKey = "LINUX-RUST";
expect(actionUtils.isExactKeyMatch(key, cacheKey)).toBe(true);
});
test("setOutputAndState with undefined entry to set cache-hit output", () => {
const key = "linux-rust";
const cacheKey = undefined;
const setOutputMock = jest.spyOn(core, "setOutput");
const saveStateMock = jest.spyOn(core, "saveState");
actionUtils.setOutputAndState(key, cacheKey);
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 cacheKey = "linux-rust";
const setOutputMock = jest.spyOn(core, "setOutput");
const saveStateMock = jest.spyOn(core, "saveState");
actionUtils.setOutputAndState(key, cacheKey);
expect(setOutputMock).toHaveBeenCalledWith(Outputs.CacheHit, "true");
expect(setOutputMock).toHaveBeenCalledTimes(1);
expect(saveStateMock).toHaveBeenCalledWith(State.CacheMatchedKey, cacheKey);
expect(saveStateMock).toHaveBeenCalledTimes(1);
});
test("setOutputAndState with no exact match to set cache-hit output and state", () => {
const key = "linux-rust";
const cacheKey = "linux-rust-bb828da54c148048dd17899ba9fda624811cfb43";
const setOutputMock = jest.spyOn(core, "setOutput");
const saveStateMock = jest.spyOn(core, "saveState");
actionUtils.setOutputAndState(key, cacheKey);
expect(setOutputMock).toHaveBeenCalledWith(Outputs.CacheHit, "false");
expect(setOutputMock).toHaveBeenCalledTimes(1);
expect(saveStateMock).toHaveBeenCalledWith(State.CacheMatchedKey, cacheKey);
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.CacheMatchedKey);
expect(getStateMock).toHaveBeenCalledTimes(1);
});
test("getCacheState with valid state", () => {
const cacheKey = "Linux-node-bb828da54c148048dd17899ba9fda624811cfb43";
const getStateMock = jest.spyOn(core, "getState");
getStateMock.mockImplementation(() => {
return cacheKey;
});
const state = actionUtils.getCacheState();
expect(state).toEqual(cacheKey);
expect(getStateMock).toHaveBeenCalledWith(State.CacheMatchedKey);
expect(getStateMock).toHaveBeenCalledTimes(1);
});
test("logWarning logs a message with a warning prefix", () => {
const message = "A warning occurred.";
const infoMock = jest.spyOn(core, "info");
actionUtils.logWarning(message);
expect(infoMock).toHaveBeenCalledWith(`[warning]${message}`);
});
2020-04-17 21:46:46 +02:00
test("isValidEvent returns false for event that does not have a branch or tag", () => {
const event = "foo";
process.env[Events.Key] = event;
const isValidEvent = actionUtils.isValidEvent();
expect(isValidEvent).toBe(false);
});
2020-04-17 21:46:46 +02:00
test("isValidEvent returns true for event that has a ref", () => {
const event = Events.Push;
process.env[Events.Key] = event;
2020-04-17 21:46:46 +02:00
process.env[RefKey] = "ref/heads/feature";
const isValidEvent = actionUtils.isValidEvent();
expect(isValidEvent).toBe(true);
});
2020-06-02 17:21:03 +02:00
test("getInputAsArray returns empty array if not required and missing", () => {
expect(actionUtils.getInputAsArray("foo")).toEqual([]);
});
test("getInputAsArray throws error if required and missing", () => {
expect(() =>
actionUtils.getInputAsArray("foo", { required: true })
).toThrowError();
});
test("getInputAsArray handles single line correctly", () => {
testUtils.setInput("foo", "bar");
expect(actionUtils.getInputAsArray("foo")).toEqual(["bar"]);
});
test("getInputAsArray handles multiple lines correctly", () => {
testUtils.setInput("foo", "bar\nbaz");
expect(actionUtils.getInputAsArray("foo")).toEqual(["bar", "baz"]);
});
test("getInputAsArray handles different new lines correctly", () => {
testUtils.setInput("foo", "bar\r\nbaz");
expect(actionUtils.getInputAsArray("foo")).toEqual(["bar", "baz"]);
});
test("getInputAsArray handles empty lines correctly", () => {
testUtils.setInput("foo", "\n\nbar\n\nbaz\n\n");
expect(actionUtils.getInputAsArray("foo")).toEqual(["bar", "baz"]);
});
2020-10-02 16:59:55 +02:00
test("getInputAsInt returns undefined if input not set", () => {
expect(actionUtils.getInputAsInt("undefined")).toBeUndefined();
2020-10-02 16:59:55 +02:00
});
test("getInputAsInt returns value if input is valid", () => {
testUtils.setInput("foo", "8");
expect(actionUtils.getInputAsInt("foo")).toBe(8);
});
test("getInputAsInt returns undefined if input is invalid or NaN", () => {
testUtils.setInput("foo", "bar");
expect(actionUtils.getInputAsInt("foo")).toBeUndefined();
});
test("getInputAsInt throws if required and value missing", () => {
expect(() =>
actionUtils.getInputAsInt("undefined", { required: true })
).toThrowError();
});