Skip to content

Commit

Permalink
Improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
placintaalexandru committed Oct 28, 2023
1 parent 815cc6f commit 92ed4a1
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 22 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/installers/rustup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class RustUp {
const args = ["target", "add"]
.concat(options.targets)
.concat(["--toolchain", options.toolchain]);
return await this.call(args);
return this.call(args);
}

public async activeToolchain(): Promise<string> {
Expand Down
15 changes: 3 additions & 12 deletions src/toolchain_install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,7 @@ export const installToolchainCommand = new Command()
parser.getInput("force", { type: "boolean", default: false }),
),
)
.addOption(
new Option(
"--no-self-update",
"Don't perform self update when running the`rustup toolchain install` command",
).default(true),
)
.addOption(
new Option(
"--allow-downgrade",
"Allows rustup to downgrade the toolchain to satisfy your component choice",
).default(true),
)
// eslint-disable-next-line @typescript-eslint/require-await
.action(async (options: RawInput) => {
core.debug(JSON.stringify(options));

Expand Down Expand Up @@ -155,6 +144,8 @@ export const installToolchainCommand = new Command()
await rustup.addTargets(toolchainOptions);
}

core.info(await rustup.activeToolchain());

(await new Outputs().outputs()).forEach((output) => {
core.setOutput(output.name, output.value);
});
Expand Down
2 changes: 1 addition & 1 deletion src/utils/conversions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const rawInputToToolchainOptions = (
default: input.default || false,
override: input.override || false,
noSelfUpdate: false,
allowDowngrade: true,
allowDowngrade: false,
force: input.force || false,
};
};
Expand Down
122 changes: 117 additions & 5 deletions tests/installers/rustup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ import { describe } from "jest-circus";
import { RustUp } from "../../src/installers/rustup";
import { RustUpInit } from "../../src/installers/rustup_init";
import * as mockedIo from "@actions/io";
import * as mockedExec from "@actions/exec";
import * as fcj from "@fast-check/jest";

jest.mock("@actions/io", () => ({
which: jest.fn(),
}));
jest.mock("@actions/exec", () => ({
exec: jest.fn(),
}));
jest.mock("../../src/installers/rustup_init", () => ({
RustUpInit: { install: jest.fn() },
}));
Expand All @@ -18,7 +22,7 @@ describe("Tests of rustup", () => {

test("Get or install: rustup exists", async () => {
(mockedIo.which as jest.Mock).mockReturnValue(
new Promise((resolve) => resolve("/somepath/rustup")),
Promise.resolve("/somepath/rustup"),
);
await RustUp.getOrInstall();

Expand All @@ -38,9 +42,7 @@ describe("Tests of rustup", () => {
// The second time it gets called, we mock its existence
return "/myrustup/rustup";
});
(RustUpInit.install as jest.Mock).mockReturnValue(
new Promise((resolve) => resolve("")),
);
(RustUpInit.install as jest.Mock).mockReturnValue(Promise.resolve(""));

await RustUp.getOrInstall();
expect(mockedIo.which).toHaveBeenCalledTimes(2);
Expand Down Expand Up @@ -72,7 +74,7 @@ describe("Tests of rustup", () => {
fcj.fc.integer({ min: 1, max: 100 }),
])("Version: valid", async (major, minor, patch) => {
(mockedIo.which as jest.Mock).mockReturnValue(
new Promise((resolve) => resolve("/somepath/rustup")),
Promise.resolve("/somepath/rustup"),
);

const rustUp = await RustUp.getOrInstall();
Expand All @@ -91,4 +93,114 @@ describe("Tests of rustup", () => {
expect(version.minor).toBe(minor);
expect(version.patch).toBe(patch);
});

test("Install toolchain: components", async () => {
(mockedIo.which as jest.Mock).mockReturnValue(
Promise.resolve("/somepath/rustup"),
);

const rustUpPath = await mockedIo.which("rustup");
const rustUp = await RustUp.getOrInstall();

const toolchainOptions = {
toolchain: "1.73.0",
profile: "minimal",
components: ["rustfmt", "clippy"],
targets: [],
default: false,
override: false,
force: false,
noSelfUpdate: false,
allowDowngrade: false,
};

await rustUp.installToolchain(toolchainOptions);

expect(mockedExec.exec).toHaveBeenCalledTimes(1);
expect(mockedExec.exec).toHaveBeenCalledWith(
rustUpPath,
[
"toolchain",
"install",
toolchainOptions.toolchain,
"--component",
toolchainOptions.components[0],
"--component",
toolchainOptions.components[1],
],
undefined,
);
});

test("Install toolchain: no self update, downgrade & force", async () => {
(mockedIo.which as jest.Mock).mockReturnValue(
Promise.resolve("/somepath/rustup"),
);

const rustUpPath = await mockedIo.which("rustup");
const rustUp = await RustUp.getOrInstall();

const toolchainOptions = {
toolchain: "1.73.0",
profile: "minimal",
components: [],
targets: [],
default: false,
override: false,
force: true,
noSelfUpdate: true,
allowDowngrade: true,
};

await rustUp.installToolchain(toolchainOptions);

expect(mockedExec.exec).toHaveBeenCalledTimes(1);
expect(mockedExec.exec).toHaveBeenCalledWith(
rustUpPath,
[
"toolchain",
"install",
toolchainOptions.toolchain,
"--no-self-update",
"--allow-downgrade",
"--force",
],
undefined,
);
});

test("Install toolchain: default & override", async () => {
(mockedIo.which as jest.Mock).mockReturnValue(
Promise.resolve("/somepath/rustup"),
);

const rustUpPath = await mockedIo.which("rustup");
const rustUp = await RustUp.getOrInstall();

const toolchainOptions = {
toolchain: "1.73.0",
profile: "minimal",
components: [],
targets: [],
default: true,
override: true,
force: false,
noSelfUpdate: false,
allowDowngrade: false,
};

await rustUp.installToolchain(toolchainOptions);

expect(mockedExec.exec).toHaveBeenCalledTimes(3);
expect(mockedExec.exec).toHaveBeenCalledWith(
rustUpPath,
["default", toolchainOptions.toolchain],
undefined,
);
expect(mockedExec.exec).toHaveBeenCalledWith(
rustUpPath,
["override", "set", toolchainOptions.toolchain],
undefined,
);
});
});
4 changes: 2 additions & 2 deletions tests/utils/conversion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ describe("Tests of conversion utils module", () => {
expect(toolchanOptions.default).toBe(false);
expect(toolchanOptions.override).toBe(false);
expect(toolchanOptions.force).toBe(false);
expect(toolchanOptions.allowDowngrade).toBe(true);
expect(toolchanOptions.allowDowngrade).toBe(false);
expect(toolchanOptions.noSelfUpdate).toBe(false);
});

Expand Down Expand Up @@ -154,7 +154,7 @@ describe("Tests of conversion utils module", () => {
expect(toolchanOptions.default).toBe(rawInput.default);
expect(toolchanOptions.override).toBe(rawInput.override);
expect(toolchanOptions.force).toBe(rawInput.force);
expect(toolchanOptions.allowDowngrade).toBe(true);
expect(toolchanOptions.allowDowngrade).toBe(false);
expect(toolchanOptions.noSelfUpdate).toBe(false);
});
});

0 comments on commit 92ed4a1

Please sign in to comment.