Skip to content

Commit

Permalink
feat: improve error handling when getting cache key in main function
Browse files Browse the repository at this point in the history
  • Loading branch information
threeal committed Feb 21, 2024
1 parent 21a70f3 commit c33126f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
12 changes: 11 additions & 1 deletion dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 29 additions & 1 deletion src/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ jest.unstable_mockModule("@actions/cache", () => ({
}));

jest.unstable_mockModule("@actions/core", () => ({
endGroup: jest.fn(),
group: jest.fn(),
info: jest.fn(),
setFailed: jest.fn(),
startGroup: jest.fn(),
warning: jest.fn(),
}));

Expand Down Expand Up @@ -58,10 +60,14 @@ describe("install Yarn dependencies", () => {
return 0;
});

jest.mocked(core.endGroup).mockImplementation(() => {
logs.push("::endgroup::");
});

jest.mocked(core.group).mockImplementation(async (name, fn) => {
logs.push(`::group::${name}`);
const res = await fn();
logs.push(`::endgroup::`);
logs.push("::endgroup::");
return res;
});

Expand All @@ -74,6 +80,10 @@ describe("install Yarn dependencies", () => {
logs.push(message);
});

jest.mocked(core.startGroup).mockImplementation((name) => {
logs.push(`::group::${name}`);
});

jest.mocked(core.warning).mockImplementation((message) => {
logs.push(message);
});
Expand Down Expand Up @@ -158,4 +168,22 @@ describe("install Yarn dependencies", () => {
"Failed to enable Yarn: some error",
]);
});

it("should failed to get cache key", async () => {
const { getCacheKey } = await import("./cache.js");
const { main } = await import("./main.js");

jest.mocked(getCacheKey).mockRejectedValue(new Error("some error"));

await main();

expect(failed).toBe(true);
expect(logs).toStrictEqual([
"Enabling Yarn...",
"Yarn enabled",
"::group::Getting cache key",
"::endgroup::",
"Failed to get cache key: some error",
]);
});
});
12 changes: 11 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,17 @@ export async function main(): Promise<void> {
return;
}

const cacheKey = await core.group("Getting cache key", getCacheKey);
core.startGroup("Getting cache key");
let cacheKey: string;
try {
cacheKey = await getCacheKey();
} catch (err) {
core.endGroup();
core.setFailed(`Failed to get cache key: ${err.message}`);
return;
}
core.endGroup();

const cachePaths = await core.group("Getting cache paths", getCachePaths);

const cacheFound = await core.group("Restoring cache", async () => {
Expand Down

0 comments on commit c33126f

Please sign in to comment.