Skip to content

Commit

Permalink
feat: add step in enableYarn function to check if using the same Ya…
Browse files Browse the repository at this point in the history
…rn version (#189)
  • Loading branch information
threeal authored Feb 25, 2024
1 parent 1812e76 commit 36a77cf
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 24 deletions.
41 changes: 24 additions & 17 deletions dist/index.js

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

44 changes: 37 additions & 7 deletions src/yarn/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,49 @@ jest.unstable_mockModule("@actions/exec", () => ({
getExecOutput: jest.fn(),
}));

jest.unstable_mockModule("./version.js", () => ({
getYarnVersion: jest.fn(),
}));

beforeEach(() => {
jest.clearAllMocks();
});

it("should enable Yarn", async () => {
const { exec } = await import("@actions/exec");
const { enableYarn } = await import("./index.js");
describe("enable Yarn", () => {
beforeEach(async () => {
const { getYarnVersion } = await import("./version.js");

jest.mocked(getYarnVersion).mockResolvedValue("1.2.3");
});

it("should enable Yarn", async () => {
const { exec } = await import("@actions/exec");
const { enableYarn } = await import("./index.js");

await expect(enableYarn()).resolves.toBeUndefined();

expect(exec).toHaveBeenCalledTimes(1);
expect(exec).toHaveBeenCalledWith("corepack", ["enable", "yarn"], {
silent: true,
});
});

describe("with different Yarn versions", () => {
beforeEach(async () => {
const { getYarnVersion } = await import("./version.js");

jest.mocked(getYarnVersion).mockImplementation(async (options) => {
return options?.corepack ? "1.2.3" : "1.2.4";
});
});

await enableYarn();
it("should failed to enable Yarn", async () => {
const { enableYarn } = await import("./index.js");

expect(exec).toHaveBeenCalledTimes(1);
expect(exec).toHaveBeenCalledWith("corepack", ["enable", "yarn"], {
silent: true,
await expect(enableYarn()).rejects.toThrow(
"The `yarn` command is using different version of Yarn, expected `1.2.3` but got `1.2.4`",
);
});
});
});

Expand Down
10 changes: 10 additions & 0 deletions src/yarn/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import { exec, getExecOutput } from "@actions/exec";
import { getYarnVersion } from "./version.js";
export { yarnInstall } from "./install.js";
export { getYarnVersion } from "./version.js";

export async function enableYarn(): Promise<void> {
await exec("corepack", ["enable", "yarn"], { silent: true });

// Check if the `yarn` command is using the same version as the `corepack yarn` command.
const version = await getYarnVersion();
const corepackVersion = await getYarnVersion({ corepack: true });
if (version != corepackVersion) {
throw new Error(
`The \`yarn\` command is using different version of Yarn, expected \`${corepackVersion}\` but got \`${version}\``,
);
}
}

export async function getYarnConfig(name: string): Promise<string> {
Expand Down

0 comments on commit 36a77cf

Please sign in to comment.