test: refactor testing in inputs.test.ts

This commit is contained in:
Alfi Maulana 2024-03-24 23:35:54 +07:00
parent 0df49588a5
commit 667f2d1359
No known key found for this signature in database
GPG Key ID: 2242A64C2A8DF5A4

View File

@ -1,4 +1,5 @@
import { jest } from "@jest/globals"; import { jest } from "@jest/globals";
import type { Inputs } from "./inputs.js";
jest.unstable_mockModule("@actions/core", () => ({ jest.unstable_mockModule("@actions/core", () => ({
getBooleanInput: jest.fn(), getBooleanInput: jest.fn(),
@ -7,110 +8,87 @@ jest.unstable_mockModule("@actions/core", () => ({
})); }));
describe("get action inputs", () => { describe("get action inputs", () => {
describe("with default values", () => { const defaultInputs: Inputs = {
beforeEach(async () => { sourceDir: ".",
const { getBooleanInput, getInput, getMultilineInput } = await import( buildDir: "build",
"@actions/core" generator: "",
); cCompiler: "",
cxxCompiler: "",
cFlags: "",
cxxFlags: "",
options: [],
args: [],
runBuild: true,
buildArgs: [],
};
jest.mocked(getBooleanInput).mockReturnValue(false); beforeEach(async () => {
jest.mocked(getInput).mockReturnValue(""); const core = await import("@actions/core");
jest.mocked(getMultilineInput).mockReturnValue([]);
jest.mocked(core.getBooleanInput).mockImplementation((name) => {
return name == "run-build";
}); });
it("should get the action inputs", async () => { jest.mocked(core.getInput).mockReturnValue("");
const { getInputs } = await import("./inputs.js"); jest.mocked(core.getMultilineInput).mockReturnValue([]);
const inputs = getInputs();
expect(inputs).toStrictEqual({
sourceDir: ".",
buildDir: "build",
generator: "",
cCompiler: "",
cxxCompiler: "",
cFlags: "",
cxxFlags: "",
options: [],
args: [],
runBuild: false,
buildArgs: [],
});
});
}); });
describe("with specified values", () => { it("should get the action inputs with nothing specified", async () => {
beforeEach(async () => { const { getInputs } = await import("./inputs.js");
const { getBooleanInput, getInput, getMultilineInput } = await import(
"@actions/core"
);
jest.mocked(getBooleanInput).mockImplementation((name) => { expect(getInputs()).toStrictEqual(defaultInputs);
switch (name) { });
case "run-build":
return true;
}
throw new Error(`invalid input name: ${name}`);
});
jest.mocked(getInput).mockImplementation((name) => { it("should get the action inputs with all specified", async () => {
switch (name) { const { getInputs } = await import("./inputs.js");
case "source-dir": const core = await import("@actions/core");
return "some-source";
case "build-dir":
return "some-build";
case "generator":
return "some-generator";
case "c-compiler":
return "some-c-compiler";
case "cxx-compiler":
return "some-cxx-compiler";
}
throw new Error(`invalid input name: ${name}`);
});
jest.mocked(getMultilineInput).mockImplementation((name) => { jest.mocked(core.getBooleanInput).mockReturnValue(false);
switch (name) {
case "c-flags": jest.mocked(core.getInput).mockImplementation((name) => {
return ["some-c-flag another-c-flag", "some-other-c-flag"]; switch (name) {
case "cxx-flags": case "source-dir":
return ["some-cxx-flag another-cxx-flag", "some-other-cxx-flag"]; return "project";
case "options": case "build-dir":
return ["some-options another-options", "some-other-options"]; return "output";
case "args": case "generator":
return ["some-args another-args", "some-other-args"]; return "Ninja";
case "build-args": case "c-compiler":
return [ return "clang";
"some-build-args another-build-args", case "cxx-compiler":
"some-other-build-args", return "clang++";
]; }
} return "";
throw new Error(`invalid input name: ${name}`);
});
}); });
it("should get the action inputs", async () => { jest.mocked(core.getMultilineInput).mockImplementation((name) => {
const { getInputs } = await import("./inputs.js"); switch (name) {
case "c-flags":
return ["-Werror -Wall", "-Wextra"];
case "cxx-flags":
return ["-Werror -Wall", "-Wextra -Wpedantic"];
case "options":
return ["BUILD_TESTING=ON BUILD_EXAMPLES=ON", "BUILD_DOCS=ON"];
case "args":
return ["-Wdev -Wdeprecated", "--fresh"];
case "build-args":
return ["--target foo", "--parallel 8"];
}
return [];
});
const inputs = getInputs(); expect(getInputs()).toStrictEqual({
sourceDir: "project",
expect(inputs).toStrictEqual({ buildDir: "output",
sourceDir: "some-source", generator: "Ninja",
buildDir: "some-build", cCompiler: "clang",
generator: "some-generator", cxxCompiler: "clang++",
cCompiler: "some-c-compiler", cFlags: "-Werror -Wall -Wextra",
cxxCompiler: "some-cxx-compiler", cxxFlags: "-Werror -Wall -Wextra -Wpedantic",
cFlags: "some-c-flag another-c-flag some-other-c-flag", options: ["BUILD_TESTING=ON", "BUILD_EXAMPLES=ON", "BUILD_DOCS=ON"],
cxxFlags: "some-cxx-flag another-cxx-flag some-other-cxx-flag", args: ["-Wdev", "-Wdeprecated", "--fresh"],
options: ["some-options", "another-options", "some-other-options"], runBuild: false,
args: ["some-args", "another-args", "some-other-args"], buildArgs: ["--target", "foo", "--parallel", "8"],
runBuild: true,
buildArgs: [
"some-build-args",
"another-build-args",
"some-other-build-args",
],
});
}); });
}); });
}); });