mirror of
https://github.com/threeal/cmake-action.git
synced 2025-04-22 03:21:21 +00:00
refactor: add a getInput
function
This commit is contained in:
parent
fe729787d8
commit
bf49a53fb3
19
dist/index.js
generated
vendored
19
dist/index.js
generated
vendored
@ -26753,14 +26753,23 @@ var external_node_path_default = /*#__PURE__*/__nccwpck_require__.n(external_nod
|
|||||||
;// CONCATENATED MODULE: ./src/inputs.ts
|
;// 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() {
|
function getInputs() {
|
||||||
const sourceDir = (0,core.getInput)("source-dir");
|
const sourceDir = getInput("source-dir");
|
||||||
return {
|
return {
|
||||||
sourceDir,
|
sourceDir,
|
||||||
buildDir: (0,core.getInput)("build-dir") || external_node_path_default().join(sourceDir, "build"),
|
buildDir: getInput("build-dir") || external_node_path_default().join(sourceDir, "build"),
|
||||||
generator: (0,core.getInput)("generator"),
|
generator: getInput("generator"),
|
||||||
cCompiler: (0,core.getInput)("c-compiler"),
|
cCompiler: getInput("c-compiler"),
|
||||||
cxxCompiler: (0,core.getInput)("cxx-compiler"),
|
cxxCompiler: getInput("cxx-compiler"),
|
||||||
cFlags: (0,core.getMultilineInput)("c-flags").join(" "),
|
cFlags: (0,core.getMultilineInput)("c-flags").join(" "),
|
||||||
cxxFlags: (0,core.getMultilineInput)("cxx-flags").join(" "),
|
cxxFlags: (0,core.getMultilineInput)("cxx-flags").join(" "),
|
||||||
options: (0,core.getMultilineInput)("options").flatMap((opts) => opts.split(" ")),
|
options: (0,core.getMultilineInput)("options").flatMap((opts) => opts.split(" ")),
|
||||||
|
@ -4,15 +4,14 @@ import type { Inputs } from "./inputs.js";
|
|||||||
|
|
||||||
jest.unstable_mockModule("@actions/core", () => ({
|
jest.unstable_mockModule("@actions/core", () => ({
|
||||||
getBooleanInput: jest.fn(),
|
getBooleanInput: jest.fn(),
|
||||||
getInput: jest.fn(),
|
|
||||||
getMultilineInput: jest.fn(),
|
getMultilineInput: jest.fn(),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
describe("get action inputs", () => {
|
describe("get action inputs", () => {
|
||||||
interface TestCase {
|
interface TestCase {
|
||||||
name: string;
|
name: string;
|
||||||
|
env?: Record<string, string>;
|
||||||
booleanInputs?: Record<string, boolean>;
|
booleanInputs?: Record<string, boolean>;
|
||||||
stringInputs?: Record<string, string>;
|
|
||||||
multilineInputs?: Record<string, string[]>;
|
multilineInputs?: Record<string, string[]>;
|
||||||
expectedInputs?: Partial<Inputs>;
|
expectedInputs?: Partial<Inputs>;
|
||||||
}
|
}
|
||||||
@ -23,7 +22,7 @@ describe("get action inputs", () => {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "with source directory specified",
|
name: "with source directory specified",
|
||||||
stringInputs: { "source-dir": "project" },
|
env: { "INPUT_SOURCE-DIR": "project" },
|
||||||
expectedInputs: {
|
expectedInputs: {
|
||||||
sourceDir: "project",
|
sourceDir: "project",
|
||||||
buildDir: path.join("project", "build"),
|
buildDir: path.join("project", "build"),
|
||||||
@ -31,14 +30,14 @@ describe("get action inputs", () => {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "with build directory specified",
|
name: "with build directory specified",
|
||||||
stringInputs: { "build-dir": "output" },
|
env: { "INPUT_BUILD-DIR": "output" },
|
||||||
expectedInputs: { buildDir: "output" },
|
expectedInputs: { buildDir: "output" },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "with source and build directories specified",
|
name: "with source and build directories specified",
|
||||||
stringInputs: {
|
env: {
|
||||||
"source-dir": "project",
|
"INPUT_SOURCE-DIR": "project",
|
||||||
"build-dir": "output",
|
"INPUT_BUILD-DIR": "output",
|
||||||
},
|
},
|
||||||
expectedInputs: {
|
expectedInputs: {
|
||||||
sourceDir: "project",
|
sourceDir: "project",
|
||||||
@ -47,17 +46,17 @@ describe("get action inputs", () => {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "with generator specified",
|
name: "with generator specified",
|
||||||
stringInputs: { generator: "Ninja" },
|
env: { INPUT_GENERATOR: "Ninja" },
|
||||||
expectedInputs: { generator: "Ninja" },
|
expectedInputs: { generator: "Ninja" },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "with C compiler specified",
|
name: "with C compiler specified",
|
||||||
stringInputs: { "c-compiler": "clang" },
|
env: { "INPUT_C-COMPILER": "clang" },
|
||||||
expectedInputs: { cCompiler: "clang" },
|
expectedInputs: { cCompiler: "clang" },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "with C++ compiler specified",
|
name: "with C++ compiler specified",
|
||||||
stringInputs: { "cxx-compiler": "clang++" },
|
env: { "INPUT_CXX-COMPILER": "clang++" },
|
||||||
expectedInputs: { cxxCompiler: "clang++" },
|
expectedInputs: { cxxCompiler: "clang++" },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -99,12 +98,12 @@ describe("get action inputs", () => {
|
|||||||
booleanInputs: {
|
booleanInputs: {
|
||||||
"run-build": false,
|
"run-build": false,
|
||||||
},
|
},
|
||||||
stringInputs: {
|
env: {
|
||||||
"source-dir": "project",
|
"INPUT_SOURCE-DIR": "project",
|
||||||
"build-dir": "output",
|
"INPUT_BUILD-DIR": "output",
|
||||||
generator: "Ninja",
|
INPUT_GENERATOR: "Ninja",
|
||||||
"c-compiler": "clang",
|
"INPUT_C-COMPILER": "clang",
|
||||||
"cxx-compiler": "clang++",
|
"INPUT_CXX-COMPILER": "clang++",
|
||||||
},
|
},
|
||||||
multilineInputs: {
|
multilineInputs: {
|
||||||
"c-flags": ["-Werror -Wall", "-Wextra"],
|
"c-flags": ["-Werror -Wall", "-Wextra"],
|
||||||
@ -142,10 +141,11 @@ describe("get action inputs", () => {
|
|||||||
return booleanInputs[name] ?? false;
|
return booleanInputs[name] ?? false;
|
||||||
});
|
});
|
||||||
|
|
||||||
const stringInputs = { ...testCase.stringInputs };
|
const prevEnv = process.env;
|
||||||
jest.mocked(core.getInput).mockImplementation((name) => {
|
process.env = {
|
||||||
return stringInputs[name] ?? "";
|
...process.env,
|
||||||
});
|
...testCase.env,
|
||||||
|
};
|
||||||
|
|
||||||
const multilineInputs = { ...testCase.multilineInputs };
|
const multilineInputs = { ...testCase.multilineInputs };
|
||||||
jest.mocked(core.getMultilineInput).mockImplementation((name) => {
|
jest.mocked(core.getMultilineInput).mockImplementation((name) => {
|
||||||
@ -166,6 +166,8 @@ describe("get action inputs", () => {
|
|||||||
buildArgs: [],
|
buildArgs: [],
|
||||||
...testCase.expectedInputs,
|
...testCase.expectedInputs,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
process.env = prevEnv;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { getBooleanInput, getInput, getMultilineInput } from "@actions/core";
|
import { getBooleanInput, getMultilineInput } from "@actions/core";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
|
|
||||||
export interface Inputs {
|
export interface Inputs {
|
||||||
@ -15,6 +15,16 @@ export interface Inputs {
|
|||||||
buildArgs: string[];
|
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 {
|
export function getInputs(): Inputs {
|
||||||
const sourceDir = getInput("source-dir");
|
const sourceDir = getInput("source-dir");
|
||||||
return {
|
return {
|
||||||
|
Loading…
Reference in New Issue
Block a user