refactor: add a getInput function

This commit is contained in:
Alfi Maulana 2024-08-06 16:20:06 +07:00
parent fe729787d8
commit bf49a53fb3
No known key found for this signature in database
GPG Key ID: 2242A64C2A8DF5A4
3 changed files with 47 additions and 26 deletions

19
dist/index.js generated vendored
View File

@ -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(" ")),

View File

@ -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<string, string>;
booleanInputs?: Record<string, boolean>;
stringInputs?: Record<string, string>;
multilineInputs?: Record<string, string[]>;
expectedInputs?: Partial<Inputs>;
}
@ -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;
});
}
});

View File

@ -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 {