test: modify get action context test by mocking getInput function

This commit is contained in:
Alfi Maulana 2024-08-08 15:24:55 +07:00
parent d7b762efbe
commit 18d05af0d9
No known key found for this signature in database
GPG Key ID: 2242A64C2A8DF5A4

View File

@ -1,10 +1,15 @@
import { jest } from "@jest/globals";
import path from "node:path"; import path from "node:path";
import { Context, getContext } from "./context.js"; import type { Context } from "./context.js";
jest.unstable_mockModule("gha-utils", () => ({
getInput: jest.fn(),
}));
describe("get action context", () => { describe("get action context", () => {
interface TestCase { interface TestCase {
name: string; name: string;
env?: Record<string, string>; inputs?: Record<string, string>;
expectedContext?: Partial<Context>; expectedContext?: Partial<Context>;
} }
@ -14,7 +19,7 @@ describe("get action context", () => {
}, },
{ {
name: "with source directory specified", name: "with source directory specified",
env: { "INPUT_SOURCE-DIR": "project" }, inputs: { "source-dir": "project" },
expectedContext: { expectedContext: {
sourceDir: "project", sourceDir: "project",
buildDir: path.join("project", "build"), buildDir: path.join("project", "build"),
@ -22,14 +27,14 @@ describe("get action context", () => {
}, },
{ {
name: "with build directory specified", name: "with build directory specified",
env: { "INPUT_BUILD-DIR": "output" }, inputs: { "build-dir": "output" },
expectedContext: { buildDir: "output" }, expectedContext: { buildDir: "output" },
}, },
{ {
name: "with source and build directories specified", name: "with source and build directories specified",
env: { inputs: {
"INPUT_SOURCE-DIR": "project", "source-dir": "project",
"INPUT_BUILD-DIR": "output", "build-dir": "output",
}, },
expectedContext: { expectedContext: {
sourceDir: "project", sourceDir: "project",
@ -38,7 +43,7 @@ describe("get action context", () => {
}, },
{ {
name: "with generator specified", name: "with generator specified",
env: { INPUT_GENERATOR: "Ninja" }, inputs: { generator: "Ninja" },
expectedContext: { expectedContext: {
configure: { configure: {
generator: "Ninja", generator: "Ninja",
@ -49,7 +54,7 @@ describe("get action context", () => {
}, },
{ {
name: "with C compiler specified", name: "with C compiler specified",
env: { "INPUT_C-COMPILER": "clang" }, inputs: { "c-compiler": "clang" },
expectedContext: { expectedContext: {
configure: { configure: {
generator: "", generator: "",
@ -60,7 +65,7 @@ describe("get action context", () => {
}, },
{ {
name: "with C++ compiler specified", name: "with C++ compiler specified",
env: { "INPUT_CXX-COMPILER": "clang++" }, inputs: { "cxx-compiler": "clang++" },
expectedContext: { expectedContext: {
configure: { configure: {
generator: "", generator: "",
@ -71,7 +76,7 @@ describe("get action context", () => {
}, },
{ {
name: "with C flags specified", name: "with C flags specified",
env: { "INPUT_C-FLAGS": "-Werror -Wall\n-Wextra" }, inputs: { "c-flags": "-Werror -Wall\n-Wextra" },
expectedContext: { expectedContext: {
configure: { configure: {
generator: "", generator: "",
@ -82,7 +87,7 @@ describe("get action context", () => {
}, },
{ {
name: "with C++ flags specified", name: "with C++ flags specified",
env: { "INPUT_CXX-FLAGS": "-Werror -Wall\n-Wextra -Wpedantic" }, inputs: { "cxx-flags": "-Werror -Wall\n-Wextra -Wpedantic" },
expectedContext: { expectedContext: {
configure: { configure: {
generator: "", generator: "",
@ -93,8 +98,8 @@ describe("get action context", () => {
}, },
{ {
name: "with additional options specified", name: "with additional options specified",
env: { inputs: {
INPUT_OPTIONS: "BUILD_TESTING=ON BUILD_EXAMPLES=ON\nBUILD_DOCS=ON", options: "BUILD_TESTING=ON BUILD_EXAMPLES=ON\nBUILD_DOCS=ON",
}, },
expectedContext: { expectedContext: {
configure: { configure: {
@ -106,7 +111,7 @@ describe("get action context", () => {
}, },
{ {
name: "with additional arguments specified", name: "with additional arguments specified",
env: { INPUT_ARGS: "-Wdev -Wdeprecated\n--fresh" }, inputs: { args: "-Wdev -Wdeprecated\n--fresh" },
expectedContext: { expectedContext: {
configure: { configure: {
generator: "", generator: "",
@ -117,12 +122,12 @@ describe("get action context", () => {
}, },
{ {
name: "with run build specified", name: "with run build specified",
env: { "INPUT_RUN-BUILD": "true" }, inputs: { "run-build": "true" },
expectedContext: { build: { enabled: true, args: [] } }, expectedContext: { build: { enabled: true, args: [] } },
}, },
{ {
name: "with additional build arguments specified", name: "with additional build arguments specified",
env: { "INPUT_BUILD-ARGS": "--target foo\n--parallel 8" }, inputs: { "build-args": "--target foo\n--parallel 8" },
expectedContext: { expectedContext: {
build: { build: {
enabled: false, enabled: false,
@ -132,18 +137,18 @@ describe("get action context", () => {
}, },
{ {
name: "with all specified", name: "with all specified",
env: { inputs: {
"INPUT_SOURCE-DIR": "project", "source-dir": "project",
"INPUT_BUILD-DIR": "output", "build-dir": "output",
INPUT_GENERATOR: "Ninja", generator: "Ninja",
"INPUT_C-COMPILER": "clang", "c-compiler": "clang",
"INPUT_CXX-COMPILER": "clang++", "cxx-compiler": "clang++",
"INPUT_C-FLAGS": "-Werror -Wall\n-Wextra", "c-flags": "-Werror -Wall\n-Wextra",
"INPUT_CXX-FLAGS": "-Werror -Wall\n-Wextra -Wpedantic", "cxx-flags": "-Werror -Wall\n-Wextra -Wpedantic",
INPUT_OPTIONS: "BUILD_TESTING=ON BUILD_EXAMPLES=ON\nBUILD_DOCS=ON", options: "BUILD_TESTING=ON BUILD_EXAMPLES=ON\nBUILD_DOCS=ON",
INPUT_ARGS: "-Wdev -Wdeprecated\n--fresh", args: "-Wdev -Wdeprecated\n--fresh",
"INPUT_RUN-BUILD": "true", "run-build": "true",
"INPUT_BUILD-ARGS": "--target foo\n--parallel 8", "build-args": "--target foo\n--parallel 8",
}, },
expectedContext: { expectedContext: {
sourceDir: "project", sourceDir: "project",
@ -171,11 +176,13 @@ describe("get action context", () => {
for (const testCase of testCases) { for (const testCase of testCases) {
it(`should get the action context ${testCase.name}`, async () => { it(`should get the action context ${testCase.name}`, async () => {
const prevEnv = process.env; const { getInput } = await import("gha-utils");
process.env = { const { getContext } = await import("./context.js");
...process.env,
...testCase.env, const inputs = testCase.inputs || {};
}; jest.mocked(getInput).mockImplementation((name) => {
return inputs[name] || "";
});
expect(getContext()).toStrictEqual({ expect(getContext()).toStrictEqual({
sourceDir: "", sourceDir: "",
@ -191,8 +198,6 @@ describe("get action context", () => {
}, },
...testCase.expectedContext, ...testCase.expectedContext,
}); });
process.env = prevEnv;
}); });
} }
}); });