From bf49a53fb334d94172264a4c7ae15a5c4d1dfea9 Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Tue, 6 Aug 2024 16:20:06 +0700 Subject: [PATCH] refactor: add a `getInput` function --- dist/index.js | 19 ++++++++++++++----- src/inputs.test.ts | 42 ++++++++++++++++++++++-------------------- src/inputs.ts | 12 +++++++++++- 3 files changed, 47 insertions(+), 26 deletions(-) diff --git a/dist/index.js b/dist/index.js index 58ab044..a059e7a 100644 --- a/dist/index.js +++ b/dist/index.js @@ -26753,14 +26753,23 @@ var external_node_path_default = /*#__PURE__*/__nccwpck_require__.n(external_nod ;// CONCATENATED MODULE: ./src/inputs.ts +/** + * Retrieves an action input. + * @param key - The key of the action input. + * @returns The action input value as a string. + */ +function getInput(key) { + const value = process.env[`INPUT_${key.toUpperCase()}`] || ""; + return value.trim(); +} function getInputs() { - const sourceDir = (0,core.getInput)("source-dir"); + const sourceDir = getInput("source-dir"); return { sourceDir, - buildDir: (0,core.getInput)("build-dir") || external_node_path_default().join(sourceDir, "build"), - generator: (0,core.getInput)("generator"), - cCompiler: (0,core.getInput)("c-compiler"), - cxxCompiler: (0,core.getInput)("cxx-compiler"), + buildDir: getInput("build-dir") || external_node_path_default().join(sourceDir, "build"), + generator: getInput("generator"), + cCompiler: getInput("c-compiler"), + cxxCompiler: getInput("cxx-compiler"), cFlags: (0,core.getMultilineInput)("c-flags").join(" "), cxxFlags: (0,core.getMultilineInput)("cxx-flags").join(" "), options: (0,core.getMultilineInput)("options").flatMap((opts) => opts.split(" ")), diff --git a/src/inputs.test.ts b/src/inputs.test.ts index 773e350..4d4de7d 100644 --- a/src/inputs.test.ts +++ b/src/inputs.test.ts @@ -4,15 +4,14 @@ import type { Inputs } from "./inputs.js"; jest.unstable_mockModule("@actions/core", () => ({ getBooleanInput: jest.fn(), - getInput: jest.fn(), getMultilineInput: jest.fn(), })); describe("get action inputs", () => { interface TestCase { name: string; + env?: Record; booleanInputs?: Record; - stringInputs?: Record; multilineInputs?: Record; expectedInputs?: Partial; } @@ -23,7 +22,7 @@ describe("get action inputs", () => { }, { name: "with source directory specified", - stringInputs: { "source-dir": "project" }, + env: { "INPUT_SOURCE-DIR": "project" }, expectedInputs: { sourceDir: "project", buildDir: path.join("project", "build"), @@ -31,14 +30,14 @@ describe("get action inputs", () => { }, { name: "with build directory specified", - stringInputs: { "build-dir": "output" }, + env: { "INPUT_BUILD-DIR": "output" }, expectedInputs: { buildDir: "output" }, }, { name: "with source and build directories specified", - stringInputs: { - "source-dir": "project", - "build-dir": "output", + env: { + "INPUT_SOURCE-DIR": "project", + "INPUT_BUILD-DIR": "output", }, expectedInputs: { sourceDir: "project", @@ -47,17 +46,17 @@ describe("get action inputs", () => { }, { name: "with generator specified", - stringInputs: { generator: "Ninja" }, + env: { INPUT_GENERATOR: "Ninja" }, expectedInputs: { generator: "Ninja" }, }, { name: "with C compiler specified", - stringInputs: { "c-compiler": "clang" }, + env: { "INPUT_C-COMPILER": "clang" }, expectedInputs: { cCompiler: "clang" }, }, { name: "with C++ compiler specified", - stringInputs: { "cxx-compiler": "clang++" }, + env: { "INPUT_CXX-COMPILER": "clang++" }, expectedInputs: { cxxCompiler: "clang++" }, }, { @@ -99,12 +98,12 @@ describe("get action inputs", () => { booleanInputs: { "run-build": false, }, - stringInputs: { - "source-dir": "project", - "build-dir": "output", - generator: "Ninja", - "c-compiler": "clang", - "cxx-compiler": "clang++", + env: { + "INPUT_SOURCE-DIR": "project", + "INPUT_BUILD-DIR": "output", + INPUT_GENERATOR: "Ninja", + "INPUT_C-COMPILER": "clang", + "INPUT_CXX-COMPILER": "clang++", }, multilineInputs: { "c-flags": ["-Werror -Wall", "-Wextra"], @@ -142,10 +141,11 @@ describe("get action inputs", () => { return booleanInputs[name] ?? false; }); - const stringInputs = { ...testCase.stringInputs }; - jest.mocked(core.getInput).mockImplementation((name) => { - return stringInputs[name] ?? ""; - }); + const prevEnv = process.env; + process.env = { + ...process.env, + ...testCase.env, + }; const multilineInputs = { ...testCase.multilineInputs }; jest.mocked(core.getMultilineInput).mockImplementation((name) => { @@ -166,6 +166,8 @@ describe("get action inputs", () => { buildArgs: [], ...testCase.expectedInputs, }); + + process.env = prevEnv; }); } }); diff --git a/src/inputs.ts b/src/inputs.ts index e0582a6..0ab9779 100644 --- a/src/inputs.ts +++ b/src/inputs.ts @@ -1,4 +1,4 @@ -import { getBooleanInput, getInput, getMultilineInput } from "@actions/core"; +import { getBooleanInput, getMultilineInput } from "@actions/core"; import path from "node:path"; export interface Inputs { @@ -15,6 +15,16 @@ export interface Inputs { buildArgs: string[]; } +/** + * Retrieves an action input. + * @param key - The key of the action input. + * @returns The action input value as a string. + */ +function getInput(key: string): string { + const value = process.env[`INPUT_${key.toUpperCase()}`] || ""; + return value.trim(); +} + export function getInputs(): Inputs { const sourceDir = getInput("source-dir"); return {