Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Yarn Version to the Cache Key #127

Merged
merged 3 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions dist/index.mjs

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

8 changes: 7 additions & 1 deletion src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ async function main() {
await yarn.enable();
});

const version = await core.group("Getting Yarn version", async () => {
const version = await yarn.version();
core.info(`Yarn version: ${version}`);
return version;
});

const lockFileHash = await core.group(
"Calculating lock file hash",
async () => {
Expand All @@ -26,7 +32,7 @@ async function main() {
const cachePaths = [".yarn", ".pnp.cjs", ".pnp.loader.mjs"];
const cacheKey =
lockFileHash !== undefined
? `yarn-install-action-${os.type()}-${lockFileHash}`
? `yarn-install-action-${os.type()}-${version}-${lockFileHash}`
: undefined;

if (cacheKey !== undefined) {
Expand Down
9 changes: 7 additions & 2 deletions src/yarn.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { exec } from "@actions/exec";
import { exec, getExecOutput } from "@actions/exec";

async function disableGlobalCache() {
return exec("corepack", [
Expand Down Expand Up @@ -27,4 +27,9 @@ async function install() {
return exec("corepack", ["yarn", "install"], { env });
}

export default { disableGlobalCache, enable, install };
async function version() {
const res = await getExecOutput("corepack", ["yarn", "--version"]);
return res.stdout.trim();
}

export default { disableGlobalCache, enable, install, version };
15 changes: 15 additions & 0 deletions src/yarn.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { jest } from "@jest/globals";

jest.unstable_mockModule("@actions/exec", () => ({
exec: jest.fn(),
getExecOutput: jest.fn(),
}));

beforeEach(() => {
Expand Down Expand Up @@ -50,3 +51,17 @@ it("should install package using Yarn", async () => {
},
});
});

it("should get Yarn version", async () => {
const { getExecOutput } = await import("@actions/exec");
const yarn = (await import("./yarn.mjs")).default;

getExecOutput.mockResolvedValue({ stdout: "1.2.3" });

const version = await yarn.version();

expect(getExecOutput).toHaveBeenCalledTimes(1);
expect(getExecOutput).toHaveBeenCalledWith("corepack", ["yarn", "--version"]);

expect(version).toEqual("1.2.3");
});
Loading