Skip to content

Commit

Permalink
test(unit): adjust tests to cover our own cache implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
axe312ger committed Aug 19, 2021
1 parent ea4d215 commit 7369b01
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 83 deletions.
38 changes: 1 addition & 37 deletions __tests__/restore.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as cache from "@actions/cache";
import * as core from "@actions/core";

import * as cache from "../src/cache";
import { Events, Inputs, RefKey } from "../src/constants";
import run from "../src/restore";
import * as actionUtils from "../src/utils/actionUtils";
Expand Down Expand Up @@ -94,42 +94,6 @@ test("restore with no key", async () => {
);
});

test("restore with too many keys should fail", async () => {
const path = "node_modules";
const key = "node-test";
const restoreKeys = [...Array(20).keys()].map(x => x.toString());
testUtils.setInputs({
path: path,
key,
restoreKeys
});
const failedMock = jest.spyOn(core, "setFailed");
const restoreCacheMock = jest.spyOn(cache, "restoreCache");
await run();
expect(restoreCacheMock).toHaveBeenCalledTimes(1);
expect(restoreCacheMock).toHaveBeenCalledWith([path], key, restoreKeys);
expect(failedMock).toHaveBeenCalledWith(
`Key Validation Error: Keys are limited to a maximum of 10.`
);
});

test("restore with large key should fail", async () => {
const path = "node_modules";
const key = "foo".repeat(512); // Over the 512 character limit
testUtils.setInputs({
path: path,
key
});
const failedMock = jest.spyOn(core, "setFailed");
const restoreCacheMock = jest.spyOn(cache, "restoreCache");
await run();
expect(restoreCacheMock).toHaveBeenCalledTimes(1);
expect(restoreCacheMock).toHaveBeenCalledWith([path], key, []);
expect(failedMock).toHaveBeenCalledWith(
`Key Validation Error: ${key} cannot be larger than 512 characters.`
);
});

test("restore with invalid key should fail", async () => {
const path = "node_modules";
const key = "comma,comma";
Expand Down
48 changes: 2 additions & 46 deletions __tests__/save.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import * as cache from "@actions/cache";
import * as core from "@actions/core";

import * as cache from "../src/cache";
import { Events, Inputs, RefKey } from "../src/constants";
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/cache");
jest.mock("../src/cache");
jest.mock("../src/utils/actionUtils");

beforeAll(() => {
Expand Down Expand Up @@ -170,50 +170,6 @@ test("save with missing input outputs warning", async () => {
expect(failedMock).toHaveBeenCalledTimes(0);
});

test("save with large cache outputs warning", async () => {
const logWarningMock = jest.spyOn(actionUtils, "logWarning");
const failedMock = jest.spyOn(core, "setFailed");

const primaryKey = "Linux-node-bb828da54c148048dd17899ba9fda624811cfb43";
const savedCacheKey = "Linux-node-";

jest.spyOn(core, "getState")
// Cache Entry State
.mockImplementationOnce(() => {
return savedCacheKey;
})
// Cache Key State
.mockImplementationOnce(() => {
return primaryKey;
});

const inputPath = "node_modules";
testUtils.setInput(Inputs.Path, inputPath);

const saveCacheMock = jest
.spyOn(cache, "saveCache")
.mockImplementationOnce(() => {
throw new Error(
"Cache size of ~6144 MB (6442450944 B) is over the 5GB limit, not saving cache."
);
});

await run();

expect(saveCacheMock).toHaveBeenCalledTimes(1);
expect(saveCacheMock).toHaveBeenCalledWith(
[inputPath],
primaryKey,
expect.anything()
);

expect(logWarningMock).toHaveBeenCalledTimes(1);
expect(logWarningMock).toHaveBeenCalledWith(
"Cache size of ~6144 MB (6442450944 B) is over the 5GB limit, not saving cache."
);
expect(failedMock).toHaveBeenCalledTimes(0);
});

test("save with reserve cache failure outputs warning", async () => {
const infoMock = jest.spyOn(core, "info");
const logWarningMock = jest.spyOn(actionUtils, "logWarning");
Expand Down

0 comments on commit 7369b01

Please sign in to comment.