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 version Input #212

Merged
merged 3 commits into from
Feb 28, 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
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ If you are new to GitHub Actions, you can explore the [GitHub Actions guide](htt

Here are the available input parameters for the Setup Yarn Berry Action:

| Name | Type | Default | Description |
| ------- | ----------------- | ------- | ---------------------------------------------------------- |
| `cache` | `true` or `false` | `true` | Indicates whether to use caching during Yarn installation. |
| Name | Type | Default | Description |
| --------- | ----------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `version` | String | | Specifies the version of Yarn to be set up using this action. The specified version can be a tag (e.g., `stable`), a semver range (e.g., `4.x`), or a semver version (e.g., `4.1.0`). |
| `cache` | `true` or `false` | `true` | Indicates whether to use caching during Yarn installation. |

### Example

Expand All @@ -53,6 +54,17 @@ jobs:
# Add more steps as needed for your workflow
```

#### Specifying Yarn Version

You can specify the Yarn version to be used by providing it as an input parameter:

```yaml
- name: Setup Latest Yarn
uses: threeal/setup-yarn-action@v1.0.0
with:
version: latest
```

#### Disabling Caching

By default, caching is enabled. To disable caching, set the `cache` input parameter to `false` as shown below:
Expand Down
2 changes: 2 additions & 0 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ branding:
icon: chevrons-right
color: black
inputs:
version:
description: The Yarn version to be set up
cache:
description: Use caching during Yarn installation
default: true
Expand Down
25 changes: 18 additions & 7 deletions dist/index.js

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

13 changes: 11 additions & 2 deletions src/inputs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { jest } from "@jest/globals";

jest.unstable_mockModule("@actions/core", () => ({
getBooleanInput: jest.fn(),
getInput: jest.fn(),
}));

beforeEach(async () => {
const { getBooleanInput } = await import("@actions/core");
const { getBooleanInput, getInput } = await import("@actions/core");

jest.mocked(getBooleanInput).mockImplementation((name) => {
switch (name) {
Expand All @@ -14,12 +15,20 @@ beforeEach(async () => {
}
throw new Error(`unknown input: ${name}`);
});

jest.mocked(getInput).mockImplementation((name) => {
switch (name) {
case "version":
return "";
}
throw new Error(`unknown input: ${name}`);
});
});

it("should get the action inputs", async () => {
const { getInputs } = await import("./inputs.js");

const inputs = getInputs();

expect(inputs).toStrictEqual({ cache: true });
expect(inputs).toStrictEqual({ version: "", cache: true });
});
10 changes: 8 additions & 2 deletions src/inputs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { getBooleanInput } from "@actions/core";
import { getBooleanInput, getInput } from "@actions/core";

export function getInputs() {
interface Inputs {
version: string;
cache: boolean;
}

export function getInputs(): Inputs {
return {
version: getInput("version"),
cache: getBooleanInput("cache"),
};
}
70 changes: 67 additions & 3 deletions src/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ jest.unstable_mockModule("@actions/core", () => ({
}));

jest.unstable_mockModule("./yarn/index.js", () => ({
setYarnVersion: jest.fn(),
yarnInstall: jest.fn(),
}));

Expand All @@ -38,7 +39,7 @@ describe("install Yarn dependencies", () => {
beforeEach(async () => {
const { restoreCache, saveCache } = await import("@actions/cache");
const core = await import("@actions/core");
const { yarnInstall } = await import("./yarn/index.js");
const { setYarnVersion, yarnInstall } = await import("./yarn/index.js");
const { getCacheKey, getCachePaths } = await import("./cache.js");
const { corepackEnableYarn } = await import("./corepack.js");
const { getInputs } = await import("./inputs.js");
Expand Down Expand Up @@ -90,14 +91,18 @@ describe("install Yarn dependencies", () => {
core.info("Yarn enabled");
});

jest.mocked(setYarnVersion).mockImplementation(async (version) => {
core.info(`Yarn version set to ${version}`);
});

jest.mocked(yarnInstall).mockImplementation(async () => {
core.info("Dependencies installed");
});

jest.mocked(getCacheKey).mockResolvedValue("unavailable-key");
jest.mocked(getCachePaths).mockResolvedValue(["some/path", "another/path"]);

jest.mocked(getInputs).mockReturnValue({ cache: true });
jest.mocked(getInputs).mockReturnValue({ version: "", cache: true });
});

it("should failed to enable Yarn", async () => {
Expand Down Expand Up @@ -288,11 +293,70 @@ describe("install Yarn dependencies", () => {
]);
});

describe("with version specified", () => {
beforeEach(async () => {
const { getInputs } = await import("./inputs.js");

jest.mocked(getInputs).mockReturnValue({
version: "stable",
cache: true,
});
});

it("should failed to set Yarn version", async () => {
const { setYarnVersion } = await import("./yarn/index.js");
const { main } = await import("./main.js");

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

await main();

expect(failed).toBe(true);
expect(logs).toStrictEqual([
"Getting action inputs...",
"Enabling Yarn...",
"Yarn enabled",
"Setting Yarn version...",
"Failed to set Yarn version: some error",
]);
});

it("should successfully install dependencies", async () => {
const { main } = await import("./main.js");

await main();

expect(failed).toBe(false);
expect(logs).toStrictEqual([
"Getting action inputs...",
"Enabling Yarn...",
"Yarn enabled",
"Setting Yarn version...",
"Yarn version set to stable",
"::group::Getting cache key",
"::endgroup::",
"::group::Getting cache paths",
"::endgroup::",
"::group::Restoring cache",
"Cache not found",
"::endgroup::",
"::group::Installing dependencies",
"Dependencies installed",
"::endgroup::",
"::group::Saving cache",
"Compressing some/path...",
"Compressing another/path...",
"Cache unavailable-key saved",
"::endgroup::",
]);
});
});

describe("with cache disabled", () => {
beforeEach(async () => {
const { getInputs } = await import("./inputs.js");

jest.mocked(getInputs).mockReturnValue({ cache: false });
jest.mocked(getInputs).mockReturnValue({ version: "", cache: false });
});

it("should successfully install dependencies", async () => {
Expand Down
13 changes: 12 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as cache from "@actions/cache";
import * as core from "@actions/core";
import { getCacheKey, getCachePaths } from "./cache.js";
import { corepackAssertYarnVersion, corepackEnableYarn } from "./corepack.js";
import { yarnInstall } from "./yarn/index.js";
import { setYarnVersion, yarnInstall } from "./yarn/index.js";
import { getInputs } from "./inputs.js";

export async function main(): Promise<void> {
Expand All @@ -18,6 +18,17 @@ export async function main(): Promise<void> {
return;
}

if (inputs.version != "") {
core.info("Setting Yarn version...");
try {
await setYarnVersion(inputs.version);
await corepackAssertYarnVersion();
} catch (err) {
core.setFailed(`Failed to set Yarn version: ${err.message}`);
return;
}
}

let cacheKey: string;
let cachePaths: string[];
if (inputs.cache) {
Expand Down
Loading